Advertisement
lzedl

ChaiScript Dummy

Jun 3rd, 2012
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.24 KB | None | 0 0
  1. #include <iostream>
  2. #include <chaiscript/chaiscript.hpp>
  3.  
  4. using namespace std;
  5. using namespace chaiscript;
  6.  
  7. class B
  8. {
  9. public:
  10.     B() { value = 1; }
  11.     ~B() {}
  12.     void setValue(int val) { value = val; }
  13.     int getValue() const { return value; }
  14. private:
  15.     int value;
  16. };
  17.  
  18. class A
  19. {
  20. public:
  21.     A() {}
  22.     ~A() {}
  23.  
  24.     void setB(B* b) { b_ = b; }
  25.     B* getB() const { return b_; }
  26. private:
  27.     B* b_;
  28. };
  29.  
  30. void
  31. chaiInit(ChaiScript &chai)
  32. {
  33.     chai.add(user_type<A>(), "A");
  34.     chai.add(constructor<A()>(), "A");
  35.     chai.add(constructor<A(const A&)>(), "A");
  36.     chai.add(fun(&A::operator=), "=");
  37.     chai.add(fun(&A::getB), "getB");
  38.     chai.add(user_type<B>(), "B");
  39.     chai.add(constructor<B()>(), "B");
  40.     chai.add(constructor<B(const B&)>(), "B");
  41.     chai.add(fun(&B::operator=), "=");
  42.     chai.add(fun(&B::getValue), "getValue");
  43.     chai.add(fun(&B::setValue), "setValue");
  44. }
  45.  
  46. int main()
  47. {
  48.     ChaiScript chai;
  49.     chaiInit(chai);
  50.     A a;
  51.     B b;
  52.     a.setB(&b);
  53.     chai.add(var(&a), "a");
  54.     chai.add(var(&b), "b");
  55.     cout << "Script output: " << endl;
  56.     chai.eval_file("chai.chai");
  57.     cout << "Other output" << endl;
  58.     cout << "B->value: " << b.getValue() << endl;
  59.     return 0;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement