www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Get difficulties with variadics

reply "Flamaros" <flamaros.xavier gmail.com> writes:
I am using a variadic function for a resource manager to be able 
to load resources with custom parameters.

My problem is I don't get the second parameter correctly, i am 
expecting to retrieve a dynamic array.

[CODE]
class ResourceManager
     T    getResource(T : IResource)(string filePath, ...)
     {
         T	instance;

	...
		
	instance = new T;
	instance.load(filePath, _arguments);

	...
		
	return instance;
     }
}
	
class VBO(T) : IResource
{
     mixin ResourceBase;

public:
     void	load(...)
     {
	string	filePath = va_arg!string(_argptr);
		
	auto size = _arguments[0].tsize();
	_argptr += ((size + size_t.sizeof - 1) & ~(size_t.sizeof - 1));
	mArray = va_arg!(T[])(_argptr);

	mArray = cast(T[])_arguments[1];

	...
     }
}

// Somewhere else I try to retrieve the resource
mIndexes = 
resourceManager.getResource!(VBO!GLushort)("dquick.renderer_2d.opengl
rectangle.indexes", 
[0, 1, 2, 1, 3, 2]);	// We use the module name as basePath and 
the variable name as filename

[/CODE]

In the load(...) method I am getting filePath without issue, but 
for next parameters I am little confused on how to do.
May 10 2013
parent reply "evilrat" <evilrat666 gmail.com> writes:
On Friday, 10 May 2013 at 21:11:57 UTC, Flamaros wrote:
 In the load(...) method I am getting filePath without issue, 
 but for next parameters I am little confused on how to do.
what all that stuff in ur load method? O_O look at my example in this thread, that at least should give you idea on how get related types from it - http://forum.dlang.org/thread/kmepi8$1tjp$1 digitalmars.com
May 10 2013
parent reply "Flamaros" <flamaros.xavier gmail.com> writes:
On Saturday, 11 May 2013 at 01:48:12 UTC, evilrat wrote:
 On Friday, 10 May 2013 at 21:11:57 UTC, Flamaros wrote:
 In the load(...) method I am getting filePath without issue, 
 but for next parameters I am little confused on how to do.
what all that stuff in ur load method? O_O look at my example in this thread, that at least should give you idea on how get related types from it - http://forum.dlang.org/thread/kmepi8$1tjp$1 digitalmars.com
I think my major mistake is to call load method with _arguments in my ResourceManager: instance.load(filePath, _arguments); Passing _argptr doesn't seems to be the right solution neither, because I am receiving a void* in the load method of my Resource object instead of a ushort[].
May 11 2013
parent reply "evilrat" <evilrat666 gmail.com> writes:
On Saturday, 11 May 2013 at 13:00:42 UTC, Flamaros wrote:
 On Saturday, 11 May 2013 at 01:48:12 UTC, evilrat wrote:
 On Friday, 10 May 2013 at 21:11:57 UTC, Flamaros wrote:
 In the load(...) method I am getting filePath without issue, 
 but for next parameters I am little confused on how to do.
what all that stuff in ur load method? O_O look at my example in this thread, that at least should give you idea on how get related types from it - http://forum.dlang.org/thread/kmepi8$1tjp$1 digitalmars.com
I think my major mistake is to call load method with _arguments in my ResourceManager: instance.load(filePath, _arguments); Passing _argptr doesn't seems to be the right solution neither, because I am receiving a void* in the load method of my Resource object instead of a ushort[].
uhm well now i see what you are doing, in your getResource() method you will need to pack args somehow, as Variant[] array for example and then pass them to load(which now should have load(Variant[] params) signature), also you may want to restrict func to accept just few types, or at least push enum control values before data for distinction purposes. maybe it is possible to do what you are trying to do, but _arguments and their typeinfo valid only in current scope, maybe it is possible to hack it somehow but it would be easier and safer just to pack all arguments in array. and concerning ur resource stuff - moving primitive types as raw data isn't good idea, using minimalistic structs instead would give you lot more control(and safety) later as long as easier API tweaking later p.s. i hope this just for testing, that vbo/resource api isn't convenient at all, and if you need vertex data loader i recommend you try assimp/derelict3 pair -_-
May 11 2013
parent reply "Flamaros" <flamaros.xavier gmail.com> writes:
On Saturday, 11 May 2013 at 13:40:15 UTC, evilrat wrote:
 On Saturday, 11 May 2013 at 13:00:42 UTC, Flamaros wrote:
 On Saturday, 11 May 2013 at 01:48:12 UTC, evilrat wrote:
 On Friday, 10 May 2013 at 21:11:57 UTC, Flamaros wrote:
 In the load(...) method I am getting filePath without issue, 
 but for next parameters I am little confused on how to do.
