method argument wildcard

Apprentice
Posts: 14
Joined: 2010.01
Post: #1
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.
Quote this message in a reply
Member
Posts: 34
Joined: 2010.01
Post: #2
Maybe you want to use void * ?
Quote this message in a reply
Moderator
Posts: 1,140
Joined: 2005.07
Post: #3
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.
Quote this message in a reply
Apprentice
Posts: 14
Joined: 2010.01
Post: #4
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.
Quote this message in a reply
Member
Posts: 34
Joined: 2010.01
Post: #5
Code:
something = ((Vector2f*)vertices)[i]

should also work by the way.
Quote this message in a reply
Post Reply