UgMisc 0.3
Miscellaneous C++ header library
Loading...
Searching...
No Matches
typelist.hpp File Reference

Lists of types which may be used in some of the ugmisc templates where a single type would usually be used. More...

#include <cstddef>
#include <limits>
#include <type_traits>
#include <utility>

Go to the source code of this file.

Classes

struct  ugmisc::is_type_list< T >
struct  ugmisc::wrapped_list< T >
struct  ugmisc::get_as_list< T >
struct  ugmisc::flatten< T >
struct  ugmisc::type_list< T >
 Encapsulates a list of types. More...

Typedefs

template<class... T>
using ugmisc::concatenate_type_lists_t = typename concatenate_type_lists<T...>::type
template<class T>
using ugmisc::get_as_list_t = typename get_as_list<T>::type
template<std::size_t I, class T>
using ugmisc::type_list_member
template<class T>
using ugmisc::type_list_from_tparams
template<class... T>
using ugmisc::flatten_t = typename flatten<T...>::type
 Converts the template parameters into a type_list.
template<std::size_t From, std::size_t To, class T>
using ugmisc::type_list_sublist = typename get_type_list_sublist<From, To, T>::type
template<std::size_t From, std::size_t Count, class T>
using ugmisc::type_list_sized_sublist = type_list_sublist<From, From + Count, T>
template<std::size_t Count, class T>
using ugmisc::type_list_prefix = type_list_sublist<0, Count, T>
template<std::size_t Count, class T>
using ugmisc::type_list_suffix = type_list_sublist<T::size - Count, T::size, T>
template<std::size_t Count, class T>
using ugmisc::type_list_remove_prefix = type_list_sublist<Count, T::size, T>
template<std::size_t Count, class T>
using ugmisc::type_list_remove_suffix = type_list_sublist<0, T::size-Count, T>
template<std::size_t From, std::size_t To, class... T>
using ugmisc::flatten_sublist = type_list_sublist<From, To, flatten_t<T...>>
template<std::size_t From, std::size_t Count, class... T>
using ugmisc::flatten_sized_sublist = type_list_sized_sublist<From, Count, flatten_t<T...>>
template<std::size_t Count, class... T>
using ugmisc::flatten_prefix = type_list_prefix<Count, flatten_t<T...>>
template<std::size_t Count, class... T>
using ugmisc::flatten_suffix = type_list_suffix<Count, flatten_t<T...>>
template<std::size_t Count, class... T>
using ugmisc::flatten_remove_prefix = type_list_remove_prefix<Count, flatten_t<T...>>
template<std::size_t Count, class... T>
using ugmisc::flatten_remove_suffix = type_list_remove_suffix<Count, flatten_t<T...>>
template<template< class > class Test, class T, test_modifier Mod = no_invert_test>
using ugmisc::type_list_filter
template<template< class > class Test, class T, test_modifier Mod = static_cast<test_modifier>(0)>
using ugmisc::filter_type_list = type_list_filter<Test, T, Mod>
template<template< class > class F, class T>
using ugmisc::type_list_apply_each_class = typename typelist_::apply_each<F, T>::class_types
template<template< class > class F, class T>
using ugmisc::type_list_apply_each = typename typelist_::apply_each<F, T>::inner_types
template<template< class > class F, class... T>
using ugmisc::flatten_apply_each_class = type_list_apply_each_class<F, flatten_t<T...>>
template<template< class > class F, class... T>
using ugmisc::flatten_apply_each = type_list_apply_each<F, flatten_t<T...>>
template<template< class > class Test, test_modifier Mod, class... T>
using ugmisc::flatten_filter = type_list_filter<Test, flatten_t<T...>, Mod>
template<std::ptrdiff_t Base, std::ptrdiff_t Stride, std::size_t Count, class T>
using ugmisc::type_list_stride
template<std::ptrdiff_t Base, std::ptrdiff_t Stride, std::size_t Count, class... T>
using ugmisc::flatten_stride = type_list_stride<Base, Stride, Count, flatten_t<T...>>

Enumerations

enum  ugmisc::test_modifier { ugmisc::no_invert_test , ugmisc::invert_test }

Functions

template<class T, class U>
constexpr auto ugmisc::operator+ (T, U) -> concatenate_type_lists_t< T, U >
template<class T, class U>
constexpr auto ugmisc::operator== (T, U) -> std::enable_if_t< is_type_list_v< T > &&is_type_list_v< U >, bool >
template<class T, class U>
constexpr auto ugmisc::operator!= (T, U) -> std::enable_if_t< is_type_list_v< T > &&is_type_list_v< U >, bool >

Detailed Description

Lists of types which may be used in some of the ugmisc templates where a single type would usually be used.

Note
The sample program typelist demonstrates the use of this header.

Why?

