git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
C++: clean up 'any' ctors and casts
769e5e3Andy Arvanitis
pcc/include/purescript.cc+7−3
@@ -108,6 +108,7 @@ any::~any() {
108 } \
108 } \
109 valuePtr = &value; \
109 valuePtr = &value; \
110 } while (valuePtr->type != Type::Unknown); \
110 } while (valuePtr->type != Type::Unknown); \
111+ assert(false && "Unknown value type"); \
111 return FN(VAL);
112 return FN(VAL);
112
113
113 template <typename T>
114 template <typename T>
@@ -158,7 +159,6 @@ any::~any() {
158 }
159 }
159 template auto any::cast<any::vector>() const -> const vector&;
160 template auto any::cast<any::vector>() const -> const vector&;
160
161
161−
162 auto any::operator()(const any& arg) const -> any {
162 auto any::operator()(const any& arg) const -> any {
163 if (type == Type::Function) {
163 if (type == Type::Function) {
164 return (*f)(arg);
164 return (*f)(arg);
@@ -172,6 +172,7 @@ any::~any() {
172 }
172 }
173 valuePtr = &value;
173 valuePtr = &value;
174 } while (valuePtr->type != Type::Unknown);
174 } while (valuePtr->type != Type::Unknown);
175+ assert(false && "Unknown value type");
175 return nullptr;
176 return nullptr;
176 }
177 }
177
178
@@ -193,6 +194,7 @@ any::~any() {
193 }
194 }
194 valuePtr = &value;
195 valuePtr = &value;
195 } while (valuePtr->type != Type::Unknown);
196 } while (valuePtr->type != Type::Unknown);
197+ assert(false && "Unknown value type");
196 return nullptr;
198 return nullptr;
197 }
199 }
198
200
@@ -220,7 +222,7 @@ any::~any() {
220 RETURN_VALUE(Type::Vector, v, *)
222 RETURN_VALUE(Type::Vector, v, *)
221 }
223 }
222
224
223− auto any::extractPointer() const -> const void* {
225+ auto any::extractPointer() const -> void* {
224 RETURN_VALUE(Type::Pointer, p.get(),)
226 RETURN_VALUE(Type::Pointer, p.get(),)
225 }
227 }
226
228
@@ -235,7 +237,9 @@ any::~any() {
235 return value;
237 return value;
236 }
238 }
237 valuePtr = &value;
239 valuePtr = &value;
238− } while (true);
240+ } while (valuePtr->type != Type::Unknown);
241+ assert(false && "Unknown value type");
242+ return a;
239 }
243 }
240
244
241 auto any::operator[](const map_key_t& rhs) const -> const any& {
245 auto any::operator[](const map_key_t& rhs) const -> const any& {
pcc/include/purescript.hh+7−49
@@ -86,11 +86,6 @@ class any {
86 return std::make_shared<T>(std::move(arg));
86 return std::make_shared<T>(std::move(arg));
87 }
87 }
88
88
89− template <typename T, typename U>
90− static constexpr auto static_cast_shared(U arg) -> T* {
91− return static_cast<T*>(arg.get());
92− }
93−
94 private:
89 private:
95 union {
90 union {
96 mutable long i;
91 mutable long i;
@@ -111,55 +106,30 @@ class any {
111 any(const long val) : type(Type::Integer), i(val) {}
106 any(const long val) : type(Type::Integer), i(val) {}
112 any(const int val) : type(Type::Integer), i(val) {}
107 any(const int val) : type(Type::Integer), i(val) {}
113 any(const unsigned int val) : type(Type::Integer), i(val) {}
108 any(const unsigned int val) : type(Type::Integer), i(val) {}
114−
115 any(const double val) : type(Type::Double), d(val) {}
109 any(const double val) : type(Type::Double), d(val) {}
116−
117 any(const char val) : type(Type::Character), c(val) {}
110 any(const char val) : type(Type::Character), c(val) {}
118
111
112+ template <typename T, typename = typename std::enable_if<std::is_same<bool,T>::value>::type>
119− any(const bool val) : type(Type::Boolean), b(val) {}
113+ any(const T val) : type(Type::Boolean), b(val) {}
120
114
121 any(const string& val) : type(Type::String), s(make_shared<string>(val)) {}
115 any(const string& val) : type(Type::String), s(make_shared<string>(val)) {}
122 any(string&& val) : type(Type::String), s(make_shared<string>(std::move(val))) {}
116 any(string&& val) : type(Type::String), s(make_shared<string>(std::move(val))) {}
123−
124 any(const char val[]) : type(Type::String), s(make_shared<string>(val)) {}
117 any(const char val[]) : type(Type::String), s(make_shared<string>(val)) {}
125
118
126− any(const shared<string>& val) : type(Type::String), s(val) {}
127− any(shared<string>&& val) : type(Type::String), s(std::move(val)) {}
128−
129 any(const map& val) : type(Type::Map), m(make_shared<map>(val)) {}
119 any(const map& val) : type(Type::Map), m(make_shared<map>(val)) {}
130 any(map&& val) : type(Type::Map), m(make_shared<map>(std::move(val))) {}
120 any(map&& val) : type(Type::Map), m(make_shared<map>(std::move(val))) {}
131
121
132− any(const shared<map>& val) : type(Type::Map), m(val) {}
133− any(shared<map>&& val) : type(Type::Map), m(std::move(val)) {}
134−
135 any(const vector& val) : type(Type::Vector), v(make_shared<vector>(val)) {}
122 any(const vector& val) : type(Type::Vector), v(make_shared<vector>(val)) {}
136 any(vector&& val) : type(Type::Vector), v(make_shared<vector>(std::move(val))) {}
123 any(vector&& val) : type(Type::Vector), v(make_shared<vector>(std::move(val))) {}
137
124
138− any(const shared<vector>& val) : type(Type::Vector), v(val) {}
139− any(shared<vector>&& val) : type(Type::Vector), v(std::move(val)) {}
140−
141− any(const shared<fn>& val) : type(Type::Function), f(val) {}
142− any(shared<fn>&& val) : type(Type::Function), f(std::move(val)) {}
143−
144− template <typename T,
145− typename = typename std::enable_if<!std::is_same<any,T>::value>::type>
125+ template <typename T, typename = typename std::enable_if<!std::is_same<any,T>::value>::type>
146 any(const T& val, typename std::enable_if<std::is_assignable<fn,T>::value>::type* = 0)
126 any(const T& val, typename std::enable_if<std::is_assignable<fn,T>::value>::type* = 0)
147 : type(Type::Function), f(make_shared<fn>(val)) {}
127 : type(Type::Function), f(make_shared<fn>(val)) {}
148
128
149− // template <typename T>
150− // any(T&& val, typename std::enable_if<std::is_assignable<fn,T>::value>::type* = 0)
151− // : type(Type::Function), f(std::move(val)) {}
152−
153− any(const shared<eff_fn>& val) : type(Type::EffFunction), e(val) {}
154− any(shared<eff_fn>&& val) : type(Type::EffFunction), e(std::move(val)) {}
155−
156 template <typename T>
129 template <typename T>
157 any(const T& val, typename std::enable_if<std::is_assignable<eff_fn,T>::value>::type* = 0)
130 any(const T& val, typename std::enable_if<std::is_assignable<eff_fn,T>::value>::type* = 0)
158 : type(Type::EffFunction), e(make_shared<eff_fn>(val)) {}
131 : type(Type::EffFunction), e(make_shared<eff_fn>(val)) {}
159
132
160− any(const shared<thunk>& val) : type(Type::Thunk), t(val) {}
161− any(shared<thunk>&& val) : type(Type::Thunk), t(std::move(val)) {}
162−
163 template <typename T>
133 template <typename T>
164 any(const T& val, typename std::enable_if<std::is_assignable<thunk,T>::value>::type* = 0)
134 any(const T& val, typename std::enable_if<std::is_assignable<thunk,T>::value>::type* = 0)
165 : type(Type::Thunk), t(make_shared<thunk>(val)) {}
135 : type(Type::Thunk), t(make_shared<thunk>(val)) {}
@@ -168,12 +138,12 @@ class any {
168 any(const T& val, typename std::enable_if<std::is_assignable<shared<void>,T>::value>::type* = 0)
138 any(const T& val, typename std::enable_if<std::is_assignable<shared<void>,T>::value>::type* = 0)
169 : type(Type::Pointer), p(val) {}
139 : type(Type::Pointer), p(val) {}
170
140
171− any(std::nullptr_t) : type(Type::Pointer), p(nullptr) {}
172−
173 template <typename T>
141 template <typename T>
174 any(T&& val, typename std::enable_if<std::is_assignable<shared<void>,T>::value>::type* = 0)
142 any(T&& val, typename std::enable_if<std::is_assignable<shared<void>,T>::value>::type* = 0)
175 : type(Type::Pointer), p(std::move(val)) {}
143 : type(Type::Pointer), p(std::move(val)) {}
176
144
145+ any(std::nullptr_t) : type(Type::Pointer), p(nullptr) {}
146+
177 any(const any&);
147 any(const any&);
178 any(any&&);
148 any(any&&);
179
149
@@ -210,19 +180,7 @@ class any {
210
180
211 template <typename T>
181 template <typename T>
212 auto cast() const -> typename std::enable_if<std::is_assignable<shared<void>,T>::value, typename T::element_type*>::type {
182 auto cast() const -> typename std::enable_if<std::is_assignable<shared<void>,T>::value, typename T::element_type*>::type {
213− if (type == Type::Pointer) {
214− return static_cast_shared<typename T::element_type>(p);
183+ return static_cast<typename T::element_type*>(extractPointer());
215− }
216− const any* valuePtr = this;
217− do {
218− assert(valuePtr->type == Type::Thunk);
219− const any& value = (*valuePtr->t)(unthunk);
220− if (value.type == Type::Pointer) {
221− return static_cast_shared<typename T::element_type>(value.p);
222− }
223− valuePtr = &value;
224− } while (valuePtr->type != Type::Unknown);
225− return nullptr;
226 }
184 }
227
185
228 auto operator()(const any&) const -> any;
186 auto operator()(const any&) const -> any;
@@ -243,7 +201,7 @@ class any {
243 auto operator[](const vector::size_type) const -> const any&;
201 auto operator[](const vector::size_type) const -> const any&;
244 auto operator[](const any&) const -> const any&;
202 auto operator[](const any&) const -> const any&;
245
203
246− auto extractPointer() const -> const void*;
204+ auto extractPointer() const -> void*;
247
205
248 static auto extract_value(const any&) -> const any&;
206 static auto extract_value(const any&) -> const any&;
249
207