Boost Reflect Library 0.1.0
Design Rationale

There has been discussion on the Boost mailing list regarding how the type erasure should be implemented. The obvious, "ideal", would be something where the 'weight' of the any_ptr<> is no more than boost::any. This approach would 'convert' any type passed into the constructor into a polymorphic type implementing the interface. Then the cost of a call is one virtual method invocation (assuming proper inlining).

This solution has already been implemented via Boost.Interfaces.

The interface declaration used by Boost.Interfaces is:

            BOOST_INTERFACE_CONST_FUNCTION0(GetArea, double)
            BOOST_INTERFACE_FUNCTION2(SetXY, void, (int, x), (int, y))
            BOOST_INTERFACE_FUNCTION2(OffSetXY, void, (int, x), (int, y))
            BOOST_INTERFACE_CONST_FUNCTION0(GetName, const char*)

Unfortunately, there is no clear way to define a macro based solution that achieves what Boost.Interfaces achieves without specifying your entire interface with the (relatively) messy syntax above. A downside to the approach above is that any types that happen to contain a ',' would require special handling. A benefit to the above approach is that there is potential for knowng the parameter names in addition to the types.

I opted for a simpler syntax that can apply to pre-existing types along with the potential for more dynamic interface implementations rather than going for maximum possible performance or the minimum possible memory usage. With the proper "InterfaceDelegate", there is the potential that each method turns into a Don Clugston fast delegate. Thus you can get the 'fastest possible delegation' at the price of 16 bytes per method (on 64 bit machines) and a little extra one-time initialization cost.

© Daniel Larimer 2010-2011 - Licensed under Boost Software License, Version 1.0 Boost Reflect Library