what all that stuff in ur load method? O_O look at my example in this thread, that at least should give you idea on how get related types from it - http://forum.dlang.org/thread/kmepi8$1tjp$1 digitalmars.com
I think my major mistake is to call load method with _arguments in my ResourceManager: instance.load(filePath, _arguments); Passing _argptr doesn't seems to be the right solution neither, because I am receiving a void* in the load method of my Resource object instead of a ushort[].
uhm well now i see what you are doing, in your getResource() method you will need to pack args somehow, as Variant[] array for example and then pass them to load(which now should have load(Variant[] params) signature), also you may want to restrict func to accept just few types, or at least push enum control values before data for distinction purposes. maybe it is possible to do what you are trying to do, but _arguments and their typeinfo valid only in current scope, maybe it is possible to hack it somehow but it would be easier and safer just to pack all arguments in array. and concerning ur resource stuff - moving primitive types as raw data isn't good idea, using minimalistic structs instead would give you lot more control(and safety) later as long as easier API tweaking later p.s. i hope this just for testing, that vbo/resource api isn't convenient at all, and if you need vertex data loader i recommend you try assimp/derelict3 pair -_-
Thanks a lot, I think using Variant[] is a better way. For the moment (maybe for few minutes) it's necessary to always give the Variant array to the getResource method, but it can be null. I actually use derelict3, but for this VBO isn't not necessary to have something much more advanced as it's only for a Quad generated by hand. I am sharing the indexes VBO of Quads. The operational parameters can be useful to load a Texture from an Image instance instead of only the filePath for exemple. filePath also act as key to be able to get resources.
May 11 2013
parent reply "evilrat" <evilrat666 gmail.com> writes:
On Saturday, 11 May 2013 at 22:01:33 UTC, Flamaros wrote:
 Thanks a lot, I think using Variant[] is a better way. For the 
 moment (maybe for few minutes) it's necessary to always give 
 the Variant array to the getResource method, but it can be null.

 I actually use derelict3, but for this VBO isn't not necessary 
 to have something much more advanced as it's only for a Quad 
 generated by hand. I am sharing the indexes VBO of Quads.

 The operational parameters can be useful to load a Texture from 
 an Image instance instead of only the filePath for exemple. 
 filePath also act as key to be able to get resources.
you don't even need to always pass null when you don't need anything, use default parameters. void someFunc(Variant[] array = null) {...} or void anotherFunc(Variant[] array = Variant[].init) {...} ok, if this is just a part of tutorial or demo i don't bother with my "tips" about api design anymore.
May 11 2013
parent "Flamaros" <flamaros.xavier gmail.com> writes:
On Sunday, 12 May 2013 at 01:22:16 UTC, evilrat wrote:
 On Saturday, 11 May 2013 at 22:01:33 UTC, Flamaros wrote:
 Thanks a lot, I think using Variant[] is a better way. For the 
 moment (maybe for few minutes) it's necessary to always give 
 the Variant array to the getResource method, but it can be 
 null.

 I actually use derelict3, but for this VBO isn't not necessary 
 to have something much more advanced as it's only for a Quad 
 generated by hand. I am sharing the indexes VBO of Quads.

 The operational parameters can be useful to load a Texture 
 from an Image instance instead of only the filePath for 
 exemple. filePath also act as key to be able to get resources.
you don't even need to always pass null when you don't need anything, use default parameters. void someFunc(Variant[] array = null) {...} or void anotherFunc(Variant[] array = Variant[].init) {...} ok, if this is just a part of tutorial or demo i don't bother with my "tips" about api design anymore.
We hope to open our code in few weeks, and as it will be a library, help on API design will be welcome. For the moment we are working a doing a prototype, with core features to see if the project is viable.
May 12 2013