rttr::property Class Reference

The property class provides several meta information about a property and gives read/write access to its value. More...

#include <property.h>

Public Member Functions

access_levels get_access_level () const
 Returns the access level with which this property was registered. More...
 
type get_declaring_type () const
 Returns the type of the class or struct that declares this property. More...
 
enumeration get_enumeration () const
 Returns the enumerator if this property is an enum type; otherwise the returned value is not valid. More...
 
variant get_metadata (const variant &key) const
 Returns the meta data for the given key key. More...
 
std::string get_name () const
 Returns the name of this property. More...
 
type get_type () const
 Returns the underlying type object of this property. More...
 
variant get_value (instance object) const
 Returns the current property value of the given instance object. More...
 
bool is_array () const
 Returns true if the underlying property is an array. More...
 
bool is_enumeration () const
 Returns true if the underlying property is an enumeration. More...
 
bool is_readonly () const
 Returns true if this property is read only, otherwise false. More...
 
bool is_static () const
 Returns true if this property is static property, otherwise false. More...
 
bool is_valid () const
 Returns true if this property is valid, otherwise false. More...
 
 operator bool () const
 Convenience function to check if this property is valid or not. More...
 
bool operator!= (const property &other) const
 Returns true if this property is the not the same like the other. More...
 
bool operator== (const property &other) const
 Returns true if this property is the same like the other. More...
 
bool set_value (instance object, argument arg) const
 Set the property of the given instance object to the given value arg. More...
 

Detailed Description

The property class provides several meta information about a property and gives read/write access to its value.

A instance of a property class can only be obtained from the type class. See type::get_property() and type::get_properties().

For registration a property, nested inside a class, see registration::class_<T>::property() and for global properties see registration::property().

Meta Information

A property has a name, and a type as well as attributes that specify its behavior: is_readonly(), is_static(), is_enumeration(), is_array(). When the property was declared inside a class, then get_declaring_type() can be used to obtain the type of this class.

The property's values are set and retrieved with set_value() and get_value(); When its not a static property you have to provide a class instance to set/get the property value. This instance can be the raw type on the stack; the current hierarchy level doesn't matter. It can be also a raw pointer to the object or a variant which contains the instance, again as pointer or stack object. When the property is declared as static you you still have to provide an empty instance object, use therefore the default ctor of instance(), or as shortcut use simply {}.

A property will be successfully set when the provided instance can be converted to the declared class type. The new forwarded property value must 100% match the type of the registered property. An automatically type conversion is not performed.

The return type of get_value() is variant object. This object contains not only the value of the property, it also indicates whether the property value could be retrieved or not. A valid variant object means, that the property was successfully retrieved, otherwise not.

Another way to get access a property is through type's set and get functions. See type::set_property_value() and type::get_property_value() for details.

Copying and Assignment

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

Typical Usage

using namespace rttr;
struct MyStruct { int value = 23; };
//...
variant obj = type::get_by_name("MyStruct").create({});
property prop = type::get_by_name("MyStruct").get_property("value");
if (prop)
{
variant val = prop.get_value(obj);
std::cout << val.get_value<int>(); // prints 23
MyStruct inst;
val = prop.set_value(inst, 42);
std::cout << inst.value; // prints 42
// or as pointer
MyStruct* ptr = &inst;
val = prop.set_value(ptr, 7);
std::cout << ptr->value; // prints 7
// or do it all in one call
type::get(inst).set_propert_value("value", inst, 1024);
std::cout << inst.value; // prints 1024
}
See also
method, enumeration, constructor and type

Member Function Documentation

access_levels rttr::property::get_access_level ( ) const

Returns the access level with which this property was registered.

Remarks
When the property is not valid, this function will return level access_levels::public_access.
Returns
access_levels of the property.
type rttr::property::get_declaring_type ( ) const

Returns the type of the class or struct that declares this property.

Remarks
When this property does not belong to a class (i.e. is a global property) it will return an invalid type object. When this property is not valid, this function will return an invalid type object (see type::is_valid).
Returns
Type of the declaring class/struct for this property.
enumeration rttr::property::get_enumeration ( ) const

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

See also
is_enumeration()
Returns
An enumeration object.
variant rttr::property::get_metadata ( const variant key) const

Returns the meta data for the given key key.

Remarks
When no meta data is registered with the given key, an invalid variant object is returned (see variant::is_valid).
Returns
A variant object, containing arbitrary data.
std::string rttr::property::get_name ( ) const

Returns the name of this property.

Remarks
When the property is not valid, this function will return an empty string.
Returns
Name of the property.
type rttr::property::get_type ( ) const

Returns the underlying type object of this property.

Remarks
When the property is not valid, this function will return an invalid type object.
Returns
Type of the underlying property.
variant rttr::property::get_value ( instance  object) const

Returns the current property value of the given instance object.

Remarks
When the property is static, you can forward an empty instance.
See also
set_value().
Returns
The property value of the given instance object.
bool rttr::property::is_array ( ) const

Returns true if the underlying property is an array.

Remarks
When the property is not valid, this function will return false.
Returns
True if this is a array type, otherwise false.
bool rttr::property::is_enumeration ( ) const

Returns true if the underlying property is an enumeration.

Remarks
When the property is not valid, this function will return false.
Returns
True if this is a enumeration type, otherwise false.
bool rttr::property::is_readonly ( ) const

Returns true if this property is read only, otherwise false.

Remarks
When the property is not valid, this function will return false.
Returns
True if this is a read only property, otherwise false.
bool rttr::property::is_static ( ) const

Returns true if this property is static property, otherwise false.

A static property does not need an instance for performing set_value/get_value.

Remarks
When the property is not valid, this function will return false.
Returns
True if this is a static property, otherwise false.
bool rttr::property::is_valid ( ) const

Returns true if this property is valid, otherwise false.

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

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

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

Returns true if this property is the not the same like the other.

Returns
True if both properties are different, otherwise false.
bool rttr::property::operator== ( const property other) const

Returns true if this property is the same like the other.

Returns
True if both properties are equal, otherwise false.
bool rttr::property::set_value ( instance  object,
argument  arg 
) const

Set the property of the given instance object to the given value arg.

Remarks
When the property is declared as read only this function will return false. When you have a static property just pass an empty instance as object argument. When the property is not valid, this function will return false.
See also
get_value().
Returns
The return value indicates whether the operation was successful or not.

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