Game Development Community

Need someone to review the following C++

by Vince Gee · in Torque 3D Professional · 04/07/2012 (1:01 pm) · 9 replies

Can someone look at this and tell me if anything is wrong.

I am using the string table to convert char* to const char*, but how do I clear them before exiting the function? Or does the string table take care of this for me?

Or... is there a better way to do this?

Thanks,

Vince

extern "C" __declspec(dllexport) S32 wle_fn__aiConnect(char * x__a1, char * x__a2, char * x__a3, char * x__a4, char * x__a5, char * x__a6, char * x__a7, char * x__a8, char * x__a9, char * x__a10, char * x__a11, char * x__a12, char * x__a13, char * x__a14, char * x__a15, char * x__a16, char * x__a17, char * x__a18, char * x__a19)
{
StringTableEntry a1 = StringTable->insert(x__a1);
StringTableEntry a2 = StringTable->insert(x__a2);
StringTableEntry a3 = StringTable->insert(x__a3);
StringTableEntry a4 = StringTable->insert(x__a4);
StringTableEntry a5 = StringTable->insert(x__a5);
StringTableEntry a6 = StringTable->insert(x__a6);
StringTableEntry a7 = StringTable->insert(x__a7);
StringTableEntry a8 = StringTable->insert(x__a8);
StringTableEntry a9 = StringTable->insert(x__a9);
StringTableEntry a10 = StringTable->insert(x__a10);
StringTableEntry a11 = StringTable->insert(x__a11);
StringTableEntry a12 = StringTable->insert(x__a12);
StringTableEntry a13 = StringTable->insert(x__a13);
StringTableEntry a14 = StringTable->insert(x__a14);
StringTableEntry a15 = StringTable->insert(x__a15);
StringTableEntry a16 = StringTable->insert(x__a16);
StringTableEntry a17 = StringTable->insert(x__a17);
StringTableEntry a18 = StringTable->insert(x__a18);
StringTableEntry a19 = StringTable->insert(x__a19);
{
S32 argc = 20;
std::vector<const char*> arguments;
arguments.push_back("");
arguments.push_back(a1);
arguments.push_back(a2);
arguments.push_back(a3);
arguments.push_back(a4);
arguments.push_back(a5);
arguments.push_back(a6);
arguments.push_back(a7);
arguments.push_back(a8);
arguments.push_back(a9);
arguments.push_back(a10);
arguments.push_back(a11);
arguments.push_back(a12);
arguments.push_back(a13);
arguments.push_back(a14);
arguments.push_back(a15);
arguments.push_back(a16);
arguments.push_back(a17);
arguments.push_back(a18);
arguments.push_back(a19);
const char** argv = &arguments[0];
{
   // Create the connection
   AIConnection *aiConnection = new AIConnection();
   aiConnection->registerObject();
   // Add the connection to the client group
   SimGroup *g = Sim::getClientGroup();
   g->addObject( aiConnection );
   // Prep the arguments for the console exec...
   // Make sure and leav args[1] empty.
   const char* args[21];
   args[0] = "onConnect";
   for (S32 i = 1; i < argc; i++)
      args[i + 1] = argv[i];
   // Execute the connect console function, this is the same
   // onConnect function invoked for normal client connections
   Con::execute(aiConnection, argc + 1, args);
   return aiConnection->getId();
}
}
}

#1
04/07/2012 (2:20 pm)
hi!

any C++ coders looking to join a group that plans on doin some massive things to the C++ inside of T3D.... that can help poor vince keep what hair and little sanity the man has left? if so, ping Vince at

vgee(at)winterleafentertainment(dot)com

*ducks and runs*

#2
04/08/2012 (6:14 pm)
Line 56 in stringTable.h:
Quote:
/// @note Be aware that the StringTable NEVER DEALLOCATES memory, so be careful when you
/// add strings to it. If you carelessly add many strings, you will end up wasting
/// space.

I would guess you may slow down the lookup, but it is hashed, so maybe that is not the case. It will eat up memory.

Why don't you just do a dynamic cast or a fixed cast?

Fixed cast:
char * input = "blah, blah, blah";
const char *tmpinput = (const char*)input;

Also, why so many parameters? That is a lot of stuff to put on the stack. Can you store them and access them in a array of pointers? What exactly are you passing in those strings?
#4
04/08/2012 (6:36 pm)
reinterpret Casting / Const Casting might be a better route...
#5
04/09/2012 (3:53 am)
Really appreciate it guys, yeah I thought it was bad, just couldn't think of a better way of doing it.
#6
04/09/2012 (10:29 am)
And in recent news, Vince Gee, a computer scientist was last seen wearing orange coveralls wielding a crowbar. One bystander, who wished to remain anonymous, watched in horror as Mr. Gee continually hit a steel drum and yelling in frustration that the drum "break". Mr. Gee left a trail of smashed crates, cardboard boxes, and pallets. The steel drum was left unscathed except for some minor dents. Mr. Gee has not been seen since.
#7
04/09/2012 (11:46 am)
ROFL!!!!

