iDevGames Forums
method argument wildcard - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Programming Languages & Scripting (/forum-8.html)
+--- Thread: method argument wildcard (/thread-95.html)



method argument wildcard - Sumaleth - Apr 30, 2010 10:46 PM

I'd like to do something like this:

Code:
Vector2f            *verticesA;
Vector3f            *verticesB;
TexturedVertex      *verticesC;

verticesA = malloc(10*sizeof(Vector2f));
verticesB = ...
verticesC = ...

[self registerVertices:verticesA ofNumber:10 forType:0];
[self registerVertices:verticesB ofNumber:22 forType:1];
[self registerVertices:verticesC ofNumber:39 forType:2];

--//--

Vector2f            *vertices_BatchA;
Vector3f            *vertices_BatchB;
TexturedVertex      *vertices_BatchC;

- (void)registerVertices:([b]_wildcard_[/b]*)vertices ofNumber:(uint) forType:(uint)type
{
    switch(type) {
        case 0:
            for(int i=0; i<number; i++)
                vertices_BatchA[i] = vertices[i];
                break;
        case 1:
            ...
        case 2:
            ...
    }
}

I use three different "resisterVertices" methods right now, but I'd like to have one method that can be used by all three vertex types.

If I was using NS objects I presume I could use id as the argument type, and then recast them in the switch statement, but this method sits in that area between the Objective-C and C parts of my project.

Is this possible, without wrapping them in some sort of NS object? I'm happy (even keen) to make it a C function if that helps.


method argument wildcard - Zorg - May 4, 2010 10:38 AM

Maybe you want to use void * ?


method argument wildcard - akb825 - May 4, 2010 09:08 PM

You can use a C++ template function to accomplish this. (http://www.cplusplus.com/doc/tutorial/templates/) You can compile C++ code with ObjectiveC by making that file's extension .mm instead of .m.


method argument wildcard - Sumaleth - May 5, 2010 07:59 AM

void* did the trick. I had tried it at one point, but didn't entirely understand how to use that pointer to access an array. This was the solution, with the void* argument:

Code:
for(int i=0; i<max; i++)
  something = *((Vector2f*)vertices + i)

Thanks for the help.


method argument wildcard - Zorg - May 5, 2010 08:16 AM

Code:
something = ((Vector2f*)vertices)[i]

should also work by the way.