1 """Code generation templates for the Matlab wrapper."""
7 """Class to encapsulate string templates for use in wrapper generation"""
8 boost_headers = textwrap.dedent(
"""
9 #include <boost/archive/text_iarchive.hpp>
10 #include <boost/archive/text_oarchive.hpp>
11 #include <boost/serialization/export.hpp>
14 typdef_collectors = textwrap.dedent(
'''\
15 typedef std::set<std::shared_ptr<{class_name_sep}>*> Collector_{class_name};
16 static Collector_{class_name} collector_{class_name};
19 delete_obj = textwrap.indent(textwrap.dedent(
'''\
20 {{ for(Collector_{class_name}::iterator iter = collector_{class_name}.begin();
21 iter != collector_{class_name}.end(); ) {{
23 collector_{class_name}.erase(iter++);
29 delete_all_objects = textwrap.dedent(
'''
30 void _deleteAllObjects()
33 std::streambuf *outbuf = std::cout.rdbuf(&mout);\n
34 bool anyDeleted = false;
38 "WARNING: Wrap modules with variables in the workspace have been reloaded due to\\n"
39 "calling destructors, call \'clear all\' again if you plan to now recompile a wrap\\n"
40 "module, so that your recompiled module is used instead of the old one." << endl;
41 std::cout.rdbuf(outbuf);
45 rtti_register = textwrap.dedent(
'''\
46 void _{module_name}_RTTIRegister() {{
47 const mxArray *alreadyCreated = mexGetVariablePtr("global", "gtsam_{module_name}_rttiRegistry_created");
48 if(!alreadyCreated) {{
49 std::map<std::string, std::string> types;
53 mxArray *registry = mexGetVariable("global", "gtsamwrap_rttiRegistry");
55 registry = mxCreateStructMatrix(1, 1, 0, NULL);
56 typedef std::pair<std::string, std::string> StringPair;
57 for(const StringPair& rtti_matlab: types) {{
58 int fieldId = mxAddField(registry, rtti_matlab.first.c_str());
60 mexErrMsgTxt("gtsam wrap: Error indexing RTTI types, inheritance will not work correctly");
62 mxArray *matlabName = mxCreateString(rtti_matlab.second.c_str());
63 mxSetFieldByNumber(registry, 0, fieldId, matlabName);
65 if(mexPutVariable("global", "gtsamwrap_rttiRegistry", registry) != 0) {{
66 mexErrMsgTxt("gtsam wrap: Error indexing RTTI types, inheritance will not work correctly");
68 mxDestroyArray(registry);
70 mxArray *newAlreadyCreated = mxCreateNumericMatrix(0, 0, mxINT8_CLASS, mxREAL);
71 if(mexPutVariable("global", "gtsam_{module_name}_rttiRegistry_created", newAlreadyCreated) != 0) {{
72 mexErrMsgTxt("gtsam wrap: Error indexing RTTI types, inheritance will not work correctly");
74 mxDestroyArray(newAlreadyCreated);
79 collector_function_upcast_from_void = textwrap.dedent(
'''\
80 void {class_name}_upcastFromVoid_{id}(int nargout, mxArray *out[], int nargin, const mxArray *in[]) {{
81 mexAtExit(&_deleteAllObjects);
82 typedef std::shared_ptr<{cpp_name}> Shared;
83 std::shared_ptr<void> *asVoid = *reinterpret_cast<std::shared_ptr<void>**> (mxGetData(in[0]));
84 out[0] = mxCreateNumericMatrix(1, 1, mxUINT32OR64_CLASS, mxREAL);
85 Shared *self = new Shared(std::static_pointer_cast<{cpp_name}>(*asVoid));
86 *reinterpret_cast<Shared**>(mxGetData(out[0])) = self;
90 class_serialize_method = textwrap.dedent(
'''\
91 function varargout = string_serialize(this, varargin)
92 % STRING_SERIALIZE usage: string_serialize() : returns string
93 % Doxygen can be found at https://gtsam.org/doxygen/
94 if length(varargin) == 0
95 varargout{{1}} = {wrapper}({wrapper_id}, this, varargin{{:}});
97 error('Arguments do not match any overload of function {class_name}.string_serialize');
100 function sobj = saveobj(obj)
101 % SAVEOBJ Saves the object to a matlab-readable format
102 sobj = obj.string_serialize();
106 collector_function_serialize = textwrap.indent(textwrap.dedent(
"""\
107 typedef std::shared_ptr<{full_name}> Shared;
108 checkArguments("string_serialize",nargout,nargin-1,0);
109 Shared obj = unwrap_shared_ptr<{full_name}>(in[0], "ptr_{namespace}{class_name}");
110 ostringstream out_archive_stream;
111 boost::archive::text_oarchive out_archive(out_archive_stream);
113 out[0] = wrap< string >(out_archive_stream.str());
117 collector_function_deserialize = textwrap.indent(textwrap.dedent(
"""\
118 typedef std::shared_ptr<{full_name}> Shared;
119 checkArguments("{namespace}{class_name}.string_deserialize",nargout,nargin,1);
120 string serialized = unwrap< string >(in[0]);
121 istringstream in_archive_stream(serialized);
122 boost::archive::text_iarchive in_archive(in_archive_stream);
123 Shared output(new {full_name}());
124 in_archive >> *output;
125 out[0] = wrap_shared_ptr(output,"{namespace}.{class_name}", false);
129 mex_function = textwrap.dedent(
'''
130 void mexFunction(int nargout, mxArray *out[], int nargin, const mxArray *in[])
133 std::streambuf *outbuf = std::cout.rdbuf(&mout);\n
134 _{module_name}_RTTIRegister();\n
135 int id = unwrap<int>(in[0]);\n
139 }} catch(const std::exception& e) {{
140 mexErrMsgTxt(("Exception from gtsam:\\n" + std::string(e.what()) + "\\n").c_str());
142 std::cout.rdbuf(outbuf);
146 collector_function_shared_return = textwrap.indent(textwrap.dedent(
'''\
148 std::shared_ptr<{name}> shared({shared_obj});
149 out[{id}] = wrap_shared_ptr(shared,"{name}");
153 matlab_deserialize = textwrap.indent(textwrap.dedent(
"""\
154 function varargout = string_deserialize(varargin)
155 % STRING_DESERIALIZE usage: string_deserialize() : returns {class_name}
156 % Doxygen can be found at https://gtsam.org/doxygen/
157 if length(varargin) == 1
158 varargout{{1}} = {wrapper}({id}, varargin{{:}});
160 error('Arguments do not match any overload of function {class_name}.string_deserialize');
163 function obj = loadobj(sobj)
164 % LOADOBJ Saves the object to a matlab-readable format
165 obj = {class_name}.string_deserialize(sobj);