ponder 3.2
C++ reflection library
ponder::Class Class Reference

ponder::Class represents a metaclass composed of properties and functions More...

#include <class.hpp>

+ Inheritance diagram for ponder::Class:

Public Types

typedef View< const Function &, FunctionTable::const_iterator > FunctionView
 
typedef View< const Property &, PropertyTable::const_iterator > PropertyView
 

Public Member Functions

IdReturn name () const
 Return the name of the metaclass. More...
 
size_t baseCount () const
 Return the total number of base metaclasses of this metaclass. More...
 
const Classbase (size_t index) const
 Return a base metaclass from its index. More...
 
size_t constructorCount () const
 Return the total number of constructors of this metaclass. More...
 
const Constructorconstructor (size_t index) const
 Access constructors by index. More...
 
void destruct (const UserObject &uobj, bool destruct) const
 Destroy a UserObject instance. More...
 
size_t functionCount () const
 Return the total number of functions of this metaclass. More...
 
bool hasFunction (IdRef name) const
 Check if this metaclass contains the given function. More...
 
const Functionfunction (size_t index) const
 Get a function from its index in this metaclass. More...
 
const Functionfunction (IdRef name) const
 Get a function from its name. More...
 
FunctionView functions () const
 Get a function iterator. More...
 
bool tryFunction (const IdRef name, const Function *&funcRet) const
 Look up a function by name and return success. More...
 
size_t propertyCount () const
 Return the total number of properties of this metaclass. More...
 
bool hasProperty (IdRef name) const
 Check if this metaclass contains the given property. More...
 
const Propertyproperty (size_t index) const
 Get a property from its index in this metaclass. More...
 
const Propertyproperty (IdRef name) const
 Get a property from its name. More...
 
PropertyView properties () const
 Get a property iterator. More...
 
bool tryProperty (const IdRef name, const Property *&propRet) const
 Look up a property by name and return success. More...
 
size_t sizeOf () const
 Return the memory size of a class instance. More...
 
UserObject getUserObjectFromPointer (void *ptr) const
 Create a UserObject from an opaque user pointer. More...
 
void visit (ClassVisitor &visitor) const
 Start visitation of a class. More...
 
void * applyOffset (void *pointer, const Class &target) const
 Convert a pointer to an object compatible with a base or derived metaclass. More...
 
bool operator== (const Class &other) const
 Operator == to check equality between two metaclasses. More...
 
bool operator!= (const Class &other) const
 Operator != to check inequality between two metaclasses. More...
 

Static Public Member Functions

template<typename T >
static ClassBuilder< T > declare (IdRef id=ponder::IdRef())
 Declare a new metaclass. More...
 
template<typename T >
static void undeclare ()
 Undeclare an existing metaclass. More...
 

Friends

template<typename T >
class ClassBuilder
 
class detail::ClassManager
 

Related Functions

(Note that these are not member functions.)

size_t classCount ()
 Get the total number of existing metaclasses. More...
 
detail::ClassManager::ClassView classes ()
 Get an iterator that can be used to iterate over all registered classes. More...
 
const ClassclassByName (IdRef name)
 Get a metaclass from its name. More...
 
template<typename T >
const ClassclassByObject (const T &object)
 Get a metaclass from a C++ object. More...
 
template<typename T >
const ClassclassByType ()
 Get a metaclass from its C++ type. More...
 
template<typename T >
const ClassclassByTypeSafe ()
 Get a metaclass from its C++ type. More...
 

Detailed Description

ponder::Class represents a metaclass composed of properties and functions

ponder::Class is the main class of the Ponder API. It defines a metaclass, which is an abstract representation of a C++ class with its own properties, functions, constructors, base classes, etc.

Classes are declared, bound to a C++ type and filled with the Class::declare() function.

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;
};
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)
;
}

It then provides a set of accessors to retrieve its member functions and properties. See Class::function() and Class::property().

Another way to inspect a class, which is more type-safe, is to use a ClassVisitor.

MyVisitor visitor;
metaclass.visit(visitor);

It also allows to create and destroy instances of the bound C++ class.

// construct a new person
ponder::UserObject person = ponder::runtime::create(metaclass, "Bozo");
Remarks
All function and property names are unique within the metaclass.
See also
ClassBuilder, Function, Property, Enum

Member Function Documentation

◆ applyOffset()

void* ponder::Class::applyOffset ( void *  pointer,
const Class target 
) const

Convert a pointer to an object compatible with a base or derived metaclass.

The target metaclass may be a base or a derived of this, both cases are properly handled.

