diff --git a/lib/binary/serializable.h b/lib/binary/serializable.h index f514151..0a67d7b 100644 --- a/lib/binary/serializable.h +++ b/lib/binary/serializable.h @@ -18,22 +18,31 @@ namespace ReflectiveRapidJSON { * \brief The BinarySerializable class provides the CRTP-base for (de)serializable objects. */ template struct BinarySerializable { - void serialize(std::ostream &outputStream) const; - void deserialize(std::istream &inputStream); + void toBinary(std::ostream &outputStream) const; + void restoreFromBinary(std::istream &inputStream); + static Type fromBinary(std::istream &inputStream); static constexpr const char *qualifiedName = "ReflectiveRapidJSON::BinarySerializable"; }; -template inline void BinarySerializable::serialize(std::ostream &outputStream) const +template inline void BinarySerializable::toBinary(std::ostream &outputStream) const { BinaryReflector::BinarySerializer(&outputStream).write(static_cast(*this)); } -template inline void BinarySerializable::deserialize(std::istream &inputStream) +template inline void BinarySerializable::restoreFromBinary(std::istream &inputStream) { BinaryReflector::BinaryDeserializer(&inputStream).read(static_cast(*this)); } +template +Type BinarySerializable::fromBinary(std::istream &inputStream) +{ + Type object; + static_cast &>(object).restoreFromBinary(inputStream); + return object; +} + /*! * \def The REFLECTIVE_RAPIDJSON_MAKE_BINARY_SERIALIZABLE macro allows to adapt (de)serialization for types defined in 3rd party header files. * \remarks The struct will not have the toBinary() and fromBinary() methods available. Use the corresponding functions in the namespace diff --git a/lib/tests/binaryreflector-boosthana.cpp b/lib/tests/binaryreflector-boosthana.cpp index 4dc68fd..5328cf1 100644 --- a/lib/tests/binaryreflector-boosthana.cpp +++ b/lib/tests/binaryreflector-boosthana.cpp @@ -83,12 +83,10 @@ void BinaryReflectorBoostHanaTests::testSerializingAndDeserializing() stringstream stream(ios_base::in | ios_base::out | ios_base::binary); stream.exceptions(ios_base::failbit | ios_base::badbit); - nestingObject.serialize(stream); - - NestingArrayBinaryHana deserializedObject; - deserializedObject.deserialize(stream); - const TestObjectBinaryHana &deserializedTestObj(deserializedObject.testObjects.at(0)); + nestingObject.toBinary(stream); + const auto deserializedObject(NestingArrayBinaryHana::fromBinary(stream)); + const auto &deserializedTestObj(deserializedObject.testObjects.at(0)); CPPUNIT_ASSERT_EQUAL(nestingObject.name, deserializedObject.name); CPPUNIT_ASSERT_EQUAL(testObject.number, deserializedTestObj.number); CPPUNIT_ASSERT_EQUAL(testObject.number2, deserializedTestObj.number2); diff --git a/lib/tests/binaryreflector.cpp b/lib/tests/binaryreflector.cpp index 86a6a16..c20c24e 100644 --- a/lib/tests/binaryreflector.cpp +++ b/lib/tests/binaryreflector.cpp @@ -240,7 +240,7 @@ void BinaryReflectorTests::testSerializeSimpleStruct() stream.exceptions(ios_base::failbit | ios_base::badbit); m_buffer.resize(m_expectedTestObj.size()); stream.rdbuf()->pubsetbuf(reinterpret_cast(m_buffer.data()), static_cast(m_buffer.size())); - m_testObj.serialize(stream); + m_testObj.toBinary(stream); CPPUNIT_ASSERT_EQUAL(m_expectedTestObj, m_buffer); } @@ -250,8 +250,7 @@ void BinaryReflectorTests::testDeserializeSimpleStruct() stringstream stream(ios_base::in | ios_base::binary); stream.exceptions(ios_base::failbit | ios_base::badbit); stream.rdbuf()->pubsetbuf(reinterpret_cast(m_expectedTestObj.data()), static_cast(m_expectedTestObj.size())); - TestObjectBinary deserialized; - deserialized.deserialize(stream); + const auto deserialized(TestObjectBinary::fromBinary(stream)); assertTestObject(deserialized); } @@ -261,7 +260,7 @@ void BinaryReflectorTests::testSerializeNestedStruct() stream.exceptions(ios_base::failbit | ios_base::badbit); m_buffer.resize(m_expectedNestedTestObj.size()); stream.rdbuf()->pubsetbuf(reinterpret_cast(m_buffer.data()), static_cast(m_buffer.size())); - m_nestedTestObj.serialize(stream); + m_nestedTestObj.toBinary(stream); CPPUNIT_ASSERT_EQUAL(m_expectedNestedTestObj, m_buffer); } @@ -271,9 +270,8 @@ void BinaryReflectorTests::testDeserializeNestedStruct() stringstream stream(ios_base::in | ios_base::binary); stream.exceptions(ios_base::failbit | ios_base::badbit); stream.rdbuf()->pubsetbuf(reinterpret_cast(m_expectedNestedTestObj.data()), static_cast(m_expectedNestedTestObj.size())); - NestingArrayBinary deserialized; - deserialized.deserialize(stream); + const auto deserialized(NestingArrayBinary::fromBinary(stream)); CPPUNIT_ASSERT_EQUAL(m_nestedTestObj.name, deserialized.name); for (const auto &testObj : deserialized.testObjects) { assertTestObject(testObj);