Jump to content
  • Advertisement
Sign in to follow this  
ColeFreeman

Template specialization and hidden handles

This topic is 3799 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

I am trying to find a convenient way to handle element handles without explicitly needing to use them, the problem at the moment is that it's not possible to specialize the templates in the same class scope. For instance, we have two classes, and one container class of arbitrary elements with a handle pointer requirement:

class Object{
public:
  Handle handle;
};

template <class T, Handle T::*handleptr>
class Con{
public:
  ...
};

class User{
public:
  Con<Object,&Object::handle> objects; //Valid
  Con<User,&User::handle> subusers; //invalid because of class scope
  Handle handle;
};


What I am looking for is a similar way to handle elements as the first Container in user, not needing to "care" about their respective handles when dealing with classes able to contain lists of the same type as themselves. One option is ofc. to inherit and use it as an extension, however it fails if several handles are needed. Also another option of declaring a subclass with the handle is quite crude. Any input is welcome, thank you.

Share this post


Link to post
Share on other sites
Advertisement
So far the most elegant solution I have found is:



template <class T, Handle T::*handleptr, class Convert = T>
class Con{
public:
Convert * Retrieve(int i){
return (Convert*) buffer;
}
...
};

class _User{
public:
Handle handle;
};

class User : public _User{
public:
Con<_User,&_User::handle,User> subusers; //valid, however still quite crude
};




Passing the type with which to use the container externally. An acceptable solution, however far from elegant.
Any suggestions on improvements?

Share this post


Link to post
Share on other sites
I'm not exactly sure what you want to do, but doesn't this work for you?

class Object {
public:
Handle handle;
};

template < typename T >
class Con {
Handle T::*hptr;
public:
Con() : hptr( &T::handle ) { }
};

class User {
public:
Con< Object > objects;
Con< User > subusers;
Handle handle;
};

Share this post


Link to post
Share on other sites
Thanks for the reply.

Well, since it assumes every class to have the "handle" with that specific name, and this is not the case.
It is quite exactly the same solution as to inherit the handle and using the type directly in the container as the handle.

Any class may have a handle or several with any names, it would work if it was divided into two distinct container classes, one assumes the one handle, and one that expects the template parameter. A possible solution, however leaves alot to be desired since it does not allow for the multiple or arbitrary named handle cases.

Passing a variable name would be nice as a template parameter, but thats just dreaming of the impossible ^^

Any further input is greatly appreciated.

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!