-
Multi type array wrapper
Hello all,
I have been working on this project for a while, and I cannot seem to find a way to implement it in a fast and efficient way. Here is the problem:
I am working with Mathematica and C++, using the MathLink package as provided by MMA. MathLink is a C/C++ library that can be used to communicate with sessions within MMA. It acts as the communication channel between MMA and custom C++ programs. Using this you can extend the functionality of the MMA package.
When one creates a function in C++, you can specify that you are responsible for acquiring the variables by yourself. Data is retrieved from the link by using functions such as MLGetInteger16Array(). The array will now be a pointer to a block of memory containing the data. In the end, every array gotten from the link needs to be freed using the function MLReleaseInteger16Array().
Working in this way, i notice several problems, such as being to lazy to free the allocated memory and the problem of finding the right index to the data. What I would like to do is to create a wrapper class so that when the array goes out of scope, the destructor will take care of the release of the allocated memory. Also, using operator overloading, I would like to be able to access values, and use automatic interpolation when needed.
MathLink supports a number of data types. There are 5 that I am most interested in, namely short, int, long, float and double.
In order to implement this wrapper class, i have ventured into two approaches, but running into problems for both. My first approach was to use Templates, but i ran into many problems with type mismatches (brilliantly, I somehow managed to throw the code away). Main problem is that for each type of data I need to use a specific function to acquire data from the link.
The second approach i took, was to store the data in a void pointer, and storing the type as a flag-value. This worked fine for the constructor and destructor, but now that i try to get values out of the array, I am a bit stuck. I would like to do that using overloading of the operator(). But for each type I would need a different function, mainly differing in the return value, but this, of course, is not possible. On the other hand, since I am interested in 5 data types, but I would like to implement 4 different operator() per data type (1D, 2D, 3D and nD), adding up to 20 different operator overloaders. This seems a bit over done.
Since I am using MathLink for speed-optimalizations, all of the wrapper should have as little overhead as possible. Any suggestions, on where to go from here? Any suggestions are welcome!
Gao Han