The member.hpp header finds this stuff useful. You might too.

List of types can be manipulated easily, as if they are lists of objects.

Operations

Some operations are templates which come in two variants.

  • flatten_ prefixed template type aliases
  • type_list_ prefixed template type aliases

The flatten_ variants are more flexible, and will always work as a drop in replacement for their type_list_ twins. That might not be what you want though. If you think you have an instance of the type_list<> template to pass to e.g. flatten_filter then using type_list_filter is effectively like doing the same thing but also making a static assertion that it is indeed a type_list.

Operations include adding and removing prefixes and suffixes or taking sublists from type_lists, filtering out some of the members of a type_list, and concatenating two or more type_lists.

Methods

An instance of the ugmisc::type_list template is an empty type with trivial constructors and destructor. It is possible to use objects of these types to perform operations, then take the type of the resulting object.

// A, B, C are types.
constexpr type_list<A, B> ab;
constexpr type_list<A, B> ab2;
constexpr type_list<A, B, C> abc;
constexpr type_list<C, B, A> cba;
constexpr type_list<B, C> bc;
constexpr type_list<A, B, B, C> abbc;
constexpr type_list<B, B> bb;
static_assert(ab == ab2); // Equality of objects is based only on type.
static_assert(ab != abc);
static_assert(ab + bc == abbc);
static_assert(ab.append(bc) == abbc);
static_assert(bc.prepend(ab) == abbc);
constexpr int fromIdx = 1;
constexpr int toIdx = 3;
std::integer_sequence<int, fromIdx, toIdx> middle; // Params for middle slice.
std::integral_constant<int, fromIdx> from; // Same params again.
std::integral_constant<int, toIdx> to;
static_assert(abbc.sublist(middle) == bb);
static_assert(abbc.sublist(from, to) == bb);
std::integer_sequence<int, 2, -1, 3> reverse3;
static_assert(abc.stride(reverse3) == cba);

Definition in file typelist.hpp.

Typedef Documentation

◆ concatenate_type_lists_t

template<class... T>
using ugmisc::concatenate_type_lists_t = typename concatenate_type_lists<T...>::type

Aliases a type which is a type_list containing all the member types of the template paramter type_list types.

For example:

#include <type_traits>
using namespace ugmisc;
using B = type_list<>; // Empty list.
static_assert( std::is_same_v< all, type_list<int, float, char, int, int> > );
Encapsulates a list of types.
Definition typelist.hpp:860
Lists of types which may be used in some of the ugmisc templates where a single type would usually be...
typename concatenate_type_lists< T... >::type concatenate_type_lists_t
Definition typelist.hpp:173
Template Parameters
TEach type is an instance of type_list.

Definition at line 173 of file typelist.hpp.

◆ filter_type_list

template<template< class > class Test, class T, test_modifier Mod = static_cast<test_modifier>(0)>
using ugmisc::filter_type_list = type_list_filter<Test, T, Mod>
Deprecated
This is an alias of the preferred type_list_filter.

Definition at line 378 of file typelist.hpp.

◆ flatten_apply_each

template<template< class > class F, class... T>
using ugmisc::flatten_apply_each = type_list_apply_each<F, flatten_t<T...>>

Like type_list_apply_each, but creates a type list using flatten_t<T...>.

See also
flatten_apply_each_class, type_list_apply_each

Definition at line 421 of file typelist.hpp.

◆ flatten_apply_each_class

template<template< class > class F, class... T>
using ugmisc::flatten_apply_each_class = type_list_apply_each_class<F, flatten_t<T...>>

Like type_list_apply_each_class, but creates a type list using flatten_t<T...>.

See also
flatten_apply_each, type_list_apply_each_class

Definition at line 411 of file typelist.hpp.

◆ flatten_filter

template<template< class > class Test, test_modifier Mod, class... T>
using ugmisc::flatten_filter = type_list_filter<Test, flatten_t<T...>, Mod>
See also
type_list_filter

Definition at line 432 of file typelist.hpp.

◆ flatten_prefix

template<std::size_t Count, class... T>
using ugmisc::flatten_prefix = type_list_prefix<Count, flatten_t<T...>>
See also
ugmisc::flatten_sublist, ugmisc::type_list_prefix.

Definition at line 317 of file typelist.hpp.

◆ flatten_remove_prefix

template<std::size_t Count, class... T>
using ugmisc::flatten_remove_prefix = type_list_remove_prefix<Count, flatten_t<T...>>

Aliases an ugmisc::type_list with the first Count members of the parameter types T removed.

See also
ugmisc::flatten_sublist, ugmisc::type_list_remove_prefix.

Definition at line 332 of file typelist.hpp.

◆ flatten_remove_suffix

