From c7400e4b41d180b02c213e2bc7de24736b594538 Mon Sep 17 00:00:00 2001 From: Igor Sapego Date: Wed, 10 Apr 2019 15:37:56 +0300 Subject: [PATCH 1/3] IGNITE-11703: Implemented default implementation of boilerplate BinaryType methods. --- .../include/ignite/binary/binary_type.h | 66 +++++++++++ .../ignite/impl/binary/binary_type_impl.h | 2 +- .../include/ignite/binary_test_defs.h | 112 +----------------- .../cpp/core-test/src/compute_test.cpp | 66 +---------- .../core-test/src/continuous_query_test.cpp | 36 ++---- .../impl/cache/cache_entry_processor_holder.h | 5 +- .../compute-example/src/compute_example.cpp | 27 +---- .../src/continuous_query_example.cpp | 23 +--- .../include/ignite/examples/address.h | 31 +---- .../include/ignite/examples/organization.h | 35 +----- 10 files changed, 100 insertions(+), 303 deletions(-) diff --git a/modules/platforms/cpp/binary/include/ignite/binary/binary_type.h b/modules/platforms/cpp/binary/include/ignite/binary/binary_type.h index 14a95b6dc37da..65f05ff2b3c99 100644 --- a/modules/platforms/cpp/binary/include/ignite/binary/binary_type.h +++ b/modules/platforms/cpp/binary/include/ignite/binary/binary_type.h @@ -148,6 +148,72 @@ namespace ignite template struct IGNITE_IMPORT_EXPORT BinaryType { }; + /** + * Default implementations of BinaryType hashing functions. + */ + template + struct IGNITE_IMPORT_EXPORT BinaryTypeDefaultHashing + { + /** + * Get binary object type ID. + * + * @return Type ID. + */ + static int32_t GetTypeId() + { + std::string typeName; + BinaryType::GetTypeName(typeName); + + return GetBinaryStringHashCode(typeName.c_str()); + } + + /** + * Get binary object field ID. + * + * @param name Field name. + * @return Field ID. + */ + static int32_t GetFieldId(const char* name) + { + return GetBinaryStringHashCode(name); + } + }; + + /** + * Default implementations of BinaryType methods for non-null type. + */ + template + struct IGNITE_IMPORT_EXPORT BinaryTypeNonNullableType + { + /** + * Check whether passed binary object should be interpreted as NULL. + * + * @return True if binary object should be interpreted as NULL. + */ + static bool IsNull(const T&) + { + return false; + } + + /** + * Get NULL value for the given binary type. + * + * @param dst Null value for the type. + */ + static void GetNull(T& dst) + { + dst = T(); + } + }; + + /** + * Default implementations of BinaryType hashing functions. + */ + template + struct IGNITE_IMPORT_EXPORT BinaryTypeDefaultAll : + BinaryTypeDefaultHashing, + BinaryTypeNonNullableType { }; + /** * Templated binary type specification for pointers. */ diff --git a/modules/platforms/cpp/binary/include/ignite/impl/binary/binary_type_impl.h b/modules/platforms/cpp/binary/include/ignite/impl/binary/binary_type_impl.h index e9bbace63611f..c93ba20370a16 100644 --- a/modules/platforms/cpp/binary/include/ignite/impl/binary/binary_type_impl.h +++ b/modules/platforms/cpp/binary/include/ignite/impl/binary/binary_type_impl.h @@ -45,7 +45,7 @@ namespace ignite static int32_t GetFieldId(const char* name); - static bool IsNull(const IgniteError& obj) + static bool IsNull(const IgniteError&) { return false; } diff --git a/modules/platforms/cpp/core-test/include/ignite/binary_test_defs.h b/modules/platforms/cpp/core-test/include/ignite/binary_test_defs.h index 9d7dcd2715d46..2cfaa60c88236 100644 --- a/modules/platforms/cpp/core-test/include/ignite/binary_test_defs.h +++ b/modules/platforms/cpp/core-test/include/ignite/binary_test_defs.h @@ -141,72 +141,32 @@ namespace ignite namespace gt = ignite_test::core::binary; template<> - struct BinaryType + struct BinaryType : BinaryTypeDefaultAll { - static int32_t GetTypeId() - { - return GetBinaryStringHashCode("BinaryDummy"); - } - static void GetTypeName(std::string& dst) { dst = "BinaryDummy"; } - static int32_t GetFieldId(const char* name) - { - return GetBinaryStringHashCode(name); - } - - static bool IsNull(const gt::BinaryInner& obj) - { - return false; - } - - static void GetNull(gt::BinaryDummy& dst) - { - dst = gt::BinaryDummy(); - } - static void Write(BinaryWriter& writer, const gt::BinaryDummy& obj) { // No-op. } - static void Read(BinaryReader& reader, gt::BinaryDummy& dst) + static void Read(BinaryReader&, gt::BinaryDummy& dst) { dst = gt::BinaryDummy(); } }; template<> - struct BinaryType + struct BinaryType : BinaryTypeDefaultAll { - static int32_t GetTypeId() - { - return GetBinaryStringHashCode("BinaryInner"); - } - static void GetTypeName(std::string& dst) { dst = "BinaryInner"; } - static int32_t GetFieldId(const char* name) - { - return GetBinaryStringHashCode(name); - } - - static bool IsNull(const gt::BinaryInner& obj) - { - return obj.GetValue() == 0; - } - - static void GetNull(gt::BinaryInner& dst) - { - dst = gt::BinaryInner(0); - } - static void Write(BinaryWriter& writer, const gt::BinaryInner& obj) { writer.WriteInt32("val", obj.GetValue()); @@ -221,33 +181,13 @@ namespace ignite }; template<> - struct BinaryType + struct BinaryType : BinaryTypeDefaultAll { - static int32_t GetTypeId() - { - return GetBinaryStringHashCode("BinaryOuter"); - } - static void GetTypeName(std::string& dst) { dst = "BinaryOuter"; } - static int32_t GetFieldId(const char* name) - { - return GetBinaryStringHashCode(name); - } - - static bool IsNull(const gt::BinaryOuter& obj) - { - return obj.GetValue() == 0 && obj.GetInner().GetValue(); - } - - static void GetNull(gt::BinaryOuter& dst) - { - dst = gt::BinaryOuter(0, 0); - } - static void Write(BinaryWriter& writer, const gt::BinaryOuter& obj) { writer.WriteObject("inner", obj.GetInner()); @@ -264,33 +204,13 @@ namespace ignite }; template<> - struct BinaryType + struct BinaryType : BinaryTypeDefaultAll { - static int32_t GetTypeId() - { - return GetBinaryStringHashCode("BinaryFields"); - } - static void GetTypeName(std::string& dst) { dst = "BinaryFields"; } - static int32_t GetFieldId(const char* name) - { - return GetBinaryStringHashCode(name); - } - - static bool IsNull(const gt::BinaryFields& obj) - { - return false; - } - - static void GetNull(gt::BinaryFields&) - { - throw std::runtime_error("Must not be called."); - } - static void Write(BinaryWriter& writer, const gt::BinaryFields& obj) { writer.WriteInt32("val1", obj.val1); @@ -317,33 +237,13 @@ namespace ignite }; template<> - struct BinaryType + struct BinaryType : BinaryTypeDefaultAll { - static int32_t GetTypeId() - { - return GetBinaryStringHashCode("PureRaw"); - } - static void GetTypeName(std::string& dst) { dst = "PureRaw"; } - static int32_t GetFieldId(const char* name) - { - return GetBinaryStringHashCode(name); - } - - static bool IsNull(const gt::PureRaw& obj) - { - return false; - } - - static void GetNull(gt::PureRaw&) - { - throw std::runtime_error("Must not be called."); - } - static void Write(BinaryWriter& writer, const gt::PureRaw& obj) { BinaryRawWriter rawWriter = writer.RawWriter(); diff --git a/modules/platforms/cpp/core-test/src/compute_test.cpp b/modules/platforms/cpp/core-test/src/compute_test.cpp index 0a873f5b1a025..2055443df3bee 100644 --- a/modules/platforms/cpp/core-test/src/compute_test.cpp +++ b/modules/platforms/cpp/core-test/src/compute_test.cpp @@ -190,33 +190,13 @@ namespace ignite namespace binary { template<> - struct BinaryType + struct BinaryType : BinaryTypeDefaultAll { - static int32_t GetTypeId() - { - return GetBinaryStringHashCode("Func1"); - } - static void GetTypeName(std::string& dst) { dst = "Func1"; } - static int32_t GetFieldId(const char* name) - { - return GetBinaryStringHashCode(name); - } - - static bool IsNull(const Func1& obj) - { - return false; - } - - static void GetNull(Func1& dst) - { - dst = Func1(0, 0); - } - static void Write(BinaryWriter& writer, const Func1& obj) { writer.WriteInt32("a", obj.a); @@ -233,33 +213,13 @@ namespace ignite }; template<> - struct BinaryType + struct BinaryType : BinaryTypeDefaultAll { - static int32_t GetTypeId() - { - return GetBinaryStringHashCode("Func2"); - } - static void GetTypeName(std::string& dst) { dst = "Func2"; } - static int32_t GetFieldId(const char* name) - { - return GetBinaryStringHashCode(name); - } - - static bool IsNull(const Func2& obj) - { - return false; - } - - static void GetNull(Func2& dst) - { - dst = Func2(0, 0); - } - static void Write(BinaryWriter& writer, const Func2& obj) { writer.WriteInt32("a", obj.a); @@ -276,33 +236,13 @@ namespace ignite }; template<> - struct BinaryType + struct BinaryType : BinaryTypeDefaultAll { - static int32_t GetTypeId() - { - return GetBinaryStringHashCode("Func3"); - } - static void GetTypeName(std::string& dst) { dst = "Func3"; } - static int32_t GetFieldId(const char* name) - { - return GetBinaryStringHashCode(name); - } - - static bool IsNull(const Func3& obj) - { - return false; - } - - static void GetNull(Func3& dst) - { - dst = Func3(0, 0); - } - static void Write(BinaryWriter& writer, const Func3& obj) { writer.WriteInt32("a", obj.a); diff --git a/modules/platforms/cpp/core-test/src/continuous_query_test.cpp b/modules/platforms/cpp/core-test/src/continuous_query_test.cpp index 81ea0cf13f05a..62b08066c2d6c 100644 --- a/modules/platforms/cpp/core-test/src/continuous_query_test.cpp +++ b/modules/platforms/cpp/core-test/src/continuous_query_test.cpp @@ -256,13 +256,12 @@ namespace ignite namespace binary { template<> - struct BinaryType + struct BinaryType : BinaryTypeDefaultAll { - IGNITE_BINARY_GET_TYPE_ID_AS_HASH(TestEntry) - IGNITE_BINARY_GET_TYPE_NAME_AS_IS(TestEntry) - IGNITE_BINARY_GET_FIELD_ID_AS_HASH - IGNITE_BINARY_IS_NULL_FALSE(TestEntry) - IGNITE_BINARY_GET_NULL_DEFAULT_CTOR(TestEntry) + static void GetTypeName(std::string& dst) + { + dst = "TestEntry"; + } static void Write(BinaryWriter& writer, const TestEntry& obj) { @@ -275,30 +274,17 @@ namespace ignite } }; - template - struct BinaryType< RangeFilter > + namespace { - static int32_t GetTypeId() - { - return GetBinaryStringHashCode("RangeFilter"); - } + extern const char typeName[] = "RangeFilter"; + } + template + struct BinaryType< RangeFilter > : BinaryTypeDefaultAll< RangeFilter > + { static void GetTypeName(std::string& dst) { dst = "RangeFilter"; - - } - - IGNITE_BINARY_GET_FIELD_ID_AS_HASH - - static bool IsNull(const RangeFilter&) - { - return false; - } - - static void GetNull(RangeFilter& dst) - { - dst = RangeFilter(); } static void Write(BinaryWriter& writer, const RangeFilter& obj) diff --git a/modules/platforms/cpp/core/include/ignite/impl/cache/cache_entry_processor_holder.h b/modules/platforms/cpp/core/include/ignite/impl/cache/cache_entry_processor_holder.h index a821407378565..cfa0e55f33ec1 100644 --- a/modules/platforms/cpp/core/include/ignite/impl/cache/cache_entry_processor_holder.h +++ b/modules/platforms/cpp/core/include/ignite/impl/cache/cache_entry_processor_holder.h @@ -186,13 +186,12 @@ namespace ignite * Binary type specialization for CacheEntryProcessorHolder. */ template - struct BinaryType > + struct BinaryType > : + BinaryTypeNonNullableType< impl::cache::CacheEntryProcessorHolder > { typedef impl::cache::CacheEntryProcessorHolder UnderlyingType; IGNITE_BINARY_GET_FIELD_ID_AS_HASH - IGNITE_BINARY_IS_NULL_FALSE(UnderlyingType) - IGNITE_BINARY_GET_NULL_DEFAULT_CTOR(UnderlyingType) static int32_t GetTypeId() { diff --git a/modules/platforms/cpp/examples/compute-example/src/compute_example.cpp b/modules/platforms/cpp/examples/compute-example/src/compute_example.cpp index e6bee8d9433d4..7ca6e0295723a 100644 --- a/modules/platforms/cpp/examples/compute-example/src/compute_example.cpp +++ b/modules/platforms/cpp/examples/compute-example/src/compute_example.cpp @@ -79,38 +79,13 @@ namespace ignite namespace binary { template<> - struct BinaryType + struct BinaryType: BinaryTypeDefaultAll { - static int32_t GetTypeId() - { - return GetBinaryStringHashCode("CountWords"); - } - static void GetTypeName(std::string& dst) { dst = "CountWords"; } - static int32_t GetFieldId(const char* name) - { - return GetBinaryStringHashCode(name); - } - - static int32_t GetHashCode(const CountWords& obj) - { - return 0; - } - - static bool IsNull(const CountWords& obj) - { - return false; - } - - static void GetNull(CountWords& dst) - { - dst = CountWords(""); - } - static void Write(BinaryWriter& writer, const CountWords& obj) { writer.RawWriter().WriteString(obj.text); diff --git a/modules/platforms/cpp/examples/continuous-query-example/src/continuous_query_example.cpp b/modules/platforms/cpp/examples/continuous-query-example/src/continuous_query_example.cpp index 8670bf6bfc27d..230d150e6e037 100644 --- a/modules/platforms/cpp/examples/continuous-query-example/src/continuous_query_example.cpp +++ b/modules/platforms/cpp/examples/continuous-query-example/src/continuous_query_example.cpp @@ -124,34 +124,15 @@ namespace ignite namespace binary { template<> - struct BinaryType< RangeFilter > + struct BinaryType< RangeFilter > : + BinaryTypeDefaultAll< RangeFilter > { - static int32_t GetTypeId() - { - return GetBinaryStringHashCode("RangeFilter"); - } - static void GetTypeName(std::string& dst) { dst = "RangeFilter"; } - static int32_t GetFieldId(const char* name) - { - return GetBinaryStringHashCode(name); - } - - static bool IsNull(const RangeFilter&) - { - return false; - } - - static void GetNull(RangeFilter& dst) - { - dst = RangeFilter(); - } - static void Write(BinaryWriter& writer, const RangeFilter& obj) { writer.WriteInt32("rangeBegin", obj.rangeBegin); diff --git a/modules/platforms/cpp/examples/include/ignite/examples/address.h b/modules/platforms/cpp/examples/include/ignite/examples/address.h index 8f2689cf8c2f0..6f28f89d3655e 100644 --- a/modules/platforms/cpp/examples/include/ignite/examples/address.h +++ b/modules/platforms/cpp/examples/include/ignite/examples/address.h @@ -57,45 +57,20 @@ namespace ignite namespace binary { template<> - struct BinaryType + struct BinaryType : BinaryTypeDefaultAll { - static int32_t GetTypeId() - { - return GetBinaryStringHashCode("Address"); - } - static void GetTypeName(std::string& dst) { dst = "Address"; } - static int32_t GetFieldId(const char* name) - { - return GetBinaryStringHashCode(name); - } - - static int32_t GetHashCode(ignite::examples::Address& obj) - { - return 0; - } - - static bool IsNull(ignite::examples::Address obj) - { - return false; - } - - static void GetNull(ignite::examples::Address& dst) - { - dst = ignite::examples::Address("", 0); - } - - static void Write(BinaryWriter& writer, const ignite::examples::Address& obj) + static void Write(BinaryWriter& writer, const examples::Address& obj) { writer.WriteString("street", obj.street); writer.WriteInt32("zip", obj.zip); } - static void Read(BinaryReader& reader, ignite::examples::Address& dst) + static void Read(BinaryReader& reader, examples::Address& dst) { dst.street = reader.ReadString("street"); dst.zip = reader.ReadInt32("zip"); diff --git a/modules/platforms/cpp/examples/include/ignite/examples/organization.h b/modules/platforms/cpp/examples/include/ignite/examples/organization.h index 2701933d80a69..61df3f6695d39 100644 --- a/modules/platforms/cpp/examples/include/ignite/examples/organization.h +++ b/modules/platforms/cpp/examples/include/ignite/examples/organization.h @@ -66,48 +66,23 @@ namespace ignite namespace binary { template<> - struct BinaryType + struct BinaryType : BinaryTypeDefaultAll { - static int32_t GetTypeId() - { - return GetBinaryStringHashCode("Organization"); - } - static void GetTypeName(std::string& dst) { dst = "Organization"; } - static int32_t GetFieldId(const char* name) - { - return GetBinaryStringHashCode(name); - } - - static int32_t GetHashCode(const ignite::examples::Organization& obj) - { - return 0; - } - - static bool IsNull(const ignite::examples::Organization& obj) - { - return false; - } - - static void GetNull(ignite::examples::Organization& dst) - { - dst = ignite::examples::Organization("", ignite::examples::Address()); - } - - static void Write(BinaryWriter& writer, const ignite::examples::Organization& obj) + static void Write(BinaryWriter& writer, const examples::Organization& obj) { writer.WriteString("name", obj.name); - writer.WriteObject("addr", obj.addr); + writer.WriteObject("addr", obj.addr); } - static void Read(BinaryReader& reader, ignite::examples::Organization& dst) + static void Read(BinaryReader& reader, examples::Organization& dst) { dst.name = reader.ReadString("name"); - dst.addr = reader.ReadObject("addr"); + dst.addr = reader.ReadObject("addr"); } }; } From a726b464629c042ffb1c499dfc761208c00a519a Mon Sep 17 00:00:00 2001 From: Igor Sapego Date: Fri, 12 Apr 2019 14:46:55 +0300 Subject: [PATCH 2/3] IGNITE-11703: Fix for tests --- .../include/ignite/binary_test_defs.h | 62 +++++++++++++++---- 1 file changed, 51 insertions(+), 11 deletions(-) diff --git a/modules/platforms/cpp/core-test/include/ignite/binary_test_defs.h b/modules/platforms/cpp/core-test/include/ignite/binary_test_defs.h index 2cfaa60c88236..34f43ac728e74 100644 --- a/modules/platforms/cpp/core-test/include/ignite/binary_test_defs.h +++ b/modules/platforms/cpp/core-test/include/ignite/binary_test_defs.h @@ -27,14 +27,14 @@ namespace ignite_test { namespace core { - namespace binary + namespace binary { class BinaryDummy { // No-op. }; - class BinaryInner + class BinaryInner { public: BinaryInner(); @@ -76,12 +76,12 @@ namespace ignite_test BinaryFields(int32_t val1, int32_t val2, int32_t rawVal1, int32_t rawVal2) : val1(val1), val2(val2), rawVal1(rawVal1), rawVal2(rawVal2) { - // No-op. + // No-op. } friend bool operator==(const BinaryFields& one, const BinaryFields& two) { - return one.val1 == two.val1 && one.val2 == two.val2 && + return one.val1 == two.val1 && one.val2 == two.val2 && one.rawVal1 == two.rawVal1 &&one.rawVal2 == two.rawVal2; } }; @@ -159,14 +159,24 @@ namespace ignite } }; - template<> - struct BinaryType : BinaryTypeDefaultAll + template<> + struct BinaryType : BinaryTypeDefaultHashing { static void GetTypeName(std::string& dst) { dst = "BinaryInner"; } + static bool IsNull(const gt::BinaryInner& obj) + { + return obj.GetValue() == 0; + } + + static void GetNull(gt::BinaryInner& dst) + { + dst = gt::BinaryInner(0); + } + static void Write(BinaryWriter& writer, const gt::BinaryInner& obj) { writer.WriteInt32("val", obj.GetValue()); @@ -181,17 +191,27 @@ namespace ignite }; template<> - struct BinaryType : BinaryTypeDefaultAll + struct BinaryType : BinaryTypeDefaultHashing { static void GetTypeName(std::string& dst) { dst = "BinaryOuter"; } + static bool IsNull(const gt::BinaryOuter& obj) + { + return obj.GetValue() == 0 && obj.GetInner().GetValue(); + } + + static void GetNull(gt::BinaryOuter& dst) + { + dst = gt::BinaryOuter(0, 0); + } + static void Write(BinaryWriter& writer, const gt::BinaryOuter& obj) { writer.WriteObject("inner", obj.GetInner()); - writer.WriteInt32("val", obj.GetValue()); + writer.WriteInt32("val", obj.GetValue()); } static void Read(BinaryReader& reader, gt::BinaryOuter& dst) @@ -204,13 +224,23 @@ namespace ignite }; template<> - struct BinaryType : BinaryTypeDefaultAll + struct BinaryType : BinaryTypeDefaultHashing { static void GetTypeName(std::string& dst) { dst = "BinaryFields"; } + static bool IsNull(const gt::BinaryFields& obj) + { + return false; + } + + static void GetNull(gt::BinaryFields&) + { + throw std::runtime_error("Must not be called."); + } + static void Write(BinaryWriter& writer, const gt::BinaryFields& obj) { writer.WriteInt32("val1", obj.val1); @@ -237,13 +267,23 @@ namespace ignite }; template<> - struct BinaryType : BinaryTypeDefaultAll + struct BinaryType : BinaryTypeDefaultHashing { static void GetTypeName(std::string& dst) { dst = "PureRaw"; } + static bool IsNull(const gt::PureRaw& obj) + { + return false; + } + + static void GetNull(gt::PureRaw&) + { + throw std::runtime_error("Must not be called."); + } + static void Write(BinaryWriter& writer, const gt::PureRaw& obj) { BinaryRawWriter rawWriter = writer.RawWriter(); @@ -263,4 +303,4 @@ namespace ignite } } -#endif \ No newline at end of file +#endif From d5489147d412b61c4391d59f34a66befde5b59e6 Mon Sep 17 00:00:00 2001 From: Igor Sapego Date: Wed, 17 Apr 2019 18:56:29 +0300 Subject: [PATCH 3/3] IGNITE-11703: Review-related fixes --- .../platforms/cpp/binary/include/ignite/binary/binary_type.h | 2 +- .../platforms/cpp/core-test/include/ignite/binary_test_defs.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/platforms/cpp/binary/include/ignite/binary/binary_type.h b/modules/platforms/cpp/binary/include/ignite/binary/binary_type.h index 65f05ff2b3c99..84de6483ddfba 100644 --- a/modules/platforms/cpp/binary/include/ignite/binary/binary_type.h +++ b/modules/platforms/cpp/binary/include/ignite/binary/binary_type.h @@ -207,7 +207,7 @@ namespace ignite }; /** - * Default implementations of BinaryType hashing functions. + * Default implementations of BinaryType hashing functions and non-null type behaviour. */ template struct IGNITE_IMPORT_EXPORT BinaryTypeDefaultAll : diff --git a/modules/platforms/cpp/core-test/include/ignite/binary_test_defs.h b/modules/platforms/cpp/core-test/include/ignite/binary_test_defs.h index 34f43ac728e74..51aeff0bdcf58 100644 --- a/modules/platforms/cpp/core-test/include/ignite/binary_test_defs.h +++ b/modules/platforms/cpp/core-test/include/ignite/binary_test_defs.h @@ -82,7 +82,7 @@ namespace ignite_test friend bool operator==(const BinaryFields& one, const BinaryFields& two) { return one.val1 == two.val1 && one.val2 == two.val2 && - one.rawVal1 == two.rawVal1 &&one.rawVal2 == two.rawVal2; + one.rawVal1 == two.rawVal1 && one.rawVal2 == two.rawVal2; } };