Jump to content
  • Advertisement
Sign in to follow this  

Templates question

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

So I am writing an ObjectFactory class, and basically I want to give it the ability to return pointers to objects it creates and manages. So I had a couple design ideas: One:
Entity *t = g_ObjectFactory.createEntity();
Where the object factory has a method for every single thing it creates. Not exactly what I want... So then I thought (well, what if they all inherited a base entity class. That would make things a LOT easier! So I came up with this:
Entity *t = g_ObjectFactory.create(ENTITY);
ENTITY is an enumerated value...but even that would require a huge switch case statement. Not fun. What I really want is something like this:
Entity *t = g_ObjectFactory.create<Entity>();
However, I was wondering if there was anyway in the method definition to ensure that the class passed as the template would be an inherited class from my Entity base object. If not, does anyone have any ideas for this? Thanks.

Share this post


Link to post
Share on other sites
Advertisement
im making an Interface class that creates objects like this:

// in ioglwindow.h
class IOGLWindow : public ml::Interface<IOGLWindow>{/*stuff here*/};

// in some cpp file we dont need to know about
class COGLWindow : public IOGLWindow{/*stuff here*/}
static ml::CAutoRegister<COGLWindow,IOGLWindow> s_autoRegister("COGLWindow");

// in main.cpp
IOGLWindow* window = IOGLWindow::createInstance(); // create any object (unpredictable)
// or
IOGLWindow* window = IOGLWindow::createInstance("COGLWindow");

if your interested i could post the code

Share this post


Link to post
Share on other sites
Quote:
Original post by visage
However, I was wondering if there was anyway in the method definition to ensure that the class passed as the template would be an inherited class from my Entity base object.

Yes, relying on the SFINAE principle you can ensure that only certain arguments produce valid instantiations. Alternatively, you can use compile-time assertions.

An example using the SFINAE principle via boost:


#include <boost/utility/enable_if.hpp>
#include <boost/type_traits/is_convertible.hpp>

class Entity {};

class object_factory
{
public:
// Ensures Entity is a non-ambiguous public base of Type
template< typename Type >
typename
::boost::enable_if< ::boost::is_convertible< Type*
, Entity*
>
, Type*
>::type
create()
{
return new Type; // Or whatever
}
};






Or, perhaps less elegantly, using compile-time assertions with boost:


#include <boost/static_assert.hpp>
#include <boost/type_traits/is_convertible.hpp>

class Entity {};

class object_factory
{
public:
template< typename Type >
Type*
create()
{
BOOST_STATIC_ASSERT( ( ::boost::is_convertible< Type*
, Entity*
>::value
)
);
return new Type;
}
};




Share this post


Link to post
Share on other sites
mike -- that method would work pretty well if I used some hashtables or something. thanks for the idea.

polymorphic -- mm, I like that one a lot more. Thanks!

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!