1

Has anyone ever tried to create a set of macros that automatically creates a class with an arbitrary set of member variables, and then add support for serializing it?

For example, I'd like to be able to write a file that contains code like the following:

GENERATED_CLASS(MyClass)
    GENERATED_CLASS_MEMBER(int, foo);
    GENERATED_CLASS_MEMBER(std::string, bar);
END_GENERATED_CLASS();

GENERATED_CLASS(MySecondClass)
    GENERAGED_CLASS_MEMBER(double, baz);
END_GENERATED_CLASS();

GENERATED_DERIVED_CLASS(MyClass, MyThirdClass)
    GENERATED_CLASS_MEMBER(bool, bat);
END_GENERATED_CLASS();

that effectively results in

class MyClass
{
public:
    MyClass() {};
    ~MyClass() {};

    void set_foo(int value) { foo = value; }
    void set_bar(std::string value) { bar = value; }
    int get_foo() { return foo; }
    std::string get_bar() { return bar; }

private:
    int foo;
    std::string bar;
};

std::ostream& operator<<(std::ostream& os, const MyClass& obj)
{
    /* automatically generated code to serialize the obj,
     * i.e. foo and bar */
    return os;
}

std::istream& operator>>(std::istream& os, MyClass& obj)
{
    /* automatically generated code to deserialize the obj,
     * i.e. foo and bar */
    return os;
}

class MySecondClass
{
public:
    MySecondClass() {};
    ~MySecondClass() {};

    void set_baz(double value) { baz = value; }
    double get_baz() { return baz; }

private:
    double baz;
};

std::ostream& operator<<(std::ostream& os, const MySecondClass& obj)
{
    /* automatically generated code to serialize the obj,
     * i.e. baz */
    return os;
}

std::istream& operator>>(std::istream& os, MySecondClass& obj)
{
    /* automatically generated code to deserialize the obj,
     * i.e. baz */
    return os;
}

class MyThirdClass : public MyClass
{
public:
    MyThirdClass() {};
    ~MyThirdClass() {};

    void set_bat(bool value) { bat = value; }
    bool get_bat() { return bat; }

private:
    bool bat;
};

std::ostream& operator<<(std::ostream& os, const MyThirdClass& obj)
{
    /* automatically generated code to serialize the obj,
     * i.e. call the << operator for the baseclass,
     * then serialize bat */
    return os;
}

std::istream& operator>>(std::istream& os, MyThirdClass& obj)
{
    /* automatically generated code to deserialize the obj,
     * i.e. call the << operator for the baseclass,
     * then serialize bat */
    return os;
}

being generated from the precompiler.

I'm just not sure about the best way to do this. I am not opposed to using variadic templates and variadic macros if someone can tell me how, but I'd very much like to avoid boost, writing my own preprocessor, adding any custom makefile magic, etc. to accomplish this -- a pure c++ solution if possible.

Any suggestions?

2
  • 1
    Debugging code like that would be a nightmare. Oct 9, 2014 at 21:10
  • Perhaps, but g++ -E would certainly be your friend. Oct 10, 2014 at 12:37

1 Answer 1

1

One of the solutions that does almost exactly what you want to do is google protocol buffers. It allows you to define structures in certain format (IDL) and then produces c++ code (classes, serialization etc).

2
  • Yeah, but protobufs uses a separate code generator.
    – dom0
    Oct 9, 2014 at 21:03
  • True, also I assume you'd probably prefer to do it yourself :). I'm not aware of any solutions created purely in c++.
    – lisu
    Oct 9, 2014 at 21:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.