carl  24.04
Computer ARithmetic Library
mpl_utils.h
Go to the documentation of this file.
1 #pragma once
2 /**
3  * @file mpl_utils.h
4  * This file provides two helpers to mangle with type sequences using boost::mpl.
5  * `mpl_unique` makes a `mpl::vector<>` almost unique. (If two types have the same size, this may fail)
6  * `mpl_concatenate` concatenates an arbitrary number of `mpl::vector<>` types.
7  * `mpl_variant_of` constructs a variant from a `mpl::vector<>` making the types unique.
8  */
9 
10 #include <type_traits>
11 #include <boost/mpl/less.hpp>
12 #include <boost/mpl/pop_front.hpp>
13 #include <boost/mpl/sort.hpp>
14 #include <boost/mpl/unique.hpp>
15 #include <boost/mpl/vector.hpp>
16 
17 namespace carl {
18 
19 template<typename T>
20 struct mpl_unique {
21  typedef boost::mpl::less<boost::mpl::sizeof_<boost::mpl::_>, boost::mpl::sizeof_<boost::mpl::_>> Less;
22  typedef std::is_same<boost::mpl::_, boost::mpl::_> Equal;
23  typedef typename boost::mpl::sort<T, Less>::type Sorted;
24  typedef typename boost::mpl::unique<Sorted, Equal>::type Unique;
25  typedef Unique type;
26 };
27 
28 template<std::size_t S, typename Front, typename... Tail>
30  typedef typename mpl_concatenate_impl<S-1, Tail...>::type TailConcatenation;
31  typedef typename boost::mpl::copy<Front, boost::mpl::back_inserter<TailConcatenation>>::type type;
32 };
33 template<typename Front, typename... Tail>
34 struct mpl_concatenate_impl<1, Front, Tail...> {
35  typedef Front type;
36 };
37 template<typename... T>
39  typedef typename mpl_concatenate_impl<sizeof...(T), T...>::type type;
40 };
41 
42 template<bool, typename Vector, typename... Unpacked>
44  typedef typename boost::mpl::front<Vector>::type Front;
45  typedef typename boost::mpl::pop_front<Vector>::type Tail;
47 };
48 template<typename Vector, typename... Unpacked>
49 struct mpl_variant_of_impl<true, Vector, Unpacked...> {
50  typedef boost::variant<Unpacked...> type;
51 };
52 template<typename Vector>
54  typedef typename mpl_unique<Vector>::type Unique;
56 };
57 
58 }
carl is the main namespace for the library.
std::is_same< boost::mpl::_, boost::mpl::_ > Equal
Definition: mpl_utils.h:22
boost::mpl::sort< T, Less >::type Sorted
Definition: mpl_utils.h:23
boost::mpl::unique< Sorted, Equal >::type Unique
Definition: mpl_utils.h:24
boost::mpl::less< boost::mpl::sizeof_< boost::mpl::_ >, boost::mpl::sizeof_< boost::mpl::_ > > Less
Definition: mpl_utils.h:21
boost::mpl::copy< Front, boost::mpl::back_inserter< TailConcatenation > >::type type
Definition: mpl_utils.h:31
mpl_concatenate_impl< S-1, Tail... >::type TailConcatenation
Definition: mpl_utils.h:30
mpl_concatenate_impl< sizeof...(T), T... >::type type
Definition: mpl_utils.h:39
boost::mpl::pop_front< Vector >::type Tail
Definition: mpl_utils.h:45
boost::mpl::front< Vector >::type Front
Definition: mpl_utils.h:44
mpl_variant_of_impl< boost::mpl::empty< Tail >::value, Tail, Front, Unpacked... >::type type
Definition: mpl_utils.h:46
mpl_unique< Vector >::type Unique
Definition: mpl_utils.h:54
mpl_variant_of_impl< boost::mpl::empty< Unique >::value, Unique >::type type
Definition: mpl_utils.h:55