Skip to main content
GameDev.net gamedev.net
🔒 Locked

abstract template problem

Started by gimp Aug 1, 2002 at 6:57 AM 1 replies 250+ views
Original Post
gimp
gimp
The stage: Loader : A templated factory which creates object of a specific type. Resource : End end object I want to have IntermediateResource : An object used to create another object. The problem: Case 1: I want to create a Texture Object. The texture object must be initialised with an image object. I can use an image factory to create the image. I then create the texture by passing it the image. Case 2: I want to create a Model Object. The model object needs no initialisation. I just load it. This behaviour as to how to treat these two datatypes in my template resource manager appears to need to be different depending on the datatype.
  
inline void CResourceManager<ResourceType, LoaderType>::Aquire(const std::string& a_Name, CHandle<ResourceType, LoaderType>& a_Handle)
{
	std::string Name(a_Name);
	StringUtilities::Lower(Name);

	ResourceItr itr = m_Resources.find(Name);
	if (itr == m_Resources.end())
	{
		std::string Extention = CFile::GetExtention(Name);
		if (Extention.empty() == true)
			assert(0);

		boost::shared_ptr<LoaderType> Loader(CFactory<std::string, LoaderType>::GetInstance().Create(Extention));
		if (Loader.get() == 0)
			assert(0);

		boost::shared_ptr<ResourceType> BaseResource(new ResourceType);
		BaseResource->Create(Loader->Load(Name));
		
		std::pair<ResourceItr, bool> Result = m_Resources.insert(std::make_pair(Name, std::make_pair(BaseResource, 0)));
		if (Result.second == false)
			assert(0);
		else
			itr = Result.first;
	}

	++(itr->second.second);
	a_Handle.SetResource(itr);
}
  
Cut-Pasted code so it might look weird. The problem I''m having is the "BaseResource->Create(Loader->Load(Name));" part. This works fine for textures, but models and many other datatypes need no two stage creation, and where I insert it in to the map I''d like to insert the resource returned by loader directly. How do I do this? I _could_ do it at runtime but it seems sooo compile time... I should''nt have to query a type for capability to work out how to treat it... I can''t seem to nut this one out. I don''t know what ''partial template specialisation is, though I know my .net compiler doesn''t support it. Is this my problem? Suggestions? Chris Brodie http:\\fourth.flipcode.com
Chris Brodie
Cedric
Cedric
You could have cleaned up your code a little bit. That''s lazy...

I''m not sure I understand your whole example, but one obvious solution comes to mind: provide a creator function along with each data type a factory must create. This function accepts the name string, and simple ignores it if it is creating a model.

BTW, instead of accepting a reference to a_name and making a copy of it, you should accept it by value right away.

HTH,

Cédric
gimp
gimp
"You could have cleaned up your code a little bit. That''s lazy..."

Not sure I get you there? Providing the whole function was just so anyone could comment on the general method used and to sho the names of the template arguments.

"BTW, instead of accepting a reference to a_name and making a copy of it, you should accept it by value right away."

Very good pickup. I''m so used to passing almost everyting around as const references I never really considered it.

"I''m not sure I understand your whole example, but one obvious solution comes to mind: provide a creator function along with each data type a factory must create. This function accepts the name string, and simple ignores it if it is creating a model."

I''m not sure if I understand your answer but I had an idea as I read it. CTexture ''knows'' that it needs an image and CModel ''knows'' that it dosn''t need a two stage construction I can push the ''loader'' code out in to the client class. Then when I call ->Create(string) a Model will call the model loader factory and build ''itself'' a texture will call the Imageloader get the image then create the texture image.

Cool! Thanks, still don''t know if that was your idea or mine however, looks like yours... Thanks all the same.



Chris Brodie
http:\\fourth.flipcode.com
Chris Brodie

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.