SHARE
TWEET

Untitled

quantumech Apr 23rd, 2019 77 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <emscripten/bind.h>
  2.  
  3. // Bring Emscripten namespace into scope for the sake of readability
  4. using namespace emscripten;
  5.  
  6. class Vertex
  7. {
  8.     double x;  
  9.     double y;
  10.  
  11. public:
  12.  
  13.     Vertex(int x, int y): x(x), y(y)
  14.     {}
  15.  
  16.     double getX() const { return this->x; } // <--- Getters for 'properties' must be 'const' methods.
  17.     void setX(double x) { this->x = x; }    //      Notice how this is different from returning a const
  18.     double getY() const { return this->y; } //      value. 'const' keyword declared this way tells the compiler
  19.                                             //      to throw an error if any attributes get changed inside of the method
  20.  
  21.     void setY(double y) { this->y = y; }
  22.  
  23.     void invert()
  24.     {
  25.         x = -x;
  26.         y = -y;
  27.     }
  28.  
  29.     static Vertex VertexFactory(double x, double y)
  30.     {
  31.         return Vertex(x, y);
  32.     }
  33. };
  34.  
  35.  
  36. EMSCRIPTEN_BINDINGS(Module) {
  37.     class_<Vertex>("Vertex")
  38.         .constructor<double, double>()  // <--- Register contructor that takes 2 doubles as arguments. You can also do '.constructor(&func)' to register an external function as a constructor
  39.         .property("x", &Vertex::getX, &Vertex::setX) // <-- Register a 'property'. 'Properties' are JS variables
  40.         .property("y", &Vertex::getY, &Vertex::setY) //     that call getters and setters when a variable is accessed.
  41.                                                      //     So when 'Vertex.x = 4' is called, setX() will be called to
  42.                                                      //     mutate 'x'
  43.         .function("invert", &Vertex::invert) // <-- Register an instance function (ie. a function that is called per instance)
  44.         .class_function("VertexFactory", &Vertex::VertexFactory); // <-- Register a static function
  45.                                                                   //     (ie. a function that can be directly called
  46.                                                                   //      from the class)
  47. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top