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///////////////////////////////////////////////////////////////////////////////
23#include "string_literal_dict.h"
30 class boxed : public std::shared_ptr<void> {
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>;
39 using std::shared_ptr<void>::shared_ptr;
41 boxed(const int n) : std::shared_ptr<void>(std::make_shared<int>(n)) {}
42 boxed(const long n) : std::shared_ptr<void>(std::make_shared<int>(static_cast<int>(n))) {
43#if !defined(NDEBUG) // if debug build
44 if (n < std::numeric_limits<int>::min() || n > std::numeric_limits<int>::max()) {
45 throw std::runtime_error("integer out of range");
47#endif // !defined(NDEBUG)
50 boxed(const unsigned long n) : std::shared_ptr<void>(std::make_shared<int>(static_cast<int>(n))) {
51#if !defined(NDEBUG) // if debug build
52 if (n > std::numeric_limits<int>::max()) {
53 throw std::runtime_error("integer out of range");
55#endif // !defined(NDEBUG)
58 boxed(const double n) : std::shared_ptr<void>(std::make_shared<double>(n)) {}
59 boxed(const bool b) : std::shared_ptr<void>(std::make_shared<bool>(b)) {}
60 boxed(const char s[]) : std::shared_ptr<void>(std::make_shared<string>(s)) {}
61 boxed(string&& s) : std::shared_ptr<void>(std::make_shared<string>(std::move(s))) {}
62 boxed(const string& s) : std::shared_ptr<void>(std::make_shared<string>(s)) {}
63 boxed(array_t&& l) : std::shared_ptr<void>(std::make_shared<array_t>(std::move(l))) {}
64 boxed(const array_t& l) : std::shared_ptr<void>(std::make_shared<array_t>(l)) {}
65 boxed(dict_t&& m) : std::shared_ptr<void>(std::make_shared<dict_t>(std::move(m))) {}
66 boxed(const dict_t& m) : std::shared_ptr<void>(std::make_shared<dict_t>(m)) {}
69 typename = typename std::enable_if<!std::is_same<boxed,T>::value>::type>
71 typename std::enable_if<std::is_assignable<std::function<boxed(boxed)>,T>::value>::type* = 0)
72 : std::shared_ptr<void>(std::make_shared<fn_t>(f)) {
76 typename = typename std::enable_if<!std::is_same<boxed,T>::value>::type>
78 typename std::enable_if<std::is_assignable<std::function<boxed(void)>,T>::value>::type* = 0)
79 : std::shared_ptr<void>(std::make_shared<eff_fn_t>(f)) {
82 auto operator()(const boxed& arg) const -> boxed {
83 auto& f = *static_cast<fn_t*>(get());
87 auto operator()() const -> boxed {
88 auto& f = *static_cast<eff_fn_t*>(get());
92 auto operator[](const char key[]) const -> const boxed& {
93 return (*static_cast<const dict_t*>(get()))[key];
96 auto operator[](const char key[]) -> boxed& {
97 return (*static_cast<dict_t*>(get()))[key];
100#if !defined(NDEBUG) // if debug build
101 auto operator[](const int index) const -> const boxed& {
102 return static_cast<const array_t*>(get())->at(index);
105 auto operator[](const int index) -> boxed& {
106 return static_cast<array_t*>(get())->at(index);
108#else // not debug build
109 auto operator[](const int index) const -> const boxed& {
110 return (*static_cast<const array_t*>(get()))[index];
113 auto operator[](const int index) -> boxed& {
114 return (*static_cast<array_t*>(get()))[index];
116#endif // !defined(NDEBUG)
120 using fn_t = boxed::fn_t;
121 using eff_fn_t = boxed::eff_fn_t;
122 using dict_t = boxed::dict_t;
123 using array_t = boxed::array_t;
125 class boxed_r : private std::shared_ptr<void> {
127 boxed_r() : std::shared_ptr<void>(std::make_shared<boxed>()) {}
129 operator const boxed&() const {
130 return *static_cast<const boxed*>(get());
133 auto operator()(const boxed& arg) const -> boxed {
134 auto& b = *static_cast<boxed*>(get());
135 auto& f = *static_cast<fn_t*>(b.get());
139 auto operator()() const -> boxed {
140 auto& b = *static_cast<boxed*>(get());
141 auto& f = *static_cast<eff_fn_t*>(b.get());
145 template <typename T>
146 auto operator=(T&& right) -> boxed_r& {
147 auto& b = *static_cast<boxed*>(get());
148 b = std::forward<T>(right);
154 template <typename T, typename... Args>
155 inline auto box(Args&&... args) -> boxed {
156 return std::make_shared<T>(std::forward<Args>(args)...);
159 template <typename T>
160 constexpr auto unbox(const boxed& b) -> const T& {
161 return *static_cast<const T*>(b.get());
164 template <typename T>
165 constexpr auto unbox(boxed& b) -> T& {
166 return *static_cast<T*>(b.get());
169 template <typename T>
170 constexpr auto unbox(const T value) -> T {
174 template <typename T,
175 typename = typename std::enable_if<std::is_same<T, int>::value>::type>
176 constexpr auto unbox(const std::size_t value) -> long long {
180 inline auto array_length(const boxed& a) -> boxed::array_t::size_type {
181 return unbox<boxed::array_t>(a).size();
184 constexpr auto undefined = nullptr;
186} // namespace purescript
188#define DEFINE_FOREIGN_DICTIONARY_AND_ACCESSOR() \
189 inline auto foreign() -> dict_t& {\
190 static dict_t $dict$;\
194#define FOREIGN_BEGIN(NS) namespace NS {\
195 using namespace purescript;\
196 DEFINE_FOREIGN_DICTIONARY_AND_ACCESSOR()\
197 static const auto $foreign_exports_init$ = ([]() -> char {\
198 dict_t& exports = foreign();
199#define FOREIGN_END return 0; }()); }
201#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