Add to collector through matlab function to allow returning objects from other wrap modules
parent
ce4968d498
commit
8ab18498ad
|
@ -20,6 +20,7 @@
|
|||
#include <fstream>
|
||||
|
||||
#include <boost/foreach.hpp>
|
||||
#include <boost/lexical_cast.hpp>
|
||||
|
||||
#include "Class.h"
|
||||
#include "utilities.h"
|
||||
|
@ -28,23 +29,13 @@
|
|||
using namespace std;
|
||||
using namespace wrap;
|
||||
|
||||
static const uint64_t ptr_constructor_key =
|
||||
(uint64_t('G') << 56) |
|
||||
(uint64_t('T') << 48) |
|
||||
(uint64_t('S') << 40) |
|
||||
(uint64_t('A') << 32) |
|
||||
(uint64_t('M') << 24) |
|
||||
(uint64_t('p') << 16) |
|
||||
(uint64_t('t') << 8) |
|
||||
(uint64_t('r'));
|
||||
|
||||
/* ************************************************************************* */
|
||||
void Class::matlab_proxy(const string& classFile, const string& wrapperName, FileWriter& wrapperFile, vector<string>& functionNames) const {
|
||||
// open destination classFile
|
||||
FileWriter proxyFile(classFile, verbose_, "%");
|
||||
|
||||
// get the name of actual matlab object
|
||||
string matlabName = qualifiedName(), cppName = qualifiedName("::");
|
||||
const string matlabName = qualifiedName(), cppName = qualifiedName("::");
|
||||
|
||||
// emit class proxy code
|
||||
// we want our class to inherit the handle class for memory purposes
|
||||
|
@ -56,11 +47,17 @@ void Class::matlab_proxy(const string& classFile, const string& wrapperName, Fil
|
|||
|
||||
// Constructor
|
||||
proxyFile.oss << " function obj = " << matlabName << "(varargin)" << endl;
|
||||
// Special pointer constructor
|
||||
// Special pointer constructors - one in MATLAB to create an object and
|
||||
// assign a pointer returned from a C++ function. In turn this MATLAB
|
||||
// constructor calls a special C++ function that just adds the object to
|
||||
// its collector. This allows wrapped functions to return objects in
|
||||
// other wrap modules - to add these to their collectors the pointer is
|
||||
// passed from one C++ module into matlab then back into the other C++
|
||||
// module.
|
||||
{
|
||||
proxyFile.oss << " if nargin == 2 && isa(varargin{1}, 'uint64') && ";
|
||||
proxyFile.oss << "varargin{1} == uint64(" << ptr_constructor_key << ")\n";
|
||||
proxyFile.oss << " obj.self = varargin{2};\n";
|
||||
int id = functionNames.size();
|
||||
const string functionName = pointer_constructor_fragments(proxyFile, wrapperFile, wrapperName, id);
|
||||
functionNames.push_back(functionName);
|
||||
}
|
||||
// Regular constructors
|
||||
BOOST_FOREACH(ArgumentList a, constructor.args_list)
|
||||
|
@ -129,3 +126,45 @@ string Class::qualifiedName(const string& delim) const {
|
|||
}
|
||||
|
||||
/* ************************************************************************* */
|
||||
string Class::pointer_constructor_fragments(FileWriter& proxyFile, FileWriter& wrapperFile, const string& wrapperName, int id) const {
|
||||
|
||||
static const uint64_t ptr_constructor_key =
|
||||
(uint64_t('G') << 56) |
|
||||
(uint64_t('T') << 48) |
|
||||
(uint64_t('S') << 40) |
|
||||
(uint64_t('A') << 32) |
|
||||
(uint64_t('M') << 24) |
|
||||
(uint64_t('p') << 16) |
|
||||
(uint64_t('t') << 8) |
|
||||
(uint64_t('r'));
|
||||
|
||||
const string matlabName = qualifiedName(), cppName = qualifiedName("::");
|
||||
const string wrapFunctionName = matlabName + "_constructor_" + boost::lexical_cast<string>(id);
|
||||
|
||||
// MATLAB constructor that assigns pointer to matlab object then calls c++
|
||||
// function to add the object to the collector.
|
||||
proxyFile.oss << " if nargin == 2 && isa(varargin{1}, 'uint64') && ";
|
||||
proxyFile.oss << "varargin{1} == uint64(" << ptr_constructor_key << ")\n";
|
||||
proxyFile.oss << " obj.self = varargin{2};\n";
|
||||
proxyFile.oss << " " << wrapperName << "(obj.self);\n";
|
||||
|
||||
// C++ function to add pointer from MATLAB to collector. The pointer always
|
||||
// comes from a C++ return value; this mechanism allows the object to be added
|
||||
// to a collector in a different wrap module.
|
||||
wrapperFile.oss << "void " << wrapFunctionName << "(int nargout, mxArray *out[], int nargin, const mxArray *in[])" << endl;
|
||||
wrapperFile.oss << "{\n";
|
||||
wrapperFile.oss << " mexAtExit(&_deleteAllObjects);\n";
|
||||
generateUsingNamespace(wrapperFile, using_namespaces);
|
||||
// Typedef boost::shared_ptr
|
||||
wrapperFile.oss << " typedef boost::shared_ptr<" << cppName << "> Shared;\n";
|
||||
wrapperFile.oss << "\n";
|
||||
// Get self pointer passed in
|
||||
wrapperFile.oss << " Shared *self = *reinterpret_cast<Shared**> (mxGetData(in[0]));\n";
|
||||
// Add to collector
|
||||
wrapperFile.oss << " collector_" << matlabName << ".insert(self);\n";
|
||||
wrapperFile.oss << "}\n";
|
||||
|
||||
return wrapFunctionName;
|
||||
}
|
||||
|
||||
/* ************************************************************************* */
|
||||
|
|
|
@ -50,6 +50,9 @@ struct Class {
|
|||
void matlab_static_methods(const std::string& toolboxPath, const std::string& wrapperName,
|
||||
FileWriter& wrapperFile, std::vector<std::string>& functionNames) const; ///< emit static method wrappers
|
||||
std::string qualifiedName(const std::string& delim = "") const; ///< creates a namespace-qualified name, optional delimiter
|
||||
|
||||
private:
|
||||
std::string pointer_constructor_fragments(FileWriter& proxyFile, FileWriter& wrapperFile, const std::string& wrapperName, int id) const;
|
||||
};
|
||||
|
||||
} // \namespace wrap
|
||||
|
|
|
@ -67,7 +67,7 @@ string Constructor::wrapper_fragment(FileWriter& file,
|
|||
const vector<string>& using_namespaces,
|
||||
const vector<string>& includes,
|
||||
const ArgumentList& al) const {
|
||||
const string matlabName = matlab_wrapper_name(matlabClassName);
|
||||
|
||||
const string wrapFunctionName = matlabClassName + "_constructor_" + boost::lexical_cast<string>(id);
|
||||
|
||||
file.oss << "void " << wrapFunctionName << "(int nargout, mxArray *out[], int nargin, const mxArray *in[])" << endl;
|
||||
|
@ -75,7 +75,7 @@ string Constructor::wrapper_fragment(FileWriter& file,
|
|||
file.oss << " mexAtExit(&_deleteAllObjects);\n";
|
||||
generateUsingNamespace(file, using_namespaces);
|
||||
//Typedef boost::shared_ptr
|
||||
file.oss << " typedef boost::shared_ptr<" << cppClassName << "> Shared;\n";
|
||||
file.oss << " typedef boost::shared_ptr<" << cppClassName << "> Shared;\n";
|
||||
file.oss << "\n";
|
||||
|
||||
//Check to see if there will be any arguments and remove {} for consiseness
|
||||
|
|
|
@ -49,19 +49,15 @@ string ReturnValue::qualifiedType2(const string& delim) const {
|
|||
void ReturnValue::wrap_result(FileWriter& file) const {
|
||||
string cppType1 = qualifiedType1("::"), matlabType1 = qualifiedType1();
|
||||
string cppType2 = qualifiedType2("::"), matlabType2 = qualifiedType2();
|
||||
const string collectorName1 = "collector_" + matlabType1;
|
||||
const string collectorName2 = "collector_" + matlabType2;
|
||||
|
||||
if (isPair) {
|
||||
// first return value in pair
|
||||
if (isPtr1) {// if we already have a pointer
|
||||
file.oss << " Shared" << type1 <<"* ret = new Shared" << type1 << "(result.first);" << endl;
|
||||
file.oss << " " << collectorName1 << ".insert(ret);\n";
|
||||
file.oss << " out[0] = wrap_shared_ptr(ret,\"" << matlabType1 << "\");\n";
|
||||
}
|
||||
else if (category1 == ReturnValue::CLASS) { // if we are going to make one
|
||||
file.oss << " Shared" << type1 << "* ret = new Shared" << type1 << "(new " << cppType1 << "(result.first));\n";
|
||||
file.oss << " " << collectorName1 << ".insert(ret);\n";
|
||||
file.oss << " out[0] = wrap_shared_ptr(ret,\"" << matlabType1 << "\");\n";
|
||||
}
|
||||
else // if basis type
|
||||
|
@ -70,12 +66,10 @@ void ReturnValue::wrap_result(FileWriter& file) const {
|
|||
// second return value in pair
|
||||
if (isPtr2) {// if we already have a pointer
|
||||
file.oss << " Shared" << type2 <<"* ret = new Shared" << type2 << "(result.second);" << endl;
|
||||
file.oss << " " << collectorName2 << ".insert(ret);\n";
|
||||
file.oss << " out[1] = wrap_shared_ptr(ret,\"" << matlabType2 << "\");\n";
|
||||
}
|
||||
else if (category2 == ReturnValue::CLASS) { // if we are going to make one
|
||||
file.oss << " Shared" << type2 << "* ret = new Shared" << type2 << "(new " << cppType2 << "(result.first));\n";
|
||||
file.oss << " " << collectorName2 << ".insert(ret);\n";
|
||||
file.oss << " out[0] = wrap_shared_ptr(ret,\"" << matlabType2 << "\");\n";
|
||||
}
|
||||
else
|
||||
|
@ -83,12 +77,10 @@ void ReturnValue::wrap_result(FileWriter& file) const {
|
|||
}
|
||||
else if (isPtr1){
|
||||
file.oss << " Shared" << type1 <<"* ret = new Shared" << type1 << "(result);" << endl;
|
||||
file.oss << " " << collectorName1 << ".insert(ret);\n";
|
||||
file.oss << " out[0] = wrap_shared_ptr(ret,\"" << matlabType1 << "\");\n";
|
||||
}
|
||||
else if (category1 == ReturnValue::CLASS){
|
||||
file.oss << " Shared" << type1 << "* ret = new Shared" << type1 << "(new " << cppType1 << "(result));\n";
|
||||
file.oss << " " << collectorName1 << ".insert(ret);\n";
|
||||
file.oss << " out[0] = wrap_shared_ptr(ret,\"" << matlabType1 << "\");\n";
|
||||
}
|
||||
else if (matlabType1!="void")
|
||||
|
|
Loading…
Reference in New Issue