I have an application that has several objects (about 50 so far, but growing). There is only one instance of each of these objects in the app and these instances get shared
If you always know the type at compile time there is little point in calling BrokeredObject* p = GetObjectByID(TypeA::get_InterfaceID()) instead of TypeA* p = new TypeA or TypeA o directly.
If you on the other hand don't know the exact type at compile time, you could use some kind of type registry.
template
BrokeredObject* CreateObject()
{
return new T();
}
typedef int type_identity;
typedef std::map registry;
registry r;
class TypeA : public BrokeredObject
{
public:
static const type_identity identity;
};
class TypeB : public BrokeredObject
{
public:
static const type_identity identity;
};
r[TypeA::identity] = &CreateObject;
r[TypeB::identity] = &CreateObject;
or if you have RTTI enabled you could use type_info as type_identity:
typedef const type_info* type_identity;
typedef std::map registry;
registry r;
r[&typeid(TypeA)] = &CreateObject;
r[&typeid(TypeB)] = &CreateObject;
Each new class could of course, in any case, be self-registering in the registry, making the registration decentralized instead of centralized.