rttr::instance Class Reference

The instance class is used for forwarding the instance of an object to invoke a property or method. More...

#include <instance.h>

Public Member Functions

 instance () noexcept
 Creates an invalid instance object. More...
 
 instance (const variant &var) noexcept
 Creates an instance object from a variant object. More...
 
 instance (const instance &other) noexcept
 Copy constructor for an instance. More...
 
template<typename T , typename Tp = decay_instance_t<T>>
 instance (T &data) noexcept
 Creates an instance object from type T. More...
 
type get_derived_type () const noexcept
 Returns the most derived type of the hold instance. More...
 
type get_type () const noexcept
 Returns the type of the internally hold instance. More...
 
instance get_wrapped_instance () const noexcept
 Returns an instance object for the wrapped instance. More...
 
bool is_valid () const noexcept
 Returns true when the instance class contains a reference to an object. More...
 
 operator bool () const noexcept
 Returns true when the instance class contains a reference to an object. More...
 
template<typename Target_Type >
Target_Type * try_convert () const noexcept
 This function will try to convert the underlying instance to the given type Target_Type*. More...
 

Detailed Description

The instance class is used for forwarding the instance of an object to invoke a property or method.

Remarks
The instance class will internally hold a reference to the object. It will not perform any copy operation on the data itself. Make sure you don't hold an instance, while the underlying object is already destroyed. Otherwise it will lead to undefined behavior.

Constructor & Destructor Documentation

rttr::instance::instance ( )
noexcept

Creates an invalid instance object.

Use this constructor, when you need to invoke a property or method where no instance is required.

rttr::instance::instance ( const variant var)
noexcept

Creates an instance object from a variant object.

rttr::instance::instance ( const instance other)
noexcept

Copy constructor for an instance.

template<typename T , typename Tp = decay_instance_t<T>>
rttr::instance::instance ( T &  data)
noexcept

Creates an instance object from type T.

Remarks
Internally, the instance class will hold a reference to the address of the given object data.

Member Function Documentation

type rttr::instance::get_derived_type ( ) const
noexcept

Returns the most derived type of the hold instance.

See following example code:

struct base { RTTR_ENABLE() };
struct middle : base { RTTR_ENABLE(base) };
struct derived : middle { RTTR_ENABLE(middle) };
//...
derived d;
base& b = d;
instance obj = b;
obj.get_type() == type::get<base>(); // yields to true
obj.get_derived_type() == type::get<derived>(); // yields to true
type rttr::instance::get_type ( ) const
noexcept

Returns the type of the internally hold instance.

Remarks
When no reference is stored, an invalid type object will be returned.
Returns
Type object of stored reference.
instance rttr::instance::get_wrapped_instance ( ) const
noexcept

Returns an instance object for the wrapped instance.

See following example code:

std::shared_ptr<foo> f;
instance obj = f;
obj.get_type() == type::get<std::shared_ptr<foo>>(); // yields to true
obj.get_wrapped_instance().get_type() == type::get<foo>(); // yields to true
Remarks
When the current instance is not a wrapper type, an invalid instance will be returned.
Returns
An instance object from the wrapped type.
bool rttr::instance::is_valid ( ) const
noexcept

Returns true when the instance class contains a reference to an object.

Otherwise false.

Returns
True when a reference is stored, otherwise false.
rttr::instance::operator bool ( ) const
explicitnoexcept

Returns true when the instance class contains a reference to an object.

Otherwise false.

Returns
True when a reference is stored, otherwise false.
template<typename Target_Type >
Target_Type* rttr::instance::try_convert ( ) const
noexcept

This function will try to convert the underlying instance to the given type Target_Type*.

When the conversion succeeds, a valid pointer will be returned. Otherwise a nullptr.

Returns
A pointer to the instance of Target_Type, when the conversion succeeds, otherwise a nullptr.

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