template<std::size_t Count, class... T>
using ugmisc::flatten_remove_suffix = type_list_remove_suffix<Count, flatten_t<T...>>

Aliases an ugmisc::type_list with the last Count members of the template parameter types T removed.

See also
ugmisc::flatten_sublist, ugmisc::type_list_remove_suffix.

Definition at line 341 of file typelist.hpp.

◆ flatten_sized_sublist

template<std::size_t From, std::size_t Count, class... T>
using ugmisc::flatten_sized_sublist = type_list_sized_sublist<From, Count, flatten_t<T...>>

◆ flatten_stride

template<std::ptrdiff_t Base, std::ptrdiff_t Stride, std::size_t Count, class... T>
using ugmisc::flatten_stride = type_list_stride<Base, Stride, Count, flatten_t<T...>>

Like type_list_stride, but passes flatten_t<T...> as the type_list template parameter.

Definition at line 507 of file typelist.hpp.

◆ flatten_sublist

template<std::size_t From, std::size_t To, class... T>
using ugmisc::flatten_sublist = type_list_sublist<From, To, flatten_t<T...>>

Creates an ugmisc::type_alias. Uses the From'th element of the list of types T, and all types up to but not including the To'th element.

See also
ugmisc::type_list_sublist.

Definition at line 305 of file typelist.hpp.

◆ flatten_suffix

template<std::size_t Count, class... T>
using ugmisc::flatten_suffix = type_list_suffix<Count, flatten_t<T...>>
See also
ugmisc::flatten_sublist, ugmisc::type_list_suffix.

Definition at line 323 of file typelist.hpp.

◆ flatten_t

template<class... T>
using ugmisc::flatten_t = typename flatten<T...>::type

Converts the template parameters into a type_list.

Every parameter which is itself a type_list is unrolled. Every wrapped_list is unwrapped (but not recursively).

So for example:

flatten_t< typelist<A, B>, C, wrapped_list<typelist<D, E, F> >

is the type:

typelist<A, B, C, typelist<D, E, F> >

The implementation uses some single-arg template specializations to distinguish between template args which are type_list, wrapped_list, or neither. Then the non-specialised flatten concatenates the types produced by those.

A wrapped_list should be unwrapped once, whereas a type_list should be unwrapped recursively.

Definition at line 239 of file typelist.hpp.

◆ get_as_list_t

template<class T>
using ugmisc::get_as_list_t = typename get_as_list<T>::type
See also
get_as_list

Definition at line 177 of file typelist.hpp.

◆ type_list_apply_each

template<template< class > class F, class T>
using ugmisc::type_list_apply_each = typename typelist_::apply_each<F, T>::inner_types

Given a type T which is an instance of type_list type_list<U...>, this template type alias is a type type_list<typename F<U>::type...>.

See also
type_list_apply_each_class, flatten_apply_each

Definition at line 400 of file typelist.hpp.

◆ type_list_apply_each_class

template<template< class > class F, class T>
using ugmisc::type_list_apply_each_class = typename typelist_::apply_each<F, T>::class_types

Given a type T which is an instance of type_list type_list<U...>, this template type alias is a type type_list<F<U>...>.

See also
type_list_apply_each, flatten_apply_each_class

Definition at line 390 of file typelist.hpp.

◆ type_list_filter

template<template< class > class Test, class T, test_modifier Mod = no_invert_test>
using ugmisc::type_list_filter
Initial value:
typename typelist_::filter_type_list<Test, T, Mod>::type

Aliases a type_list instance only containing those member types which pass a test.

Template Parameters
TA type_list.
TestA template, such that Test<T>::value exists and can be implicitly converted to bool. If the value is true, T is included in the aliased type_list.
ModSet this to ugmisc::invert_test to reverse the result of each application of the test. This means that if Test<T>::value is false the type T is included in the result.

Definition at line 365 of file typelist.hpp.

◆ type_list_from_tparams

template<class T>
using ugmisc::type_list_from_tparams
Initial value:
typename typelist_::get_type_list_from_tparams<T>::type

Given a template F, and types U..., if T is F<U...>, this aliases ugmisc::type_list<U...>.

Given a template F, type I, and values V of type I, if T is F<I, V...>, this aliases ugmisc::type_list<std::integral_constant<I, V>...>.

Otherwise, the alias is not valid.

Definition at line 211 of file typelist.hpp.

◆ type_list_member

template<std::size_t I, class T>
using ugmisc::type_list_member
Initial value:
typename typelist_::get_type_list_member<I, T>::type

The type at an index of a typelist.

Template Parameters
IThe zero based index.
TAn instance of the type_list class template.

Definition at line 195 of file typelist.hpp.

◆ type_list_prefix

template<std::size_t Count, class T>
using ugmisc::type_list_prefix = type_list_sublist<0, Count, T>

