Sign in to follow this  
Metus

constructor functions too flexible?

Recommended Posts

I really love to fiddle with engine core design and that includes some very interesting ways of creating flexibility. Yesterday a thing popped up in my head: what if my engine isn't too flexible to work in a large-scale project? 10 minutes lates the answer to that question was something I called "constructor functions". It means that any resource-based constructor only takes one argument and that is a function pointer. Example of my basic texture class:
[source lang = "cpp"]

Texture::Texture( boost::function < IDirect3DTexture9 () > constructor )
{
 IDirect3DTexure9* texture = constructor();
 // Error checking
 m_texture = boost::intrusive_ptr < IDirect3DTexure9 >( texture, false );
}


With this constructor and the boost::bind I can allow the user to pass in any function that returns an IDirect3DDevice9* to my texture constructor
[source lang = "cpp"]
IDirect3DTexture9* CreateFromFile( std::string const& source, IDirect3DDevice9 device )
{
 IDirect3DTexture9* texture = 0;
 RESULT result = D3DXCreateTextureFromFile( device, source.c_str(), &texture );
 // Error checking
 return texture;
}

IDirect3DDevice9* device = renderer->get_IDirect3DDevice9();
typedef boost::shared_ptr < Texture > TexturePtr;
TexturePtr texture( new Texture( boost::bind( CreateFromFile, "file.dds", device ) ) );

before I start to implement these constructor functions in the rest of my engine, is this a way that you'd like to work or is it just plain stupid? edit: perhaps this should be placed in the game programming section, but since the code contains directx code I though it might fit better here edit2: changed the subject name edit3+ tried to format the code

Share this post


Link to post
Share on other sites
Just plain stupid. (I would have put it more politely, but you forced my hand. [grin]) Constructors don't have to be the only functions that create objects; if you need a function which loads a texture file and returns a Texture, go for it. Alternatively, just pass in the already-created IDirect3DTexture9, instead of making the constructor beg for it. Additionally, this exposes the innards of the creation unneccesarily; do you really want the client code to have to know about the CreateFromFile function?

Share this post


Link to post
Share on other sites
Hehe.. no hard feelings about that :)
There's one reason that I'd want the constructor to create the object: I personally prefer that way instead of

[source lang = "cpp"]

typedef boost::shared_ptr < Texture > TexturePtr;

TexturePtr texture( new Texture )
texture->load_from_file( "file.dds" );

// or this way
IDirect3DTexture* d3d_texture = { ... };
texure->assign( d3d_texture );




hmm... it seems pretty... weird anyway.. welll I think I have to get back to the design board then :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this