rttr::wrapper_mapper< T > Struct Template Reference

The wrapper_mapper class is a class template to access different wrapper types via one common interface. More...

#include <wrapper_mapper.h>

Public Types

using type = T
 
using wrapped_type = typename wrapper_type::encapsulated_type
 

Static Public Member Functions

static type create (const wrapped_type &value)
 
static wrapped_type get (const type &obj)
 

Detailed Description

template<typename T>
struct rttr::wrapper_mapper< T >

The wrapper_mapper class is a class template to access different wrapper types via one common interface.

A wrapper type is a class which encapsulate an instance of another type. This are for instance smart pointer classes, e.g. std::shared_ptr<T> or std::unique_ptr<T>. Out of the box, RTTR recognize following wrapper types:

  • std::shared_ptr<T>
  • std::reference_wrapper<T>
  • std::weak_ptr<T>
  • std::unique_ptr<T>

Custom wrapper types

In order to work with custom wrapper types, its required to specialize the class wrapper_mapper<T>. Therefore you have to provide two nested type aliases:

  1. using wrapped_type = typename T::encapsulated_type;
  2. using type = T

And two functions:

  1. static wrapped_type get(const T& obj);
  2. static T create(wrapped_type& obj);

Following code example illustrates how to add a specialization:

// custom_type.h
template<typename T>
class custom_type
{
public:
custom_type(T& obj) : m_data(std::addressof(obj)) {}
T& get_data() { return *m_value; }
private:
T* m_data;
};
// the best place for the specialization, is in the same header file like the type itself
namespace rttr
{
template<typename T>
struct wrapper_mapper<custom_type<T>>
{
using wrapped_type = decltype(std::declval<custom_type<T>>().get_data());
using type = custom_type<T>;
inline static wrapped_type get(const type& obj)
{
return obj.get_data();
}
inline static type create(const wrapped_type& value)
{
return custom_type<T>(value);
}
};
} // end namespace rttr
Remarks
It is very important that the type alias for wrapped_type is the actual return type of the getter function. Make also sure you put your specialization inside the namespace rttr. The best place for this code, is below the declaration of your wrapper type. When this is not possible, include your specialization code before registering your types to RTTR.

Member Typedef Documentation

template<typename T >
using rttr::wrapper_mapper< T >::type = T
template<typename T >
using rttr::wrapper_mapper< T >::wrapped_type = typename wrapper_type::encapsulated_type

Member Function Documentation

template<typename T >
static type rttr::wrapper_mapper< T >::create ( const wrapped_type value)
inlinestatic
template<typename T >
static wrapped_type rttr::wrapper_mapper< T >::get ( const type obj)
inlinestatic

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