1///////////////////////////////////////////////////////////////////////////////
3// Module : purescript.hh
4// Copyright : (c) Andy Arvanitis 2015
7// Maintainer : Andy Arvanitis <andy.arvanitis@gmail.com>
8// Stability : experimental
11// Basic types and functions to support purescript-to-C++1x rendering
13///////////////////////////////////////////////////////////////////////////////
32#include <unordered_map>
38using string = std::string;
39using runtime_error = std::runtime_error;
40using nullptr_t = std::nullptr_t;
42const bool undefined = false;
44// A variant data class designed to provide some features of dynamic typing.
64 mutable Type type = Type::Unknown;
68 static constexpr as_thunk unthunk = as_thunk{};
70 using map = std::unordered_map<const map_key_t, const any, map_key_t::hasher, map_key_t::equal>;
71 using vector = std::vector<any>;
72 using fn = std::function<any(const any&)>;
73 using eff_fn = std::function<any()>;
74 using thunk = std::function<const any& (const as_thunk)>;
77 using shared = std::shared_ptr<T>;
80 static constexpr auto make_shared(const T& arg) -> shared<T> {
81 return std::make_shared<T>(arg);
85 static constexpr auto make_shared(T&& arg) -> shared<T> {
86 return std::make_shared<T>(std::move(arg));
95 mutable shared<string> s;
96 mutable shared<map> m;
97 mutable shared<vector> v;
99 mutable shared<eff_fn> e;
100 mutable shared<thunk> t;
101 mutable shared<void> p;
106 any(const long val) : type(Type::Integer), i(val) {}
107 any(const int val) : type(Type::Integer), i(val) {}
108 any(const unsigned int val) : type(Type::Integer), i(val) {}
109 any(const double val) : type(Type::Double), d(val) {}
110 any(const char val) : type(Type::Character), c(val) {}
112 template <typename T, typename = typename std::enable_if<std::is_same<bool,T>::value>::type>
113 any(const T val) : type(Type::Boolean), b(val) {}
115 any(const string& val) : type(Type::String), s(make_shared<string>(val)) {}
116 any(string&& val) : type(Type::String), s(make_shared<string>(std::move(val))) {}
117 any(const char val[]) : type(Type::String), s(make_shared<string>(val)) {}
119 any(const map& val) : type(Type::Map), m(make_shared<map>(val)) {}
120 any(map&& val) : type(Type::Map), m(make_shared<map>(std::move(val))) {}
122 any(const vector& val) : type(Type::Vector), v(make_shared<vector>(val)) {}
123 any(vector&& val) : type(Type::Vector), v(make_shared<vector>(std::move(val))) {}
125 template <typename T, typename = typename std::enable_if<!std::is_same<any,T>::value>::type>
126 any(const T& val, typename std::enable_if<std::is_assignable<fn,T>::value>::type* = 0)
127 : type(Type::Function), f(make_shared<fn>(val)) {}
129 template <typename T>
130 any(const T& val, typename std::enable_if<std::is_assignable<eff_fn,T>::value>::type* = 0)
131 : type(Type::EffFunction), e(make_shared<eff_fn>(val)) {}
133 template <typename T>
134 any(const T& val, typename std::enable_if<std::is_assignable<thunk,T>::value>::type* = 0)
135 : type(Type::Thunk), t(make_shared<thunk>(val)) {}
137 template <typename T>
138 any(const T& val, typename std::enable_if<std::is_assignable<shared<void>,T>::value>::type* = 0)
139 : type(Type::Pointer), p(val) {}
141 template <typename T>
142 any(T&& val, typename std::enable_if<std::is_assignable<shared<void>,T>::value>::type* = 0)
143 : type(Type::Pointer), p(std::move(val)) {}
145 any(std::nullptr_t) : type(Type::Pointer), p(nullptr) {}
150 auto operator=(const any&) -> any&;
151 auto operator=(any&) -> any&;
152 auto operator=(any&&) -> any&;
157 template <typename T>
158 auto cast() const -> typename std::enable_if<std::is_same<T, long>::value, T>::type;
160 template <typename T>
161 auto cast() const -> typename std::enable_if<std::is_same<T, double>::value, T>::type;
163 template <typename T>
164 auto cast() const -> typename std::enable_if<std::is_same<T, char>::value, T>::type;
166 template <typename T>
167 auto cast() const -> typename std::enable_if<std::is_same<T, bool>::value, T>::type;
169 template <typename T>
170 auto cast() const -> typename std::enable_if<std::is_same<T, string>::value, const T&>::type;
172 template <typename T>
173 auto cast() const -> typename std::enable_if<std::is_same<T, const char*>::value, const T>::type;
175 template <typename T>
176 auto cast() const -> typename std::enable_if<std::is_same<T, map>::value, const T&>::type;
178 template <typename T>
179 auto cast() const -> typename std::enable_if<std::is_same<T, vector>::value, const T&>::type;
181 template <typename T>
182 auto cast() const -> typename std::enable_if<std::is_assignable<shared<void>,T>::value, typename T::element_type*>::type {
183 return static_cast<typename T::element_type*>(extractPointer());
186 auto operator()(const any&) const -> any;
188 auto operator()(const as_thunk) const -> const any&;
190 auto operator()() const -> any;
192 operator long() const;
193 operator double() const;
194 operator bool() const;
196 operator const string&() const;
197 operator const map&() const;
198 operator const vector&() const;
200 auto operator[](const map_key_t&) const -> const any&;
201 auto operator[](const vector::size_type) const -> const any&;
202 auto operator[](const any&) const -> const any&;
204 auto extractPointer() const -> void*;
206 static auto extract_value(const any&) -> const any&;
208 auto operator==(const any&) const -> bool;
209 auto operator==(const long) const -> bool;
210 auto operator==(const double) const -> bool;
211 auto operator==(const char) const -> bool;
212 auto operator==(const bool) const -> bool;
213 auto operator==(const string&) const -> bool;
214 auto operator==(const char * const) const -> bool;
216 auto operator!=(const any&) const -> bool;
217 auto operator!=(const long) const -> bool;
218 auto operator!=(const double) const -> bool;
219 auto operator!=(const char) const -> bool;
220 auto operator!=(const bool) const -> bool;
221 auto operator!=(const string&) const -> bool;
222 auto operator!=(const char * const) const -> bool;
224 auto operator<(const any&) const -> bool;
225 auto operator<(const long) const -> bool;
226 auto operator<(const double) const -> bool;
227 auto operator<(const char) const -> bool;
228 auto operator<(const bool) const -> bool;
229 auto operator<(const string&) const -> bool;
230 auto operator<(const char * const) const -> bool;
232 auto operator<=(const any&) const -> bool;
233 auto operator<=(const long) const -> bool;
234 auto operator<=(const double) const -> bool;
235 auto operator<=(const char) const -> bool;
236 auto operator<=(const bool) const -> bool;
237 auto operator<=(const string&) const -> bool;
238 auto operator<=(const char * const) const -> bool;
240 auto operator>(const any&) const -> bool;
241 auto operator>(const long) const -> bool;
242 auto operator>(const double) const -> bool;
243 auto operator>(const char) const -> bool;
244 auto operator>(const bool) const -> bool;
245 auto operator>(const string&) const -> bool;
246 auto operator>(const char * const) const -> bool;
248 auto operator>=(const any&) const -> bool;
249 auto operator>=(const long) const -> bool;
250 auto operator>=(const double) const -> bool;
251 auto operator>=(const char) const -> bool;
252 auto operator>=(const bool) const -> bool;
253 auto operator>=(const string&) const -> bool;
254 auto operator>=(const char * const) const -> bool;
256 auto operator+(const any&) const -> any;
257 auto operator+(const long) const -> long;
258 auto operator+(const double) const -> double;
259 auto operator+(const char) const -> char;
260 auto operator+(const string&) const -> string;
261 auto operator+(const char * const) const -> string;
263 auto operator-(const any&) const -> any;
264 auto operator-(const long) const -> long;
265 auto operator-(const double) const -> double;
266 auto operator-(const char) const -> char;
268 auto operator*(const any&) const -> any;
269 auto operator*(const long) const -> long;
270 auto operator*(const double) const -> double;
272 auto operator/(const any&) const -> any;
273 auto operator/(const long) const -> long;
274 auto operator/(const double) const -> double;
276 auto operator%(const any&) const -> any;
277 auto operator%(const long) const -> long;
279 auto operator-() const -> any; // unary negate
281 friend auto operator==(const long, const any&) -> bool;
282 friend auto operator!=(const long, const any&) -> bool;
283 friend auto operator< (const long, const any&) -> bool;
284 friend auto operator<=(const long, const any&) -> bool;
285 friend auto operator> (const long, const any&) -> bool;
286 friend auto operator>=(const long, const any&) -> bool;
288 friend auto operator==(const double, const any&) -> bool;
289 friend auto operator!=(const double, const any&) -> bool;
290 friend auto operator< (const double, const any&) -> bool;
291 friend auto operator<=(const double, const any&) -> bool;
292 friend auto operator> (const double, const any&) -> bool;
293 friend auto operator>=(const double, const any&) -> bool;
295 friend auto operator==(const char, const any&) -> bool;
296 friend auto operator!=(const char, const any&) -> bool;
297 friend auto operator< (const char, const any&) -> bool;
298 friend auto operator<=(const char, const any&) -> bool;
299 friend auto operator> (const char, const any&) -> bool;
300 friend auto operator>=(const char, const any&) -> bool;
302 friend auto operator==(const bool, const any&) -> bool;
303 friend auto operator!=(const bool, const any&) -> bool;
304 friend auto operator< (const bool, const any&) -> bool;
305 friend auto operator<=(const bool, const any&) -> bool;
306 friend auto operator> (const bool, const any&) -> bool;
307 friend auto operator>=(const bool, const any&) -> bool;
309 friend auto operator==(const string&, const any&) -> bool;
310 friend auto operator!=(const string&, const any&) -> bool;
311 friend auto operator< (const string&, const any&) -> bool;
312 friend auto operator<=(const string&, const any&) -> bool;
313 friend auto operator> (const string&, const any&) -> bool;
314 friend auto operator>=(const string&, const any&) -> bool;
316 friend auto operator==(const char* const, const any&) -> bool;
317 friend auto operator!=(const char* const, const any&) -> bool;
318 friend auto operator< (const char* const, const any&) -> bool;
319 friend auto operator<=(const char* const, const any&) -> bool;
320 friend auto operator> (const char* const, const any&) -> bool;
321 friend auto operator>=(const char* const, const any&) -> bool;
323 friend auto operator+(const long, const any&) -> long;
324 friend auto operator-(const long, const any&) -> long;
325 friend auto operator*(const long, const any&) -> long;
326 friend auto operator/(const long, const any&) -> long;
327 friend auto operator%(const long, const any&) -> long;
329 friend auto operator+(const double, const any&) -> double;
330 friend auto operator-(const double, const any&) -> double;
331 friend auto operator*(const double, const any&) -> double;
332 friend auto operator/(const double, const any&) -> double;
334 friend auto operator+(const char, const any&) -> char;
335 friend auto operator-(const char, const any&) -> char;
337 friend auto operator+(const string&, const any&) -> string;
338 friend auto operator+(const char* const, const any&) -> string;
342} // namespace PureScript
344#endif // PureScript_HH
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list