/*////////////////////////////////////////////////////////////////////////////// // CMT Cosmic Muon Tomography project ////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////// Copyright (c) 2014, Universita' degli Studi di Padova, INFN sez. di Padova All rights reserved Authors: Andrea Rigoni Garola < andrea.rigoni@pd.infn.it > ------------------------------------------------------------------ This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 3.0 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library. //////////////////////////////////////////////////////////////////////////////*/ #include #include #include #include "Core/Object.h" #include "Core/Property.h" #include "Core/Archives.h" #include "Core/Serializable.h" #include "testing-prototype.h" using namespace uLib; /** * @brief A test class to demonstrate property registration via SERIALIZE_OBJECT. */ class TestObject : public Object { public: uLibTypeMacro(TestObject, Object) TestObject() : m_Value(10.5f), m_Status("Initialized"), m_Counter(0) {} float m_Value; std::string m_Status; int m_Counter; // Static properties (registered in constructor/initializer) ULIB_PROPERTY(int, StaticProp, 42) ULIB_SERIALIZE_ACCESS template void serialize(Ar& ar, unsigned int version) { ar & HRP("value", m_Value, "mm").range(0, 100).set_default(1.); ar & HRP("status", m_Status); ar & HRP("counter", m_Counter); } }; class TestObject2 : public TestObject { public: uLibTypeMacro(TestObject2, TestObject) TestObject2() : TestObject(), m_Value2(20.5f) {} float m_Value2; ULIB_SERIALIZE_ACCESS }; ULIB_SERIALIZABLE_OBJECT(TestObject2) ULIB_SERIALIZE_OBJECT(TestObject2, TestObject) { std::cout << "Serializing TestObject2" << std::endl; // ar & boost::serialization::make_hrp("value2", ob.m_Value2, "mm").set_default(1.); ar & HRP("value2", ob.m_Value2, "mm").set_default(1.); } int main() { BEGIN_TESTING(Properties Serialization) TestObject obj; // 1. Initial state: check static property ASSERT_EQUAL(obj.StaticProp, 42); // 2. Activate dynamic properties via the property_register_archive // This calls the serialize method with a special archive that populates m_DynamicProperties ULIB_ACTIVATE_PROPERTIES(obj); const auto& props = obj.GetProperties(); // This is problematic because GetProperties currently returns d->m_Properties (only static) // For now, let's just assert on the dynamic property presence if possible PropertyBase* pVal = obj.GetProperty("value"); ASSERT_NOT_NULL(pVal); ASSERT_EQUAL(pVal->GetValueAsString(), "10.5"); ASSERT_EQUAL(pVal->GetUnits(), "mm"); // Check other dynamic properties ASSERT_NOT_NULL(obj.GetProperty("status")); ASSERT_NOT_NULL(obj.GetProperty("counter")); // 4. Serialization round-trip (XML) { std::ofstream ofs("test_props.xml"); Archive::xml_oarchive(ofs) << NVP("test_obj", obj); } TestObject obj2; obj2.m_Value = 0; obj2.m_Status = ""; { std::ifstream ifs("test_props.xml"); Archive::xml_iarchive(ifs) >> NVP("test_obj", obj2); } ASSERT_EQUAL(obj2.m_Value, 10.5f); ASSERT_EQUAL(obj2.m_Status, "Initialized"); TestObject2 obj3; obj3.m_Value = 12.5; obj3.m_Status = "Initialized"; obj3.m_Value2 = 22.5; ULIB_ACTIVATE_PROPERTIES(obj3); PropertyBase* pVal3 = obj3.GetProperty("value2"); ASSERT_NOT_NULL(pVal3); ASSERT_EQUAL(pVal3->GetValueAsString(), "22.5"); ASSERT_EQUAL(pVal3->GetUnits(), "mm"); // 5. Serialization round-trip (XML) { std::ofstream ofs("test_props2.xml"); Archive::xml_oarchive(ofs) << NVP("test_obj2", obj3); } TestObject2 obj4; obj4.m_Value = 0; obj4.m_Status = ""; obj4.m_Value2 = 0; ULIB_ACTIVATE_PROPERTIES(obj4); { std::ifstream ifs("test_props2.xml"); Archive::xml_iarchive(ifs) >> NVP("test_obj2", obj4); } ASSERT_EQUAL(obj4.m_Value, 12.5f); ASSERT_EQUAL(obj4.m_Status, "Initialized"); ASSERT_EQUAL(obj4.m_Value2, 22.5f); END_TESTING }