ponder 3.2
C++ reflection library
Ponder C++ library documentation
ponder: to reflect on
  • TL;DR - Expose C++ classes and objects so they can used as data. Serialisation and Lua scripting supported.
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>();
// UserObject person is dereferenced and deleted here as scope ends.
}

Examples

Useful links

Articles

License

Ponder is distributed under the terms of the MIT license.

Ponder License

ponder::runtime::create
static UserObject create(const Class &cls, A... args)
Create instance of metaclass as a UserObject.
Definition: runtime.hpp:294
ponder::UserObject
Wrapper to manipulate user objects in the Ponder system.
Definition: userobject.hpp:62
ponder::Class::function
const Function & function(size_t index) const
Get a function from its index in this metaclass.
ponder::Value::to
T to() const
Convert the value to the type T.
ponder::runtime::call
static Value call(const Function &fn, const UserObject &obj, A &&... args)
Call a member function.
Definition: runtime.hpp:341
ponder::UserObject::set
void set(IdRef property, const Value &value) const
Set the value of an object's property by name.
ponder::Class
ponder::Class represents a metaclass composed of properties and functions
Definition: class.hpp:84
PONDER_TYPE
#define PONDER_TYPE(...)
Macro used to register a C++ type to Ponder.
Definition: pondertype.hpp:104