git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Runtime headers cleanup
commited95a507ee65ca35dbe34457a8c88505e851a2fe authorAndy Arvanitis <andyarvanitis@gmail.com> dateSat 1 Sep 2018, 16:50 (8 years ago) parent3d46983
2 files changed+56−54
runtime/purescript.h+28−50 runtime/string_literal_dict.h+28−4
runtime/purescript.h+28−50
@@ -38,51 +38,37 @@ namespace purescript {
3838 public:
3939 using std::shared_ptr<void>::shared_ptr;
4040
41− boxed() noexcept : std::shared_ptr<void>() {
42− }
43−
44− 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)) {
48− }
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)) {}
4944
5045 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
5247 if (n < std::numeric_limits<int>::min() || n > std::numeric_limits<int>::max()) {
5348 throw std::runtime_error("integer out of range");
5449 }
55−#endif // !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))) {
50+#endif // !defined(NDEBUG)
7151 }
7252
73− boxed(dict_t&& m) : std::shared_ptr<void>(std::make_shared<dict_t>(std::move(m))) {
74− }
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))) {}
58+ boxed(dict_t&& m) : std::shared_ptr<void>(std::make_shared<dict_t>(std::move(m))) {}
7559
7660 template <typename T,
7761 typename = typename std::enable_if<!std::is_same<boxed,T>::value>::type>
7862 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)) {
8065 }
8166
8267 template <typename T,
8368 typename = typename std::enable_if<!std::is_same<boxed,T>::value>::type>
8469 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)) {
8672 }
8773
8874 template <typename T,
@@ -109,36 +95,30 @@ namespace purescript {
10995 }
11096
11197 auto operator[](const char key[]) const -> const boxed& {
112− const auto& dict = *static_cast<const dict_t*>(get());
113− return dict[key];
98+ return (*static_cast<const dict_t*>(get()))[key];
11499 }
115100
116101 auto operator[](const char key[]) -> boxed& {
117− auto& dict = *static_cast<dict_t*>(get());
118− return dict[key];
102+ return (*static_cast<dict_t*>(get()))[key];
119103 }
120104
121−#ifdef NDEBUG
105+#if !defined(NDEBUG) // if debug build
122106 auto operator[](const int index) const -> const boxed& {
123− const auto& array = *static_cast<const array_t*>(get());
124− return array[index];
107+ return static_cast<const array_t*>(get())->at(index);
125108 }
126109
127110 auto operator[](const int index) -> boxed& {
128− auto& array = *static_cast<array_t*>(get());
129− return array[index];
111+ return static_cast<array_t*>(get())->at(index);
130112 }
131−#else
113+#else // not debug build
132114 auto operator[](const int index) const -> const boxed& {
133− const auto& array = *static_cast<const array_t*>(get());
134− return array.at(index);
115+ return (*static_cast<const array_t*>(get()))[index];
135116 }
136117
137118 auto operator[](const int index) -> boxed& {
138− auto& array = *static_cast<array_t*>(get());
139− return array.at(index);
119+ return (*static_cast<array_t*>(get()))[index];
140120 }
141−#endif // NDEBUG
121+#endif // !defined(NDEBUG)
142122
143123 }; // class boxed
144124
@@ -148,9 +128,8 @@ namespace purescript {
148128 }
149129
150130 template <typename T>
151− inline auto unbox(const boxed& b) -> const T& {
152− const auto& unboxed = *static_cast<const T*>(b.get());
153− return unboxed;
131+ constexpr inline auto unbox(const boxed& b) -> const T& {
132+ return *static_cast<const T*>(b.get());
154133 }
155134
156135 template <typename T,
@@ -167,9 +146,8 @@ namespace purescript {
167146 }
168147
169148 template <typename T>
170− inline auto peek(boxed& b) -> T& {
171− auto& unboxed = *static_cast<T*>(b.get());
172− return unboxed;
149+ constexpr inline auto peek(boxed& b) -> T& {
150+ return *static_cast<T*>(b.get());
173151 }
174152
175153 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+//
124#ifndef string_literal_dict_H
225#define string_literal_dict_H
326
427#include <cstring>
5−#include <string>
628#include <vector>
729#include <utility>
8−
30+#if !defined(NDEBUG)
31+#include <string>
32+#endif
933
1034template <typename T>
1135class 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>> {
2549 return it->second;
2650 }
2751 }
28−#ifndef NDEBUG
29− throw std::runtime_error("dictionary key \"" + std::string(key) + "\" not found");
52+#if !defined(NDEBUG) // if debug build
53+ throw std::runtime_error("dictionary key \"" + std::string(key) + "\" not found");
3054#endif
3155 return null();
3256 }