git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Runtime headers cleanup
ed95a50Andy Arvanitis
runtime/purescript.h+28−50
@@ -38,51 +38,37 @@ namespace purescript {
38 public:
38 public:
39 using std::shared_ptr<void>::shared_ptr;
39 using std::shared_ptr<void>::shared_ptr;
40
40
41− boxed() noexcept : std::shared_ptr<void>() {
41+ boxed() noexcept : std::shared_ptr<void>() {}
42− }
43−
44− boxed(const std::nullptr_t) noexcept : std::shared_ptr<void>() {
42+ boxed(const std::nullptr_t) noexcept : std::shared_ptr<void>() {}
45− }
46−
47− boxed(const int n) : std::shared_ptr<void>(std::make_shared<int>(n)) {
43+ boxed(const int n) : std::shared_ptr<void>(std::make_shared<int>(n)) {}
48− }
49
44
50 boxed(const long n) : std::shared_ptr<void>(std::make_shared<int>(static_cast<int>(n))) {
45 boxed(const long n) : std::shared_ptr<void>(std::make_shared<int>(static_cast<int>(n))) {
51−#ifndef NDEBUG
46+#if !defined(NDEBUG) // if debug build
52 if (n < std::numeric_limits<int>::min() || n > std::numeric_limits<int>::max()) {
47 if (n < std::numeric_limits<int>::min() || n > std::numeric_limits<int>::max()) {
53 throw std::runtime_error("integer out of range");
48 throw std::runtime_error("integer out of range");
54 }
49 }
55−#endif // !NDEBUG
50+#endif // !defined(NDEBUG)
56− }
57−
58− boxed(const double n) : std::shared_ptr<void>(std::make_shared<double>(n)) {
59− }
60−
61− boxed(const bool b) : std::shared_ptr<void>(std::make_shared<bool>(b)) {
62− }
63−
64− boxed(const char s[]) : std::shared_ptr<void>(std::make_shared<string>(s)) {
65− }
66−
67− boxed(string&& s) : std::shared_ptr<void>(std::make_shared<string>(std::move(s))) {
68− }
69−
70− boxed(array_t&& l) : std::shared_ptr<void>(std::make_shared<array_t>(std::move(l))) {
71 }
51 }
72
52
53+ boxed(const double n) : std::shared_ptr<void>(std::make_shared<double>(n)) {}
54+ boxed(const bool b) : std::shared_ptr<void>(std::make_shared<bool>(b)) {}
55+ boxed(const char s[]) : std::shared_ptr<void>(std::make_shared<string>(s)) {}
56+ boxed(string&& s) : std::shared_ptr<void>(std::make_shared<string>(std::move(s))) {}
57+ boxed(array_t&& l) : std::shared_ptr<void>(std::make_shared<array_t>(std::move(l))) {}
73− boxed(dict_t&& m) : std::shared_ptr<void>(std::make_shared<dict_t>(std::move(m))) {
58+ boxed(dict_t&& m) : std::shared_ptr<void>(std::make_shared<dict_t>(std::move(m))) {}
74− }
75
59
76 template <typename T,
60 template <typename T,
77 typename = typename std::enable_if<!std::is_same<boxed,T>::value>::type>
61 typename = typename std::enable_if<!std::is_same<boxed,T>::value>::type>
78 boxed(const T& f,
62 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)) {
63+ typename std::enable_if<std::is_assignable<std::function<boxed(boxed)>,T>::value>::type* = 0)
64+ : std::shared_ptr<void>(std::make_shared<fn_t>(f)) {
80 }
65 }
81
66
82 template <typename T,
67 template <typename T,
83 typename = typename std::enable_if<!std::is_same<boxed,T>::value>::type>
68 typename = typename std::enable_if<!std::is_same<boxed,T>::value>::type>
84 boxed(const T& f,
69 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)) {
70+ typename std::enable_if<std::is_assignable<std::function<boxed(void)>,T>::value>::type* = 0)
71+ : std::shared_ptr<void>(std::make_shared<eff_fn_t>(f)) {
86 }
72 }
87
73
88 template <typename T,
74 template <typename T,
@@ -109,36 +95,30 @@ namespace purescript {
109 }
95 }
110
96
111 auto operator[](const char key[]) const -> const boxed& {
97 auto operator[](const char key[]) const -> const boxed& {
112− const auto& dict = *static_cast<const dict_t*>(get());
98+ return (*static_cast<const dict_t*>(get()))[key];
113− return dict[key];
114 }
99 }
115
100
116 auto operator[](const char key[]) -> boxed& {
101 auto operator[](const char key[]) -> boxed& {
117− auto& dict = *static_cast<dict_t*>(get());
102+ return (*static_cast<dict_t*>(get()))[key];
118− return dict[key];
119 }
103 }
120
104
121−#ifdef NDEBUG
105+#if !defined(NDEBUG) // if debug build
122 auto operator[](const int index) const -> const boxed& {
106 auto operator[](const int index) const -> const boxed& {
123− const auto& array = *static_cast<const array_t*>(get());
107+ return static_cast<const array_t*>(get())->at(index);
124− return array[index];
125 }
108 }
126
109
127 auto operator[](const int index) -> boxed& {
110 auto operator[](const int index) -> boxed& {
128− auto& array = *static_cast<array_t*>(get());
111+ return static_cast<array_t*>(get())->at(index);
129− return array[index];
130 }
112 }
131−#else
113+#else // not debug build
132 auto operator[](const int index) const -> const boxed& {
114 auto operator[](const int index) const -> const boxed& {
133− const auto& array = *static_cast<const array_t*>(get());
115+ return (*static_cast<const array_t*>(get()))[index];
134− return array.at(index);
135 }
116 }
136
117
137 auto operator[](const int index) -> boxed& {
118 auto operator[](const int index) -> boxed& {
138− auto& array = *static_cast<array_t*>(get());
119+ return (*static_cast<array_t*>(get()))[index];
139− return array.at(index);
140 }
120 }
141−#endif // NDEBUG
121+#endif // !defined(NDEBUG)
142
122
143 }; // class boxed
123 }; // class boxed
144
124
@@ -148,9 +128,8 @@ namespace purescript {
148 }
128 }
149
129
150 template <typename T>
130 template <typename T>
151− inline auto unbox(const boxed& b) -> const T& {
131+ constexpr inline auto unbox(const boxed& b) -> const T& {
152− const auto& unboxed = *static_cast<const T*>(b.get());
132+ return *static_cast<const T*>(b.get());
153− return unboxed;
154 }
133 }
155
134
156 template <typename T,
135 template <typename T,
@@ -167,9 +146,8 @@ namespace purescript {
167 }
146 }
168
147
169 template <typename T>
148 template <typename T>
170− inline auto peek(boxed& b) -> T& {
149+ constexpr inline auto peek(boxed& b) -> T& {
171− auto& unboxed = *static_cast<T*>(b.get());
150+ return *static_cast<T*>(b.get());
172− return unboxed;
173 }
151 }
174
152
175 template <typename T>
153 template <typename T>
runtime/string_literal_dict.h+28−4
@@ -1,11 +1,35 @@
1+///////////////////////////////////////////////////////////////////////////////
2+//
3+// Module : string_literal_dict.h
4+// Copyright : (c) Andy Arvanitis 2018
5+// License : BSD
6+//
7+// Maintainer : Andy Arvanitis
8+// Stability : experimental
9+// Portability :
10+//
11+// A simple dictionary optimized for this runtime's particular use –
12+// *not* intended for general use or as a replacement for containers
13+// such as std::map or std::unordered_map. It relies on the following
14+// assumptions:
15+//
16+// 1) Keys are C-type string literals. No copying or (memory management)
17+// of the keys occurs
18+// 2) The number of elements in the container is generally small – say,
19+// under 20. There is no sorting and a linear search is used to
20+// locate elements
21+//
22+///////////////////////////////////////////////////////////////////////////////
23+//
1#ifndef string_literal_dict_H
24#ifndef string_literal_dict_H
2#define string_literal_dict_H
25#define string_literal_dict_H
3
26
4#include <cstring>
27#include <cstring>
5−#include <string>
6#include <vector>
28#include <vector>
7#include <utility>
29#include <utility>
8−
30+#if !defined(NDEBUG)
31+#include <string>
32+#endif
9
33
10template <typename T>
34template <typename T>
11class string_literal_dict_t : private std::vector<std::pair<const char *, T>> {
35class string_literal_dict_t : private std::vector<std::pair<const char *, T>> {
@@ -25,8 +49,8 @@ class string_literal_dict_t : private std::vector<std::pair<const char *, T>> {
25 return it->second;
49 return it->second;
26 }
50 }
27 }
51 }
28−#ifndef NDEBUG
52+#if !defined(NDEBUG) // if debug build
29− throw std::runtime_error("dictionary key \"" + std::string(key) + "\" not found");
53+ throw std::runtime_error("dictionary key \"" + std::string(key) + "\" not found");
30#endif
54#endif
31 return null();
55 return null();
32 }
56 }