rttr::constructor Class Reference

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

#include <constructor.h>

Public Member Functions

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

Detailed Description

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

A instance of a constructor class can only be obtained from the type class. See type::get_constructor() and type::get_constructors().

For registration a constructor of a class, see registration::class_<T>::constructor().

Meta Information

A constructor has a signature (get_signature()) and instantiate exactly one type (get_instantiated_type()). With get_parameter_infos() you retrieve all information of the parameters for this constructor. When the constructor was declared inside a class, then get_declaring_type() can be used to obtain the type of this class.

The constructor can be invoked with invoke() or via the type class through type::create(). The created object will be copied into a variant and returned. Depending on the used policy during the registration process, it can be an object with automatic or dynamic storage.

Copying and Assignment

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

Typical Usage

constructor ctor = type::get_by_name("std::string").get_constructor({type::get<std::string>()}); // retrieve the copy ctor
variant var = ctor.invoke(std::string("Hello World")); // returns an object with automatic storage
std::cout << var.get_value<std::string>().c_str() << std::endl; // prints 'Hello World'
See also
method, property, enumeration, destructor and type

Member Function Documentation

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

Returns access level with which this constructor was registered.

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

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

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

Returns the type object of the instantiated type.

Returns
The instantiated type.
variant rttr::constructor::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.
array_range<parameter_info> rttr::constructor::get_parameter_infos ( ) const
noexcept

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

Returns
A range of parameter_info objects of the constructor signature.
string_view rttr::constructor::get_signature ( ) const
noexcept

Returns the signature of this constructor as readable string.

Returns
The signature as readable string.
variant rttr::constructor::invoke ( ) const

Invokes the constructor of type returned by get_instantiated_type().

The instance will always be created on the heap and will be returned as variant object.

Remarks
Returns an invalid variant object (see variant::is_valid), when the arguments does not match the parameters of the underlying constructor.
See also
get_parameter_infos()
Returns
An instance of the type get_instantiated_type().
variant rttr::constructor::invoke ( argument  arg1) const

Invokes the constructor of type returned by get_instantiated_type().

The instance will always be created on the heap and will be returned as variant object.

Remarks
The given argument type has to match exactly the type of the underling constructor parameter, otherwise the constructor cannot be invoked and an invalid variant object (see variant::is_valid) will be returned.
See also
get_parameter_infos()
Returns
An instance of the type get_instantiated_type().
variant rttr::constructor::invoke ( argument  arg1,
argument  arg2 
) const

Invokes the constructor of type returned by get_instantiated_type().

The instance will always be created on the heap and will be returned as variant object.

Remarks
The given argument type has to match exactly the type of the underling constructor parameter, otherwise the constructor cannot be invoked and an invalid variant object (see variant::is_valid) will be returned.
See also
get_parameter_infos()
Returns
An instance of the type get_instantiated_type().
variant rttr::constructor::invoke ( argument  arg1,
argument  arg2,
argument  arg3 
) const

Invokes the constructor of type returned by get_instantiated_type().

The instance will always be created on the heap and will be returned as variant object.

Remarks
The given argument type has to match exactly the type of the underling constructor parameter, otherwise the constructor cannot be invoked and an invalid variant object (see variant::is_valid) will be returned.
See also
get_parameter_infos()
Returns
An instance of the type get_instantiated_type().
variant rttr::constructor::invoke ( argument  arg1,
argument  arg2,
argument  arg3,
argument  arg4 
) const

Invokes the constructor of type returned by get_instantiated_type().

The instance will always be created on the heap and will be returned as variant object.

Remarks
The given argument type has to match exactly the type of the underling constructor parameter, otherwise the constructor cannot be invoked and an invalid variant object (see variant::is_valid) will be returned.
See also
get_parameter_infos()
Returns
An instance of the type get_instantiated_type().
variant rttr::constructor::invoke ( argument  arg1,
argument  arg2,
argument  arg3,
argument  arg4,
argument  arg5 
) const

Invokes the constructor of type returned by get_instantiated_type().

The instance will always be created on the heap and will be returned as variant object.

Remarks
The given argument type has to match exactly the type of the underling constructor parameter, otherwise the constructor cannot be invoked and an invalid variant object (see variant::is_valid) will be returned.
See also
get_parameter_infos()
Returns
An instance of the type get_instantiated_type().
variant rttr::constructor::invoke ( argument  arg1,
argument  arg2,
argument  arg3,
argument  arg4,
argument  arg5,
argument  arg6 
) const

Invokes the constructor of type returned by get_instantiated_type().

The instance will always be created on the heap and will be returned as variant object.

Remarks
The given argument type has to match exactly the type of the underling constructor parameter, otherwise the constructor cannot be invoked and an invalid variant object (see variant::is_valid) will be returned.
See also
get_parameter_infos()
Returns
An instance of the type get_instantiated_type().
variant rttr::constructor::invoke_variadic ( std::vector< argument args) const

Invokes the constructor of type returned by get_instantiated_type().

The instance will always be created on the heap and will be returned as variant object. Use this method when you need to instantiate a constructor with more then 6 parameters.

Remarks
The given argument type has to match exactly the type of the underling constructor parameter, otherwise the constructor cannot be invoked and an invalid variant object (see variant::is_valid) will be returned. Using this invoke function is slower, then specifying the arguments directly.
See also
get_parameter_infos()
Returns
An instance of the type get_instantiated_type().
bool rttr::constructor::is_valid ( ) const
noexcept

Returns true if this constructor is valid, otherwise false.

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

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

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

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

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

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

Returns
True if both constructors are equal, otherwise false.

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