Note
Because virtual inheritance implementation is compiler specific this method is unreliable where virtual inheritance is used.
Parameters
pointerPointer to convert
targetTarget metaclass to convert to
Returns
Converted pointer
Exceptions
ClassUnrelatedtarget is not a base nor a derived class of this

◆ base()

const Class& ponder::Class::base ( size_t  index) const

Return a base metaclass from its index.

Parameters
indexIndex of the base to get
Returns
Reference to the index-th base metaclass of this metaclass
Exceptions
OutOfRangeindex is out of range

◆ baseCount()

size_t ponder::Class::baseCount ( ) const

Return the total number of base metaclasses of this metaclass.

Returns
Number of base metaclasses

◆ constructor()

const Constructor* ponder::Class::constructor ( size_t  index) const

Access constructors by index.

Parameters
indexIndex
Returns
Constructor

◆ constructorCount()

size_t ponder::Class::constructorCount ( ) const

Return the total number of constructors of this metaclass.

Returns
Number of constructors

◆ declare()

template<typename T >
static ClassBuilder<T> ponder::Class::declare ( IdRef  id = ponder::IdRef())
static

Declare a new metaclass.

Call this to create a new metaclass. The template parameter T is the C++ class that will be bound to the metaclass.

Parameters
idName of the metaclass in Ponder. This name identifies the metaclass and thus has to be unique. If not specified, the C++ type id is used.
Returns
A ClassBuilder<T> object that will provide functions to fill the new metaclass with properties, functions, etc.
Remarks
It is best to leave the name blank and use the default class name.

◆ destruct()

void ponder::Class::destruct ( const UserObject uobj,
bool  destruct 
) const

Destroy a UserObject instance.

Parameters
uobjUser object to destruct
destructTrue for destruct (placement new), else destroy (new)

◆ function() [1/2]

const Function& ponder::Class::function ( IdRef  name) const

Get a function from its name.

Parameters
nameName of the function to get (case sensitive)
Returns
Reference to the function
Exceptions
FunctionNotFoundname is not a function of the metaclass

◆ function() [2/2]

const Function& ponder::Class::function ( size_t  index) const

Get a function from its index in this metaclass.

Parameters
indexIndex of the function to get
Returns
Reference to the function
Exceptions
OutOfRangeindex is out of range

◆ functionCount()

size_t ponder::Class::functionCount ( ) const

Return the total number of functions of this metaclass.

Returns
Number of functions

◆ functions()

FunctionView ponder::Class::functions ( ) const

Get a function iterator.

Returns
An iterator that can be used to iterator over all functions
for (auto&& func : classByType<MyClass>().functions())
foo(func.name(), func.value());

◆ getUserObjectFromPointer()

UserObject ponder::Class::getUserObjectFromPointer ( void *  ptr) const

Create a UserObject from an opaque user pointer.

Returns
A UserObject with this class's type
Note
This relies on the user choosing the correct metaclass. There are no checks (as the data is opaque).
auto const& metacls = ponder::classByType<MyClass>();
void *ptr = &object;
ponder::UserObject uo( metacls.getUserObjectFromPointer(ptr) );

◆ hasFunction()

bool ponder::Class::hasFunction ( IdRef  name) const

Check if this metaclass contains the given function.

Parameters
nameName of the function to check
Returns
True if the function is in the metaclass, false otherwise

◆ hasProperty()

bool ponder::Class::hasProperty ( IdRef  name) const

Check if this metaclass contains the given property.

Parameters
nameName of the property to check
Returns
True if the property is in the metaclass, false otherwise

◆ name()

IdReturn ponder::Class::name ( ) const

Return the name of the metaclass.

Returns
String containing the name of the metaclass

◆ operator!=()

bool ponder::Class::operator!= ( const Class other) const

Operator != to check inequality between two metaclasses.

Parameters
otherMetaclass to compare with this
Returns
True if metaclasses are different, false if they are equal

◆ operator==()

bool ponder::Class::operator== ( const Class other) const

Operator == to check equality between two metaclasses.

Two metaclasses are equal if their name is the same.

Parameters
otherMetaclass to compare with this
Returns
True if both metaclasses are the same, false otherwise

◆ properties()

PropertyView ponder::Class::properties ( ) const

Get a property iterator.

Returns
An iterator that can be used to iterator over all properties
for (auto&& prop : ponder::classByType<MyClass>())
foo(prop.name(), prop.value());

◆ property() [1/2]

const Property& ponder::Class::property ( IdRef  name) const

Get a property from its name.

Parameters
nameName of the property to get (case sensitive)
Returns
Reference to the property
Exceptions
PropertyNotFoundname is not a property of the metaclass

◆ property() [2/2]

