forked from joboccara/NamedType
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunderlying_functionalities.hpp
141 lines (116 loc) · 3.77 KB
/
underlying_functionalities.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#ifndef UNDERLYING_FUNCTIONALITIES_HPP
#define UNDERLYING_FUNCTIONALITIES_HPP
#include "crtp.hpp"
#include "named_type_impl.hpp"
#include <functional>
#include <iostream>
#include <memory>
namespace fluent
{
template <typename T>
struct Incrementable : crtp<T, Incrementable>
{
T& operator+=(T const& other) { this->underlying().get() += other.get(); return this->underlying(); }
};
template <typename T>
struct PreIncrementable : crtp<T, PreIncrementable>
{
T& operator++() { ++this->underlying().get(); return this->underlying(); }
};
template <typename T>
struct Addable : crtp<T, Addable>
{
T operator+(T const& other) const { return T(this->underlying().get() + other.get()); }
};
template <typename T>
struct Subtractable : crtp<T, Subtractable>
{
T operator-(T const& other) const { return T(this->underlying().get() - other.get()); }
};
template <typename T>
struct Multiplicable : crtp<T, Multiplicable>
{
T operator*(T const& other) const { return T(this->underlying().get() * other.get()); }
};
template <typename T>
struct Negatable : crtp<T, Negatable>
{
T operator-() const { return T(-this->underlying().get()); }
};
template <typename T>
struct Comparable : crtp<T, Comparable>
{
bool operator<(T const& other) const { return this->underlying().get() < other.get(); }
bool operator>(T const& other) const { return other.get() < this->underlying().get(); }
bool operator<=(T const& other) const { return !(*this > other); }
bool operator>=(T const& other) const { return !(*this < other); }
bool operator==(T const& other) const { return !(*this < other || *this > other); }
bool operator!=(T const& other) const { return !(*this == other); }
};
template <typename T>
struct Printable : crtp<T, Printable>
{
void print(std::ostream& os) const { os << this->underlying().get(); }
};
template <typename Destination>
struct ImplicitlyConvertibleTo
{
template <typename T>
struct templ : crtp<T, templ>
{
operator Destination() const
{
return this->underlying().get();
}
};
};
template <typename T, typename Parameter, template<typename> class... Skills>
std::ostream& operator<<(std::ostream& os, NamedType<T, Parameter, Skills...> const& object)
{
object.print(os);
return os;
}
template<typename T>
struct Hashable
{
static constexpr bool is_hashable = true;
};
template<typename NamedType_>
struct FunctionCallable;
template <typename T, typename Parameter, template<typename> class... Skills>
struct FunctionCallable<NamedType<T, Parameter, Skills...>> : crtp<NamedType<T, Parameter, Skills...>, FunctionCallable>
{
operator T const&() const
{
return this->underlying().get();
}
operator T&()
{
return this->underlying().get();
}
};
template<typename NamedType_>
struct MethodCallable;
template <typename T, typename Parameter, template<typename> class... Skills>
struct MethodCallable<NamedType<T, Parameter, Skills...>> : crtp<NamedType<T, Parameter, Skills...>, MethodCallable>
{
T const* operator->() const { return std::addressof(this->underlying().get()); }
T* operator->() { return std::addressof(this->underlying().get()); }
};
template<typename NamedType_>
struct Callable : FunctionCallable<NamedType_>, MethodCallable<NamedType_>{};
} // namespace fluent
namespace std
{
template <typename T, typename Parameter, template<typename> class... Skills>
struct hash<fluent::NamedType<T, Parameter, Skills...>>
{
using NamedType = fluent::NamedType<T, Parameter, Skills...>;
using checkIfHashable = typename std::enable_if<NamedType::is_hashable, void>::type;
size_t operator()(fluent::NamedType<T, Parameter, Skills...> const& x) const
{
return std::hash<T>()(x.get());
}
};
}
#endif