#ifndef SERIALIZE_H #define SERIALIZE_H ///////////////////////////////////////////////////////////////////// // serialize.h - provides 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 // ///////////////////////////////////////////////////////////////////// /* * Module Operations: * ================== * This module declares a persist interface and two helper classes * that convert to and from strings and provide type identity, * needed by object factories (not provided here). * * The module also provides a demonstration class, derived from the * persist interface to show you how to write the serialize and * deserialize functions. * * The class identity uses Run-Time Type Identification, so you must * enable: * * properties/C\C++/Language/Enable Run-Time Type Info * * If you forget to do this the class will throw run-time exceptions. * * Public Interface: * ================= * class myPersistableClass : public persist * { * public: * std::string serialize(); * void deserialize(const std::string& xmlStr); * }; * * myPersistableClass mpc; * std::string xmlStr = mpc.serialize(); * * myPersistableClass newMpc; // normally used by an object factory * newMpc.deserialize(xmlStr); // " " " " " " * * std::string convert::ToString(anInstance); * // works for any type defining operator<< * aType t = convert::FromString(aString); * // works for any type defining operator>> * std::string ID = identity::id(&anInstance); * // needs RTTI enabled * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Build Process * ============= * Required files: serialize.h, serialize.cpp * * Build command: cl /Gr /EHsc /DTEST_SERIALIZE serialize.cpp * * Maintenance History: * ==================== * ver 1.0 : 28 Mar 04 * - first release * */ // #include #include #include "..\\collection\\collection.h" #include "..\\xmlParser\\xmlParser.h" ///////////////////////////////////////////////////////////////////// // class persist - defines abstract serialization interface // class persist { public: virtual ~persist() {}; virtual std::string serialize()=0; virtual void deserialize(const std::string& xmlStr)=0; protected: xmlParser parser; // provided for (de)serialization }; ///////////////////////////////////////////////////////////////////// // class convert - converts to and from string // template class convert { public: static std::string ToString(const T& t); static T FromString(const std::string& s); }; template std::string convert::ToString(const T& t) { std::ostringstream out; out << t; return out.str(); } template T convert::FromString(const std::string &s) { T t; std::istringstream in(s); in >> t; return t; } ///////////////////////////////////////////////////////////////////// // class identity, needed for object factory // class identity { public: template static std::string id(T* t); }; template std::string identity::id(T* t) { std::string temp = typeid(*t).name(); temp = temp.substr(6,temp.size()-6); // remove "class " return temp; } #endif