Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

My question is design related.

I am not sure if this good programming. I am not the well template programmer... I have 2 Questions at the end:

I have a big simulation framework (rigid body simulation). And so far I have one special include file "TypeDefs.hpp" which only defines (prototypes) of all classes in the framework to be used as types as a hirarchical struct:

What I mean I describe here for example for my DynamicsSystem class with template parameter TDynamicsSystemConfig.

template<typename TDynamicsSystemConfig>
   class DynamicsSystem {
   public:

   typedef TDynamicsSystemConfig DynamicsSystemConfig;
   DEFINE_DYNAMICSSYTEM_CONFIG_TYPES_OF(TDynamicsSystemConfig)

   ...

}

When I instance this class I input a struct ConfigDynamicsSystem for TDynamicsSystemConfig which is defined in "TypeDefs.hpp" and contains

template < typename _TRigidBody>
struct ConfigDynamicsSystem{
    typedef _TRigidBody RigidBodyType;

    ... and more typedefs and constants

};

The macro then re-typedefs all types and static const int and more to be able to use the types easily. For this example the macro would expand for example

typedef typename TDynamicsSystemConfig::RigidBodyType RigidBodyType;

This concept has the following ad/disvantages:

  • One needs only one template parameter, makes the stuff readable
  • One needs macros (Hm...)
  • One needs templates everywhere in the framework, to make push in all defined types. So for my overall SimulationManager class the total config template parameter TConfig is a hirarchical built struct (structs with other structs in it with definitions) which inserts types and configuration stuff into the class SimulationManager and the members of this class use then sub config structs from TConfig which then propagates through the whole framework. Makes me able to have the same names and types available everywhere.

Questions:

  1. Is this idea a usable setup, what is flawless about it?
  2. What would be the disadvantage or maybee the better method of not using templates and using a "TypeDefs.hpp" file which is included everywhere which defines all these types in a namespace. So everywhere in the framework, the types and names are usable and defined.

Thanks very much for any comment and input!!

share|improve this question
You don't need the macro. You could just write your typedefs with the template parameter and you're fine. Every concrete instantiation then "picks" the proper subtypes of the concrete config type. (You would need a macro if you did somewhat like a "C struct template", where we write the whole template as one single macro to be expanded for every concrete version we want. But C++ handles this subtype scenario you want to have for you.) – leemes Nov 8 '12 at 10:33
I have this macro to just write for me these typedefs as there are so many, the template parameter is a C struct containing types, what do you mean: like a "C struct template", where we write the whole template as one single macro ? – Gabriel Nov 8 '12 at 12:27
I meant: In C we don't have templates, so we can write a struct as a macro with the "template parameter" as a macro argument. Let's say Vector(int) will expand to struct Vector_int and DEFINE_Vector(int) will expand to struct Vector_int { int* elements; ... };. Within the second macro we can introduce member variables of type T. I only wanted to say that you don't require this type of hack here, but it looks like that you wanted to use it. In a nutshell: You can avoid macros :) – leemes Nov 8 '12 at 18:15

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.