CSC/ECE 517 Fall 2010/ch3 3i IC: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
Line 52: Line 52:
:private string name;<br>
:private string name;<br>
<br>
<br>
void setName(string name)<br>
:void setName(string name)<br>
{<br>
:{<br>
:this->name = name;<br>
::this->name = name;<br>
}<br>
:}<br>
<br>
<br>
string getName()<br>
:string getName()<br>
{<br>
:{<br>
:return name;<br>
::return name;<br>
}<br>
:}<br>
};</code>
};</code>


Line 77: Line 77:
</code>
</code>


Python Code
'''Python Code'''
 
<code>
<code>
import person
import person<br>
student = person.Person()
student = person.Person()<br>
student.setName('John')
student.setName('John')<br>
student.getName()
student.getName()<br>


>> John
>> John

Revision as of 04:22, 4 October 2010

Mixing Static and Dynamic Language Code


Introduction

Many middleware solutions have been developed to combine static and dynamic languages to take advantage of both types of code. Previous wiki chapters have discussed JRuby, a Java implementation Ruby. These solutions allow programmers the productivity luxuries of newer dynamic languages with the speed, low-level functionality, and pre-existing code base of older static languages. Below, we will give even more examples of mixing several widely used dynamic languages with C, C++, and Java.

Mixing Dynamic Code and C/C++

Ruby

Ruby has an inherent relationship with the C language since the current Ruby implementation is written in C. Cplus2Ruby is a gem that allows programmers to call Ruby code within C++ and vice versa. Assuming an installation of Ruby, the Cplus2Ruby can be installed with the following command [2]:

gem install cplus2ruby

Python

Boost

Boost allows programmers to seamlessly expose C++ classes and function to Python and vice versa. No special tools are needed (however, a build tool will be referenced later). All that is required are the Boost libraries and a C++ compiler. Boost is designed to allow Python to use C++ code with no internal modification to the C++ code that will be wrapped.

Exposing Functions

Below is a “Hello, World!” example.

C++ function

string greet()
{

return "Hello, World!";

}

Add to C++ so it can be exposed to Python

#include <boost/python.hpp>
BOOST_PYTHON_MODULE(hello_ext)
{

using namespace boost::python;
def("greet", greet);

}

Python code that calls C++

import hello_ext # parameter passed to BOOST_PYTHON_MODULE
print hello.greet()
>> Hello, World!

Exposing Classes

C++ Class Declaration

class Person
{

private string name;


void setName(string name)
{
this->name = name;
}


string getName()
{
return name;
}

};

Boost.Python C++ Wrapper

#include <boost/python.hpp>
using namespace boost::python;

BOOST_PYTHON_MODULE(person)
{

class_<Person>("person")
.def("getName", &Person::getName)
.def("setName", &Person::setName);

}

Python Code

import person
student = person.Person()
student.setName('John')
student.getName()

>> John

Lua

Mixing Dynamic Code and Java

Perl

References

[1] Steve Vinoski. Ruby Extensions. Internet Computing, IEEE, Vol. 10, Issue 5. 2006. pp. 85-87.

[2] Michael Neumann. Cplus2Ruby - Gluing C++ and Ruby together in an OO manner. http://rubydoc.info/gems/cplus2ruby/1.2.0/frames