Aliases an ugmisc::type_list with the first Count members of the type_list template parameter T.

See also
ugmisc::type_list_sublist, ugmisc::type_list_suffix, ugmisc::flatten_prefix.

Definition at line 268 of file typelist.hpp.

◆ type_list_remove_prefix

template<std::size_t Count, class T>
using ugmisc::type_list_remove_prefix = type_list_sublist<Count, T::size, T>

Aliases an ugmisc::type_list with the first Count members of the type_list template parameter T removed.

See also
ugmisc::type_list_sublist, ugmisc::flatten_remove_prefix.

Definition at line 286 of file typelist.hpp.

◆ type_list_remove_suffix

template<std::size_t Count, class T>
using ugmisc::type_list_remove_suffix = type_list_sublist<0, T::size-Count, T>

Aliases an ugmisc::type_list with the last Count members of the type_list template parameter T removed.

See also
ugmisc::type_list_sublist, ugmisc::flatten_remove_suffix.

Definition at line 295 of file typelist.hpp.

◆ type_list_sized_sublist

template<std::size_t From, std::size_t Count, class T>
using ugmisc::type_list_sized_sublist = type_list_sublist<From, From + Count, T>

◆ type_list_stride

template<std::ptrdiff_t Base, std::ptrdiff_t Stride, std::size_t Count, class T>
using ugmisc::type_list_stride
Initial value:
typename typelist_::type_list_stride<Base, Stride, Count, T>::type

Creates a type list by starting from any part of the type list T and taking steps of zero or more elements.

Template Parameters
BaseThe index in T which is used as the first element of the new type_list.
StrideEach new element of the new type list is taken by taking Stride steps along the elements of T. Use a negative value to step backward.
CountThe number of elements in the new type list.
TAn instance of the type_list template.

For example:

#include <type_traits>
using namespace ugmisc;
// define some types here
// A, B ...
// Start from index 1 (type B).
// Stride of 2.
// Take 2 elements.
static_assert( std::is_same_v< t2, type_list<B, D> > );
// Use a stride of 0 to get the same element multiple times.
static_assert( std::is_same_v< t3, type_list<D, D, D, D, D> );
// Go backwards.
using t4 = type_list_stride< 4, -1, 5, t1 >;
static_assert( std::is_same_v< t4, type_list<E, D, C, B, A> > );
// Same thing again, but without having to know the size of t1.
using t5 = type_list_stride< -1, -1, t1::size, t1 >;
static_assert( std::is_same_v< t5, t4 > );
// A count of zero works.
static_assert( std::is_same_v< t6, type_list<> > );
typename typelist_::type_list_stride< Base, Stride, Count, T >::type type_list_stride
Definition typelist.hpp:493
See also
flatten_stride

Definition at line 493 of file typelist.hpp.

◆ type_list_sublist

template<std::size_t From, std::size_t To, class T>
using ugmisc::type_list_sublist = typename get_type_list_sublist<From, To, T>::type
Template Parameters
TA type_list.

Takes the types from the From index up to but not including the To index, and turns them into an ugmisc::type_list.

See also
flatten_sublist

Definition at line 253 of file typelist.hpp.

◆ type_list_suffix

template<std::size_t Count, class T>
using ugmisc::type_list_suffix = type_list_sublist<T::size - Count, T::size, T>

Aliases an ugmisc::type_list with the last Count members of the type_list template parameter T.

See also
ugmisc::type_list_sublist, ugmisc::type_list_prefix, ugmisc::flatten_suffix.

Definition at line 277 of file typelist.hpp.

Enumeration Type Documentation

◆ test_modifier

See also
filter_type_list
Enumerator
no_invert_test 

Keep types when the Filter returns true.

invert_test 

Keep types when the Filter returns false.

Definition at line 91 of file typelist.hpp.

Function Documentation

◆ operator!=()

template<class T, class U>
auto ugmisc::operator!= ( T ,
U  ) -> std::enable_if_t< is_type_list_v< T > &&is_type_list_v< U >, bool >
constexpr

Definition at line 1135 of file typelist.hpp.

◆ operator+()

template<class T, class U>
auto ugmisc::operator+ ( T ,
U  ) -> concatenate_type_lists_t< T, U >
constexpr

Concatenate type lists, using instances of the type lists. A type list is a type, not an object. However, it can be instantiated, and is a trivial empty class.

Instances can be added, and the resulting object is an instance of the type that would be produced by concatenating the two type lists.

Definition at line 1119 of file typelist.hpp.

◆ operator==()

template<class T, class U>
auto ugmisc::operator== ( T ,
U  ) -> std::enable_if_t< is_type_list_v< T > &&is_type_list_v< U >, bool >
constexpr

type_list objects compare equal if they have the exact same type

Definition at line 1128 of file typelist.hpp.