1///////////////////////////////////////////////////////////////////////////////
3// Module : purescript.h
4// Copyright : (c) Andy Arvanitis 2018
7// Maintainer : Andy Arvanitis
8// Stability : experimental
11// Basic types and functions to support purescript-to-C++ rendering
13///////////////////////////////////////////////////////////////////////////////
29 class boxed : public std::shared_ptr<void> {
32 using fn_t = std::function<boxed(const boxed&)>;
33 using eff_fn_t = std::function<boxed(void)>;
34 using dict_t = std::map<const string, boxed>;
35 using array_t = std::deque<boxed>;
38 using std::shared_ptr<void>::shared_ptr;
40 boxed() noexcept : std::shared_ptr<void>() {
43 boxed(const std::nullptr_t) noexcept : std::shared_ptr<void>() {
46 boxed(const int n) : std::shared_ptr<void>(std::make_shared<int>(n)) {
49 boxed(const long n) : std::shared_ptr<void>(std::make_shared<int>(static_cast<int>(n))) {
51 if (n < std::numeric_limits<int>::min() || n > std::numeric_limits<int>::max()) {
52 throw std::runtime_error("integer out of range");
57 boxed(const double n) : std::shared_ptr<void>(std::make_shared<double>(n)) {
60 boxed(const bool b) : std::shared_ptr<void>(std::make_shared<bool>(b)) {
63 boxed(const char s[]) : std::shared_ptr<void>(std::make_shared<string>(s)) {
66 boxed(string&& s) : std::shared_ptr<void>(std::make_shared<string>(std::move(s))) {
69 boxed(array_t&& l) : std::shared_ptr<void>(std::make_shared<array_t>(std::move(l))) {
72 boxed(dict_t&& m) : std::shared_ptr<void>(std::make_shared<dict_t>(std::move(m))) {
76 typename = typename std::enable_if<!std::is_same<boxed,T>::value>::type>
78 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)) {
82 typename = typename std::enable_if<!std::is_same<boxed,T>::value>::type>
84 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)) {
88 typename = typename std::enable_if<!std::is_same<boxed,T>::value &&
89 std::is_convertible<T,fn_t>::value>::type>
90 auto operator=(const T& right) -> boxed& {
91 if (std::shared_ptr<void>::operator bool()) {
92 auto& f = *static_cast<fn_t*>(get());
95 reset(new fn_t(right));
100 auto operator()(const boxed& arg) const -> boxed {
101 auto& f = *static_cast<fn_t*>(get());
105 auto operator()() const -> boxed {
106 auto& f = *static_cast<eff_fn_t*>(get());
110 auto operator[](const string& key) const -> const boxed& {
111 const auto& dict = *static_cast<const dict_t*>(get());
115 auto operator[](const string& key) -> boxed& {
116 auto& dict = *static_cast<dict_t*>(get());
121 auto operator[](const int index) const -> const boxed& {
122 const auto& array = *static_cast<const array_t*>(get());
126 auto operator[](const int index) -> boxed& {
127 auto& array = *static_cast<array_t*>(get());
131 auto operator[](const int index) const -> const boxed& {
132 const auto& array = *static_cast<const array_t*>(get());
133 return array.at(index);
136 auto operator[](const int index) -> boxed& {
137 auto& array = *static_cast<array_t*>(get());
138 return array.at(index);
145 template <typename T, typename... Args>
146 inline auto box(Args&&... args) -> boxed {
147 return std::make_shared<T>(std::forward<Args>(args)...);
150 template <typename T>
151 inline auto unbox(const boxed& b) -> const T& {
152 const auto& unboxed = *static_cast<const T*>(b.get());
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 {
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 {
169 template <typename T>
170 inline auto peek(boxed& b) -> T& {
171 auto& unboxed = *static_cast<T*>(b.get());
175 template <typename T>
176 inline auto copy(const boxed& b) -> boxed {
177 return box<T>(unbox<T>(b));
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
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