const Property& ponder::Class::property ( size_t  index) const

Get a property from its index in this metaclass.

Parameters
indexIndex of the property to get
Returns
Reference to the property
Exceptions
OutOfRangeindex is out of range

◆ propertyCount()

size_t ponder::Class::propertyCount ( ) const

Return the total number of properties of this metaclass.

Returns
Number of properties

◆ sizeOf()

size_t ponder::Class::sizeOf ( ) const

Return the memory size of a class instance.

Returns
Size in bytes

◆ tryFunction()

bool ponder::Class::tryFunction ( const IdRef  name,
const Function *&  funcRet 
) const

Look up a function by name and return success.

Parameters
nameName of the function to get (case sensitive)
funcRetFunction returned, if return was true
Returns
Boolean. True if function found, else if not, false
const Function *func;
if (classByType<MyClass>().tryFunction("foo", func))
func.call(...);

◆ tryProperty()

bool ponder::Class::tryProperty ( const IdRef  name,
const Property *&  propRet 
) const

Look up a property by name and return success.

Parameters
nameName of the property to get (case sensitive)
propRetProperty returned, if return was true
Returns
Boolean. True if property found, else if not, false
const Property *prop;
if (classByType<MyClass>().tryProperty("bar", prop))
... ;

◆ undeclare()

template<typename T >
static void ponder::Class::undeclare ( )
static

Undeclare an existing metaclass.

Use this to undeclare a metaclass that you no longer require. E.g. from a dynamically loaded library that is being unloaded.

Note
Do not use automatic metaclass declaration (PONDER_AUTO_TYPE) for the class or it will keep being recreated by Ponder.
See also
Class::declare, Enum::undeclare

◆ visit()

void ponder::Class::visit ( ClassVisitor visitor) const

Start visitation of a class.

Parameters
visitorVisitor to use for visitation

Friends And Related Function Documentation

◆ classByName()

const Class & classByName ( IdRef  name)
related

Get a metaclass from its name.

Note
Automated registration does not occur when using this lookup call (since we don't have the object type). Use PONDER_TYPE() registration if you use this.
Parameters
nameName of the metaclass to retrieve (case sensitive)
Returns
Reference to the requested metaclass
Exceptions
ClassNotFoundname is not a valid metaclass name

◆ classByObject()

template<typename T >
const Class & classByObject ( const T &  object)
related

Get a metaclass from a C++ object.

Parameters
objectobject to get the metaclass of
Returns
Reference to the metaclass bound to type T
Exceptions
ClassNotFoundno metaclass has been declared for T or any of its bases

◆ classByType()

template<typename T >
const Class & classByType ( )
related

Get a metaclass from its C++ type.

Returns
Reference to the metaclass bound to type T
Exceptions
ClassNotFoundno metaclass has been declared for T

◆ classByTypeSafe()

template<typename T >
const Class * classByTypeSafe ( )
related

Get a metaclass from its C++ type.

Returns
Pointer to the metaclass bound to type T, or null pointer if no metaclass has been declared

◆ classCount()

size_t classCount ( )
related

Get the total number of existing metaclasses.

Returns
Global metaclass count

◆ classes()

detail::ClassManager::ClassView classes ( )
related

Get an iterator that can be used to iterate over all registered classes.

// for (auto&& cls : ponder::classIterator())
// {
// std::cout << "Class " << cls.first << std::endl;
// }
Returns
ClassView

The documentation for this class was generated from the following files:
ponder::FunctionKind::Function
@ Function
a function
ponder::runtime::create
static UserObject create(const Class &cls, A... args)
Create instance of metaclass as a UserObject.
Definition: runtime.hpp:294
ponder::Class::visit
void visit(ClassVisitor &visitor) const
Start visitation of a class.
ponder::Class::functions
FunctionView functions() const
Get a function iterator.
ponder::UserObject
Wrapper to manipulate user objects in the Ponder system.
Definition: userobject.hpp:62
ponder::Class::tryFunction
bool tryFunction(const IdRef name, const Function *&funcRet) const
Look up a function by name and return success.
ponder::Class::name
IdReturn name() const
Return the name of the metaclass.
ponder::Class::declare
static ClassBuilder< T > declare(IdRef id=ponder::IdRef())
Declare a new metaclass.
ponder::Class::tryProperty
bool tryProperty(const IdRef name, const Property *&propRet) const
Look up a property by name and return success.
ponder::Class::property
const Property & property(size_t index) const
Get a property from its index in this metaclass.
PONDER_TYPE
#define PONDER_TYPE(...)
Macro used to register a C++ type to Ponder.
Definition: pondertype.hpp:104