Skip to content

Examples

Julio Merino edited this page Feb 4, 2014 · 1 revision

Introduction

This page provides some examples to highlight how easy it is to use Lutok in your applications. Each of the examples below is a little standalone program that you can build and run from the distribution file by going into the examples directory and typing make. The examples are also installed, so you can also build them by going into /usr/local/share/doc/lutok/examples/ (or wherever you installed Lutok) and typing make in there.

The examples here are shown in order of increasing complexity. Their code is well-commented and explains the rationale behind the example code, so they should be easy to read. If that's not the case, let us know!

hello: Hello world

The most trivial example we can construct using Lutok is a program that initializes a Lua session and runs a call to print from within Lua to display the typical Hello, world! message. This is what the hello example does.

See the source code in the hello.cpp file.

interpreter: Interactive interpreter

A common example piece of code shown when learning Lua is how to write a custom command-line interpreter for the language. While this is easy in C using the Lua API, it is even easier with Lutok! The purpose of the interpreter example is to show you this.

See the source code in the interpreter.cpp file.

raii: How RAII protects your code

One of the major selling points of Lutok is that it takes advantage of the RAII programming pattern (only applicable in C++) to ensure that the Lua interpreter state remains sane at all points during the execution of the host program. The raii example attempts to show some of the mechanisms provided by Lutok to aid in this.

See the source code in the raii.cpp file.

bindings: Binding C++ functions into Lua

Lua is well-known for being very easy to integrate within C and C++ programs so that these programs can install bindings into the Lua environment to call back native functions for performance reasons. The bindings trivial example shows how this is done by using the Lutok routines.

See the source code in the bindings.cpp file.