• Advertisement
Sign in to follow this  

return shared_ptr or Handle ?

This topic is 1374 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi

 

I am refactoring and the Asset Manager has a std::map of all the game resources. But I can't quite think of how I should encapsulate things.

 

An entity manager can for example use the asset manager and call GetResource...(ID) and use it. But should GetResource() return a HANDLE with a shared_ptr inside it, or can I just return the shared_ptr?

 

It seems if I use handles I just put another layer on the cake but it does not do anything.

 

Any tips?

struct GameAssets
{
	std::vector<std::shared_ptr<Mesh> > Meshes;
	std::vector<std::shared_ptr<Texture> > Textures;
	std::vector<std::shared_ptr<Animation> > AnimationTracks;
	std::vector<std::shared_ptr<Skeleton> > Skeletons;
};

struct HandleAssets
{
	std::shared_ptr<GameAssets> Assets;
};

class AssetManager
{
public:
	AssetManager();
	~AssetManager();

	HandleAssets GetResourceByHandle(DWORD ID);
	std::shared_ptr<GameAssets> GetResourceBysptr(DWORD ID);

private:
	std::map<DWORD, HandleAssets > AllAssetsByHandle;
	std::map<DWORD, std::shared_ptr<GameAssets> > AllAssetsBysptr;
};

Share this post


Link to post
Share on other sites
Advertisement
I am about to head out so I can only give a short answer, but you are correct that the extra layer on the cake is needless.
Just return a shared pointer and be alert as to how to and not to use them (as in passing them as references, not copies, to functions, etc.)


L. Spiro

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement