This shortcut tutorial will show you all basic information to use RTTR.
Class declaration
Suppose you have a class called node
which you want to introspect with RTTR.
#include <rttr/type>
namespace ns_3d
{
class node
{
public:
node(std::string name, node* parent = nullptr);
virtual ~node();
void set_name(const std::string& name);
const std::string& get_name() const;
std::vector<node*> get_children() const;
void set_visible(bool visible, bool cascade = true);
virtual void render();
private:
node* m_parent;
std::string m_name;
std::vector<node*> m_children;
};
}
#define RTTR_REGISTRATION_FRIEND
Place this macro inside a class, when you need to reflect properties, methods or constructors which a...
Definition registration.h:793
#define RTTR_ENABLE(...)
This macro is necessary in order to retrieve type information about the inheritance graph of a class.
Definition rttr_enable.h:76
The standard include file of rttr is: <rttr/type>
Remark the two added macros: RTTR_ENABLE() and RTTR_REGISTRATION_FRIEND. They are optional.
However, when you use class hierarchies you should add to every class: RTTR_ENABLE().
When you want to reflect private data of a class, add: RTTR_REGISTRATION_FRIEND.
Registration
Now comes the registration part, this is usually done in the corresponding source file.
#include <rttr/registration>
{
(
policy::ctor::as_std_shared_ptr,
default_arguments(nullptr)
)
.
property(
"name", &node::get_name, &node::set_name)
(
metadata("TOOL_TIP", "Set the name of node.")
)
.
property(
"parent", &ns_3d::node::m_parent, registration::private_access)
.property_readonly("children", &node::get_children)
.method("set_visible", &node::set_visible)
(
default_arguments(true),
parameter_names("visible", "cascade")
)
.
method(
"render", &node::render)
;
}
The array_range class provides a view into an underlying data structure with lower and upper limits.
Definition array_range.h:64
The method class provides several meta information about a method and can be invoked.
Definition method.h:122
The property class provides several meta information about a property and gives read/write access to ...
Definition property.h:118
The class_ is used to register classes to RTTR.
Definition registration.h:130
bind< detail::ctor, Class_Type, acc_level, Visitor_List, Args... > constructor(acc_level level=acc_level())
Register a constructor for this class type with or without arguments.
Definition access_levels.h:34
#define RTTR_REGISTRATION
Use this macro to automatically register your reflection information to RTTR before main is called.
Definition registration.h:745
The standard include file for registration types in rttr is: <rttr/registration>
Include this file only when you want to register something. The registration class is here the entry point. This registration process creates internally wrapper classes, which store for example function pointers or object pointers of the specific class. For these pointers you have to provide the data manually.
Yeah, and that's it. Now you can use RTTR to retrieve this information.
Basic usage
#include <rttr/type>
#include <iostream>
{
type t = type::get_by_name(
"ns_3d::node");
std::cout <<
var.get_type().get_name() <<
"\n";
property prop =
t.get_property(
"name");
prop.set_value(
var, std::string(
"A New Name"));
std::cout << prop.get_value(
var).to_string() <<
"\n";
std::cout << "MetaData TOOL_TIP: " << prop.get_metadata("TOOL_TIP").to_string() << "\n";
method meth =
t.get_method(
"set_visible");
std::cout << std::boolalpha <<
"invoke of method 'set_visible' was successfully: " <<
ret.is_valid() <<
"\n\n";
std::cout << "'node' properties:" << "\n";
for (
auto& prop :
t.get_properties())
{
std::cout << " name: " << prop.get_name() << "\n";
std::cout << " type: " << prop.get_type().get_name() << "\n";
}
std::cout << "\n";
std::cout << "'node' methods:" << "\n";
for (
auto& meth :
t.get_methods())
{
std::cout <<
" name: " << meth.
get_name();
for (
auto&
info : meth.get_parameter_infos())
{
std::cout <<
" param " <<
info.get_index() <<
": name: "<<
info.get_name() <<
"\n";
}
}
return 0;
}
string_view get_name() const noexcept
Returns the name of this method.
string_view get_signature() const noexcept
Returns the signature of this method as readable string.
variant invoke(instance object) const
Invokes the method represented by the current instance object.
The type class holds the type information for any arbitrary object.
Definition type.h:178
The variant class allows to store data of any type and convert between these types transparently.
Definition variant.h:199
Remark, that there is actual no include of node.h
. See the output below:
Output
class std::shared_ptr<class ns_3d::node>
A New Name
MetaData TOOL_TIP: Set the name of node.
invoke of method 'set_visible' was successfully: true
'node' properties:
name: name
type: std::string
name: parent
type: ns_3d::node*
name: children
type: class std::vector<class ns_3d::node *,class std::allocator<class ns_3d::node *> >
'node' methods:
name: set_visible signature: set_visible( bool, bool )
param 0: name: visible
param 1: name: cascade
name: render signature: render( )
Derived classes
Suppose you create now from node
a derived class called mesh
.
#include <rttr/type>
{
public:
{
};
private:
};
Now you put in RTTR_ENABLE() the name of the base class, in this case: node
.
Registration part
{
.property("render_mode", &mesh::get_render_mode, &mesh::set_render_mode)
.enumeration<mesh::render_mode>("ns_3d::render_mode")
(
value("POINTS", mesh::render_mode::POINTS),
value("WIREFRAME", mesh::render_mode::WIREFRAME),
value("SOLID", mesh::render_mode::SOLID)
);
}
Remark that it is not necessary to register again the render()
method.
Basic Usage
#include <rttr/type>
#include <iostream>
#include "mesh.h"
{
std::shared_ptr<ns_3d::node>
obj = ns_3d::mesh::create_mesh(
"House.obj");
std::cout << type::get(
obj).get_name() <<
"\n";
std::cout << type::get(
obj).get_wrapped_type().get_name() <<
"\n";
std::cout << type::get(*
obj.get()).get_name() <<
"\n";
std::cout << "\n";
std::cout << "'mesh' properties:" << "\n";
for (
auto& prop :
t.get_properties())
{
std::cout << " name: " << prop.get_name() << "\n";
std::cout << " type: " << prop.get_type().get_name() << "\n";
}
property prop =
t.get_property(
"render_mode");
bool ret = prop.set_value(
obj, ns_3d::mesh::render_mode::SOLID);
std::cout << "\n";
method meth =
t.get_method(
"render");
std::shared_ptr<ns_3d::mesh>
obj_derived = std::dynamic_pointer_cast<ns_3d::mesh>(
obj);
std::cout << std::boolalpha <<
"invoke of method 'render' was successfully: " <<
var.is_valid() <<
"\n";
}
type get_declaring_type() const noexcept
Returns the class that declares this method.
See the output below:
Output
class std::shared_ptr<class ns_3d::node>
ns_3d::node*
ns_3d::mesh
'mesh' properties:
name: name
type: std::string
name: parent
type: ns_3d::node*
name: children
type: class std::vector<class ns_3d::node *,class std::allocator<class ns_3d::node *> >
name: render_mode
type: ns_3d::render_mode
ns_3d::node
invoke of method 'render' was successfully: true
That's it.
However, in order to see all the possibilities of RTTR, it is recommend to go through the in-depth tutorial: Start Tutorial