ToolDefManager serialization
authorPerttu Ahola <celeron55@gmail.com>
Mon, 14 Nov 2011 22:05:26 +0000 (00:05 +0200)
committerPerttu Ahola <celeron55@gmail.com>
Tue, 29 Nov 2011 17:13:42 +0000 (19:13 +0200)
src/tooldef.cpp
src/tooldef.h

index 93bedf92ac1b7516c9cabb0341b3a8f01e60b049..1c85dbd8eccd98436c8b596946d7f46a6ace0337 100644 (file)
@@ -66,7 +66,7 @@ void ToolDefinition::deSerialize(std::istream &is)
 {
        int version = readU8(is);
        if(version != 0) throw SerializationError(
-                       "unsupported ToolDiggingProperties version");
+                       "unsupported ToolDefinition version");
        imagename = deSerializeString(is);
        properties.basetime = readF1000(is);
        properties.dt_weight = readF1000(is);
@@ -133,6 +133,41 @@ public:
                m_tool_definitions[toolname] = new ToolDefinition(def);
                return true;
        }
+       virtual void serialize(std::ostream &os)
+       {
+               writeU8(os, 0); // version
+               u16 count = m_tool_definitions.size();
+               writeU16(os, count);
+               for(core::map<std::string, ToolDefinition*>::Iterator
+                               i = m_tool_definitions.getIterator();
+                               i.atEnd() == false; i++){
+                       std::string name = i.getNode()->getKey();
+                       ToolDefinition *def = i.getNode()->getValue();
+                       // Serialize name
+                       os<<serializeString(name);
+                       // Serialize ToolDefinition and write wrapped in a string
+                       std::ostringstream tmp_os(std::ios::binary);
+                       def->serialize(tmp_os);
+                       os<<serializeString(tmp_os.str());
+               }
+       }
+       virtual void deSerialize(std::istream &is)
+       {
+               int version = readU8(is);
+               if(version != 0) throw SerializationError(
+                               "unsupported ToolDefManager version");
+               u16 count = readU16(is);
+               for(u16 i=0; i<count; i++){
+                       // Deserialize name
+                       std::string name = deSerializeString(is);
+                       // Deserialize a string and grab a ToolDefinition from it
+                       std::istringstream tmp_is(deSerializeString(is), std::ios::binary);
+                       ToolDefinition def;
+                       def.deSerialize(tmp_is);
+                       // Register
+                       registerTool(name, def);
+               }
+       }
 private:
        // Key is name
        core::map<std::string, ToolDefinition*> m_tool_definitions;
index fdb02b012cdd30c493b54f3b8f0dfdca980ff128..2c758d902466b97bd8917b8230bc77de1d4d6cbf 100644 (file)
@@ -85,6 +85,9 @@ public:
                        const std::string &toolname) const =0;
                        
        virtual bool registerTool(std::string toolname, const ToolDefinition &def)=0;
+
+       virtual void serialize(std::ostream &os)=0;
+       virtual void deSerialize(std::istream &is)=0;
 };
 
 IWritableToolDefManager* createToolDefManager();