Serialization template base class

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Harinezumi
    New Member
    • Feb 2008
    • 32

    Serialization template base class

    Hello,
    I need help with the design of a serialization template base class.
    The idea is that any class derived from this template base class would only need to implement the input and output operators and a named constructor, Create().
    The code is something like this:

    Code:
    template <class SerializableClass>
    class Serializable
    {
    public:
        static SerializableClass* Unserialize(std::istream& in){
            SerializableClass* instance = SerializableClass::Create();
            in >> *instance;
            return instance;
        }
    
        std::ostream& Serialize(std::ostream& out){
            out << *this;
            return out;
        }
    
    protected:
        static Create();
    
    };
    As I know, the input and output operators must be friend functions to the type they provide I/O for, so they cannot be member functions. But then I can't promise the compiler that the operators will be implemented for SerializableCla ss, so this doesn't compile.
    Is there a way to make this design work?
    Also, I think I miss some point of either the I/O operators or design with templates, so help with that is also appreciated.
  • arnaudk
    Contributor
    • Sep 2007
    • 425

    #2
    By i/o operators, do you mean Serialize/Unserialize methods? Because you haven't written them as operators.
    What about making the i/o methods of your base class pure virtual? You can still provide a base class implementation for them, but this will force them to be implemented in derived classes. You can call the base class implementations of the pure virtual methods from within derived classes if need be.

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      Actually, I believe you want to implement a Visitor.

      Read this article: http://bytes.com/forum/thread674645.html.

      How to serialize a class is covered towards the end of the article.

      Comment

      • Harinezumi
        New Member
        • Feb 2008
        • 32

        #4
        Originally posted by arnaudk
        By i/o operators, do you mean Serialize/Unserialize methods? Because you haven't written them as operators.
        By I/O operators I mean std::istream& operator >> (std::istream&, <class>&) and std::ostream& operator << (std::ostream&, <class>&), which are non-member friend functions.
        Originally posted by arnaudk
        What about making the i/o methods of your base class pure virtual? You can still provide a base class implementation for them, but this will force them to be implemented in derived classes. You can call the base class implementations of the pure virtual methods from within derived classes if need be.
        How can a base implementation be provided for a pure virtual method?
        Originally posted by weaknessforcats
        Actually, I believe you want to implement a Visitor.

        Read this article: http://bytes.com/forum/thread674645.html.

        How to serialize a class is covered towards the end of the article.
        Thanks weaknessforcats , the Visitor pattern really is what I wanted to achieve.

        Comment

        • arnaudk
          Contributor
          • Sep 2007
          • 425

          #5
          Originally posted by Harinezumi
          How can a base implementation be provided for a pure virtual method?

          Comment

          • Harinezumi
            New Member
            • Feb 2008
            • 32

            #6
            Originally posted by arnaudk
            Thanks arnaudk, that cleared my confusion.

            Comment

            Working...