rttr::method Class - 0.9.6 |RTTR
rttr::method Class Reference

The method class provides several meta information about a method and can be invoked. More...

#include <method.h>

Public Member Functions

access_levels get_access_level () const noexcept
 Returns the access level with which this method was registered. More...
 
type get_declaring_type () const noexcept
 Returns the class that declares this method. More...
 
variant get_metadata (const variant &key) const
 Returns the meta data for the given key key. More...
 
string_view get_name () const noexcept
 Returns the name of this method. More...
 
array_range< parameter_infoget_parameter_infos () const noexcept
 Returns an ordered range of parameter_info objects, which matches the signature of the method. More...
 
type get_return_type () const noexcept
 Returns the type object of the return type. More...
 
string_view get_signature () const noexcept
 Returns the signature of this method as readable string. More...
 
variant invoke (instance object) const
 Invokes the method represented by the current instance object. More...
 
variant invoke (instance object, argument arg1) const
 Invokes the method represented by the current instance object, using the specified parameters. More...
 
variant invoke (instance object, argument arg1, argument arg2) const
 Invokes the method represented by the current instance object, using the specified parameters. More...
 
variant invoke (instance object, argument arg1, argument arg2, argument arg3) const
 Invokes the method represented by the current instance object, using the specified parameters. More...
 
variant invoke (instance object, argument arg1, argument arg2, argument arg3, argument arg4) const
 Invokes the method represented by the current instance object, using the specified parameters. More...
 
variant invoke (instance object, argument arg1, argument arg2, argument arg3, argument arg4, argument arg5) const
 Invokes the method represented by the current instance object, using the specified parameters. More...
 
variant invoke (instance object, argument arg1, argument arg2, argument arg3, argument arg4, argument arg5, argument arg6) const
 Invokes the method represented by the current instance object, using the specified parameters. More...
 
variant invoke_variadic (instance object, std::vector< argument > args) const
 Invokes the method represented by the current instance object, using the specified parameters. More...
 
bool is_static () const noexcept
 Returns true if this method is static method, otherwise false. More...
 
bool is_valid () const noexcept
 Returns true if this method is valid, otherwise false. More...
 
 operator bool () const noexcept
 Convenience function to check if this method is valid or not. More...
 
bool operator!= (const method &other) const noexcept
 Returns true if this method is the not the same like the other. More...
 
bool operator== (const method &other) const noexcept
 Returns true if this method is the same like the other. More...
 

Detailed Description

The method class provides several meta information about a method and can be invoked.

A instance of a method class can only be obtained from the type class. See type::get_method() and type::get_methods().

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

Meta Information

A method has a name, a signature, a return type, a list of parameter information as well as attributes that specify its behavior: is_static(). When the method was declared inside a class, then get_declaring_type() can be used to obtain the type of this class.

The method can be invoked with invoke(); When its not a static method you have to provide a valid class instance to invoke the method. This instance can be the raw type on the stack; the current class 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 method 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 method will be successfully invoked when the provided instance can be converted to the declared class type. When the method has parameters defined, then the same number of arguments must be provided and the type itself must 100% match the type of the registered function. An automatically type conversion is not performed.

The return type of invoke() is variant object. This object contains not only the possible return value of a function, it also indicates whether the method was invoked or not. A valid variant object means, that then the method was successfully invoked, otherwise not. When the invoked method has no return type, i.e. is a void method, then a valid variant of type void is returned.

While the invoke() function can directly forward up to six arguments, it is sometime necessary to forward even more arguments. Therefore the function invoke_variadic() should be used; it allows to pack an unlimited amount of arguments into a std::vector and forward them to the function.

Another way to invoke a method is to use the type class through type::invoke().

Copying and Assignment

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

Typical Usage

using namespace rttr;
struct MyStruct { int my_method(int param) { return param; } };
//...
variant obj = type::get_by_name("MyStruct").create({});
method func = obj.get_type().get_method("my_method");
if (func)
{
variant val = func.invoke(obj, 23);
std::cout << val.get_value<int>(); // prints 23
// you can also invoke the method with an object on the stack
MyStruct inst;
val = func.invoke(inst, 42);
std::cout << val.get_value<int>(); // prints 42
// or as pointer
MyStruct* ptr = &inst;
val = func.invoke(ptr, 7);
std::cout << val.get_value<int>(); // prints 7
}
See also
property, enumeration, constructor and type

Member Function Documentation

access_levels rttr::method::get_access_level ( ) const
noexcept

Returns the access level with which this method was registered.

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

Returns the class that declares this method.

