git.iko.soy/rowtype-yoga/purescript-native
on 3d46983 help sign in
196 lines 6.1 kB history raw
1///////////////////////////////////////////////////////////////////////////////
2//
3// Module : purescript.h
4// Copyright : (c) Andy Arvanitis 2018
5// License : BSD
6//
7// Maintainer : Andy Arvanitis
8// Stability : experimental
9// Portability :
10//
11// Basic types and functions to support purescript-to-C++ rendering
12//
13///////////////////////////////////////////////////////////////////////////////
14//
15#ifndef purescript_H
16#define purescript_H
18#include <memory>
19#include <functional>
20#include <deque>
21#include <string>
22#include <limits>
23#include "string_literal_dict.h"
26namespace purescript {
28 using std::string;
30 class boxed : public std::shared_ptr<void> {
32 public:
33 using fn_t = std::function<boxed(const boxed&)>;
34 using eff_fn_t = std::function<boxed(void)>;
35 using dict_t = string_literal_dict_t<boxed>;
36 using array_t = std::deque<boxed>;
38 public:
39 using std::shared_ptr<void>::shared_ptr;
41 boxed() noexcept : std::shared_ptr<void>() {
42 }
44 boxed(const std::nullptr_t) noexcept : std::shared_ptr<void>() {
45 }
47 boxed(const int n) : std::shared_ptr<void>(std::make_shared<int>(n)) {
48 }
50 boxed(const long n) : std::shared_ptr<void>(std::make_shared<int>(static_cast<int>(n))) {
51#ifndef NDEBUG
52 if (n < std::numeric_limits<int>::min() || n > std::numeric_limits<int>::max()) {
53 throw std::runtime_error("integer out of range");
54 }
55#endif // !NDEBUG
56 }
58 boxed(const double n) : std::shared_ptr<void>(std::make_shared<double>(n)) {
59 }
61 boxed(const bool b) : std::shared_ptr<void>(std::make_shared<bool>(b)) {
62 }
64 boxed(const char s[]) : std::shared_ptr<void>(std::make_shared<string>(s)) {
65 }
67 boxed(string&& s) : std::shared_ptr<void>(std::make_shared<string>(std::move(s))) {
68 }
70 boxed(array_t&& l) : std::shared_ptr<void>(std::make_shared<array_t>(std::move(l))) {
71 }
73 boxed(dict_t&& m) : std::shared_ptr<void>(std::make_shared<dict_t>(std::move(m))) {
74 }
76 template <typename T,
77 typename = typename std::enable_if<!std::is_same<boxed,T>::value>::type>
78 boxed(const T& f,
79 typename std::enable_if<std::is_assignable<std::function<boxed(boxed)>,T>::value>::type* = 0) : std::shared_ptr<void>(std::make_shared<fn_t>(f)) {
80 }
82 template <typename T,
83 typename = typename std::enable_if<!std::is_same<boxed,T>::value>::type>
84 boxed(const T& f,
85 typename std::enable_if<std::is_assignable<std::function<boxed(void)>,T>::value>::type* = 0) : std::shared_ptr<void>(std::make_shared<eff_fn_t>(f)) {
86 }
88 template <typename T,
89 typename = typename std::enable_if<!std::is_same<boxed,T>::value &&
90 std::is_convertible<T,fn_t>::value>::type>
91 auto operator=(const T& right) -> boxed& {
92 if (std::shared_ptr<void>::operator bool()) {
93 auto& f = *static_cast<fn_t*>(get());
94 f = right;
95 } else {
96 reset(new fn_t(right));
97 }
98 return *this;
99 }
101 auto operator()(const boxed& arg) const -> boxed {
102 auto& f = *static_cast<fn_t*>(get());
103 return f(arg);
104 }
106 auto operator()() const -> boxed {
107 auto& f = *static_cast<eff_fn_t*>(get());
108 return f();
109 }
111 auto operator[](const char key[]) const -> const boxed& {
112 const auto& dict = *static_cast<const dict_t*>(get());
113 return dict[key];
114 }
116 auto operator[](const char key[]) -> boxed& {
117 auto& dict = *static_cast<dict_t*>(get());
118 return dict[key];
119 }
121#ifdef NDEBUG
122 auto operator[](const int index) const -> const boxed& {
123 const auto& array = *static_cast<const array_t*>(get());
124 return array[index];
125 }
127 auto operator[](const int index) -> boxed& {
128 auto& array = *static_cast<array_t*>(get());
129 return array[index];
130 }
131#else
132 auto operator[](const int index) const -> const boxed& {
133 const auto& array = *static_cast<const array_t*>(get());
134 return array.at(index);
135 }
137 auto operator[](const int index) -> boxed& {
138 auto& array = *static_cast<array_t*>(get());
139 return array.at(index);
140 }
141#endif // NDEBUG
143 }; // class boxed
145 template <typename T, typename... Args>
146 inline auto box(Args&&... args) -> boxed {
147 return std::make_shared<T>(std::forward<Args>(args)...);
148 }
150 template <typename T>
151 inline auto unbox(const boxed& b) -> const T& {
152 const auto& unboxed = *static_cast<const T*>(b.get());
153 return unboxed;
154 }
156 template <typename T,
157 typename = typename std::enable_if<!std::is_same<T, const boxed&>::value>::type>
158 constexpr auto unbox(const T value) -> T {
159 return value;
160 }
162 template <typename T,
163 typename = typename std::enable_if<!std::is_same<T, const boxed&>::value &&
164 std::is_same<T, int>::value>::type>
165 constexpr auto unbox(const std::size_t value) -> long long {
166 return value;
167 }
169 template <typename T>
170 inline auto peek(boxed& b) -> T& {
171 auto& unboxed = *static_cast<T*>(b.get());
172 return unboxed;
173 }
175 template <typename T>
176 inline auto copy(const boxed& b) -> boxed {
177 return box<T>(unbox<T>(b));
178 }
180 using fn_t = boxed::fn_t;
181 using eff_fn_t = boxed::eff_fn_t;
182 using dict_t = boxed::dict_t;
183 using array_t = boxed::array_t;
185 constexpr auto undefined = nullptr;
187} // namespace purescript
189#define FOREIGN_BEGIN(NS) namespace NS {\
190 using namespace purescript;\
191 auto foreign() -> dict_t&;\
192 static const auto& NS ## $foreign_init = []() {\
193 dict_t& exports = foreign();
194#define FOREIGN_END return exports; }(); }
196#endif // purescript_H