From d7bbe0a3553adc83c58cba5642624d044289dd5b Mon Sep 17 00:00:00 2001 From: Frank Dellaert Date: Sat, 12 Dec 2009 04:08:57 +0000 Subject: [PATCH] Made wrap work on 64-bit platform --- wrap/wrap-matlab.h | 145 ++++++++++++++++++++++++--------------------- 1 file changed, 78 insertions(+), 67 deletions(-) diff --git a/wrap/wrap-matlab.h b/wrap/wrap-matlab.h index 28d428ecb..ff218d3a5 100644 --- a/wrap/wrap-matlab.h +++ b/wrap/wrap-matlab.h @@ -20,6 +20,13 @@ using namespace boost; // not usual, but for consiseness of generated code typedef numeric::ublas::vector Vector; typedef numeric::ublas::matrix Matrix; +#ifdef __LP64__ +// 64-bit Mac +#define mxUINT32OR64_CLASS mxUINT64_CLASS +#else +#define mxUINT32OR64_CLASS mxUINT32_CLASS +#endif + //***************************************************************************** // Utilities //***************************************************************************** @@ -40,7 +47,7 @@ mxArray *vector(int m, mxClassID classid) { mxArray *matrix(int m, int n, mxClassID classid) { 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& value) { } // specialization to bool -> uint32 -// Warning: relies on sizeof(UINT32_T)==sizeof(bool) +// Warning: might rely on sizeof(UINT32_T)==sizeof(bool) template<> mxArray* wrap(bool& value) { - mxArray *result = scalar(mxUINT32_CLASS); + mxArray *result = scalar(mxUINT32OR64_CLASS); *(bool*)mxGetData(result) = value; return result; } // 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<> mxArray* wrap(size_t& value) { - mxArray *result = scalar(mxUINT32_CLASS); + mxArray *result = scalar(mxUINT32OR64_CLASS); *(size_t*)mxGetData(result) = value; return result; } // specialization to int -> uint32 -// Warning: relies on sizeof(INT32_T)==sizeof(int) +// Warning: might rely on sizeof(INT32_T)==sizeof(int) template<> mxArray* wrap(int& value) { mxArray *result = scalar(mxINT32_CLASS); @@ -231,84 +238,88 @@ Matrix unwrap< Matrix >(const mxArray* array) { template class Collector; -template +template class ObjectHandle { private: - ObjectHandle* signature; // use 'this' as a unique object signature - const std::type_info* type; // type checking information - shared_ptr t; // object pointer + ObjectHandle* signature; // use 'this' as a unique object signature + const std::type_info* type; // type checking information + shared_ptr t; // object pointer public: - // Constructor for free-store allocated objects. - // Creates shared pointer, will delete if is last one to hold pointer -ObjectHandle(T* ptr) : type(&typeid(T)), t(shared_ptr(ptr)) { - signature = this; - Collector::register_handle(this); - } + // Constructor for free-store allocated objects. + // Creates shared pointer, will delete if is last one to hold pointer + ObjectHandle(T* ptr) : + type(&typeid(T)), t(shared_ptr (ptr)) { + signature = this; + Collector::register_handle(this); + } - // Constructor for shared pointers - // Creates shared pointer, will delete if is last one to hold pointer -ObjectHandle(shared_ptr ptr) : type(&typeid(T)), t(ptr) { - signature= this; - } + // Constructor for shared pointers + // Creates shared pointer, will delete if is last one to hold pointer + ObjectHandle(shared_ptr ptr) : + type(&typeid(T)), t(ptr) { + signature = this; + } - ~ObjectHandle() { - // object is in shared_ptr, will be automatically deleted - signature= 0; // destroy signature - } + ~ObjectHandle() { + // object is in shared_ptr, will be automatically deleted + signature = 0; // destroy signature + } - // Get the actual object contained by handle - shared_ptr get_object() const { return t; } + // Get the actual object contained by handle + shared_ptr get_object() const { + return t; + } - // Print the mexhandle for debugging - void print(const char* str) { - mexPrintf("mexhandle %s:\n", str); - mexPrintf(" signature = %d:\n", signature); - mexPrintf(" pointer = %d:\n", t.get()); - } + // Print the mexhandle for debugging + void print(const char* str) { + mexPrintf("mexhandle %s:\n", str); + mexPrintf(" signature = %d:\n", signature); + mexPrintf(" pointer = %d:\n", t.get()); + } - // Convert ObjectHandle to a mxArray handle (to pass back from mex-function). - // Create a numeric array as handle for an ObjectHandle. - // We ASSUME we can store object pointer in the mxUINT32 element of mxArray. - mxArray* to_mex_handle() - { - mxArray* handle = mxCreateNumericMatrix(1, 1, mxUINT32_CLASS, mxREAL); - *reinterpret_cast**>(mxGetPr(handle)) = this; - return handle; - } + // Convert ObjectHandle to a mxArray handle (to pass back from mex-function). + // Create a numeric array as handle for an ObjectHandle. + // We ASSUME we can store object pointer in the mxUINT32 element of mxArray. + mxArray* to_mex_handle() { + mxArray* handle = mxCreateNumericMatrix(1, 1, mxUINT32OR64_CLASS, mxREAL); + *reinterpret_cast**> (mxGetPr(handle)) = this; + 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. - // Import a handle from MatLab as a mxArray of UINT32. Check that - // it is actually a pointer to an ObjectHandle. - static ObjectHandle* from_mex_handle(const mxArray* handle) - { - if (mxGetClassID(handle) != mxUINT32_CLASS - || mxIsComplex(handle) || mxGetM(handle)!=1 || mxGetN(handle)!=1) - error("Parameter is not an ObjectHandle type."); + // Convert mxArray (passed to mex-function) to an ObjectHandle. + // Import a handle from MatLab as a mxArray of UINT32. Check that + // it is actually a pointer to an ObjectHandle. + static ObjectHandle* from_mex_handle(const mxArray* handle) { + if (mxGetClassID(handle) != mxUINT32OR64_CLASS || mxIsComplex(handle) + || mxGetM(handle) != 1 || mxGetN(handle) != 1) error( + "Parameter is not an ObjectHandle type."); - // We *assume* we can store ObjectHandle pointer in the mxUINT32 of handle - ObjectHandle* obj = *reinterpret_cast(mxGetPr(handle)); + // We *assume* we can store ObjectHandle pointer in the mxUINT32 of handle + ObjectHandle* obj = *reinterpret_cast (mxGetPr(handle)); - if (!obj) // gross check to see we don't have an invalid pointer - error("Parameter is NULL. It does not represent an ObjectHandle object."); - // TODO: change this for max-min check for pointer values + if (!obj) // gross check to see we don't have an invalid pointer + error("Parameter is NULL. It does not represent an ObjectHandle object."); + // TODO: change this for max-min check for pointer values - if (obj->signature != obj) // check memory has correct signature - error("Parameter does not represent an ObjectHandle object."); + if (obj->signature != obj) // check memory has correct signature + error("Parameter does not represent an ObjectHandle object."); -/* - if (*(obj->type) != typeid(T)) { // check type - mexPrintf("Given: <%s>, Required: <%s>.\n", obj->type_name(), typeid(T).name()); - error("Given ObjectHandle does not represent the correct type."); - } -*/ + /* + if (*(obj->type) != typeid(T)) { // check type + mexPrintf("Given: <%s>, Required: <%s>.\n", obj->type_name(), typeid(T).name()); + error("Given ObjectHandle does not represent the correct type."); + } + */ - return obj; - } + return obj; + } - friend class Collector; // allow Collector access to signature + friend class Collector ; // allow Collector access to signature }; // ---------------------------------------------------------