diff --git a/src/main.cpp b/src/main.cpp index 8687263..10a7008 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,67 +6,116 @@ #include #include -class Foo : public Serializable +/** + * @brief Plop = Serializable mother class (incomplete) + */ +class Plop : public Serializable +{ +public: + P_REFERENCE (Plop, myPlop) +}; + +/** + * @brief Bar = Serializable class Inheriting Plop + */ +class Bar : public Plop +{ +public: + P_STRING (myTruc) + + SERIALIZABLE(Bar, + CAST(myTruc), + CAST(myPlop)) + + Bar() + { + myTruc = "Je suis un Bar"; + myPlop = this; + } +}; + +/** + * @brief Bar = Other Serializable class Inheriting Plop, referencing an instance of the Bar class + */ +class Foo : public Plop { public: P_INT (myInt) P_STRING (myString) P_VECTOR_FLOAT(myFloatArray) P_VEC3 (myVec3) - P_REFERENCE (Foo, myFoo) SERIALIZABLE(Foo, CAST(myInt), CAST(myString), CAST(myFloatArray), CAST(myVec3), - CAST(myFoo)) + CAST(myPlop)) Foo() { myInt = 3; - myString = "plop"; + myString = "Je suis un Foo"; myFloatArray.push_back(1.5); myFloatArray.push_back(1.3); myFloatArray.push_back(2.5); myFloatArray.push_back(6.2); myVec3 = glm::vec3(0.1, 0, 5.4); - myFoo = this; + Bar* bar = new Bar(); + bar->myPlop = nullptr; + myPlop = bar; } }; +INIT_SERIALIZABLE(Bar) INIT_SERIALIZABLE(Foo) int main() { - // init - Foo* f1 = new Foo(); - f1->myString = "Hello, i am foo 1"; - Foo* f2 = new Foo(); - f2->myString = "How's it goin', my name is \"Foo 2\""; - f1->myFloatArray.clear(); - f2->myVec3 = glm::vec3(1, 2, 3); - f1->myFoo = f2; - f2->myFoo = nullptr; + // testing polymorphism on Serializables + Foo* foo = new Foo(); + + // print before + std::cout << "Before :" << std::endl; + ObjectSaver printer; + printer.addObject(foo); + printer.saveAscii(std::cout); // saving as binary - ObjectSaver saver; - saver.addObject(f1); - std::fstream outFile; - outFile.open("test.bin", std::ios_base::out); - saver.saveBinary(outFile); - outFile.close(); + ObjectSaver saverBinary; + saverBinary.addObject(foo); + std::fstream outFileBinary; + outFileBinary.open("test.bin", std::ios_base::out); + saverBinary.saveBinary(outFileBinary); + outFileBinary.close(); // loading as binary - ObjectLoader loader; - std::fstream inFile; - inFile.open("test.bin", std::ios_base::in); - loader.loadBinary(inFile); - inFile.close(); + ObjectLoader loaderBinary; + std::fstream inFileBinary; + inFileBinary.open("test.bin", std::ios_base::in); + loaderBinary.loadBinary(inFileBinary); + inFileBinary.close(); - // print result - ObjectSaver printer; - for(Foo* f : loader.getObjects()) + // saving as json + ObjectSaver saverAscii; + for(Foo* f : loaderBinary.getObjects()) + saverAscii.addObject(f); + std::fstream outFileAscii; + outFileAscii.open("test.txt", std::ios_base::out); + saverAscii.saveAscii(outFileAscii); + outFileAscii.close(); + + // loading as json + ObjectLoader loaderAscii; + std::fstream inFileAscii; + inFileAscii.open("test.txt", std::ios_base::in); + loaderAscii.loadAscii(inFileAscii); + inFileAscii.close(); + + // print after + std::cout << "After :" << std::endl; + printer.clearObjects(); + for(Foo* f : loaderAscii.getObjects()) printer.addObject(f); printer.saveAscii(std::cout); diff --git a/src/property.h b/src/property.h index 81614db..a136f7f 100644 --- a/src/property.h +++ b/src/property.h @@ -110,6 +110,7 @@ public: template class Property : public AbstractProperty { +protected: T m_value; public: Property(T val) : m_value(val) {} @@ -136,6 +137,7 @@ public: template class Property : public AbstractProperty { +protected: T* m_value; public: Property(T* val) : m_value(val) {} @@ -166,6 +168,7 @@ public: template class ArrayProperty : public AbstractArrayProperty { +protected: std::vector m_value; public: ArrayProperty(const std::vector &val) : m_value(val) {} diff --git a/src/serializationmanager.cpp b/src/serializationmanager.cpp index 6052b36..7fa452a 100644 --- a/src/serializationmanager.cpp +++ b/src/serializationmanager.cpp @@ -28,6 +28,17 @@ void ObjectSaver::addObject(Serializable *object) } } +void ObjectSaver::removeObject(Serializable *object) +{ + if(object == nullptr) + return; + const std::string &type = object->getType(); + ObjectType &objectsOfSameType = m_objects[type]; + objectsOfSameType.erase(object); + if(objectsOfSameType.size() == 0) + m_objects.erase(type); +} + std::ostream& ObjectSaver::saveBinary(std::ostream& os) { // loop on serializable types @@ -87,9 +98,13 @@ std::ostream& ObjectSaver::saveBinary(std::ostream& os) std::ostream& ObjectSaver::saveAscii(std::ostream& os) { os << "{" << std::endl; + int nbTypes = m_objects.size(); + int currentType = 0; // loop on serializable types for(const std::pair &type : m_objects) { + ++currentType; + // serialize type os << "\t\"" << type.first << "\": [" << std::endl; @@ -144,7 +159,10 @@ std::ostream& ObjectSaver::saveAscii(std::ostream& os) else os << "," << std::endl; } - os << "\t]" << std::endl; + if(nbTypes == currentType) + os << "\t]" << std::endl; + else + os << "\t]," << std::endl; } return os << "}" << std::endl; } @@ -272,7 +290,7 @@ std::istream& ObjectLoader::loadAscii(std::istream& is) Serializable* object = instancer(); objectsOfThatType.push_back(object); PropertySet* properties = object->getProperties(); - for(AbstractProperty* p : *properties) + for(AbstractProperty* p : *properties) // loop on the properties of this object { if(p->getPropertyType() == AbstractProperty::REFERENCE) { @@ -284,7 +302,6 @@ std::istream& ObjectLoader::loadAscii(std::istream& is) ss.ignore(1); // , Property id; // id of the object id.loadAscii(ss); - ss.ignore(1); PendingReference ref; ref.ptr = &(((Property*)p)->getValueRef()); ref.type = type.getValueRef(); @@ -322,19 +339,26 @@ std::istream& ObjectLoader::loadAscii(std::istream& is) } else ss.ignore(6); // "null" - if(ss.peek() != ',') + if(ss.peek() == ',') ss.ignore(1); // , } ss.ignore(1); // ] } else p->loadAscii(ss); + // loop on the properties of this object if(p != properties->back()) ss.ignore(1); // , } + // loop on the objects of that type ss.ignore(1); // } + if(ss.peek() == ',') + ss.ignore(1); // , } + // loop on serializable types ss.ignore(1); // ] + if(ss.peek() == ',') + ss.ignore(1); // , } buildReferences(); return is; diff --git a/src/serializationmanager.h b/src/serializationmanager.h index 267ab50..28be2a6 100644 --- a/src/serializationmanager.h +++ b/src/serializationmanager.h @@ -15,6 +15,12 @@ public: template void addObject(T *object) { addObject((Serializable*)object); } + void removeObject(Serializable *object); + template + void removeObject(T *object) { removeObject((Serializable*)object); } + + void clearObjects() { m_objects.clear(); } + std::ostream& saveBinary(std::ostream& os); std::ostream& saveAscii(std::ostream& os); };