Calling C++ class functions from Ruby/Python -


in specific circumstance, have complex class (a class of classes of classes) want expose scripting language (aka ruby). rather directly pass complex class, gave me idea of opening few functions scripting language ruby, seemed simpler. i've seen rice examples i've seen use simple functions multiply something, rather interfacing class.

for simplicity, have simple class functions want expose:

class foo {     private:         //integer vector:         std::vector<int> foovector;      public:         //functions expose ruby:         void pushback(const int& newint) {foovector.push_back(newint);}         int& getint(const int& element) {return foovector.at(element);} }; 

also:

i'd prefer not have link download page of swig, or article explaining how rice written in 2010, guide work (i haven't had luck yet)

aswell:

i'm using linux (ubuntu) cross-compatible program, must able compile on windows , os x

edit:

i understand shared libraries exist (dll , files), don't know if can have library depends on .hpp file containing class(es).

you can use cython or boost.python call native code python. since using c++, i'd recommend looking boost.python offers natural way of wrapping c++ classes python.

as example (close provided), consider following class definitions

class bar { private:     int value;  public:     bar() : value(42){ }      //functions expose python:     int getvalue() const { return value; }     void setvalue(int newvalue) { value = newvalue; } };  class foo { private:     //integer vector:     std::vector<int> foovector;     bar bar;  public:     //functions expose python:     void pushback(const int& newint) { foovector.push_back(newint); }     int getint(const int& element) { return foovector.at(element); }     bar& getbar() { return bar; } };  double compute() { return 18.3; } 

this can wrapped python using boost.python

#include <boost/python.hpp> boost_python_module(mylibrary) {     using namespace boost::python;      class_<foo>("foo", init<>())         .def("pushback", &foo::pushback, (arg("newint")))         .def("getint", &foo::getint, (arg("element")))         .def("getbar", &foo::getbar, return_value_policy<reference_existing_object>())     ;      class_<bar>("bar", init<>())         .def("getvalue", &bar::getvalue)         .def("setvalue", &bar::setvalue, (arg("newvalue")))     ;      def("compute", compute); } 

this code can compiled static library mylibrary.pyd , used this

import mylibrary  foo = mylibrary.foo() foo.pushback(10); foo.pushback(20); foo.pushback(30); print(foo.getint(0)) # 10 print(foo.getint(1)) # 20 print(foo.getint(2)) # 30  bar = foo.getbar() print(bar.getvalue()) # 42 bar.setvalue(17) print(foo.getbar().getvalue()) #17  print(mylibrary.compute()) # 18.3 

Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -