Empirical
d3_init.h
Go to the documentation of this file.
1 #ifndef D3_INIT_H
2 #define D3_INIT_H
3 
4 #include <iostream>
5 
6 #include "../../base/errors.h"
7 #include "../init.h"
8 #include "../JSWrap.h"
9 #include "utils.h"
10 
11 // #ifdef EMSCRIPTEN
12 // extern "C" {
13 // extern void D3_Initialize();
14 // }
15 // #endif
16 
17 namespace D3 {
18 
19 
20  // #ifdef EMSCRIPTEN
21  // static void InitD3() {
22  // static bool init = false;
23  // if (!init) {
24  // D3_Initialize();
25  // init = true;
26  // }
27  // }
28  //
29  // #endif
30 
31  int NextD3ID() {
32  return EM_ASM_INT_V({
33  id = js.next_id++;
34  js.counts[id] = 0;
35  js.objects[id] = -1;
36  return id;
37  });
38  }
39 
42 
43  class D3_Base {
44  protected:
45  int id;
46 
49  this->id = NextD3ID();
50  // std::cout << "Default constructor: " << this->id << std::endl;
51  EM_ASM_ARGS({
52  js.counts[$0] = 1;
53  }, this->id);
54  }
55 
58  D3_Base(int id){
59  this->id = id;
60  // std::cout << "Int constructor: " << id << std::endl;
61  emp_assert(EM_ASM_INT({return $0 in js.counts;}, this->id));
62 
63  EM_ASM_ARGS({
64  js.counts[$0]++;
65  }, this->id);
66  }
67 
68  D3_Base(const D3_Base & other) {
69  //TODO: Make this a deep copy
70  // std::cout << "Copying: " << other.id << std::endl;
71  this->id = other.id;
72  EM_ASM_ARGS({js.counts[$0]++;}, this->id);
73  }
74 
75  D3_Base& operator= (const D3_Base & other) {
76  // std::cout << "Calling assingment: " << this->id << " " << other.id << std::endl;
77  this->id = other.id;
78  EM_ASM_ARGS({js.counts[$0]++;}, this->id);
79  return (*this);
80  }
81 
83  // std::cout << "Destructing: " <<this->id << std::endl;
84  EM_ASM_ARGS({
85  js.counts[$0]--;
86  if (js.counts[$0] == 0) {
87  // console.log("Deleting: ", $0);
88  delete js.objects[$0];
89  delete js.counts[$0];
90  }
91  }, this->id);
92  }
93 
94  public:
95  // Get this object's ID (i.e. it's location in the js.objects array in Javascript)
96  int GetID() const {
97  return this->id;
98  }
99 
100  void Log() const {
101  EM_ASM_ARGS({console.log($0+":", js.objects[$0]);}, id);
102  }
103  };
104 
106  class ToolTip : public D3_Base {
107  public:
110  EM_ASM_ARGS({
111  js.objects[$0] = d3.tip().attr('class', 'd3-tip')
112  .offset([-10, 0])
113  .html(function(d, i) { return d; });
114  }, this->id);
115  }
116 
136  ToolTip(std::string func) {
137  EM_ASM_ARGS({
138  var in_string = Pointer_stringify($1);
139  if (typeof window["d3"][in_string] === "function"){
140  in_string = window["d3"][in_string];
141  } else if (typeof window["emp"][in_string] === "function"){
142  in_string = window["emp"][in_string];
143  } else if (typeof window[in_string] === "function"){
144  in_string = window[in_string];
145  }
146 
147  js.objects[$0] = d3.tip().attr('class', 'd3-tip').offset([-10, 0]).html(in_string);
148  }, this->id, func.c_str());
149  }
150 
152  template <typename T>
153  ToolTip(T func) {
154  emp::JSWrap(func, emp::to_string(this->id)+"_html_func");
155  EM_ASM_ARGS({
156  js.objects[$0] = d3.tip().attr('class', 'd3-tip')
157  .offset([-10, 0])
158  .html(emp[$0+"_html_func"]);
159  }, id);
160 
161  }
163 
164  void SetHtml(std::string func) {
165  EM_ASM_ARGS({
166  var in_string = Pointer_stringify($1);
167  if (typeof window["d3"][in_string] === "function"){
168  in_string = window["d3"][in_string];
169  } else if (typeof window["emp"][in_string] === "function"){
170  in_string = window["emp"][in_string];
171  } else if (typeof window[in_string] === "function"){
172  in_string = window[in_string];
173  }
174 
175  js.objects[$0].html(in_string);
176  }, this->id, func.c_str());
177  }
178 
180  template <typename T>
182  SetHtml(T func) {
183  emp::JSWrap(func, emp::to_string(id)+"_html_func");
184  EM_ASM_ARGS({js.objects[$0].html(emp[$0+"_html_func"]);}, id);
185  }
187 
188  };
189 
191  class FormatFunction : public D3_Base {
192  public:
193  FormatFunction(std::string format = "") {
194  EM_ASM_ARGS({
195  js.objects[$1] = d3.format(Pointer_stringify($0));
196  }, format.c_str(), this->id);
197  }
198 
199  std::string operator() (double d){
200  char * buffer = (char *) EM_ASM_INT({
201  var text = js.objects[$0]($1);
202  var buffer = Module._malloc(text.length+1);
203  Module.writeStringToMemory(text, buffer);
204  return buffer;
205  }, this->id, d);
206  std::string result = std::string(buffer);
207  free(buffer);
208  return result;
209  }
210  };
211 
213  class JSObject : public D3_Base {
214  public:
215  JSObject(){;};
216 };
217 
219  class JSFunction : public D3_Base {
220  public:
222  JSFunction(std::string name) {
223  int fail = EM_ASM_INT({
224  var fn = window["d3"][Pointer_stringify($2)];
225  if (typeof fn === "function") {
226  js.objects[$0] = fn;
227  return 0;
228  } else {
229  var fn = window["emp"][Pointer_stringify($2)];
230  if (typeof fn === "function") {
231  js.objects[$0] = fn;
232  return 0;
233  } else {
234  var fn = window[Pointer_stringify($2)];
235  if (typeof fn === "function") {
236  js.objects[$0] = fn;
237  return 0;
238  }
239  }
240  }
241  return 1;
242  }, this->id, name.c_str());
243 
244  if (fail) {
245  emp::NotifyWarning("Invalid function name passed to JSFunction");
246  }
247  }
248 
250  void operator() () {
251  EM_ASM_ARGS({
252  js.objects[$0]();
253  }, this->id);
254  }
255 
256  };
257 }
258 
259 #endif
D3_Base(const D3_Base &other)
Definition: d3_init.h:68
ToolTip(std::string func)
Definition: d3_init.h:136
D3_Base & operator=(const D3_Base &other)
Definition: d3_init.h:75
std::string to_string(ALL_TYPES &&...all_values)
Definition: string_utils.h:511
REAL_TYPE sfinae_decoy
Definition: meta.h:93
A callable string d3.format() string formatting function.
Definition: d3_init.h:191
~D3_Base()
Definition: d3_init.h:82
JSFunction(std::string name)
Definition: d3_init.h:222
FormatFunction(std::string format="")
Definition: d3_init.h:193
Definition: d3_init.h:43
JSFunction()
Definition: d3_init.h:221
Create a tooltup using the d3.tip Javascript library.
Definition: d3_init.h:106
Catch-all object for storing references to things created in JS.
Definition: d3_init.h:213
void SetHtml(std::string func)
Definition: d3_init.h:164
int id
Definition: d3_init.h:45
D3_Base()
Default constructor - adds placeholder to js.objects array in Javascript.
Definition: d3_init.h:48
ToolTip()
Default constructor - displays whatever data is bound on mouseover.
Definition: d3_init.h:109
D3_Base(int id)
Definition: d3_init.h:58
Wrapper for creating functions in javascript and calling them there.
Definition: d3_init.h:219
int NextD3ID()
Definition: d3_init.h:31
If we are in emscripten, make sure to include the header.
Definition: array.h:37
void Log() const
Definition: d3_init.h:100
void NotifyWarning(Ts &&...msg)
End user has done something possibly a problem.
Definition: errors.h:141
#define emp_assert(...)
Definition: assert.h:199
Definition: axis.h:20
JSObject()
Definition: d3_init.h:215
int GetID() const
Definition: d3_init.h:96