THinking again... it's not a so good idea, since even though it's easy to pass from LUA the pointer of the encapsulation class to C++, I have some C++ that push poitners to LUA. In order for this to work, I would have to maintain new instances of these "encapsulation" objects, and that would consume a lot of memory. I don't want to duplicate my data.
Therefore my idea of implementing SafePointer was that it returns a table with two values, the pointer, and the pointer type, so that i make the structure in LUA for every C++ pointer returning function, and then check the pointer parameters in LUA checking if the PointerType of that structure is the one that corresponds.
Quote:
2): This seems like overkill to me. You want to check each and every function parameter to ensure its correctness? That adds a small penalty cost for one function, but to put it in all your functions the penalty cost will add up linearly to the # of calls you make.
I think the answer depends on what binding library (if any) you are choosing to use in your game. For example, Luabind uses an exception handling mechanism: link. Using existing functionality like that seems like a much less painful idea to me.
Thanks Roots for your answer :). No I just want to check the parameters that are passed as pointers, no more. All the other parameters are left unchecked, and it is used only in functions that use pointer parameters.
Could you explain me more what do you mind by "binding library"? I am using LUA 5.1. Are you proposing me to catch C++ errors with LUA? the thing is that if I pass a bad pointer to the C++ function, there is no check in C++ to see if the parameter is right, so the code would "throw" no error, but crash the program instead. This is because I would never expect in C++ to pass a parameter of other type to a pointer (in C++ you would get compile error, while from LUA as I am using pMYObject* = (static_cast) LuaGetuserData(s,1), anything can be passed).
Note: I don't have my project with me now, the syntax of the previous function surely is different, but you get the idea :)
What do you suggest?!
Thanks so much in advance!
Rod
P.S: The use of my solution would be like this:
TYPE_3ds =1000; -->Constantfunction SafePointer(myPointer, myPointerType) return {Pointer = myPointer, PointerType = myPointerType);endfunction Mesh3D.New3ds(Filepath,p1,p2) local tempPointer = Mesh3D.Unsafe_New3ds(p1,p2); return SafePointer(tempPointer , TYPE_3ds};endfunction Render.3ds(My3ds, p1,p2,p3)if (My3ds.PointerType= TYPE_3ds) thenRender.3ds(My3ds.Pointer,p1,p2,p3);else Debug.MessageBox("Render.3ds was called by passing incorrect object. Only My3ds object should be passed. A pointer of type " .. My3ds.PointerType .. "was passed."); endendAirplaneMesh= Mesh3D.New3ds("MyPlane.3ds",34,2);Render.3ds(AirplaneMesh, 5,2,3); --> I am sure that AirplaneMesh is of type 3ds since I can check it's "PointerType" parameter.