rttr  0.9.0
 All Classes Namespaces Files Functions Variables Typedefs Friends Macros Pages
Public Types | Public Member Functions | Static Public Member Functions | Friends | List of all members
rttr::type Class Reference

This class holds the type information for any arbitrary object. More...

#include <type.h>

Public Types

typedef uint16 type_id
 

Public Member Functions

 type (const type &other)
 Assigns a type to another one. More...
 
variant create (std::vector< detail::argument > args) const
 Creates an instance of the given type, with the given arguments for the constructor. More...
 
void destroy (variant &obj) const
 Destroys the given object obj. More...
 
std::vector< typeget_base_classes () const
 Returns a list of all base classes of this type. More...
 
constructor get_constructor (const std::vector< type > &params=std::vector< type >()) const
 Returns a public constructor whose parameters match the types in the specified list. More...
 
std::vector< constructorget_constructors () const
 Returns a list of all registered constructors for this type; the order is unspecified. More...
 
std::vector< typeget_derived_classes () const
 Returns a list of all derived classes of this type. More...
 
destructor get_destructor () const
 Returns the corresponding destructor for this type. More...
 
enumeration get_enumeration () const
 Returns the enumerator if this type is an enum type; otherwise the returned value is not valid. More...
 
type_id get_id () const
 Returns the id of this type. More...
 
method get_method (const std::string &name) const
 Returns a method with the name name. More...
 
method get_method (const std::string &name, const std::vector< type > &params) const
 Returns a method with the name name which match the given parameter list params. More...
 
std::vector< methodget_methods () const
 Returns a list of all registered methods for this type and all its base classes. More...
 
std::string get_name () const
 Returns the unique and human-readable name of the type. More...
 
std::vector< propertyget_properties () const
 Returns a list of all registered properties for this type and all its base classes. More...
 
property get_property (const std::string &name) const
 Returns a property with the name name. More...
 
variant get_property_value (const std::string &name, detail::instance obj) const
 Returns the property value of property named name from the instance obj. More...
 
type get_raw_type () const
 Returns a type object which represent the raw type. More...
 
variant invoke (const std::string &name, detail::instance obj, std::vector< detail::argument > args) const
 Invokes the method represented by the current instance object. More...
 
bool is_array () const
 Returns true whether the given type represents an array. More...
 
bool is_class () const
 Returns true whether the given type is class; that is not an atomic type or a method. More...
 
bool is_derived_from (const type &other) const
 Returns true if this type is derived from the given type other, otherwise false. More...
 
template<typename T >
bool is_derived_from () const
 Returns true if this type is derived from the given type T, otherwise false. More...
 
bool is_enumeration () const
 Returns true whether the given type represents an enumeration. More...
 
bool is_pointer () const
 Returns true whether the given type represents a pointer. More...
 
