rttr  0.9.0
 All Classes Namespaces Files Functions Variables Typedefs Friends Macros Pages
Public Member Functions | List of all members
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

type get_declaring_type () const
 Returns the class that declares this method. More...
 
variant get_metadata (int key) const
 Returns the metadata for the given key key. More...
 
variant get_metadata (const std::string &key) const
 Returns the metadata for the given key key. More...
 
std::string get_name () const
 Returns the name of this method. More...
 
std::vector< typeget_parameter_types () const
 Returns an ordered list of type objects which represents the parameters of this method. More...
 
type get_return_type () const
 Returns the type object of the return type. More...
 
std::string get_signature () const
 Returns the signature of this method as readable string. More...
 
variant invoke (detail::instance object) const
 Invokes the method represented by the current instance object. More...
 
variant invoke (detail::instance object, detail::argument arg1) const
 Invokes the method represented by the current instance object, using the specified parameters. More...
 
variant invoke (detail::instance object, detail::argument arg1, detail::argument arg2) const
 Invokes the method represented by the current instance object, using the specified parameters. More...
 
variant invoke (detail::instance object, detail::argument arg1, detail::argument arg2, detail::argument arg3) const
 Invokes the method represented by the current instance object, using the specified parameters. More...
 
variant invoke (detail::instance object, detail::argument arg1, detail::argument arg2, detail::argument arg3, detail::argument arg4) const
 Invokes the method represented by the current instance object, using the specified parameters. More...
 
variant invoke (detail::instance object, detail::argument arg1, detail::argument arg2, detail::argument arg3, detail::argument arg4, detail::argument arg5) const
 Invokes the method represented by the current instance object, using the specified parameters. More...
 
variant invoke (detail::instance object, detail::argument arg1, detail::argument arg2, detail::argument arg3, detail::argument arg4, detail::argument arg5, detail::argument arg6) const
 Invokes the method represented by the current instance object, using the specified parameters. More...
 
variant invoke_variadic (detail::instance object, std::vector< detail::argument > args) const
 Invokes the method represented by the current instance object, using the specified parameters. More...
 
bool is_static () const
 Returns true if this method is static method, otherwise false. More...
 
bool is_valid () const
 Returns true if this method is valid, otherwise false. More...
 
 operator bool () const
 Convenience function to check if this method is valid or not. More...
 
bool operator!= (const method &other) const
 Returns true if this method is the not the same like the other. More...
 
bool operator== (const method &other) const
 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 class_::method() and for global methods see register_global::method.

Meta Information

A method has a name, a signature, a return type, a list of parameter types 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 a dummy instance object, therefore the function rttr::empty_instance() should be used.

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 invoke_method().

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("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

type rttr::method::get_declaring_type ( ) const

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 underlying property.
variant rttr::method::get_metadata ( int  key) const

Returns the metadata for the given key key.

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

Returns the metadata for the given key key.

Remarks
When no metadata 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::method::get_name ( ) const

Returns the name of this method.

Returns
Name of the method.
std::vector<type> rttr::method::get_parameter_types ( ) const

Returns an ordered list of type objects which represents the parameters of this method.

Returns
A list of type objects from the arguments.
type rttr::method::get_return_type ( ) const

Returns the type object of the return type.

Returns
The type of the return type.
std::string rttr::method::get_signature ( ) const

Returns the signature of this method as readable string.

Returns
The signature as readable string.
variant rttr::method::invoke ( detail::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 function empty_instance().
Returns
The type of the return type.
variant rttr::method::invoke ( detail::instance  object,
detail::argument  arg1 
) const

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

Remarks
When it's a static method you still need to provide an instance object, use therefore the function empty_instance().
Returns
The type of the return type.
variant rttr::method::invoke ( detail::instance  object,
detail::argument  arg1,
detail::argument  arg2 
) const

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

Remarks
When it's a static method you still need to provide an instance object, use therefore the function empty_instance().
Returns
The type of the return type.
variant rttr::method::invoke ( detail::instance  object,
detail::argument  arg1,
detail::argument  arg2,
detail::argument  arg3 
) const

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

Remarks
When it's a static method you still need to provide an instance object, use therefore the function empty_instance().
Returns
The type of the return type.
variant rttr::method::invoke ( detail::instance  object,
detail::argument  arg1,
detail::argument  arg2,
detail::argument  arg3,
detail::argument  arg4 
) const

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

Remarks
When it's a static method you still need to provide an instance object, use therefore the function empty_instance().
Returns
The type of the return type.
variant rttr::method::invoke ( detail::instance  object,
detail::argument  arg1,
detail::argument  arg2,
detail::argument  arg3,
detail::argument  arg4,
detail::argument  arg5 
) const

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

Remarks
When it's a static method you still need to provide an instance object, use therefore the function empty_instance().
Returns
The type of the return type.
variant rttr::method::invoke ( detail::instance  object,
detail::argument  arg1,
detail::argument  arg2,
detail::argument  arg3,
detail::argument  arg4,
detail::argument  arg5,
detail::argument  arg6 
) const

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

Remarks
When it's a static method you still need to provide an instance object, use therefore the function empty_instance().
Returns
The type of the return type.
variant rttr::method::invoke_variadic ( detail::instance  object,
std::vector< detail::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
Using this invoke function is slower, then specifying the arguments directly. When it's a static method you still need to provide an instance object, use therefore the function empty_instance().
Returns
The type of the return type.
bool rttr::method::is_static ( ) const

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

Returns true if this method is valid, otherwise false.

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

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

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

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: