diff --git a/wrap/Constructor.cpp b/wrap/Constructor.cpp index e999951b2..7785de08f 100644 --- a/wrap/Constructor.cpp +++ b/wrap/Constructor.cpp @@ -128,8 +128,8 @@ void Constructor::matlab_wrapper(const string& toolboxPath, file.oss << " if(self) {" << endl; file.oss << " if(nargin > 1) {" << endl; file.oss << " collector.insert(self);" << endl; - //TODO: Add verbosity flag - //file.oss << " std::cout << \"Collected\" << collector.size() << std::endl;" << endl; + if(verbose_) + file.oss << " std::cout << \"Collected\" << collector.size() << std::endl;" << endl; file.oss << " }" << endl; file.oss << " else if(collector.erase(self))" << endl; file.oss << " delete self;" << endl; @@ -148,7 +148,8 @@ void Constructor::matlab_wrapper(const string& toolboxPath, //file.oss << " self = construct(nc, in);" << endl; file.oss << " collector.insert(self);" << endl; - file.oss << " std::cout << \"constructed \" << self << \", size=\" << collector.size() << std::endl;" << endl; + if(verbose_) + file.oss << " std::cout << \"constructed \" << self << \", size=\" << collector.size() << std::endl;" << endl; file.oss << " out[0] = mxCreateNumericMatrix(1, 1, mxUINT32OR64_CLASS, mxREAL);" << endl; file.oss << " *reinterpret_cast (mxGetPr(out[0])) = self;" << endl; file.oss << " }" << endl; diff --git a/wrap/Constructor.h b/wrap/Constructor.h index a766905b6..467ff3b22 100644 --- a/wrap/Constructor.h +++ b/wrap/Constructor.h @@ -28,12 +28,11 @@ namespace wrap { struct Constructor { /// Constructor creates an empty class - Constructor(bool verbose = true) : + Constructor(bool verbose = false) : verbose_(verbose) { } // Then the instance variables are set directly by the Module constructor - // TODO:Vector of argument lists? std::vector args_list; std::string name; bool verbose_; diff --git a/wrap/matlab.h b/wrap/matlab.h index 74c0823e4..3cfebdfe7 100644 --- a/wrap/matlab.h +++ b/wrap/matlab.h @@ -376,138 +376,3 @@ boost::shared_ptr unwrap_shared_ptr(const mxArray* obj, const string& cla boost::shared_ptr* spp = *reinterpret_cast**> (mxGetPr(mxh)); return *spp; } - - -//***************************************************************************** -// Shared Pointer Handle -// inspired by mexhandle, but using shared_ptr -//***************************************************************************** - -template -class ObjectHandle { -private: - ObjectHandle* signature; // use 'this' as a unique object signature - const std::type_info* type; // type checking information - boost::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(ptr) { - signature = this; - mexPrintf("Created Shared Pointer use_count = %li\n", t.use_count()); - mexPrintf("Created Pointer points to %d\n", t.get()); - - } - - // Constructor for shared pointers - // Creates shared pointer, will delete if is last one to hold pointer - ObjectHandle(boost::shared_ptr shared_ptr) : - /*type(&typeid(T)),*/ t(shared_ptr) { - signature = this; - mexPrintf("Created sp from sp use_count = %li\n", t.use_count()); - mexPrintf("Created sp from sp points to %d\n", t.get()); - } - - ~ObjectHandle() { - // object is in shared_ptr, will be automatically deleted - signature = 0; // destroy signature - // std::cout << "ObjectHandle destructor" << std::endl; - } - - // Get the actual object contained by handle - boost::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()); - } - - // 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(); - } - - // 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)); - - 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->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; - } - -}; - -//***************************************************************************** -// wrapping C++ objects in a MATLAB proxy class -//***************************************************************************** - -/* - For every C++ class Class, a matlab proxy class @Class/Class.m object - is created. Its constructor will check which of the C++ constructors - needs to be called, based on nr of arguments. It then calls the - corresponding mex function new_Class_signature, which will create a - C++ object using new, and pass the pointer to wrap_constructed - (below). This creates a mexhandle and returns it to the proxy class - constructor, which assigns it to self. Matlab owns this handle now. -*/ -template -mxArray* wrap_constructed(Class* pointer, const char *classname) { - ObjectHandle* handle = new ObjectHandle(pointer); - return handle->to_mex_handle(); -} - - - -//***************************************************************************** -// unwrapping a MATLAB proxy class to a C++ object reference -//***************************************************************************** - -/* - Besides the basis types, the only other argument type allowed is a - shared pointer to a C++ object. In this case, matlab needs to pass a - proxy class object to the mex function. [unwrap_shared_ptr] extracts - the ObjectHandle from the self property, and returns a shared pointer - to the object. -*/ - - -template -void delete_shared_ptr(const mxArray* obj, const string& className) { - mxArray* mxh = mxGetProperty(obj,0,"self"); - ObjectHandle* handle = ObjectHandle::from_mex_handle(mxh); - delete handle; -} diff --git a/wrap/wrap.cpp b/wrap/wrap.cpp index eabba647e..f4490e583 100644 --- a/wrap/wrap.cpp +++ b/wrap/wrap.cpp @@ -42,7 +42,7 @@ void generate_matlab_toolbox( { // Parse interface file into class object // This recursively creates Class objects, Method objects, etc... - wrap::Module module(interfacePath, moduleName, true); + wrap::Module module(interfacePath, moduleName, false); // Then emit MATLAB code module.matlab_code(mexCommand,toolboxPath,mexExt,mexFlags);