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:
- Is this idea a usable setup, what is flawless about it?
- 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!!
Vector(int)will expand tostruct Vector_intandDEFINE_Vector(int)will expand tostruct Vector_int { int* elements; ... };. Within the second macro we can introduce member variables of typeT. 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