Minsky
typescriptAPI.cc
Go to the documentation of this file.
1 #include "function.h"
2 #include "typescriptAPI_base.h"
3 #include "minsky.h"
4 #include "minsky.tcd"
5 
6 #include "assetClass.tcd"
7 #include "bookmark.h"
8 #include "bookmark.tcd"
9 #include "cairoSurfaceImage.tcd"
10 #include "callableFunction.tcd"
11 #include "canvas.tcd"
12 #include "CSVDialog.tcd"
13 #include "CSVParser.tcd"
14 #include "constMap.tcd"
15 #include "dataSpecSchema.tcd"
16 #include "dataOp.h"
17 #include "dataOp.tcd"
18 #include "dimension.tcd"
19 #include "engNotation.tcd"
20 #include "equationDisplay.tcd"
21 #include "evalGodley.tcd"
22 #include "eventInterface.tcd"
23 #include "fontDisplay.tcd"
24 #include "godleyIcon.tcd"
25 #include "godleyTable.tcd"
26 #include "godleyTableWindow.tcd"
27 #include "grid.tcd"
28 #include "group.tcd"
29 #include "handleLockInfo.tcd"
30 #include "hypercube.tcd"
31 #include "index.tcd"
32 #include "intOp.tcd"
33 #include "item.tcd"
34 #include "itemT.tcd"
35 #include "lasso.tcd"
36 #include "lock.tcd"
37 #include "noteBase.tcd"
38 #include "operation.tcd"
39 #include "operationBase.tcd"
40 #include "operationType.tcd"
41 #include "pango.tcd"
42 #include "pannableTab.tcd"
43 #include "phillipsDiagram.tcd"
44 #include "plot.tcd"
45 #include "plotOptions.tcd"
46 #include "plotWidget.tcd"
47 #include "polyRESTProcessBase.tcd"
48 #include "port.h"
49 #include "port.tcd"
50 #include "publication.tcd"
51 #include "pubTab.tcd"
52 #include "ravelState.tcd"
53 #include "renderNativeWindow.tcd"
54 #include "ravelWrap.tcd"
55 #include "rungeKutta.tcd"
56 #include "selection.tcd"
57 #include "sheet.tcd"
58 #include "simulation.tcd"
59 #include "slider.tcd"
60 #include "SVGItem.tcd"
61 #include "switchIcon.tcd"
62 #include "tensorInterface.tcd"
63 #include "tensorVal.tcd"
64 #include "units.tcd"
65 #include "userFunction.h"
66 #include "userFunction.tcd"
67 #include "variableInstanceList.h"
68 #include "variableInstanceList.tcd"
69 #include "variable.tcd"
70 #include "variablePane.tcd"
71 #include "variableType.tcd"
72 #include "variableValue.tcd"
73 #include "variableValues.tcd"
74 #include "wire.tcd"
75 #include "xvector.tcd"
76 
77 namespace classdesc_access
78 {
79  // clobbers
80  template <class T>
81  struct access_typescriptAPI<classdesc::Exclude<T>>:
82  public classdesc::NullDescriptor<classdesc::typescriptAPI_t> {};
83 
84  template <class T, class V, int N>
85  struct access_typescriptAPI<ecolab::TCLAccessor<T,V,N>>:
86  public classdesc::NullDescriptor<classdesc::typescriptAPI_t> {};
87 
88  template <>
89  struct access_typescriptAPI<classdesc::PolyPackBase>:
90  public classdesc::NullDescriptor<classdesc::typescriptAPI_t> {};
91 
92  template <class T>
93  struct access_typescriptAPI<classdesc::PolyPack<T>>:
94  public classdesc::NullDescriptor<classdesc::typescriptAPI_t> {};
95 
96  template <>
97  struct access_typescriptAPI<std::vector<civita::any>>:
98  public classdesc::NullDescriptor<classdesc::typescriptAPI_t> {};
99 
100  template <class T>
102  public classdesc::NullDescriptor<classdesc::typescriptAPI_t> {};
103 }
104 
105 namespace classdesc
106 {
107  // dummies
108  template <> string typeName<cairo_t>() {return "minsky__dummy";}
109  template <> string typeName<cairo_surface_t>() {return "minsky__dummy";}
110 
111  template <>
112  struct tn<civita::any>
113  {
114  static string name() {return "minsky__dummy";}
115  };
116  template <class T>
117  struct tn<boost::geometry::model::d2::point_xy<T>>
118  {
119  static string name() {return "minsky__dummy";}
120  };
121  template <>
122  struct tn<RESTProcess_t>
123  {
124  static string name() {return "minsky__dummy";}
125  };
126  template <class C, class D>
127  struct tn<std::chrono::time_point<C,D>>
128  {
129  static string name() {return "minsky__dummy";}
130  };
131  template <>
132  struct tn<std::istream>
133  {
134  static string name() {return "minsky__dummy";}
135  };
136 
137  // typescript has difficulties with specialised templates
138  template <>
139  struct tn<minsky::PannableTab<minsky::EquationDisplay>>
140  {
141  static string name() {return "EquationDisplay";}
142  };
143 
144 }
145 
146 #include "minsky_epilogue.h"
147 
148 namespace classdesc
149 {
150  template <class T, class Base>
151  typename enable_if<Not<is_map<Base>>, void>::T
152  typescriptAPI(typescriptAPI_t& t, const string& d)
153  {
154  // bail out of object heirarchy drill down once reaching named base class
155  if (typescriptType<Base>()==t[typescriptType<T>()].super) return;
156  classdesc_access::access_typescriptAPI<Base>().template type<T>(t,d);
157  }
158 
159  template <class T> void typescriptAPI_t::addClass() {typescriptAPI<T,T>(*this,"");}
160  template <class T, class Base> void typescriptAPI_t::addSubclass()
161  {
162  operator[](typescriptType<T>()).super=typescriptType<Base>();
163  typescriptAPI<T,T>(*this,"");
164  }
165 }
166 
167 namespace minsky
168 {
169  Minsky& minsky()
170  {
171  static Minsky s_minsky;
172  return s_minsky;
173  }
174 
177 
178  // GUI callback needed only to solve linkage problems
179  void doOneEvent(bool idleTasksOnly) {}
180 }
181 
182 using namespace classdesc;
183 using namespace std;
184 using namespace minsky;
185 
186 void exportClass(const std::string& name, const minsky::typescriptAPI_ns::ClassType& klass)
187 {
188  cout << "export class "+name+" extends "+(klass.super.empty()? "CppClass": klass.super)+" {\n";
189 
190  // properties
191  for (auto& prop: klass.properties)
192  cout << " "<<prop.first <<": "<<prop.second.type<<";\n";
193 
194  // constructor
195  if (!klass.super.empty())
196  {
197  cout << " constructor(prefix: string|"<<klass.super<<"){\n";
198  cout << " if (typeof prefix==='string')\n";
199  cout << " super(prefix";
200  if (!klass.valueType.empty())
201  cout <<","<<klass.valueType;
202  cout << ")\n";
203  cout << " else\n";
204  cout << " super(prefix.$prefix()";
205  if (!klass.valueType.empty())
206  cout <<","<<klass.valueType;
207  cout <<")\n";
208  }
209  else
210  {
211  cout << " constructor(prefix: string){\n";
212  cout << " super(prefix);\n";
213  }
214  for (auto& prop: klass.properties)
215  {
216  if (!prop.second.construction.empty())
217  cout << " this."<<prop.first<<"="<<prop.second.construction<<"\n";
218  else
219  cout << " this."<<prop.first<<"=new "<<prop.second.type<<"(this.$prefix()+'."<<prop.first<<"');\n";
220  }
221  cout << " }\n";
222 
223  // methods
224  for (auto& method: klass.methods)
225  {
226  cout << " async "<<method.first<<"(";
227  for (size_t i=0; i<method.second.args.size(); ++i)
228  {
229  if (i>0) cout<<",";
230  cout<<method.second.args[i].name<<": "<<method.second.args[i].type;
231  }
232  cout << "): Promise<"<<method.second.returnType<<"> {return this.$callMethod('"<<method.first<<"'";
233  for (auto& arg: method.second.args)
234  cout<<","<<arg.name;
235  cout<<");}\n";
236  }
237  cout << "}\n\n";
238 }
239 
240 int main()
241 {
242  typescriptAPI_t api;
243  api.addClass<Minsky>();
244 
245  // supporting types
246  api.addClass<Bookmark>();
247  api.addClass<Canvas::ZoomCrop>();
248  api.addClass<civita::Dimension>();
249  api.addClass<civita::Hypercube>();
250  api.addClass<civita::Index>();
251  api.addClass<civita::ITensor>();
252  api.addClass<civita::XVector>();
253  api.addClass<DataSpecSchema>();
254  api.addClass<ecolab::Plot::LineStyle>();
255  api.addClass<EngNotation>();
256  api.addClass<EventInterface>();
257  api.addClass<GroupItems>();
258  api.addClass<HandleLockInfo>();
259  api.addClass<Port>();
260  api.addClass<PubItem>();
261  api.addClass<ravel::HandleState>();
262  api.addClass<ravel::RavelState>();
263  api.addClass<Units>();
264  api.addClass<VariablePaneCell>();
265  api.addClass<VariableValue>();
266  api.addClass<VariableValues>();
267  api["VariableValues"].valueType="VariableValue";
268 
269  // Item subclasses
270  api.addSubclass<DataOp,Item>();
271  api.addSubclass<Group,Item>();
272  api.addSubclass<GodleyIcon,Item>();
273  api.addSubclass<IntOp,Item>();
274  api.addSubclass<Lock,Item>();
278  api.addSubclass<PlotWidget,Item>();
279  api.addSubclass<Ravel,Item>();
280  api.addSubclass<Sheet,Item>();
281  api.addSubclass<SwitchIcon,Item>();
284 
285  // RenderNativeWindow subclasses
291 
292 
293  // to prevent Group recursively calling itself on construction
294  api["Group"].properties.erase("parent");
295 
296  cout << "/*\nThis is a built file, please do not edit.\n";
297  cout << "See RESTService/typescriptAPI for more information.\n*/\n\n";
298  cout << "import {CppClass, Sequence, Container, Map, Pair} from './backend';\n\n";
299 
300  // dummy types
301  cout << "class minsky__dummy {}\n";
302  cout << "class minsky__EventInterface__KeyPressArgs {}\n";
303  cout << "class minsky__GodleyIcon__MoveCellArgs {}\n";
304  cout << "class minsky__RenderNativeWindow__RenderFrameArgs {}\n";
305  cout << "class minsky__VariableType__TypeT {}\n";
306  cout << "class civita__ITensor__Args {}\n";
307  cout << "class classdesc__json_pack_t {}\n";
308  cout << "class classdesc__pack_t {}\n";
309  cout << "class classdesc__RESTProcess_t {}\n";
310  cout << "class classdesc__TCL_obj_t {}\n";
311  cout << "class ecolab__cairo__Surface {}\n";
312  cout<<endl;
313 
314  // these need to be declared in a specific order
315  vector<string> exportFirst{"EventInterface","Item","OperationBase","RenderNativeWindow","VariableBase"};
316  for (auto& i: exportFirst) exportClass(i,api[i]);
317 
318  cout << "class minsky__Variable<T> extends VariableBase {}\n";
319 
320  // then export the rest
321  for (auto& i: api)
322  if (find(exportFirst.begin(), exportFirst.end(), i.first)==exportFirst.end())
323  exportClass(i.first, i.second);
324 
325  cout << "export var minsky=new Minsky('minsky');\n";
326 }
void doOneEvent(bool idletasksOnly)
checks if any GUI events are waiting, and proces an event if so
Definition: tclmain.cc:161
LocalMinsky(Minsky &m)
Definition: minskyTCL.cc:59
STL namespace.
string typeName< cairo_surface_t >()
convenience class to omit writing XML records when data absent or empty
Definition: optional.h:39
int main()
Creation and access to the minskyTCL_obj object, which has code to record whenever Minsky&#39;s state cha...
Definition: constMap.h:22
enable_if< Not< is_map< B > >, void >::T typescriptAPI(typescriptAPI_t &, const std::string &)
Mixin implementing common panning functionality in tabs.
Definition: pannableTab.h:36
represents the units (in sense of dimensional analysis) of a variable.
Definition: units.h:34
string typeName< cairo_t >()
void exportClass(const std::string &name, const minsky::typescriptAPI_ns::ClassType &klass)
legacy data importer object
Definition: dataOp.h:28
Minsky & minsky()
global minsky object
Definition: minskyTCL.cc:51
a container item for a plot widget
Definition: plotWidget.h:46