ponder 3.1
C++ reflection library
Ponder C++ library documentation
ponder1.jpg
ponder: to reflect on
  • TL;DR - Expose C++ classes and objects so they can be more easily used as data. Serialisation and Lua scripting supported.
Lua-Logo-128.png
Lua binding

Overview

Ponder is a C++ library which provides runtime reflection for types. It provides an abstraction for most of the high-level concepts of C++ like classes, enumerations, properties, functions, and objects. By wrapping all these concepts into abstract structures, Ponder provides an extra layer of flexibility to programs, and allow them to expose and manipulate their data structures at runtime.

Many applications can take advantage of Ponder, in order to automate tasks which would otherwise require a huge amount of work. For example, Ponder can be used to expose and edit objects' attributes into a graphical user interface. It can also be used to do automatic binding of C++ classes to script languages such as Python or Lua. Another possible application would be the serialization of objects to XML, text or binary formats. Or you can even combine all these examples to provide a powerful and consistent interface for manipulating your objects outside C++ code.

Features:

  • Expose C++ classes so they can be found by name or type.
  • Create and destroy instance of classes at runtime.
  • Get and set properties on member variables.
  • Call member and static functions.
  • Save class instance data as JSON or XML.
  • Automatic Lua binding generation.

Quick example

Here is a simple example of how to use Ponder. First we need a class to expose:

class Person
{
public:
// constructor
Person(const std::string& name)
: m_name(name)
{}
// accessors for private members
std::string name() const { return m_name; }
void setName(const std::string& name) { m_name = name; }
// public members
float height;
unsigned int shoeSize;
// member function
bool hasBigFeet() const { return shoeSize > 10; } // U.K.!
private:
std::string m_name;
};

Then we declare what we want to expose to Ponder:

PONDER_TYPE(Person) // declare the type to Ponder
static void declare() // declare the class members to Ponder
{
ponder::Class::declare<Person>("Person")
.constructor<std::string>()
.property("name", &Person::name, &Person::setName)
.property("height", &Person::height)
.property("shoeSize", &Person::shoeSize)
.function("hasBigFeet", &Person::hasBigFeet)
;
}

We can then create and manipulate instances of the class using the API:

// An example of how you might use Ponder:
static void use()
{
// retrieve the metaclass (containing the member data)
const ponder::Class& metaclass = ponder::classByType<Person>();
// construct a new person
ponder::UserObject person = ponder::runtime::create(metaclass, "Bozo");
// set attributes
person.set("height", 1.62f);
person.set("shoeSize", 28);
// retrieve a function we would like to call
const auto& func = metaclass.function("hasBigFeet");
// call the function and get the result
const bool bigFeet = ponder::runtime::call(func, person).to<bool>();
// nasty
}

Examples

Useful links

Articles

License

Ponder is distributed under the terms of the MIT license.

Ponder License