Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


I have two of the same function and variable value. Which I want to remove atleast one of them


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 Conny14156   Members   -  Reputation: 270

Like
0Likes
Like

Posted 04 July 2014 - 02:43 PM

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 laugh.png).

 

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


Edited by Conny14156, 04 July 2014 - 03:41 PM.


Sponsor:

#2 Jan2go   Members   -  Reputation: 939

Like
0Likes
Like

Posted 04 July 2014 - 03:30 PM

I'm not sure if you want the ID to be the same for all instances of the same class or a unique one for each instance. In Base typeID isn't static but in IBase it is.

Anyway, this is the code that I use to get unique IDs for each class (identical for each instance of the same class) in my event management system. Perhaps you can use it for your purpose.
 

class IEventBase
{
public:
	virtual size_t GetType() const = 0;
};

template<class T>
class IEvent : public IEventBase
public:
	size_t GetType() const override final;
	static const size_t Type;
};

template<class T>
const size_t IEvent<T>::Type = typeid(IEvent<T>).hash_code();

class KeyHitEvent final : public IEvent<KeyHitEvent>
{
};

In this case, IEventBase is the class that need to be used to hold variables of any event type.
To get the unique ID of a specific class you access the member variable (e.g. KeyHitEvent::Type), to get the unique class ID of an IEventBase* you use the virtual member function (e.g. ptr->GetType()).


Edited by Jan2go, 05 July 2014 - 04:15 AM.


#3 Conny14156   Members   -  Reputation: 270

Like
0Likes
Like

Posted 04 July 2014 - 03:49 PM

I'm not sure if you want the ID to be the same for all instances of the same class or a unique one for each instance. In Base typeID isn't static but in IBase it is.

Anyway, this is the code that I use to get unique IDs for each class (identical for each instance of the same class) in my event management system. Perhaps you can use it for your purpose.
 


In this case, IEventBase is the class that need to be used to hold variables of any event type.
To get the unique ID of a specific class you access the member variable (e.g. KeyHitEvent::Type), to get the unique class ID of an IEventBase* you use the virtual member function (e.g. ptr->GetType()).

 

I forgot to mention it, but I want the ID to be unique across all instances of the class. and yeah your way works wonder, if I just makes my ID public instead of protected, and just have my base be pure virtual. 

Man I feel so stupid, If I just made my id public instead of protected I coulda override the base virtual funtion, I tried to have a static override but the compiler didnt like that. Man I feel so stupid I been at this for the past days. 



#4 Jan2go   Members   -  Reputation: 939

Like
0Likes
Like

Posted 05 July 2014 - 04:17 AM

if I just makes my ID public instead of protected


Now that you mention it, I realized that I removed the public together with ID-unrelated functions from the code I posted. Obviously it isn't of much use when everything is private. biggrin.png
Fixed it for future reference.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS