Hey guys,
I'm having a little trouble with a templated function, and I could use some advice. This function has to do with my memory manager, which I nearly have finished.
Here is the testing routine. I think this is pretty self-explanatory as to what it is meant to do:
//---------------------------------------------------------------------------
int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
unsigned int Block1, Block2, Block3;
int TestInt;
float TestFloat;
struct MyStruct{int TestInt; float TestFloat;} TestStruct;
MemoryMgr.CreatePool("EnginePool1");
Block1 = MemoryMgr.CreateBlock("EnginePool1", sizeof(int));
Block2 = MemoryMgr.CreateBlock("EnginePool1", sizeof(float));
Block3 = MemoryMgr.CreateBlock("EnginePool1", sizeof(TestStruct));
TestInt = 23;
TestFloat = 8.12f;
TestStruct.TestInt = 12;
TestStruct.TestFloat = 3.14f;
MemoryMgr.WriteBlock("EnginePool1", Block1, &TestInt, sizeof(TestInt));
MemoryMgr.WriteBlock("EnginePool1", Block2, &TestFloat, sizeof(TestFloat));
MemoryMgr.WriteBlock("EnginePool1", Block3, &TestStruct, sizeof(TestStruct));
MemoryMgr.WriteBlock("EnginePool1", Block1, 12, sizeof(int), 0);
MemoryMgr.WriteBlock("EnginePool1", Block2, 7.345f, sizeof(float), 0);
return 0;
}
Ignore the CreatePool and CreateBlocks. Those work fine. What I'm interested in is getting the WriteBlocks to work. As you can see, I would like to get the routine to either take a reference or an actual value and store it in the block. This would be the most flexible behavior for the client of the memory manager class.
I wrote a couple of functions to handle the pass by reference:
void WriteBlock(char *PoolName, unsigned int BlockHandle, void *Data, size_t ElementSize, unsigned short ArrayIndex=0);
void WriteBlock(unsigned int PoolHandle, unsigned int BlockHandle, void *Data, size_t ElementSize, unsigned short ArrayIndex=0);
Okay? This works fine. However, to handle the pass by value, I tried to add a templated function:
template <class AType> void WriteBlock(char *PoolName, unsigned int BlockHandle, AType Data, size_t ElementSize, unsigned short ArrayIndex);
Here is the issue. Everything works okay unless I specifiy a value for that defaulted argument (ArrayIndex) in the "pass by reference" style call, like such:
MemoryMgr.WriteBlock("EnginePool1", Block1, &TestInt, sizeof(TestInt), 0);
Instead of calling the first function, it is overloading the templated function (as an int* in this case), which was not my intent, and the problem is I can't use one function to handle both a pass by reference and a pass by value (they need different functionality).
I'm not really sure how to handle this, and would appreciate any advice.
[EDIT:
I suppose I could just not use a templated function for the pass by values, and simply define a bunch of overloaded routines, like such:
void WriteBlock(char *PoolName, unsigned int BlockHandle, int Data, unsigned short ArrayIndex=0);
void WriteBlock(char *PoolName, unsigned int BlockHandle, float Data, unsigned short ArrayIndex=0);
void WriteBlock(char *PoolName, unsigned int BlockHandle, double Data, unsigned short ArrayIndex=0);
//etc
One advantage is that I could then specify a default value for ArrayIndex (which 9 times out of 10 the client will want to be 0). But that's a lot of code duplication. It seems like a template is the way to go here, but I can't seem to figure out how to get it to work the way I want.]