templates.py
Go to the documentation of this file.
1 """Code generation templates for the Matlab wrapper."""
2 
3 import textwrap
4 
5 
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>
12  """)
13 
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};
17  ''')
18 
19  delete_obj = textwrap.indent(textwrap.dedent('''\
20  {{ for(Collector_{class_name}::iterator iter = collector_{class_name}.begin();
21  iter != collector_{class_name}.end(); ) {{
22  delete *iter;
23  collector_{class_name}.erase(iter++);
24  anyDeleted = true;
25  }} }}
26  '''),
27  prefix=' ')
28 
29  delete_all_objects = textwrap.dedent('''
30  void _deleteAllObjects()
31  {{
32  mstream mout;
33  std::streambuf *outbuf = std::cout.rdbuf(&mout);\n
34  bool anyDeleted = false;
35  {delete_objs}
36  if(anyDeleted)
37  cout <<
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);
42  }}
43  ''')
44 
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;
50 
51  {rtti_classes}
52 
53  mxArray *registry = mexGetVariable("global", "gtsamwrap_rttiRegistry");
54  if(!registry)
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());
59  if(fieldId < 0) {{
60  mexErrMsgTxt("gtsam wrap: Error indexing RTTI types, inheritance will not work correctly");
61  }}
62  mxArray *matlabName = mxCreateString(rtti_matlab.second.c_str());
63  mxSetFieldByNumber(registry, 0, fieldId, matlabName);
64  }}
65  if(mexPutVariable("global", "gtsamwrap_rttiRegistry", registry) != 0) {{
66  mexErrMsgTxt("gtsam wrap: Error indexing RTTI types, inheritance will not work correctly");
67  }}
68  mxDestroyArray(registry);
69 
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");
73  }}
74  mxDestroyArray(newAlreadyCreated);
75  }}
76  }}
77  ''')
78 
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;
87  }}\n
88  ''')
89 
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{{:}});
96  else
97  error('Arguments do not match any overload of function {class_name}.string_serialize');
98  end
99  end\n
100  function sobj = saveobj(obj)
101  % SAVEOBJ Saves the object to a matlab-readable format
102  sobj = obj.string_serialize();
103  end
104  ''')
105 
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);
112  out_archive << *obj;
113  out[0] = wrap< string >(out_archive_stream.str());
114  """),
115  prefix=' ')
116 
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);
126  """),
127  prefix=' ')
128 
129  mex_function = textwrap.dedent('''
130  void mexFunction(int nargout, mxArray *out[], int nargin, const mxArray *in[])
131  {{
132  mstream mout;
133  std::streambuf *outbuf = std::cout.rdbuf(&mout);\n
134  _{module_name}_RTTIRegister();\n
135  int id = unwrap<int>(in[0]);\n
136  try {{
137  switch(id) {{
138  {cases} }}
139  }} catch(const std::exception& e) {{
140  mexErrMsgTxt(("Exception from gtsam:\\n" + std::string(e.what()) + "\\n").c_str());
141  }}\n
142  std::cout.rdbuf(outbuf);
143  }}
144  ''')
145 
146  collector_function_shared_return = textwrap.indent(textwrap.dedent('''\
147  {{
148  std::shared_ptr<{name}> shared({shared_obj});
149  out[{id}] = wrap_shared_ptr(shared,"{name}");
150  }}{new_line}'''),
151  prefix=' ')
152 
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{{:}});
159  else
160  error('Arguments do not match any overload of function {class_name}.string_deserialize');
161  end
162  end\n
163  function obj = loadobj(sobj)
164  % LOADOBJ Saves the object to a matlab-readable format
165  obj = {class_name}.string_deserialize(sobj);
166  end
167  """),
168  prefix=' ')
gtwrap.matlab_wrapper.templates.WrapperTemplate
Definition: templates.py:6


gtsam
Author(s):
autogenerated on Fri Nov 1 2024 03:36:56