Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // this is the thing that we build up with data that we want to save
- class SerializedObject {
- public:
- SerializedObject();
- std::string ToString() const;
- Set(const std::string &k, int v);
- Set(const std::string &k, double v);
- Set(const std::string &k, const SerializedObject &v);
- };
- // some random class
- class Something {
- public:
- // regular constructor
- Something(...);
- // construct from saved data
- Something(const SerializedObject &so);
- // return data for save
- SerializedObject Serialize() const;
- };
- // Serialize() method for some class that has a field that can be serialized
- SerializedObject Thing::Serialized() const {
- SerializedObject so;
- so.Set("int", m_int); // int m_int
- so.Set("double", m_double); // double m_double
- so.Set("something", m_something.Serialize(); // Something m_something
- return so;
- }
- // the last one sucks. I want to write:
- // so.Set("something", m_something)
- // and it calls its Serialize automatically
- class Serializable {
- public:
- virtual SerializerObject Serialize() const = 0;
- };
- class SerializerObject {
- ...
- Set(const std::string &k, const SerializedObject &v);
- void Set(const std::string &k, const Serializable &v) {
- Set(k, v.Serialize());
- }
- ...
- };
- class Something : public Serializable {
- ...
- SerializedObject Serialize() const;
- };
- // except now we have a circular dependency, which can't be resolved by
- // forward declarations because there's no pointers or references involved
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement