rttr::policy::meth Struct Reference

The meth class groups all policies that can be used during registration of methods. More...

#include <policy.h>

Static Public Attributes

static const detail::discard_return discard_return
 This policy should be used when the return value of a method should not be forwarded to the caller. More...
 
static const detail::return_as_ptr return_ref_as_ptr
 This policy can be used when a method return a reference to an object and the caller should be able to access this object via the returned variant. More...
 

Detailed Description

The meth class groups all policies that can be used during registration of methods.

Member Data Documentation

◆ discard_return

const detail::discard_return rttr::policy::meth::discard_return
static

This policy should be used when the return value of a method should not be forwarded to the caller.

For the caller it looks like the method has no return value, the return type will be void.

See following example code:

using namespace rttr;
int my_func() { return 42; }
{
registration::method("my_func", &my_func)
(
);
}
int main()
{
method meth = type::get_global_method("my_func");
std::cout << meth.get_return_type().get_name(); // prints "void"
variant var = meth.invoke(instance());
std::cout << var.is_type<void>(); // prints "true"
return 0;
}

◆ return_ref_as_ptr

const detail::return_as_ptr rttr::policy::meth::return_ref_as_ptr
static

This policy can be used when a method return a reference to an object and the caller should be able to access this object via the returned variant.

Reference cannot be copied directly in a variant, therefore it is possible transform the reference to a pointer.

See following example code:

std::string& get_text() { static std:string text("hello world"); return text; }
{
registration::method("get_text", &get_text)
(
);
}
int main()
{
method meth = type::get_global_method("get_text");
std::cout << meth.get_return_type().get_name(); // prints "std::string*"
variant var = meth.invoke(instance());
std::cout << var.is_type<std::string*>(); // prints "true"
return 0;
}

The documentation for this struct was generated from the following file:
static const detail::discard_return discard_return
This policy should be used when the return value of a method should not be forwarded to the caller.
Definition: policy.h:191
bool is_type() const
Returns true if the containing variant data is of the given template type T.
static method get_global_method(string_view name) noexcept
Returns a global method with the name name.
Definition: access_levels.h:33
variant invoke(instance object) const
Invokes the method represented by the current instance object.
static bind< detail::meth, detail::invalid_type, F, detail::public_access > method(string_view name, F f)
Register a method to this class.
static const detail::return_as_ptr return_ref_as_ptr
This policy can be used when a method return a reference to an object and the caller should be able t...
Definition: policy.h:161
#define RTTR_REGISTRATION
Use this macro to automatically register your reflection information to RTTR before main is called.
Definition: registration.h:770
type get_return_type() const noexcept
Returns the type object of the return type.
string_view get_name() const noexcept
Returns the unique and human-readable name of the type.
The method class provides several meta information about a method and can be invoked.
Definition: method.h:145
The instance class is used for forwarding the instance of an object to invoke a property or method.
Definition: instance.h:72
The variant class allows to store data of any type and convert between these types transparently.
Definition: variant.h:222