API reference - Class Interpreter

Notation used in Ruby API documentation

Module: tl

Description: A generalization of script interpreters

The main purpose of this class is to provide cross-language call options. Using the Python interpreter, it is possible to execute Python code from Ruby for example.

The following example shows how to use the interpreter class to execute Python code from Ruby and how to pass values from Ruby to Python and back using the Value wrapper object:

pya = RBA::Interpreter::python_interpreter
out_param = RBA::Value::new(17)
pya.define_variable("out_param", out_param)
pya.eval_string(<<END)
print("This is Python now!")
out_param.value = out_param.value + 25
END
puts out_param.value  # gives '42'

This class was introduced in version 0.27.5.

Public constructors

new Interpreter ptrnewCreates a new object of this class

Public methods

[const]Interpreter ptr_const_castReturns a non-const reference to self.
void_createEnsures the C++ object is created
void_destroyExplicitly destroys the object
[const]bool_destroyed?Returns a value indicating whether the object was already destroyed
[const]bool_is_const_object?Returns a value indicating whether the reference is a const reference
void_manageMarks the object as managed by the script side.
void_unmanageMarks the object as no longer owned by the script side.
voiddefine_variable(string name,
variant value)
Defines a (global) variable with the given name and value
varianteval_expr(string string,
string filename = nil,
int line = 1)
Executes the expression inside the given string and returns the result value
voideval_string(string string,
string filename = nil,
int line = 1)
Executes the code inside the given string
voidload_file(string path)Loads the given file into the interpreter

Public static methods and constants

Interpreter ptrpython_interpreterGets the instance of the Python interpreter
Interpreter ptrruby_interpreterGets the instance of the Ruby interpreter

Deprecated methods (protected, public, static, non-static and constructors)

voidcreateUse of this method is deprecated. Use _create instead
voiddestroyUse of this method is deprecated. Use _destroy instead
[const]booldestroyed?Use of this method is deprecated. Use _destroyed? instead
[const]boolis_const_object?Use of this method is deprecated. Use _is_const_object? instead

Detailed description

_const_cast

Signature: [const] Interpreter ptr _const_cast

Description: Returns a non-const reference to self.

Basically, this method allows turning a const object reference to a non-const one. This method is provided as last resort to remove the constness from an object. Usually there is a good reason for a const object reference, so using this method may have undesired side effects.

This method has been introduced in version 0.29.6.

_create

Signature: void _create

Description: Ensures the C++ object is created

Use this method to ensure the C++ object is created, for example to ensure that resources are allocated. Usually C++ objects are created on demand and not necessarily when the script object is created.

_destroy

Signature: void _destroy

Description: Explicitly destroys the object

Explicitly destroys the object on C++ side if it was owned by the script interpreter. Subsequent access to this object will throw an exception. If the object is not owned by the script, this method will do nothing.

_destroyed?

Signature: [const] bool _destroyed?

Description: Returns a value indicating whether the object was already destroyed

This method returns true, if the object was destroyed, either explicitly or by the C++ side. The latter may happen, if the object is owned by a C++ object which got destroyed itself.

_is_const_object?

Signature: [const] bool _is_const_object?

Description: Returns a value indicating whether the reference is a const reference

This method returns true, if self is a const reference. In that case, only const methods may be called on self.

_manage

Signature: void _manage

Description: Marks the object as managed by the script side.

After calling this method on an object, the script side will be responsible for the management of the object. This method may be called if an object is returned from a C++ function and the object is known not to be owned by any C++ instance. If necessary, the script side may delete the object if the script's reference is no longer required.

Usually it's not required to call this method. It has been introduced in version 0.24.

_unmanage

Signature: void _unmanage

Description: Marks the object as no longer owned by the script side.

Calling this method will make this object no longer owned by the script's memory management. Instead, the object must be managed in some other way. Usually this method may be called if it is known that some C++ object holds and manages this object. Technically speaking, this method will turn the script's reference into a weak reference. After the script engine decides to delete the reference, the object itself will still exist. If the object is not managed otherwise, memory leaks will occur.

Usually it's not required to call this method. It has been introduced in version 0.24.

create

Signature: void create

Description: Ensures the C++ object is created

Use of this method is deprecated. Use _create instead

Use this method to ensure the C++ object is created, for example to ensure that resources are allocated. Usually C++ objects are created on demand and not necessarily when the script object is created.

define_variable

Signature: void define_variable (string name, variant value)

Description: Defines a (global) variable with the given name and value

You can use the Value class to provide 'out' or 'inout' parameters which can be modified by code executed inside the interpreter and read back by the caller.

destroy

Signature: void destroy

Description: Explicitly destroys the object

Use of this method is deprecated. Use _destroy instead

Explicitly destroys the object on C++ side if it was owned by the script interpreter. Subsequent access to this object will throw an exception. If the object is not owned by the script, this method will do nothing.

destroyed?

Signature: [const] bool destroyed?

Description: Returns a value indicating whether the object was already destroyed

Use of this method is deprecated. Use _destroyed? instead

This method returns true, if the object was destroyed, either explicitly or by the C++ side. The latter may happen, if the object is owned by a C++ object which got destroyed itself.

eval_expr

Signature: variant eval_expr (string string, string filename = nil, int line = 1)

Description: Executes the expression inside the given string and returns the result value

Use 'filename' and 'line' to indicate the original source for the error messages.

eval_string

Signature: void eval_string (string string, string filename = nil, int line = 1)

Description: Executes the code inside the given string

Use 'filename' and 'line' to indicate the original source for the error messages.

is_const_object?

Signature: [const] bool is_const_object?

Description: Returns a value indicating whether the reference is a const reference

Use of this method is deprecated. Use _is_const_object? instead

This method returns true, if self is a const reference. In that case, only const methods may be called on self.

load_file

Signature: void load_file (string path)

Description: Loads the given file into the interpreter

This will execute the code inside the file.

new

Signature: [static] new Interpreter ptr new

Description: Creates a new object of this class

Python specific notes:
This method is the default initializer of the object.

python_interpreter

Signature: [static] Interpreter ptr python_interpreter

Description: Gets the instance of the Python interpreter

ruby_interpreter

Signature: [static] Interpreter ptr ruby_interpreter

Description: Gets the instance of the Ruby interpreter