Hi,
Iam trying to remove one of the two function and the corresponding variable, because they basically does the same thing. Both the variable holds a "unique ID".
Pseudo code
class Base
{
int typeID;
virtual int getType()//Edit: forgot virtual
{
return typeID;
}
}
template<class>
class IBase : public IBase<Base>
{
static unsigned int Type()
{
return typeID;
}
protected:
static int typeID;//Edit: forgot protected
}
class Render : public IBase<Render>
{
}
class GameObject
{
std::map<int, Base*> map;
template<class T_S>
T_S* GetComponentT()
{
std::map<std::string, BaseComponentTest*>::iterator it;
int a = IBaseComponent<T_S>::Type()
it = map.find(a);
if (it != map.end())
return (T_S*)it->second;
return NULL;
}
}
I have two diffrent scenario where I had to "split" into two function.
The first one is when I only have a Base pointer
void GameObject::AddComponent(Base* componentToAttach)
{
if (!componentMapT.count(componentToAttach->getType()) > 0)//I want to get the unique ID here
{
componentToAttach->attachOn(this);
componentMapT.insert(componentMapT.end(),
std::make_pair
(
componentToAttach->getType(),//I want to get the Unique ID here
componentToAttach
)
);
}
}
Here I want to add the pointer into a map, with the key bieng the ID, so I will need to have some kind of virtual function (so far as I know of) which returns the correctly id.
and in my second scenario I want to get the pointer out of the map
template<class T_S>
T_S* GetComponentT()
{
std::map<std::string, Base*>::iterator it;
int a = IBase<T_S>::Type();
it = componentMapT.find(a);
if (it != componentMapT.end())
return (T_S*)it->second;
return NULL;
}
and in a "third" scenario which is the combined scenario.
template<class Archive>
void save(Archive& archive, const GameObject& go)
{
//if (go != NULL)
{
archive(versionName);
archive(go.name);
archive(go.componentMap.size());
for (std::map<int, Base*>::const_iterator it = go.map.begin(); it != go.map.end(); ++it)
{
archive(it->first);
if(it->first == IBaseComponent<RenderComponent>::Type())
{
IBaseComponent<RenderComponent>* const rcp = (RenderComponent*)it->second;
archive(*rcp);
}
}
}
}
Is there a way to remove the base.getType? I tried to make the ID in base static, but than figured out that its is shared across all derivied classes which gave me wrong ID. I tried to do a few diffrent way but cannot bypass the problem.
For the third scenario I tried to have a template<class Archive, class T> but cereal start giving me error messages, so I do not think that will be possible, but if it were possible I could remake scenario 1 into a template function similar to scenario 2(But that may give me problem in the future, but thats for the future me to worry ).
So I guess that I only got a few choices to choice from:
1. Keep both the function and have dublicated variables
2. use something else beside cereal to output into binary
3. Some kind of magic way I do not know of