📄 engine.cpp
字号:
return; } // First off - has this Object been serialised already? ArchiveMap::const_iterator itor = myArchiveMap.find(object); if (itor == myArchiveMap.end()) { // Unfortunately we need to serialise it - here we go .... uint32 id = (uint32)myArchiveMap.size(); myArchiveMap[object] = id; // bumps id automatically for next one write(id); ClassMap::const_iterator classItor = myClassMap.find(object->getPersistenceID()); if (classItor == myClassMap.end()) { uint32 classId = (uint32)myClassMap.size(); myClassMap[object->getPersistenceID()] = classId; write(classId); write(static_cast<String>(object->getPersistenceID())); } else { write(classItor->second); } String majik; majik = "OBST"; write(majik); object->write(*this); majik = "OBEN"; write(majik); } else { // This object has been serialised, so just pop its ID out write(itor->second); }}/* * reads in a BaseObject into a reference (pre-instantiated object) */void Engine::read(BaseObject &object) THROWS (Engine::Exception){ uint32 id = 0; read(id); if (id == NullObject) THROW("Object Id should not be NULL when unpersisting to a reference"); // Do we already have this object in memory? if (id < myArchiveVector.size()) { object = *(myArchiveVector[id]); return; } // Okay - read the identifier for the class in... // we won't need it later since this object is already allocated readClass(); // Okay then - we can read data straight into this object readObject(&object);}/* * reads in a BaseObject into a pointer allocating if the pointer is NULL */void Engine::read(BaseObject *&object) THROWS (Engine::Exception){ uint32 id = 0; read(id); // Is the ID a NULL object? if (id == NullObject) { object = NULL; return; } // Do we already have this object in memory? if (id < myArchiveVector.size()) { object = myArchiveVector[id]; return; } // Okay - read the identifier for the class in... String className = readClass(); // is the pointer already initialized? if so then no need to reallocate if (object != NULL) { readObject(object); return; } // Create the object (of the relevant type) object = TypeManager::createInstanceOf(className.c_str()); if (object) { // Okay then - we can make this object readObject(object); } else THROW (Exception(String("Unable to instantiate object of class ")+className));}/* * reads the actual object data in */void Engine::readObject(BaseObject* object){ // Okay then - we can make this object myArchiveVector.push_back(object); String majik; read(majik); if(majik != String("OBST")) THROW("Missing Start-of-Object marker"); object->read(*this); read(majik); if(majik != String("OBEN")) THROW("Missing End-of-Object marker");}/* * reads the class information in */const String Engine::readClass(){ // Okay - read the identifier for the class in... uint32 classId = 0; read(classId); String className; if (classId < myClassVector.size()) { className = myClassVector[classId]; } else { // Okay the class wasn't known yet - save its name read(className); myClassVector.push_back(className); } return className;}/* * note, does not (yet?) throw an exception, but interface * prepared .. */void Engine::write(const String& str) THROWS (Engine::Exception){ uint32 len = (uint32)str.length(); write(len); writeBinary((uint8*)str.c_str(),len);}void Engine::read(String& str) THROWS (Engine::Exception){ uint32 len = 0; read(len); uint8 *buffer = new uint8[len+1]; readBinary(buffer,len); buffer[len] = 0; str = (char*)buffer; delete[] buffer;}/* * note, does not (yet?) throw an exception, but interface * prepared .. */void Engine::write(const std::string& str) THROWS (Engine::Exception){ uint32 len = (uint32)str.length(); write(len); writeBinary((uint8*)str.c_str(),len);}void Engine::read(std::string& str) THROWS (Engine::Exception){ uint32 len = 0; read(len); uint8 *buffer = new uint8[len+1]; readBinary(buffer,len); buffer[len] = 0; str = (char*)buffer; delete[] buffer;}#define CCXX_RE(ar,ob) ar.read(ob); return ar#define CCXX_WE(ar,ob) ar.write(ob); return arCCXX_EXPORT(Engine&) operator >>( Engine& ar, BaseObject &ob) THROWS (Engine::Exception) {CCXX_RE(ar,ob);}CCXX_EXPORT(Engine&) operator >>( Engine& ar, BaseObject *&ob) THROWS (Engine::Exception) {CCXX_RE(ar,ob);}CCXX_EXPORT(Engine&) operator <<( Engine& ar, BaseObject const &ob) THROWS (Engine::Exception) {CCXX_WE(ar,&ob);}CCXX_EXPORT(Engine&) operator <<( Engine& ar, BaseObject const *ob) THROWS (Engine::Exception) {CCXX_WE(ar,ob);}CCXX_EXPORT(Engine&) operator >>( Engine& ar, int8& ob) THROWS (Engine::Exception) {CCXX_RE (ar,ob);}CCXX_EXPORT(Engine&) operator <<( Engine& ar, int8 ob) THROWS (Engine::Exception) {CCXX_WE (ar,ob);}CCXX_EXPORT(Engine&) operator >>( Engine& ar, uint8& ob) THROWS (Engine::Exception) {CCXX_RE (ar,ob);}CCXX_EXPORT(Engine&) operator <<( Engine& ar, uint8 ob) THROWS (Engine::Exception) {CCXX_WE (ar,ob);}CCXX_EXPORT(Engine&) operator >>( Engine& ar, int16& ob) THROWS (Engine::Exception) {CCXX_RE (ar,ob);}CCXX_EXPORT(Engine&) operator <<( Engine& ar, int16 ob) THROWS (Engine::Exception) {CCXX_WE (ar,ob);}CCXX_EXPORT(Engine&) operator >>( Engine& ar, uint16& ob) THROWS (Engine::Exception) {CCXX_RE (ar,ob);}CCXX_EXPORT(Engine&) operator <<( Engine& ar, uint16 ob) THROWS (Engine::Exception) {CCXX_WE (ar,ob);}CCXX_EXPORT(Engine&) operator >>( Engine& ar, int32& ob) THROWS (Engine::Exception) {CCXX_RE (ar,ob);}CCXX_EXPORT(Engine&) operator <<( Engine& ar, int32 ob) THROWS (Engine::Exception) {CCXX_WE (ar,ob);}CCXX_EXPORT(Engine&) operator >>( Engine& ar, uint32& ob) THROWS (Engine::Exception) {CCXX_RE (ar,ob);}CCXX_EXPORT(Engine&) operator <<( Engine& ar, uint32 ob) THROWS (Engine::Exception) {CCXX_WE (ar,ob);}#ifdef HAVE_64_BITSCCXX_EXPORT(Engine&) operator >>( Engine& ar, int64& ob) THROWS (Engine::Exception) {CCXX_RE (ar,ob);}CCXX_EXPORT(Engine&) operator <<( Engine& ar, int64 ob) THROWS (Engine::Exception) {CCXX_WE (ar,ob);}CCXX_EXPORT(Engine&) operator >>( Engine& ar, uint64& ob) THROWS (Engine::Exception) {CCXX_RE (ar,ob);}CCXX_EXPORT(Engine&) operator <<( Engine& ar, uint64 ob) THROWS (Engine::Exception) {CCXX_WE (ar,ob);}#endifCCXX_EXPORT(Engine&) operator >>( Engine& ar, float& ob) THROWS (Engine::Exception) {CCXX_RE (ar,ob);}CCXX_EXPORT(Engine&) operator <<( Engine& ar, float ob) THROWS (Engine::Exception) {CCXX_WE (ar,ob);}CCXX_EXPORT(Engine&) operator >>( Engine& ar, double& ob) THROWS (Engine::Exception) {CCXX_RE (ar,ob);}CCXX_EXPORT(Engine&) operator <<( Engine& ar, double ob) THROWS (Engine::Exception) {CCXX_WE (ar,ob);}CCXX_EXPORT(Engine&) operator >>( Engine& ar, String& ob) THROWS (Engine::Exception) {CCXX_RE (ar,ob);}CCXX_EXPORT(Engine&) operator <<( Engine& ar, String ob) THROWS (Engine::Exception) {CCXX_WE (ar,ob);}CCXX_EXPORT(Engine&) operator >>( Engine& ar, std::string& ob) THROWS (Engine::Exception) {CCXX_RE (ar,ob);}CCXX_EXPORT(Engine&) operator <<( Engine& ar, std::string ob) THROWS (Engine::Exception) {CCXX_WE (ar,ob);}CCXX_EXPORT(Engine&) operator >>( Engine& ar, bool& ob) THROWS (Engine::Exception) { uint32 a; ar.read(a); ob=a==1;return ar;}CCXX_EXPORT(Engine&) operator <<( Engine& ar, bool ob) THROWS (Engine::Exception) { uint32 a=ob?1:0; ar.write(a); return ar;}#undef CCXX_RE#undef CCXX_WE#ifdef CCXX_NAMESPACES}#endif#endif/** EMACS ** * Local variables: * mode: c++ * c-basic-offset: 8 * End: */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -