interreality.org [VOS]
[Home] [About]
[Screenshots]
[Download]
[News]
[Community]
[Documentation] [Manual]
[Bugs & Requests] [Wiki]

/home/tetron/hack/vos/libs/vos/vos/vobjectstore.hh

Go to the documentation of this file.
00001 #ifndef _VOBJECTSTORE_HH_
00002 #define _VOBJECTSTORE_HH_
00003 
00004 #include <vos/vos/vosdefs.hh>
00005 #include <vos/vos/vobject.hh>
00006 #include <vos/vos/site.hh>
00007 #include <vos/vos/message.hh>
00008 #include <vos/vos/vobjectstore.hh>
00009 #include <vos/vutil/timer.hh>
00010 
00011 namespace VOS {
00012 
00013 /** @class VobjectStore vos/vos/vobjectstore.hh
00014  */
00015 class VOS_API VobjectStore
00016 {
00017 public:
00018     /** An entry in the Vobject store table */
00019     struct Entry
00020     {
00021         Entry() { }
00022         Entry(const Entry& e)
00023             : vobject(e.vobject), types(e.types), mb(e.mb),
00024               pos(e.pos), savechildren(e.savechildren), blocks(e.blocks),
00025               acls(e.acls)
00026             { }
00027 
00028         ~Entry() {}
00029 
00030         /** The Vobject itself. */
00031         VUtil::vRef<Vobject> vobject;
00032 
00033         /** The type strings to save. */
00034         std::set<std::string> types;
00035 
00036         /** A list of messages which, when replayed, will restore the state
00037             of this Vobject. */
00038         VUtil::vRef<MessageBlock> mb;
00039 
00040         /** Used by remote CODs, the position of this object on the
00041             @em remote site. */
00042         int pos;
00043 
00044         /** Should this vobject's child table be written out? */
00045         bool savechildren;
00046 
00047         // The following two are used by data serialization formats
00048         // like XOD that use the persistance interface and save ACLs
00049         // explicitly rather than reconstructing the state via message
00050         // replays.
00051 
00052         /** Persistance data blocks */
00053         std::map<std::string, std::string> blocks;
00054 
00055         /** Access control lists */
00056         std::map< std::string, std::vector<std::string> > acls;
00057     };
00058 
00059     struct PCR
00060     {
00061         std::string context;
00062         std::string child;
00063     };
00064 
00065 protected:
00066     VUtil::vRef<Site> ls;
00067     std::map<std::string, Entry*> vobjectTable;
00068     std::deque<Entry*> vobjectorder;
00069 
00070 public:
00071     /** Constructor.
00072         @param ls The local site any new vobjects will be created on.
00073      */
00074     VobjectStore(Site& loadsite);
00075 
00076     VobjectStore(const VobjectStore& store);
00077 
00078     virtual ~VobjectStore();
00079 
00080     /** Add a Vobject to this store.
00081         @param v the Vobject
00082         @param portable passed to Vobject::saveState -- should this
00083         save site- or configuration-specific information (possibly
00084         making for a smaller file) or embed all necessary data?
00085         @param saveChildren should the child list be saved?
00086         @return the position of this entry in the vobject table
00087         @note This will use Vobject::saveState to get additional details
00088     */
00089     virtual int addVobject(Vobject* v, bool portable = true, bool saveChildren = true);
00090 
00091     /** Add a Vobject to this store.  This uses the supplied values,
00092         overriding the information stored in the Vobject itself.  It does not
00093         call Vobject::saveState()
00094         @param v the Vobject
00095         @param types the type strings to be saved
00096         @param mb a message block of messages used to restore the
00097         state of this Vobject
00098         @param saveChildren should the child list be saved?
00099         @return the position of this entry in the vobject table
00100     */
00101     virtual int addVobject(Vobject* v, const std::set<std::string>& types,
00102                            MessageBlock* mb, bool saveChildren);
00103 
00104     /** Add a Vobject to this store.  This uses the supplied values,
00105         overriding the information stored in the Vobject itself.  It does not
00106         call Vobject::saveState()
00107         @param v the Vobject
00108         @param types the type strings to be saved
00109         @param mb a message block of messages used to restore the
00110         state of this Vobject
00111         @param saveChildren should the child list be saved?
00112         @return the position of this entry in the vobject table
00113     */
00114     virtual int addVobject(Vobject* v, const std::deque<std::string>& types,
00115                            MessageBlock* mb, bool saveChildren);
00116 
00117     virtual int addVobject(Vobject* v, TypeSetIterator types,
00118                            MessageBlock* mb, bool saveChildren);
00119 
00120     /** Number of Vobjects in the Vobject table.
00121         @return the table size
00122     */
00123     virtual int numVobjects();
00124 
00125     /** Get the Vobject from the Vobject table by site name.
00126         @param name the site name
00127         @return the entry, or NULL if invalid
00128     */
00129     virtual Entry* getVobject(const std::string& name);
00130 
00131     /** Get the Vobject by position in the Vobject table.
00132         @param pos the position
00133         @return the entry, or NULL if invalid
00134      */
00135     virtual Entry* getVobject(int pos);
00136 
00137     /** Remove a Vobject from the Vobject table.
00138         @param name the site name
00139     */
00140     virtual void removeVobject(const std::string& name);
00141 
00142     /** Remove a Vobject from the Vobject table.
00143         @param pos the position in the Vobject table
00144     */
00145     virtual void removeVobject(int pos);
00146 
00147     /** Clear the Vobject table. */
00148     virtual void clear();
00149 
00150     /** Get the site that vobjects are to be added to if loading a
00151         vobject store from a file.  Has no effect on saving.
00152         @return the site
00153     */
00154     virtual VUtil::vRef<Site> getTargetSite();
00155 
00156     /** Merge the contents of a Vobject store into this one (must be on the same site).
00157         Vobjects are added with their site name.
00158         @param vs the store holding vobjects that are added to this store
00159      */
00160     virtual void addStore(VobjectStore* vs);
00161 };
00162 
00163 typedef std::map<std::string, void*> ImpExpEnv;
00164 
00165 class VOS_API VobjectInput : public virtual VobjectStore
00166 {
00167 public:
00168     VobjectInput(Site& ls) : VobjectStore(ls) { }
00169     VobjectInput(const VobjectStore& v) : VobjectStore(v) { }
00170     virtual ~VobjectInput() { };
00171 
00172     /** Read a set of Vobjects from the initialized iostream */
00173     virtual void readVobjects(std::istream& input,
00174                               ImpExpEnv& environment) = 0;
00175 
00176     /** Read a set of Vobjects from the supplied data buffer.
00177         @param buffer the source data
00178         @param size the length of the data
00179     */
00180     virtual void readVobjects(unsigned char* buffer, unsigned int size,
00181                               ImpExpEnv& environment) = 0;
00182 
00183 };
00184 
00185 class VOS_API VobjectOutput : public virtual VobjectStore
00186 {
00187 public:
00188     VobjectOutput(Site& ls) : VobjectStore(ls) { }
00189     VobjectOutput(const VobjectStore& vs) : VobjectStore(vs) { }
00190     virtual ~VobjectOutput() { }
00191 
00192     /** Write a set of Vobjects to the supplied ostream.
00193     */
00194     virtual void writeVobjects(std::ostream& output,
00195                                ImpExpEnv& environment) = 0;
00196 
00197     /** Write a set of Vobjects to a buffer.  The buffer is malloc()'d
00198         and returned via the supplied parameters.
00199         @param buffer the data buffer holding the data.  You
00200         should free() this when you're done.
00201         @param size the size of the buffer
00202     */
00203     virtual void writeVobjects(unsigned char** buffer,
00204                                unsigned int* size,
00205                                ImpExpEnv& environment) = 0;
00206 };
00207 
00208 class VOS_API VobjectStoreLoader
00209 {
00210 public:
00211     virtual ~VobjectStoreLoader() { }
00212 
00213     virtual std::string getLoaderDescription() = 0;
00214 
00215     virtual bool isSupportedLoadFormat(unsigned char* buffer, unsigned int size) = 0;
00216     virtual bool isSupportedLoadFormat(std::istream& input) = 0;
00217 
00218     virtual VobjectStore* loadVobjects(Site& ls,
00219                                        unsigned char* buffer, unsigned int size,
00220                                        ImpExpEnv& environment) = 0;
00221     virtual VobjectStore* loadVobjects(Site& ls,
00222                                        std::istream& input,
00223                                        ImpExpEnv& environment) = 0;
00224 };
00225 
00226 class VOS_API VobjectStoreSaver
00227 {
00228 public:
00229     virtual ~VobjectStoreSaver() { }
00230 
00231     virtual std::string getFileExtension(const std::string& filetype) = 0;
00232     virtual bool isSupportedSaveFormat(const std::string& filetype) = 0;
00233     virtual std::string getSaverDescription() = 0;
00234 
00235     virtual void saveVobjects(VobjectStore* store,
00236                               const std::string& filetype,
00237                               unsigned char** buffer, unsigned int* size,
00238                               ImpExpEnv& environment) = 0;
00239     virtual void saveVobjects(VobjectStore* store,
00240                               const std::string& filetype,
00241                               std::ostream& output,
00242                               ImpExpEnv& environment) = 0;
00243 };
00244 
00245 class VOS_API UnsupportedFiletypeError : public std::runtime_error
00246 {
00247 public:
00248     UnsupportedFiletypeError(const std::string& s) : runtime_error(s) { };
00249 };
00250 
00251 class VOS_API FileAccessError : public std::runtime_error
00252 {
00253 public:
00254     FileAccessError(const std::string& s) : runtime_error(s) { };
00255 };
00256 
00257 class VOS_API VobjectImportExport
00258 {
00259 private:
00260     static std::vector<VobjectStoreLoader*>& loaders();
00261     static std::vector<VobjectStoreSaver*>& savers();
00262 
00263     VobjectImportExport() { }
00264 
00265 public:
00266     static void addLoader(VobjectStoreLoader* l);
00267     static void addSaver(VobjectStoreSaver* s);
00268 
00269     static const std::vector<VobjectStoreLoader*>& getLoaders();
00270     static const std::vector<VobjectStoreSaver*>& getSavers();
00271 
00272     static VobjectStoreLoader* selectLoader(unsigned char* buffer, unsigned int size);
00273     static VobjectStoreLoader* selectLoader(std::istream& input);
00274 
00275     static VobjectStore* load(Site& ls, unsigned char* buffer, unsigned int size,
00276                               VobjectStoreLoader** l = 0);
00277     static VobjectStore* load(Site& ls, unsigned char* buffer, unsigned int size,
00278                               ImpExpEnv& environment,
00279                               VobjectStoreLoader** l = 0);
00280 
00281 
00282     static VobjectStore* load(Site& ls, std::istream& input,
00283                               VobjectStoreLoader** l = 0);
00284     static VobjectStore* load(Site& ls, std::istream& input,
00285                               ImpExpEnv& environment,
00286                               VobjectStoreLoader** l = 0);
00287 
00288 
00289     static VobjectStore* loadFile(Site& ls, const std::string& filename,
00290                                   VobjectStoreLoader** l = 0);
00291     static VobjectStore* loadFile(Site& ls, const std::string& filename,
00292                                   ImpExpEnv& environment,
00293                                   VobjectStoreLoader** l = 0);
00294 
00295 
00296     static VobjectStoreSaver* selectSaver(const std::string& filetype);
00297 
00298     static void save(VobjectStore* vs, const std::string& filetype,
00299                      unsigned char** buffer, unsigned int* size,
00300                      VobjectStoreSaver** s = 0);
00301     static void save(VobjectStore* vs, const std::string& filetype,
00302                      unsigned char** buffer, unsigned int* size,
00303                      ImpExpEnv& environment,
00304                      VobjectStoreSaver** s = 0);
00305 
00306 
00307     static void save(VobjectStore* vs, const std::string& filetype,
00308                      std::ostream& out,
00309                      VobjectStoreSaver** s = 0);
00310     static void save(VobjectStore* vs, const std::string& filetype,
00311                      std::ostream& out,
00312                      ImpExpEnv& environment,
00313                      VobjectStoreSaver** s = 0);
00314 
00315 
00316     static void saveFile(VobjectStore* vs,
00317                          const std::string& filetype,
00318                          const std::string& filename,
00319                          VobjectStoreSaver** s = 0);
00320     static void saveFile(VobjectStore* vs,
00321                          const std::string& filetype,
00322                          const std::string& filename,
00323                          ImpExpEnv& environment,
00324                          VobjectStoreSaver** s = 0);
00325 };
00326 
00327 
00328 }
00329 
00330 #endif