3.6.0
Freundlich's C++ toolkit
Classes | Typedefs | Functions
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/make_strong_typedef.hpp>
#include <fcppt/strong_typedef.hpp>
#include <fcppt/type_iso/decorate.hpp>
#include <fcppt/type_iso/strong_typedef.hpp>
#include <fcppt/type_iso/undecorate.hpp>
int,
strong_int
);
auto const var(
fcppt::type_iso::decorate<strong_int>(42)
);
int const result(
);
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
>
{
int;
using decorated_type =
mine::custom_int;
static decorated_type
{
return
}
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. More...
 

Functions

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

Typedef Documentation

◆ undecorated_type

template<typename Type >
using fcppt::type_iso::undecorated_type = typedef 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.

fcppt::options::result
fcppt::either::object< fcppt::options::error, T > result
The result of a parse operation.
Definition: result_fwd.hpp:35
fcppt::type_iso::decorate
Result decorate(fcppt::type_iso::undecorated_type< Result > const &_value)
Decorates a value.
Definition: decorate.hpp:31
FCPPT_MAKE_STRONG_TYPEDEF
#define FCPPT_MAKE_STRONG_TYPEDEF(type, name)
Creates a new strong typedef using strong_typedef.
Definition: make_strong_typedef.hpp:21
fcppt::type_iso
Conversions between isomorphic types.
Definition: decorate.hpp:16
fcppt::type_iso::undecorate
fcppt::type_iso::undecorated_type< Type > undecorate(Type const &_value)
Undecorates a value.
Definition: undecorate.hpp:33
fcppt::container::array::make
std::array< ::metal::front< ::metal::list< fcppt::type_traits::remove_cv_ref_t< Args >... > >, sizeof...(Args)> make(Args &&... _args)
Make an array out of a parameter pack.
Definition: make.hpp:52
fcppt::type_iso::undecorated_type
typename fcppt::type_iso::detail::undecorated_type< Type >::type undecorated_type
The undecorated type.
Definition: undecorated_type.hpp:33
fcppt::io::cout
FCPPT_DETAIL_SYMBOL fcppt::io::ostream & cout()
Returns either std::cout or std::wcout, depending on fcppt::char_type.
fcppt::io::get
fcppt::optional::object< Ch > get(std::basic_istream< Ch, Traits > &_stream)
Gets a character from a stream.
Definition: get.hpp:35