DSC Engine
Loading...
Searching...
No Matches
templates.hpp
Go to the documentation of this file.
1
6#pragma once
7
8namespace DSC
9{
10 // type switch (https://en.cppreference.com/w/cpp/types/conditional)
11
13 template<bool B, typename T, typename F>
14 struct __if__ { using type = T; };
15 template<typename T, typename F>
16 struct __if__<false, T, F> { using type = F; };
29 template<bool B, typename T, typename F>
30 using _if_ = typename __if__<B,T,F>::type;
31
32 //https://en.cppreference.com/w/cpp/types/remove_reference
33
35 template<typename T> struct __no_ref__ { typedef T type; };
36 template<typename T> struct __no_ref__<T&> { typedef T type; };
37 template<typename T> struct __no_ref__<T&&> { typedef T type; };
50 template<typename T>
51 using _no_ref_ = typename __no_ref__<T>::type;
52
53
62 template<unsigned int C>
64 _if_< C<256, unsigned char,
66 >;
67
77 template<class T>
79 {
80 return ((T&&) arg);
81 }
82
83 template<class T>
84 _no_ref_<T>&& _move_(T& arg)
85 {
86 return ((T&&) arg);
87 }
88
89
90 template<typename T>
91 constexpr const T& min(const T& first, const T& second)
92 {
93 return first<second ? first : second;
94 }
95
96 template<typename T, typename... Arguments>
97 constexpr const T& min(const T& first, const Arguments& ...args)
98 {
99 return min(first, min(args...));
100 }
101
102
103 template<typename T>
104 constexpr const T& max(const T& first, const T& second)
105 {
106 return first>second ? first : second;
107 }
108
109 template<typename T, typename... Arguments>
110 constexpr const T& max(const T& first, const Arguments& ...args)
111 {
112 return max(first, max(args...));
113 }
114
115
116 template<typename T>
117 constexpr void swap(T& first, T& second)
118 {
119 T tmp = _move_(first);
120 first = _move_(second);
121 second = _move_(tmp);
122 }
123}
124
_if_< C< 256, unsigned char, _if_< C< 65536, unsigned short, unsigned int > > uint_best_fit
Chooses the smallest unsigned integer type that fits a constant.
Definition: templates.hpp:66
_no_ref_< T > && _move_(T &&arg)
Creates rvalue reference from object (useful in move semantics)
Definition: templates.hpp:78
typename __no_ref__< T >::type _no_ref_
gets rid of type reference
Definition: templates.hpp:51
typename __if__< B, T, F >::type _if_
Decides type based on a boolean expression.
Definition: templates.hpp:30