00001 /* SimData: Data Infrastructure for Simulations 00002 * Copyright (C) 2002, 2003 Mark Rose <tm2@stm.lbl.gov> 00003 * 00004 * This file is part of SimData. 00005 * 00006 * This program is free software; you can redistribute it and/or 00007 * modify it under the terms of the GNU General Public License 00008 * as published by the Free Software Foundation; either version 2 00009 * of the License, or (at your option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU General Public License 00017 * along with this program; if not, write to the Free Software 00018 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00019 */ 00020 00027 #ifndef __SIMDATA_DATAARCHIVE_H__ 00028 #define __SIMDATA_DATAARCHIVE_H__ 00029 00030 #include <SimData/Export.h> 00031 #include <SimData/Uniform.h> 00032 #include <SimData/Link.h> 00033 #include <SimData/Exception.h> 00034 #include <SimData/HashUtility.h> 00035 #include <SimData/Namespace.h> 00036 00037 #include <string> 00038 #include <cstdio> 00039 #include <cstdlib> 00040 00041 00042 NAMESPACE_SIMDATA 00043 00044 00046 SIMDATA_EXCEPTION(BadMagic) 00047 00048 00049 SIMDATA_EXCEPTION(BadByteOrder) 00050 00051 00052 SIMDATA_EXCEPTION(CorruptArchive) 00053 00054 00055 SIMDATA_EXCEPTION(IndexError) 00056 00057 00058 SIMDATA_EXCEPTION(ObjectMismatch) 00059 00060 00061 SIMDATA_EXCEPTION(IOError) 00062 00063 00064 SIMDATA_EXCEPTION(MissingInterface); 00065 00066 00067 class DataManager; 00068 class Object; 00069 class InterfaceProxy; 00070 00071 00072 //For SWIG (not currently used): 00073 //struct FP { FILE* f; std::string name; std::string mode; }; 00074 00075 00087 class SIMDATA_EXPORT DataArchive { 00088 00089 friend class LinkBase; 00090 friend class DataManager; 00091 00092 private: 00093 00094 typedef std::vector<char> Buffer; 00095 00102 struct TableEntry { 00104 ObjectID pathhash; 00106 ObjectID classhash; 00108 uint32 offset; 00110 uint32 length; 00111 }; 00112 00125 static DataArchive* defaultArchive; 00126 00127 enum { 00129 AS = 1024, 00131 BUFFERSIZE = 4096, 00133 BUFFERS = 10 00134 }; 00135 00138 std::vector<TableEntry> _table; 00139 00149 std::vector<Buffer> _buffers; 00151 unsigned int _buffer; 00152 00153 00154 typedef HASH_MAPS<ObjectID, std::vector<ObjectID>, ObjectID_hash, ObjectID_eq>::Type ChildMap; 00156 ChildMap _children; 00157 00158 typedef HASH_MAPS<ObjectID, std::string, ObjectID_hash, ObjectID_eq>::Type PathMap; 00160 PathMap _pathmap; 00161 00162 typedef HASH_MAPS<ObjectID, std::size_t, ObjectID_hash, ObjectID_eq>::Type TableMap; 00164 TableMap _table_map; 00165 00166 typedef HASH_MAPS<ObjectID, LinkBase, ObjectID_hash, ObjectID_eq>::Type CacheMap; 00168 CacheMap _static_map; 00169 00170 std::vector<std::string> _paths; 00171 00172 FILE *_f; 00173 bool _is_read; 00174 uint32 _table_offset; 00175 bool _finalized; 00176 std::string _fn; 00177 bool _chain; 00178 DataManager *_manager; 00179 00183 void writeMagic(); 00184 00188 void readMagic(); 00189 00198 void readTable(); 00199 00202 void writeTable(); 00203 00212 void _readPaths(); 00213 00216 void _writePaths() const; 00217 00218 public: 00219 00226 DataArchive(std::string const &fn, bool read, bool chain=true); 00227 00230 ~DataArchive(); 00231 00237 void addObject(Object &object, std::string const & path); 00238 00244 void finalize(); 00245 00250 bool isFinalized(); 00251 00256 bool isWrite(); 00257 00266 void setDefault(); 00267 00275 static DataArchive* getDefault(); 00276 00282 const LinkBase getObject(std::string const &path_str); 00283 00290 const LinkBase getObject(const Path& path, std::string const &path_str=""); 00291 00296 std::string getFileName() const { return _fn; } 00297 00306 std::vector<ObjectID> getChildren(ObjectID const &id) const; 00307 00316 std::vector<ObjectID> getChildren(std::string const & path) const; 00317 00322 bool hasObject(ObjectID const &id) const; 00323 00328 bool hasObject(std::string const & path) const; 00329 00337 std::string getPathString(ObjectID const &id) const; 00338 00341 std::vector<ObjectID> getAllObjects() const; 00342 00345 std::vector<std::string> getAllPathStrings() const; 00346 00352 void cleanStatic(); 00353 00357 InterfaceProxy *getObjectInterface(ObjectID const &id, std::string const &path="") const; 00358 00362 InterfaceProxy *getObjectInterface(std::string const &path) const; 00363 00364 00367 void dump() const; 00368 00369 // protected methods made public for Python access, don't use! 00370 /* 00371 long _getOffset(); 00372 FP _filePointer(); 00373 */ 00374 00375 protected: 00383 void _addEntry(int offset, int length, ObjectID hash, std::string const &path); 00384 00385 /* 00386 Object* getObject(const Object &a, const char* path); 00387 Object* getObject(const Object &a, ObjectID path, const char* path=""); 00388 Object* getObject(ObjectID path); 00389 */ 00390 00397 void _addStatic(Object* ptr, std::string const &path_str, ObjectID key=0); 00398 00403 LinkBase const * _getStatic(ObjectID key); 00404 00410 Object* _createObject(ObjectID classhash); 00411 00418 const TableEntry* _lookupPath(Path const& path, std::string const &path_str="") const; 00419 00426 const TableEntry* _lookupPath(ObjectID const &id, std::string const &path_str="") const; 00427 00430 DataManager *getManager() const { return _manager; } 00431 00432 private: 00435 void setManager(DataManager *m); 00436 ChildMap const &getChildMap() const { return _children; } 00437 }; 00438 00439 00440 00441 00442 NAMESPACE_SIMDATA_END 00443 00444 #endif //__SIMDATA_DATAARCHIVE_H__ 00445
|
SimData version pre-0.4.0. For more information on SimData, visit the SimData Homepage. Generated on Tue Oct 14 12:06:38 2003, using Doxygen 1.2.18. |