#ifndef __SerializeValuesByName_h_ #define __SerializeValuesByName_h_ #include "DataFormat.h" #include "Strategy.h" /* This is a nice way to turn one or more ValuesByName objects into a string * and to restore them later. This is mostly aimed at network communications * between the servers. * * This is built on top of Records. A Record knows how to serialize and * deserialize a ValueBox. A ValueBox does not know how to serialize itself. * * Records are flexible. Other items can inserted in addition to ValuesByName * objects. */ class SerializeRecords { private: std::map< std::string, int > _names; public: typedef Parse::ValuesByName ValuesByName; // This allows you to put several records into a single string. // DeserializeRecords::getRecords() will undo this to give you back the // original records. static void addRecord(std::string &destination, RecordBuilder &source); // Start with an empty record for your header. Call loadNames() on each of // the ValuesByName that you plan to store. This will collect all of the // field names in one place, so they are not duplicated. Then call go on // each ValuesByName. This will copy the values into the given record. // It is an error to call go() without first calling loadNames(). Use the // DeserializeRecords class to undo this and get back to your original // ValuesByName. // // Note: We will fill fields in the header record starting at 0. If there // are n unique names, we will use fields 0 through (n-1). You can use the // other fields as you see fit. Typically start from field 0xffff, and work // backwards. void loadNames(RecordBuilder &header, ValuesByName const &source); void go(RecordBuilder &record, ValuesByName const &source) const; }; class DeserializeRecords { private: std::map< FieldId, std::string > _names; public: typedef Parse::ValuesByName ValuesByName; // Very simple error reporting via the log. static void reportError(std::string const &type); // Undo SerializeRecords::addRecord(). If there are any errors, this returns // the empty list. static std::vector< Record::Ref > getRecords(std::string const &source); // Load the field names from the header. DeserializeRecords(Record const &header); // Load the field values from this record, and mix them with the field names. // Returns true on success or false on error. bool go(ValuesByName &dest, Record const &source) const; }; std::string debugDump(Parse::ValuesByName const &row); void debugDump(TclList &addTo, std::vector< Parse::ValuesByName > const &rows); std::string debugDump(std::vector< Parse::ValuesByName > const &rows); #endif