jdeans289 / icg2

Interface Code Generator 2: Electric Boogaloo
Other
0 stars 2 forks source link

Try defining CompositeTypes with template specialization #40

Closed jdeans289 closed 10 months ago

jdeans289 commented 11 months ago

I think that we can do some cooler stuff with the generated code and shift some of the initialization time responsibilities to compile time.

Experiment with defining CompositeType using SpecifiedCompositeType, and having ICG generate template specialization code. Maybe we could even make the member maps constexpr, I'm not sure if c++11 can do that though.

Something like this:

// Some class that we want to build a DataType for
class Foo {
public:
    int a;
    std::string b;
};
// CompositeDataType definition 
class CompositeDataType : public DataType {
    public:

    CompositeDataType (std::string n) : DataType(n) {}

    virtual MemberMap& getMemberMap () = 0; 

    protected:
};

// SpecifiedCompositeType template
template <typename T>
class SpecifiedCompositeType : public CompositeDataType {

    public:
    SpecifiedCompositeType() : CompositeDataType("<error>") {}

};
// This is what would be generated by ICG
template <>
class SpecifiedCompositeType<Foo> : public CompositeDataType {

    public:
    SpecifiedCompositeType() : CompositeDataType("Foo") {}

    MemberMap& getMemberMap () override {
        return member_map;
    }

    static MemberMap member_map;
};

MemberMap SpecifiedCompositeType<Foo>::member_map = {
    {"a", StructMember("a", "int", offsetof(Foo, a))},
    {"b", StructMember("b", "std::string", offsetof(Foo, b))},
};

This also gives us a clean way to define a macro for private member access -

#define ICG_PRIVATE_MEMBERS(C) friend class SpecifiedCompositeType<C>;
jdeans289 commented 10 months ago

Did this in #44