Added small verbosity changes, verbose now defaults to false in wrap.cpp
parent
3f4446df03
commit
f0c8c023a0
|
@ -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,6 +148,7 @@ void Constructor::matlab_wrapper(const string& toolboxPath,
|
|||
|
||||
//file.oss << " self = construct(nc, in);" << endl;
|
||||
file.oss << " collector.insert(self);" << 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<Shared**> (mxGetPr(out[0])) = self;" << endl;
|
||||
|
|
|
@ -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<ArgumentList> args_list;
|
||||
std::string name;
|
||||
bool verbose_;
|
||||
|
|
135
wrap/matlab.h
135
wrap/matlab.h
|
@ -376,138 +376,3 @@ boost::shared_ptr<Class> unwrap_shared_ptr(const mxArray* obj, const string& cla
|
|||
boost::shared_ptr<Class>* spp = *reinterpret_cast<boost::shared_ptr<Class>**> (mxGetPr(mxh));
|
||||
return *spp;
|
||||
}
|
||||
|
||||
|
||||
//*****************************************************************************
|
||||
// Shared Pointer Handle
|
||||
// inspired by mexhandle, but using shared_ptr
|
||||
//*****************************************************************************
|
||||
|
||||
template<typename T>
|
||||
class ObjectHandle {
|
||||
private:
|
||||
ObjectHandle* signature; // use 'this' as a unique object signature
|
||||
const std::type_info* type; // type checking information
|
||||
boost::shared_ptr<T> 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<T> 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<T> 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<T> 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<ObjectHandle<T>**> (mxGetPr(handle)) = this;
|
||||
return handle;
|
||||
}
|
||||
|
||||
string type_name() const {
|
||||
return type->name();
|
||||
}
|
||||
|
||||
// Convert mxArray (passed to mex-function) to an ObjectHandle<T>.
|
||||
// Import a handle from MatLab as a mxArray of UINT32. Check that
|
||||
// it is actually a pointer to an ObjectHandle<T>.
|
||||
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<T> pointer in the mxUINT32 of handle
|
||||
ObjectHandle* obj = *reinterpret_cast<ObjectHandle**> (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 <typename Class>
|
||||
mxArray* wrap_constructed(Class* pointer, const char *classname) {
|
||||
ObjectHandle<Class>* handle = new ObjectHandle<Class>(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 <typename Class>
|
||||
void delete_shared_ptr(const mxArray* obj, const string& className) {
|
||||
mxArray* mxh = mxGetProperty(obj,0,"self");
|
||||
ObjectHandle<Class>* handle = ObjectHandle<Class>::from_mex_handle(mxh);
|
||||
delete handle;
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in New Issue