Remarks
When this method does not belong to a class (i.e. is a global method) it will return an invalid type. When this method is not valid, this function will return an invalid type object (see type::is_valid).
Returns
Type of the declaring class/struct for this method.
variant rttr::method::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.
string_view rttr::method::get_name ( ) const
noexcept

Returns the name of this method.

Returns
Name of the method.
array_range<parameter_info> rttr::method::get_parameter_infos ( ) const
noexcept

Returns an ordered range of parameter_info objects, which matches the signature of the method.

Returns
A range of parameter_info objects of the method signature.
type rttr::method::get_return_type ( ) const
noexcept

Returns the type object of the return type.

Returns
The type of the return type.
string_view rttr::method::get_signature ( ) const
noexcept

Returns the signature of this method as readable string.

Returns
The signature as readable string.
variant rttr::method::invoke ( instance  object) 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 default ctor of instance().
See also
get_parameter_infos()
Returns
The possible return value of the method.
variant rttr::method::invoke ( instance  object,
argument  arg1 
) const

Invokes the method represented by the current instance object, using the specified parameters.

Remarks
The given argument type has to match exactly the type of the underling method parameter, otherwise the method cannot be invoked and an invalid variant object (see variant::is_valid) will be returned. When it's a static method you still need to provide an instance object, use therefore the default ctor of instance().
Returns
The possible return value of the method.
See also
get_parameter_infos()
variant rttr::method::invoke ( instance  object,
argument  arg1,
argument  arg2 
) const

Invokes the method represented by the current instance object, using the specified parameters.

Remarks
The given argument type has to match exactly the type of the underling method parameter, otherwise the method cannot be invoked and an invalid variant object (see variant::is_valid) will be returned. When it's a static method you still need to provide an instance object, use therefore the default ctor of instance().
See also
get_parameter_infos()
Returns
The possible return value of the method.
variant rttr::method::invoke ( instance  object,
argument  arg1,
argument  arg2,
argument  arg3 
) const

Invokes the method represented by the current instance object, using the specified parameters.

Remarks
The given argument type has to match exactly the type of the underling method parameter, otherwise the method cannot be invoked and an invalid variant object (see variant::is_valid) will be returned. When it's a static method you still need to provide an instance object, use therefore the default ctor of instance().
See also
get_parameter_infos()
Returns
The possible return value of the method.
variant rttr::method::invoke ( instance  object,
argument  arg1,
argument  arg2,
argument  arg3,
argument  arg4 
) const

Invokes the method represented by the current instance object, using the specified parameters.

Remarks
The given argument type has to match exactly the type of the underling method parameter, otherwise the method cannot be invoked and an invalid variant object (see variant::is_valid) will be returned. When it's a static method you still need to provide an instance object, use therefore the default ctor of instance().
See also
get_parameter_infos()
Returns
The possible return value of the method.
variant rttr::method::invoke ( instance  object,
argument  arg1,
argument  arg2,
argument  arg3,
argument  arg4,
argument  arg5 
) const

Invokes the method represented by the current instance object, using the specified parameters.

Remarks
The given argument type has to match exactly the type of the underling method parameter, otherwise the method cannot be invoked and an invalid variant object (see variant::is_valid) will be returned. When it's a static method you still need to provide an instance object, use therefore the default ctor of instance().
See also
get_parameter_infos()
Returns
The possible return value of the method.
variant rttr::method::invoke ( instance  object,
argument  arg1,
argument  arg2,
argument  arg3,
argument  arg4,
argument  arg5,
argument  arg6 
) const

Invokes the method represented by the current instance object, using the specified parameters.

Remarks
The given argument type has to match exactly the type of the underling method parameter, otherwise the method cannot be invoked and an invalid variant object (see variant::is_valid) will be returned. When it's a static method you still need to provide an instance object, use therefore the default ctor of instance().
See also
get_parameter_infos()
Returns
The possible return value of the method.
variant rttr::method::invoke_variadic ( instance  object,
std::vector< argument args 
) const

Invokes the method represented by the current instance object, using the specified parameters.

Use this method when the argument count is higher then six.

Remarks
The given argument type has to match exactly the type of the underling method parameter, otherwise the method cannot be invoked and an invalid variant object (see variant::is_valid) will be returned. When it's a static method you still need to provide an instance object, use therefore the default ctor of instance(). Using this invoke function is slower, then specifying the arguments directly.
See also
get_parameter_infos()
Returns
The possible return value of the method.
bool rttr::method::is_static ( ) const
noexcept

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

A static method does not need an instance for performing an invoke.

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

Returns true if this method is valid, otherwise false.

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

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

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

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

Returns
True if both methods are different, otherwise false.
bool rttr::method::operator== ( const method other) const
noexcept

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

Returns
True if both methods are equal, otherwise false.

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