Made wrap work on 64-bit platform

release/4.3a0
Frank Dellaert 2009-12-12 04:08:57 +00:00
parent be10fc2a90
commit d7bbe0a355
1 changed files with 78 additions and 67 deletions

View File

@ -20,6 +20,13 @@ using namespace boost; // not usual, but for consiseness of generated code
typedef numeric::ublas::vector<double> Vector; typedef numeric::ublas::vector<double> Vector;
typedef numeric::ublas::matrix<double> Matrix; typedef numeric::ublas::matrix<double> Matrix;
#ifdef __LP64__
// 64-bit Mac
#define mxUINT32OR64_CLASS mxUINT64_CLASS
#else
#define mxUINT32OR64_CLASS mxUINT32_CLASS
#endif
//***************************************************************************** //*****************************************************************************
// Utilities // Utilities
//***************************************************************************** //*****************************************************************************
@ -40,7 +47,7 @@ mxArray *vector(int m, mxClassID classid) {
mxArray *matrix(int m, int n, mxClassID classid) { mxArray *matrix(int m, int n, mxClassID classid) {
mwSize dims[2]; dims[0]=m; dims[1]=n; mwSize dims[2]; dims[0]=m; dims[1]=n;
return mxCreateNumericArray(2, dims, mxUINT32_CLASS, mxREAL); return mxCreateNumericArray(2, dims, mxUINT32OR64_CLASS, mxREAL);
} }
//***************************************************************************** //*****************************************************************************
@ -72,25 +79,25 @@ mxArray* wrap<string>(string& value) {
} }
// specialization to bool -> uint32 // specialization to bool -> uint32
// Warning: relies on sizeof(UINT32_T)==sizeof(bool) // Warning: might rely on sizeof(UINT32_T)==sizeof(bool)
template<> template<>
mxArray* wrap<bool>(bool& value) { mxArray* wrap<bool>(bool& value) {
mxArray *result = scalar(mxUINT32_CLASS); mxArray *result = scalar(mxUINT32OR64_CLASS);
*(bool*)mxGetData(result) = value; *(bool*)mxGetData(result) = value;
return result; return result;
} }
// specialization to size_t -> uint32 // specialization to size_t -> uint32
// Warning: relies on sizeof(UINT32_T)==sizeof(size_t) // Warning: might rely on sizeof(UINT32_T)==sizeof(size_t)
template<> template<>
mxArray* wrap<size_t>(size_t& value) { mxArray* wrap<size_t>(size_t& value) {
mxArray *result = scalar(mxUINT32_CLASS); mxArray *result = scalar(mxUINT32OR64_CLASS);
*(size_t*)mxGetData(result) = value; *(size_t*)mxGetData(result) = value;
return result; return result;
} }
// specialization to int -> uint32 // specialization to int -> uint32
// Warning: relies on sizeof(INT32_T)==sizeof(int) // Warning: might rely on sizeof(INT32_T)==sizeof(int)
template<> template<>
mxArray* wrap<int>(int& value) { mxArray* wrap<int>(int& value) {
mxArray *result = scalar(mxINT32_CLASS); mxArray *result = scalar(mxINT32_CLASS);
@ -241,14 +248,16 @@ private:
public: public:
// Constructor for free-store allocated objects. // Constructor for free-store allocated objects.
// Creates shared pointer, will delete if is last one to hold pointer // Creates shared pointer, will delete if is last one to hold pointer
ObjectHandle(T* ptr) : type(&typeid(T)), t(shared_ptr<T>(ptr)) { ObjectHandle(T* ptr) :
type(&typeid(T)), t(shared_ptr<T> (ptr)) {
signature = this; signature = this;
Collector<T>::register_handle(this); Collector<T>::register_handle(this);
} }
// Constructor for shared pointers // Constructor for shared pointers
// Creates shared pointer, will delete if is last one to hold pointer // Creates shared pointer, will delete if is last one to hold pointer
ObjectHandle(shared_ptr<T> ptr) : type(&typeid(T)), t(ptr) { ObjectHandle(shared_ptr<T> ptr) :
type(&typeid(T)), t(ptr) {
signature = this; signature = this;
} }
@ -258,7 +267,9 @@ ObjectHandle(shared_ptr<T> ptr) : type(&typeid(T)), t(ptr) {
} }
// Get the actual object contained by handle // Get the actual object contained by handle
shared_ptr<T> get_object() const { return t; } shared_ptr<T> get_object() const {
return t;
}
// Print the mexhandle for debugging // Print the mexhandle for debugging
void print(const char* str) { void print(const char* str) {
@ -270,23 +281,23 @@ ObjectHandle(shared_ptr<T> ptr) : type(&typeid(T)), t(ptr) {
// Convert ObjectHandle<T> to a mxArray handle (to pass back from mex-function). // Convert ObjectHandle<T> to a mxArray handle (to pass back from mex-function).
// Create a numeric array as handle for an ObjectHandle. // Create a numeric array as handle for an ObjectHandle.
// We ASSUME we can store object pointer in the mxUINT32 element of mxArray. // We ASSUME we can store object pointer in the mxUINT32 element of mxArray.
mxArray* to_mex_handle() mxArray* to_mex_handle() {
{ mxArray* handle = mxCreateNumericMatrix(1, 1, mxUINT32OR64_CLASS, mxREAL);
mxArray* handle = mxCreateNumericMatrix(1, 1, mxUINT32_CLASS, mxREAL);
*reinterpret_cast<ObjectHandle<T>**> (mxGetPr(handle)) = this; *reinterpret_cast<ObjectHandle<T>**> (mxGetPr(handle)) = this;
return handle; return handle;
} }
string type_name() const {return type->name();} string type_name() const {
return type->name();
}
// Convert mxArray (passed to mex-function) to an ObjectHandle<T>. // Convert mxArray (passed to mex-function) to an ObjectHandle<T>.
// Import a handle from MatLab as a mxArray of UINT32. Check that // Import a handle from MatLab as a mxArray of UINT32. Check that
// it is actually a pointer to an ObjectHandle<T>. // it is actually a pointer to an ObjectHandle<T>.
static ObjectHandle* from_mex_handle(const mxArray* handle) static ObjectHandle* from_mex_handle(const mxArray* handle) {
{ if (mxGetClassID(handle) != mxUINT32OR64_CLASS || mxIsComplex(handle)
if (mxGetClassID(handle) != mxUINT32_CLASS || mxGetM(handle) != 1 || mxGetN(handle) != 1) error(
|| mxIsComplex(handle) || mxGetM(handle)!=1 || mxGetN(handle)!=1) "Parameter is not an ObjectHandle type.");
error("Parameter is not an ObjectHandle type.");
// We *assume* we can store ObjectHandle<T> pointer in the mxUINT32 of handle // We *assume* we can store ObjectHandle<T> pointer in the mxUINT32 of handle
ObjectHandle* obj = *reinterpret_cast<ObjectHandle**> (mxGetPr(handle)); ObjectHandle* obj = *reinterpret_cast<ObjectHandle**> (mxGetPr(handle));