bool is_primitive () const
 Returns true whether the given type represents an primitive type (e.g. More...
 
bool is_valid () const
 Returns true if this type is valid, that means the type holds valid data to a type. More...
 
 operator bool () const
 Convenience function to check if this type is valid or not. More...
 
bool operator!= (const type &other) const
 Compares this type with the other type and returns true if both describe different types, otherwise returns false. More...
 
bool operator< (const type &other) const
 Comparison operator for sorting the type data according to some internal criterion. More...
 
bool operator<= (const type &other) const
 Comparison operator for sorting the type data according to some internal criterion. More...
 
typeoperator= (const type &other)
 Assigns a type to another one. More...
 
bool operator== (const type &other) const
 Compares this type with the other type and returns true if both describe the same type, otherwise returns false. More...
 
bool operator> (const type &other) const
 Comparison operator for sorting the type data according to some internal criterion. More...
 
bool operator>= (const type &other) const
 Comparison operator for sorting the type data according to some internal criterion. More...
 
bool set_property_value (const std::string &name, detail::instance obj, detail::argument arg) const
 This function will set the given value arg to a property named name to the instance obj. More...
 

Static Public Member Functions

template<typename T >
static type get ()
 Returns a type object for the given template type T. More...
 
template<typename T >
static type get (T &&object)
 Returns a type object for the given instance object. More...
 
static type get (const char *name)
 Returns the type object for the given name. More...
 
static method get_global_method (const std::string &name)
 Returns a global method with the name name. More...
 
static method get_global_method (const std::string &name, const std::vector< type > &params)
 Returns a global method with the name name which match the given parameter list params. More...
 
static std::vector< methodget_global_methods ()
 Returns a list of all registered global methods. More...
 
static std::vector< propertyget_global_properties ()
 Returns a list of all registered global properties. More...
 
static property get_global_property (const std::string &name)
 Returns a global property with the name name. More...
 
static variant get_property_value (const std::string &name)
 Returns the property value of property named name. More...
 
static std::vector< typeget_types ()
 Returns a list of all registered type objects. More...
 
static variant invoke (const std::string &name, std::vector< detail::argument > args)
 Invokes a global method named name with the specified argument args. More...
 
template<typename F >
static void register_converter_func (F func)
 Register a converter func F. More...
 
static bool set_property_value (const std::string &name, detail::argument arg)
 This function will set the given value arg to a property named name. More...
 

Friends

template<typename TargetType , typename SourceType >
TargetType rttr_cast (SourceType object)
 Casts the given object of type SourceType to an object of type TargetType. More...
 

Detailed Description

This class holds the type information for any arbitrary object.

Every class or primitive data type can have an unique type object. With the help of this object you can compare unknown types for equality at runtime or introspect the type for its properties, methods, enumerations, constructors and destructor.

Preparation

Before you can retrieve data from type, you have to register your struct or class. Therefore use the macro RTTR_DECLARE_TYPE(Type) to make the type known to the type system.

This example shows a typical usage:

// MyStruct.h
struct MyStruct
{
int i;
};

Retrieve type

A type object cannot be created. It is only possible to retrieve a type object via three static template member functions:

type::get(const char*)

This function just expects the name of the type. This is useful when you know only the name of the type and cannot include the type itself into the source code. The name of the type is the same like you have registered with RTTR_DECLARE_TYPE but as string literal. When you have used a typedef then you need to provide this typedef also as string literal.

type::get("int") == type::get("int"); // yields to true
type::get("bool") == type::get("int"); // yields to false
type::get("MyNameSpace::MyStruct") == type::get("MyNameSpace::MyStruct"); // yields to true

type::get<T>()

This function just expects one template argument. Use it to check against a known type.

type::get<int>() == type::get<int>(); // yields to true
type::get<int>() == type::get<bool>(); // yields to false

type::get<T>(T&& obj)

This function is a universal reference and returns from every given object the corresponding type object.

int int_obj;
int* int_obj_ptr = &int_obj;
const int* c_int_obj_ptr = int_obj_ptr;
type::get<int>() == type::get(int_obj); // yields to true
type::get<int*>() == type::get(int_obj_ptr); // yields to true
type::get<const int*>() == type::get(c_int_obj_ptr);// yields to true

When this function is called for a glvalue expression whose type is a polymorphic class type, then the result refers to a type object representing the type of the most derived object.

struct Base {};
struct Derived : Base {};
Derived d;
Base& base = d;
type::get<Derived>() == type::get(base) // yields to true
type::get<Base>() == type::get(base) // yields to false
// remark, when called with pointers:
Base* base_ptr = &d;
type::get<Derived>() == type::get(base_ptr); // yields to false
type::get<Base*>() == type::get(base_ptr); // yields to true
Remarks
If the type of the expression is a cv-qualified type, the result of the rttr::type::get expression refers to a rttr::type object representing the cv-unqualified type.
class D { ... };
D d1;
const D d2;
type::get(d1) == type::get(d2); // yields true
type::get<D>() == type::get<const D>(); // yields true
type::get<D>() == type::get(d2); // yields true
type::get<D>() == type::get<const D&>(); // yields true
type::get<D>() == type::get<const D*>(); // yields false

Any top level cv-qualifier of the given type T will be removed.

Copying and Assignment

A type object is lightweight and can be copied by value. However, each copy will refer to the same underlying type.

Member Typedef Documentation

Constructor & Destructor Documentation

rttr::type::type ( const type other)

Assigns a type to another one.

Member Function Documentation

variant rttr::type::create ( std::vector< detail::argument >  args) const

Creates an instance of the given type, with the given arguments for the constructor.

Remarks
When the arguments does not match the parameter list of the constructor then he will not be invoked.
Returns
Returns an instance of the given type.
void rttr::type::destroy ( variant obj) const

Destroys the given object obj.

Remarks
When the obj could be destroyed the given obj is invalid after calling this method; Otherwise it is still valid.
template<typename T >
static type rttr::type::get ( )
static

Returns a type object for the given template type T.

Returns
type for the template type T.
template<typename T >
static type rttr::type::get ( T &&  object)
static

Returns a type object for the given instance object.

Remarks
If the type of the expression is a cv-qualified type, the result of the type::get() expression refers to a type object representing the cv-unqualified type. When type::get() is applied to a glvalue expression whose type is a polymorphic class type, the result refers to a type object representing the type of the most derived object.
Returns
type for an object of type T.
static type rttr::type::get ( const char *  name)
static

Returns the type object for the given name.

Remarks
The search for the type is case sensitive. The name itself correspond to the name registered with RTTR_DECLARE_TYPE.
Returns
type for an object of name name.
std::vector<type> rttr::type::get_base_classes ( ) const

Returns a list of all base classes of this type.

Returns
A list of type objects.
constructor rttr::type::get_constructor ( const std::vector< type > &  params = std::vector< type >()) const

Returns a public constructor whose parameters match the types in the specified list.

Remarks
When no parameter list is given, it will be searched for the default constructor.
Returns
A valid constructor will be returned when the parameter matches the registered constructor; otherwise an invalid constructor.
std::vector<constructor> rttr::type::get_constructors ( ) const

Returns a list of all registered constructors for this type; the order is unspecified.

Returns
Returns a list of all registered constructors.
std::vector<type> rttr::type::get_derived_classes ( ) const

Returns a list of all derived classes of this type.

Returns
A list of type objects.
destructor rttr::type::get_destructor ( ) const

Returns the corresponding destructor for this type.

Remarks
When there is no constructor registered for this type, then also the destructor is not available. A destructor will always been automatically registered.
Returns
Returns the destructor for this type.
enumeration rttr::type::get_enumeration ( ) const

Returns the enumerator if this type is an enum type; otherwise the returned value is not valid.

See also
is_enumeration()
Returns
A enumeration object.
static method rttr::type::get_global_method ( const std::string &  name)
static

Returns a global method with the name name.

Remarks
When there exists not method with the name name, and invalid method is returned.
Returns
A method with name name.
static method rttr::type::get_global_method ( const std::string &  name,
const std::vector< type > &  params 
)
static

Returns a global method with the name name which match the given parameter list params.

Remarks
When there exists not method with the name name and matching parameter list params, then an invalid method is returned.
Returns
A method with name name and parameter signature params.
static std::vector<method> rttr::type::get_global_methods ( )
static

Returns a list of all registered global methods.

Returns
A vector with methods.
static std::vector<property> rttr::type::get_global_properties ( )
static

Returns a list of all registered global properties.

Returns
A vector with properties.
static property rttr::type::get_global_property ( const std::string &  name)
static

Returns a global property with the name name.

Remarks
When there exists not property with the name name, and invalid property is returned.
Returns
A property with name name.
type_id rttr::type::get_id ( ) const

Returns the id of this type.

Note
This id is unique at process runtime, but the id can be changed every time the process is executed.
Returns
The type id.
method rttr::type::get_method ( const std::string &  name) const

Returns a method with the name name.

Remarks
When there exists not method with the name name, then an invalid method is returned.
Returns
A method with name name.
method rttr::type::get_method ( const std::string &  name,
const std::vector< type > &  params 
) const

Returns a method with the name name which match the given parameter list params.

Remarks
When there exists not method with the name name and matching parameter list params, then an invalid method is returned.
Returns
A method with name name.
std::vector<method> rttr::type::get_methods ( ) const

Returns a list of all registered methods for this type and all its base classes.

Returns
A vector with method objects
std::string rttr::type::get_name ( ) const

Returns the unique and human-readable name of the type.

Returns
The type name.
std::vector<property> rttr::type::get_properties ( ) const

Returns a list of all registered properties for this type and all its base classes.

Returns
A vector with properties.
property rttr::type::get_property ( const std::string &  name) const

Returns a property with the name name.

Remarks
When there exists not property with the name name, and invalid property is returned.
Returns
A property with name name.
variant rttr::type::get_property_value ( const std::string &  name,
detail::instance  obj 
) const

Returns the property value of property named name from the instance obj.

Remarks
When the given instance is empty, the value of global property will be tryed to returned.
Returns
A variant containing the value of the property.
static variant rttr::type::get_property_value ( const std::string &  name)
static

Returns the property value of property named name.

Returns
A variant containing the value of the property.
type rttr::type::get_raw_type ( ) const

Returns a type object which represent the raw type.

A raw type, is a type type without any qualifiers (const and volatile) nor any pointer.

Remarks
When the current type is already the raw type, it will return an copy from itself.
Returns
The corresponding raw type object.
static std::vector<type> rttr::type::get_types ( )
static

Returns a list of all registered type objects.

Remarks
The order of the type object is unspecified.
Returns
A vector of type objects.
variant rttr::type::invoke ( const std::string &  name,
detail::instance  obj,
std::vector< detail::argument >  args 
) const

Invokes the method represented by the current instance object.

Remarks
When it's a static method you still need to provide an instance object, use therefore the function empty_instance().
Returns
The a variant object containing the possible return value, otherwise when it is a void function an empty but valid variant object.
static variant rttr::type::invoke ( const std::string &  name,
std::vector< detail::argument >  args 
)
static

Invokes a global method named name with the specified argument args.

Returns
The a variant object containing the possible return value, otherwise when it is a void function an empty but valid variant object.
bool rttr::type::is_array ( ) const

Returns true whether the given type represents an array.

Returns
True if the type is an array, otherwise false.
bool rttr::type::is_class ( ) const

Returns true whether the given type is class; that is not an atomic type or a method.

Returns
True if the type is a class, otherwise false.
bool rttr::type::is_derived_from ( const type other) const

Returns true if this type is derived from the given type other, otherwise false.

Returns
Returns true if this type is a derived type from other, otherwise false.
template<typename T >
bool rttr::type::is_derived_from ( ) const

Returns true if this type is derived from the given type T, otherwise false.

Returns
Returns true if this type is a derived type from T, otherwise false.
bool rttr::type::is_enumeration ( ) const

Returns true whether the given type represents an enumeration.

Returns
True if the type is an enumeration, otherwise false.
bool rttr::type::is_pointer ( ) const

Returns true whether the given type represents a pointer.

Returns
True if the type is a pointer, otherwise false.
bool rttr::type::is_primitive ( ) const

Returns true whether the given type represents an primitive type (e.g.

int, bool, etc.).

Returns
True if the type is a primitive type, otherwise false.
bool rttr::type::is_valid ( ) const

Returns true if this type is valid, that means the type holds valid data to a type.

Returns
True if this type is valid, otherwise false.
rttr::type::operator bool ( ) const

Convenience function to check if this type is valid or not.

Returns
True if this type is valid, otherwise false.
bool rttr::type::operator!= ( const type other) const

Compares this type with the other type and returns true if both describe different types, otherwise returns false.

Returns
True if both type are not equal, otherwise false.
bool rttr::type::operator< ( const type other) const

Comparison operator for sorting the type data according to some internal criterion.

Returns
True if this type is less than the other.
bool rttr::type::operator<= ( const type other) const

Comparison operator for sorting the type data according to some internal criterion.

Returns
True if this type is less than or equal to other.
type& rttr::type::operator= ( const type other)

Assigns a type to another one.

Returns
A type object.
bool rttr::type::operator== ( const type other) const

Compares this type with the other type and returns true if both describe the same type, otherwise returns false.

Returns
True if both type are equal, otherwise false.
bool rttr::type::operator> ( const type other) const

Comparison operator for sorting the type data according to some internal criterion.

Returns
True if this type is greater than the other.
bool rttr::type::operator>= ( const type other) const

Comparison operator for sorting the type data according to some internal criterion.

Returns
True if this type is greater than or equal to other.
template<typename F >
static void rttr::type::register_converter_func ( func)
static

Register a converter func F.

This function converts a source Type to a target type. The signature of this function has to be the following: <TargetType (SourceType, bool& ok)> e.g.:

std::string conv_func(int value, bool& ok)
{
std::string result = std::to_string(value);
ok = true;
return result;
}
//...
bool rttr::type::set_property_value ( const std::string &  name,
detail::instance  obj,
detail::argument  arg 
) const

This function will set the given value arg to a property named name to the instance obj.

Remarks
When the given instance is empty, the value of a global property with name name will be tryed to set.
Returns
A bool value, which is true, when the value could be set, otherwise false.
static bool rttr::type::set_property_value ( const std::string &  name,
detail::argument  arg 
)
static

This function will set the given value arg to a property named name.

Returns
A bool value, which is true, when the value could be set, otherwise false.

Friends And Related Function Documentation

template<typename TargetType , typename SourceType >
TargetType rttr_cast ( SourceType  object)
friend

Casts the given object of type SourceType to an object of type TargetType.

When the given the given object is an instance of type TargetType, then this function will cast the pointer to the TargetType; otherwise it will return a nullptr. If object is already a nullptr then it will also return a nullptr.

Remarks
SourceType and TargetType must be both pointer types.
Returns
A pointer of type TargetType

The documentation for this class was generated from the following file: