/////////////////////////////////////////////////////////////////////// // serialize.cpp - provides example use of serialization interface // // ver 1.0 // // Language: Visual C++, ver 7.0 // // Platform: Dell Dimension 8300, Windows XP Pro, SP1.0 // // Application: CSE687 - Object Oriented Design, Pr#3 prototype // // Author: Jim Fawcett, CST 2-187, Syracuse University // // (315) 443-3948, jfawcett@twcny.rr.com // /////////////////////////////////////////////////////////////////////// #ifdef TEST_SERIALIZE #include #include #include "serialize.h" /////////////////////////////////////////////////////////////////////// // class announcer demonstrates persistence via serialization // holds pointer to double value on heap // class announcer : public persist { public: announcer() : pDouble(new double(0)) { } ~announcer() { delete pDouble; } void say(); double& value() { return *pDouble; } std::string serialize(); void deserialize(const std::string& xmlStr); private: double* pDouble; }; //----< function to identify type and value >-------------------------- void announcer::say() { std::cout << "\n " << identity::id(this) << "\n my value = " << (*pDouble); } //----< convert XML string to instance state and copy in >------------- void announcer::deserialize(const std::string& xmlStr) { std::string msg = "invalied xml string in deserialize"; // extract XML type tag, validate, and get element body parser.xmlString(xmlStr); std::string Tag = parser.tag(); if(Tag != identity::id(this)) throw exception(msg.c_str()); std::string valueElement = parser.body(); // convert body to persisted value and put on heap, // deleting value put there by constructor parser.xmlString(valueElement); Tag = parser.tag(); if(Tag != "double") throw exception(msg.c_str()); valueElement = parser.body(); double value = convert::FromString(valueElement); delete pDouble; pDouble = new double(value); } // //----< convert instance state to an XML string >---------------------- std::string announcer::serialize() { // make inner xml element to hold double value element v("double"); v.body(convert::ToString(*pDouble)); // make outer xml element to identify type element e(identity::id(this)); e.body(v); return e; } //----< entry point >-------------------------------------------------- void main() { std::cout << "\n Testing Serialization Interface " << "\n =================================\n"; std::cout << "\n Creating an instance of announcer " << "\n -----------------------------------\n"; announcer a; a.value() = 3.1415927; a.say(); std::cout << "\n"; std::cout << "\n Serializing announcer instance " << "\n --------------------------------\n"; std::string s = a.serialize(); std::cout << "\n " << s; std::cout << "\n\n"; std::cout << "\n Creating a new instance of announcer " << "\n --------------------------------------\n"; announcer newa; newa.say(); std::cout << "\n\n"; std::cout << "\n Deserializing persisted value to new instance " << "\n -----------------------------------------------\n"; newa.deserialize(s); // usually newa would come from an object factory // which would call deserialize,using // user supplied XML string newa.say(); std::cout << "\n\n"; } #endif