Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

plist.cc

Go to the documentation of this file.
00001 #include "plist.h"
00002 #include <stdexcept>
00003 
00004 using namespace std;
00005 
00006 namespace plist {
00007 
00008   ObjectBase* loadXML(xmlNode* node) {
00009     ObjectBase* obj=NULL;
00010     if(ObjectBase::xNodeHasName(node,"array")) {
00011       Array * a=new Array;
00012       a->setLoadSavePolicy(Collection::SYNC,Collection::SYNC); // just in case defaults change...
00013       obj=a;
00014     } else if(ObjectBase::xNodeHasName(node,"dict")) {
00015       Dictionary * d=new Dictionary;
00016       d->setLoadSavePolicy(Collection::SYNC,Collection::SYNC); // just in case defaults change...
00017       obj=d;
00018     } else if(ObjectBase::xNodeHasName(node,"real"))
00019       obj=new Primitive<double>;
00020     else if(ObjectBase::xNodeHasName(node,"integer"))
00021       obj=new Primitive<long>;
00022     else if(ObjectBase::xNodeHasName(node,"string"))
00023       obj=new Primitive<std::string>;
00024     else if(ObjectBase::xNodeHasName(node,"true"))
00025       obj=new Primitive<bool>;
00026     else if(ObjectBase::xNodeHasName(node,"false"))
00027       obj=new Primitive<bool>;
00028     else
00029       return NULL;
00030     obj->loadXML(node);
00031     return obj;
00032   }
00033   
00034   //! allows us to use the LoadSave suite for loading and parsing general XML functions, but forwards loadXML to plist::loadXML() and stores the result as a member
00035   class PolymorphicLoader : public ObjectBase {
00036   public:
00037     PolymorphicLoader() : ObjectBase(), obj(NULL) {} //!< constructor
00038     virtual ~PolymorphicLoader() {} //!< destructor
00039     PolymorphicLoader(const PolymorphicLoader& p) : ObjectBase(p), obj(NULL) {} //!< copy constructor -- doesn't copy #obj, sets the local instance to NULL
00040     PolymorphicLoader& operator=(const PolymorphicLoader& p) { ObjectBase::operator=(p); return *this; } //!< copy constructor -- doesn't copy #obj, keeps current value of #obj
00041     virtual void loadXML(xmlNode* node) { obj=plist::loadXML(node); } //!< forward call to the static plist implementation
00042     virtual void saveXML(xmlNode * /*node*/) const { throw std::logic_error("PolymorphicLoader::saveXML called, should not happen (saveXML on the object itself...)"); } //!< shouldn't ever be called -- call saveXML() on #obj directly
00043     
00044     virtual std::string toString() const { return ""; } //!< shouldn't ever be called
00045     virtual long toLong() const { return 0; } //!< shouldn't ever be called
00046     virtual double toDouble() const { return 0; } //!< shouldn't ever be called
00047     virtual void set(const ObjectBase&) {} //!< shouldn't ever be called
00048     PLIST_CLONE_DEF(PolymorphicLoader,new PolymorphicLoader); //!< shouldn't ever be called
00049     
00050     ObjectBase * obj; //!< storage of results from loadXML
00051   };
00052   PLIST_CLONE_IMP(PolymorphicLoader,new PolymorphicLoader);
00053   
00054   ObjectBase* loadFile(const std::string& file) {
00055     PolymorphicLoader loader;
00056     if(!loader.loadFile(file.c_str()))
00057       return NULL;
00058     return loader.obj;
00059   }
00060   
00061   ObjectBase* loadBuffer(const char* buf, unsigned int len) {
00062     PolymorphicLoader loader;
00063     if(!loader.loadBuffer(buf,len))
00064       return NULL;
00065     return loader.obj;
00066   }
00067   
00068 } //namespace plist
00069 
00070 /*! @file
00071  * @brief 
00072  * @author Ethan Tira-Thompson (ejt) (Creator)
00073  */

Tekkotsu v5.1CVS
Generated Mon May 9 04:58:47 2016 by Doxygen 1.6.3