4.6.0
Freundlich's C++ toolkit
Loading...
Searching...
No Matches
fcppt.type_iso

Description

Conversions between isomorphic types.

Motivation

Types can be decorated to constrain their use in a program, for example, strong typedefs, boost units and so on. This is mainly done to prevent accidental usage of their values. Sometimes, however, it is necessary to undecorate them in a generic way. Consider a range of strong typedefs of integers. To compute the size of such a range (for example as a std::size_t), the strong typedefs have to be undecorated first. Because the integer range is a generic class, it needs to know how to do this for every type.

Design

A type T can be decorated by a type constructor D to yield D<T>. An isomorphism between T and D<T> is a pair of functions:

such that their compositions are the identity. How they are implemented exactly can vary for every D significantly. In fcppt, these two functions are implemented as a specialization of fcppt::type_iso::transform. A specialization needs to contain the two aforementioned functions along with the typedefs decorated_type and undecorated_type.

In general, multiple type constructors can be applied one by one to yield: D_1<...D_n<T>...>. The lifted functions that decorate and undecorate between this type and T are called fcppt::type_iso::decorate and fcppt::type_iso::undecorate.

Example

The first example shows how the decorate and undecorate functions can be used:

#include <fcppt/declare_strong_typedef.hpp>
#include <fcppt/strong_typedef.hpp> // NOLINT(misc-include-cleaner)
#include <fcppt/type_iso/decorate.hpp>
#include <fcppt/type_iso/strong_typedef.hpp> // NOLINT(misc-include-cleaner)
#include <fcppt/type_iso/undecorate.hpp>
FCPPT_DECLARE_STRONG_TYPEDEF(int, strong_int);
std::cout << result << '\n';

The next example shows how to implement a type decoration:

namespace mine
{
class custom_int
{
// This class has a private constructor ...
private:
explicit custom_int(int const _val) : val_{_val} {}
public:
// ... and a public factory function
static custom_int make(int const _val) { return custom_int(_val); }
[[nodiscard]] int get() const { return val_; }
private:
int val_;
};
}
namespace fcppt::type_iso
{
template <>
struct transform<mine::custom_int>
{
using undecorated_type = int;
using decorated_type = mine::custom_int;
static decorated_type decorate(undecorated_type const _val) { return decorated_type::make(_val); }
static undecorated_type undecorate(decorated_type const _val) { return _val.get(); }
};
}

Header files

Header file Description
decorate.hpp Contains fcppt::type_iso::decorate.
undecorate.hpp Contains fcppt::type_iso::undecorate.
undecorated_type.hpp Contains the fcppt::type_iso::undecorated_type typedef.
transform_fwd.hpp Contains the forward declaration of fcppt::type_iso::transform.
strong_typedef.hpp Bindings for fcppt.strong_typedef.
boost_units.hpp Bindings for Boost.Units.
enum.hpp Bindings for fcppt.enum.

Classes

struct  fcppt::type_iso::transform< Type, Enable >
 Customization point for decorate/undecorate. More...
 

Typedefs

template<typename Type >
using fcppt::type_iso::undecorated_type = typename fcppt::type_iso::detail::undecorated_type<Type>::type
 The undecorated type.
 

Functions

template<typename Result >
Result fcppt::type_iso::decorate (fcppt::type_iso::undecorated_type< Result > const &_value)
 Decorates a value.
 
template<typename Type >
fcppt::type_iso::undecorated_type< Type > fcppt::type_iso::undecorate (Type const &_value)
 Undecorates a value.
 

Typedef Documentation

◆ undecorated_type

template<typename Type >
using fcppt::type_iso::undecorated_type = typename fcppt::type_iso::detail::undecorated_type<Type>::type

The undecorated type.

Undecorates Type by removing all (nested) type constructors.

Function Documentation

◆ decorate()

template<typename Result >
Result fcppt::type_iso::decorate ( fcppt::type_iso::undecorated_type< Result > const & _value)
inline

Decorates a value.

Decorates _value by applying all (nested) type constructors of Result.

◆ undecorate()

template<typename Type >
fcppt::type_iso::undecorated_type< Type > fcppt::type_iso::undecorate ( Type const & _value)
inline

Undecorates a value.

Undecorates _value by removing all (nested) type constructors.