It was traumatizing!

Frank, come save me!!!!
#8
04/10/2012 (4:52 am)
I ended up removing the stringtable entries and instead cast the pointers to const char * pointers instead. Since the objects passed cannot be altered (i.e. their values changed) once passed to the extern I shouldn't have to worry about the "const" part of the variable being violated.

I also added a vector.clear() to the end of the function. I thought the destructor would do it automatically, but I just added it for safety.
#9
04/10/2012 (4:54 am)
The auto-generated code looks like

extern "C" __declspec(dllexport) S32 wle_fn__aiConnect(char * x__a1, char * x__a2, char * x__a3, char * x__a4, char * x__a5, char * x__a6, char * x__a7, char * x__a8, char * x__a9, char * x__a10, char * x__a11, char * x__a12, char * x__a13, char * x__a14, char * x__a15, char * x__a16, char * x__a17, char * x__a18, char * x__a19)
{
const char* a1 = (const char*)x__a1;
const char* a2 = (const char*)x__a2;
const char* a3 = (const char*)x__a3;
const char* a4 = (const char*)x__a4;
const char* a5 = (const char*)x__a5;
const char* a6 = (const char*)x__a6;
const char* a7 = (const char*)x__a7;
const char* a8 = (const char*)x__a8;
const char* a9 = (const char*)x__a9;
const char* a10 = (const char*)x__a10;
const char* a11 = (const char*)x__a11;
const char* a12 = (const char*)x__a12;
const char* a13 = (const char*)x__a13;
const char* a14 = (const char*)x__a14;
const char* a15 = (const char*)x__a15;
const char* a16 = (const char*)x__a16;
const char* a17 = (const char*)x__a17;
const char* a18 = (const char*)x__a18;
const char* a19 = (const char*)x__a19;
{
S32 argc = 20;
if (dStrlen(a19)==0)
if (dStrlen(a18)==0)
if (dStrlen(a17)==0)
if (dStrlen(a16)==0)
if (dStrlen(a15)==0)
if (dStrlen(a14)==0)
if (dStrlen(a13)==0)
if (dStrlen(a12)==0)
if (dStrlen(a11)==0)
if (dStrlen(a10)==0)
if (dStrlen(a9)==0)
if (dStrlen(a8)==0)
if (dStrlen(a7)==0)
if (dStrlen(a6)==0)
if (dStrlen(a5)==0)
if (dStrlen(a4)==0)
if (dStrlen(a3)==0)
if (dStrlen(a2)==0)
if (dStrlen(a1)==0)
argc=1;
else
argc=2;
else
argc=3;
else
argc=4;
else
argc=5;
else
argc=6;
else
argc=7;
else
argc=8;
else
argc=9;
else
argc=10;
else
argc=11;
else
argc=12;
else
argc=13;
else
argc=14;
else
argc=15;
else
argc=16;
else
argc=17;
else
argc=18;
else
argc=19;
else
argc=20;
std::vector<const char*> arguments;
arguments.push_back("");
if (argc>=2)
arguments.push_back(a1);
if (argc>=3)
arguments.push_back(a2);
if (argc>=4)
arguments.push_back(a3);
if (argc>=5)
arguments.push_back(a4);
if (argc>=6)
arguments.push_back(a5);
if (argc>=7)
arguments.push_back(a6);
if (argc>=8)
arguments.push_back(a7);
if (argc>=9)
arguments.push_back(a8);
if (argc>=10)
arguments.push_back(a9);
if (argc>=11)
arguments.push_back(a10);
if (argc>=12)
arguments.push_back(a11);
if (argc>=13)
arguments.push_back(a12);
if (argc>=14)
arguments.push_back(a13);
if (argc>=15)
arguments.push_back(a14);
if (argc>=16)
arguments.push_back(a15);
if (argc>=17)
arguments.push_back(a16);
if (argc>=18)
arguments.push_back(a17);
if (argc>=19)
arguments.push_back(a18);
if (argc>=20)
arguments.push_back(a19);
const char** argv = &arguments[0];
{
   // Create the connection
   AIConnection *aiConnection = new AIConnection();
   aiConnection->registerObject();
   // Add the connection to the client group
   SimGroup *g = Sim::getClientGroup();
   g->addObject( aiConnection );
   // Prep the arguments for the console exec...
   // Make sure and leav args[1] empty.
   const char* args[21];
   args[0] = "onConnect";
   for (S32 i = 1; i < argc; i++)
      args[i + 1] = argv[i];
   // Execute the connect console function, this is the same
   // onConnect function invoked for normal client connections
   Con::execute(aiConnection, argc + 1, args);
   return aiConnection->getId();
}
arguments.clear();
}
}