git.iko.soy/rowtype-yoga/purescript-native
on 90fcee2 help sign in
182 lines 6.3 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 boxed(const std::nullptr_t) noexcept : std::shared_ptr<void>() {}
43 boxed(const int n) : std::shared_ptr<void>(std::make_shared<int>(n)) {}
45 boxed(const long n) : std::shared_ptr<void>(std::make_shared<int>(static_cast<int>(n))) {
46#if !defined(NDEBUG) // if debug build
47 if (n < std::numeric_limits<int>::min() || n > std::numeric_limits<int>::max()) {
48 throw std::runtime_error("integer out of range");
49 }
50#endif // !defined(NDEBUG)
51 }
53 boxed(const unsigned long n) : std::shared_ptr<void>(std::make_shared<int>(static_cast<int>(n))) {
54#if !defined(NDEBUG) // if debug build
55 if (n > std::numeric_limits<int>::max()) {
56 throw std::runtime_error("integer out of range");
57 }
58#endif // !defined(NDEBUG)
59 }
61 boxed(const double n) : std::shared_ptr<void>(std::make_shared<double>(n)) {}
62 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)) {}
64 boxed(string&& s) : std::shared_ptr<void>(std::make_shared<string>(std::move(s))) {}
65 boxed(const string& s) : std::shared_ptr<void>(std::make_shared<string>(s)) {}
66 boxed(array_t&& l) : std::shared_ptr<void>(std::make_shared<array_t>(std::move(l))) {}
67 boxed(const array_t& l) : std::shared_ptr<void>(std::make_shared<array_t>(l)) {}
68 boxed(dict_t&& m) : std::shared_ptr<void>(std::make_shared<dict_t>(std::move(m))) {}
69 boxed(const dict_t& m) : std::shared_ptr<void>(std::make_shared<dict_t>(m)) {}
71 template <typename T,
72 typename = typename std::enable_if<!std::is_same<boxed,T>::value>::type>
73 boxed(const T& f,
74 typename std::enable_if<std::is_assignable<std::function<boxed(boxed)>,T>::value>::type* = 0)
75 : std::shared_ptr<void>(std::make_shared<fn_t>(f)) {
76 }
78 template <typename T,
79 typename = typename std::enable_if<!std::is_same<boxed,T>::value>::type>
80 boxed(const T& f,
81 typename std::enable_if<std::is_assignable<std::function<boxed(void)>,T>::value>::type* = 0)
82 : std::shared_ptr<void>(std::make_shared<eff_fn_t>(f)) {
83 }
85 template <typename T,
86 typename = typename std::enable_if<!std::is_same<boxed,T>::value &&
87 std::is_convertible<T,fn_t>::value>::type>
88 auto operator=(const T& right) -> boxed& {
89 if (std::shared_ptr<void>::operator bool()) {
90 auto& f = *static_cast<fn_t*>(get());
91 f = right;
92 } else {
93 reset(new fn_t(right));
94 }
95 return *this;
96 }
98 auto operator()(const boxed& arg) const -> boxed {
99 auto& f = *static_cast<fn_t*>(get());
100 return f(arg);
101 }
103 auto operator()() const -> boxed {
104 auto& f = *static_cast<eff_fn_t*>(get());
105 return f();
106 }
108 auto operator[](const char key[]) const -> const boxed& {
109 return (*static_cast<const dict_t*>(get()))[key];
110 }
112 auto operator[](const char key[]) -> boxed& {
113 return (*static_cast<dict_t*>(get()))[key];
114 }
116#if !defined(NDEBUG) // if debug build
117 auto operator[](const int index) const -> const boxed& {
118 return static_cast<const array_t*>(get())->at(index);
119 }
121 auto operator[](const int index) -> boxed& {
122 return static_cast<array_t*>(get())->at(index);
123 }
124#else // not debug build
125 auto operator[](const int index) const -> const boxed& {
126 return (*static_cast<const array_t*>(get()))[index];
127 }
129 auto operator[](const int index) -> boxed& {
130 return (*static_cast<array_t*>(get()))[index];
131 }
132#endif // !defined(NDEBUG)
134 }; // class boxed
136 template <typename T, typename... Args>
137 inline auto box(Args&&... args) -> boxed {
138 return std::make_shared<T>(std::forward<Args>(args)...);
139 }
141 template <typename T>
142 constexpr auto unbox(const boxed& b) -> const T& {
143 return *static_cast<const T*>(b.get());
144 }
146 template <typename T>
147 constexpr auto unbox(boxed& b) -> T& {
148 return *static_cast<T*>(b.get());
149 }
151 template <typename T>
152 constexpr auto unbox(const T value) -> T {
153 return value;
154 }
156 template <typename T,
157 typename = typename std::enable_if<std::is_same<T, int>::value>::type>
158 constexpr auto unbox(const std::size_t value) -> long long {
159 return value;
160 }
162 inline auto array_length(const boxed& a) -> boxed::array_t::size_type {
163 return unbox<boxed::array_t>(a).size();
164 }
166 using fn_t = boxed::fn_t;
167 using eff_fn_t = boxed::eff_fn_t;
168 using dict_t = boxed::dict_t;
169 using array_t = boxed::array_t;
171 constexpr auto undefined = nullptr;
173} // namespace purescript
175#define FOREIGN_BEGIN(NS) namespace NS {\
176 using namespace purescript;\
177 auto foreign() -> dict_t&;\
178 static const auto& NS ## $foreign_init = []() {\
179 dict_t& exports = foreign();
180#define FOREIGN_END return exports; }(); }
182#endif // purescript_H