@@ -1,244 +0,0 @@
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−///////////////////////////////////////////////////////////////////////////////
25−#if !defined(PURESCRIPT_DISABLE_EXCEPTIONS)
28−#include "functions.h"
29−#include "dictionary.h"
30−#include "recursion.h"
32−namespace purescript {
38− std::shared_ptr<void> shared;
46− using fn_t = _template_::fn_t<boxed>;
47− using eff_fn_t = _template_::eff_fn_t<boxed>;
49− using dict_t = _template_::dict_t<boxed>;
50− using array_t = std::vector<boxed>;
52− using weak = _template_::weak<boxed>;
53− using recur = _template_::recur<boxed>;
56− template <typename T>
57− using fn_T = _template_::fn_T<boxed, T>;
59− template <typename T>
60− using eff_fn_T = _template_::eff_fn_T<boxed, T>;
63− boxed() noexcept : shared() {}
64− boxed(const std::nullptr_t) noexcept : shared() {}
66− boxed(const weak& w) : shared(w.shared()) {}
67− boxed(const recur& r) : shared(r.shared()) {}
68− boxed(const recur::weak& w) : shared(w.shared()) {}
70− template <typename T>
71− boxed(std::shared_ptr<T>&& other) noexcept : shared(std::move(other)) {}
73− template <typename T>
74− boxed(const std::shared_ptr<T>& other) : shared(other) {}
76− boxed(const int n) noexcept : _int_(n) {}
78− boxed(const unsigned long n);
79− boxed(const double n) noexcept : _double_(n) {}
80− boxed(const bool b) noexcept : _bool_(b) {}
81− boxed(const char s[]) : shared(std::make_shared<string>(s)) {}
82− boxed(string&& s) : shared(std::make_shared<string>(std::move(s))) {}
83− boxed(const string& s) : shared(std::make_shared<string>(s)) {}
84− boxed(array_t&& l) : shared(std::make_shared<array_t>(std::move(l))) {}
85− boxed(const array_t& l) : shared(std::make_shared<array_t>(l)) {}
86− boxed(dict_t&& m) : shared(std::make_shared<dict_t>(std::move(m))) {}
87− boxed(const dict_t& m) : shared(std::make_shared<dict_t>(m)) {}
89− template <typename T,
90− typename U = typename std::remove_reference<T>::type,
91− typename V = typename std::remove_const<U>::type,
92− typename = typename std::enable_if<!std::is_same<boxed,V>::value && !std::is_same<recur,V>::value>::type>
94− typename std::enable_if<std::is_same<decltype(std::declval<U>()(std::declval<boxed>())), boxed>::value>::type* = 0)
95− : shared(std::shared_ptr<fn_t>(std::make_shared<fn_T<T>>(std::forward<T>(f)))) {
98− template <typename T,
99− typename U = typename std::remove_reference<T>::type,
100− typename V = typename std::remove_const<U>::type,
101− typename = typename std::enable_if<!std::is_same<boxed,V>::value && !std::is_same<recur,V>::value>::type>
103− typename std::enable_if<std::is_same<decltype(std::declval<U>()()), boxed>::value>::type* = 0)
104− : shared(std::shared_ptr<eff_fn_t>(std::make_shared<eff_fn_T<T>>(std::forward<T>(f)))) {
107− auto get() const noexcept -> void * {
108− return shared.get();
111− auto operator()(const boxed& arg) const -> boxed {
112− return (*static_cast<fn_t*>(shared.get()))(arg);
115− auto operator()() const -> boxed {
116− return (*static_cast<eff_fn_t*>(shared.get()))();
119− auto operator[](const char key[]) const -> const boxed& {
120− return (*static_cast<const dict_t*>(shared.get()))[key];
123− auto operator[](const char key[]) -> boxed& {
124− return (*static_cast<dict_t*>(shared.get()))[key];
127− auto operator[](const int index) const -> const boxed&;
128− auto operator[](const int index) -> boxed&;
132− extern template class _template_::fn_t<boxed>;
133− extern template class _template_::eff_fn_t<boxed>;
134− extern template class _template_::dict_t<boxed>;
135− extern template class _template_::weak<boxed>;
136− extern template class _template_::recur<boxed>;
138− extern const boxed undefined;
140− using fn_t = boxed::fn_t;
141− using eff_fn_t = boxed::eff_fn_t;
142− using dict_t = boxed::dict_t;
143− using array_t = boxed::array_t;
145− template <typename T, typename... Args,
146− typename = typename std::enable_if<!std::is_same<T, int>::value &&
147− !std::is_same<T, double>::value &&
148− !std::is_same<T, bool>::value
150− inline auto box(Args&&... args) -> boxed {
151− return std::make_shared<T>(std::forward<Args>(args)...);
154− template <typename T,
155− typename std::enable_if<std::is_same<T, int>::value, T>::type* = nullptr>
156− inline auto box(T arg) noexcept -> boxed {
160− template <typename T,
161− typename std::enable_if<std::is_same<T, double>::value, T>::type* = nullptr>
162− inline auto box(T arg) noexcept -> boxed {
166− template <typename T,
167− typename std::enable_if<std::is_same<T, bool>::value, T>::type* = nullptr>
168− inline auto box(T arg) noexcept -> boxed {
172− template <typename T,
173− typename = typename std::enable_if<!std::is_same<T, int>::value &&
174− !std::is_same<T, double>::value &&
175− !std::is_same<T, bool>::value
177− constexpr auto unbox(const boxed& b) -> const T& {
178− return *static_cast<const T*>(b.get());
181− template <typename T,
182− typename std::enable_if<std::is_same<T, int>::value, T>::type* = nullptr>
183− constexpr auto unbox(const boxed& b) noexcept -> T {
187− template <typename T,
188− typename std::enable_if<std::is_same<T, double>::value, T>::type* = nullptr>
189− constexpr auto unbox(const boxed& b) noexcept -> T {
193− template <typename T,
194− typename std::enable_if<std::is_same<T, bool>::value, T>::type* = nullptr>
195− constexpr auto unbox(const boxed& b) noexcept -> T {
199− template <typename T,
200− typename = typename std::enable_if<!std::is_same<T, int>::value &&
201− !std::is_same<T, double>::value &&
202− !std::is_same<T, bool>::value
204− constexpr auto unbox(boxed& b) -> T& {
205− return *static_cast<T*>(b.get());
208− template <typename T>
209− constexpr auto unbox(const T value) noexcept -> T {
213− template <typename T,
214− typename = typename std::enable_if<std::is_same<T, int>::value>::type>
215− constexpr auto unbox(const std::size_t value) noexcept -> long long {
219− inline auto array_length(const boxed& a) -> boxed::array_t::size_type {
220− return unbox<boxed::array_t>(a).size();
223−} // namespace purescript
225−#if !defined(PURESCRIPT_DISABLE_EXCEPTIONS)
226−#define THROW_(s) throw std::runtime_error(s)
228−#define THROW_(_) return undefined
231−#define DEFINE_FOREIGN_DICTIONARY_AND_ACCESSOR() \
232− inline auto foreign() -> dict_t& {\
237−#define FOREIGN_BEGIN(NS) namespace NS {\
238− using namespace purescript;\
239− DEFINE_FOREIGN_DICTIONARY_AND_ACCESSOR()\
240− static const auto data = ([]() -> char {\
241− dict_t& exports = foreign();
242−#define FOREIGN_END return 0; }()); }
244−#endif // purescript_H