git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
C++: clean up 'any' ctors and casts
commit769e5e304878ade4054d4ab1cb66f765d1230950 authorAndy Arvanitis <andyarvanitis@gmail.com> dateSat 9 Jan 2016, 21:27 (11 years ago) parentf010163
2 files changed+14−52
pcc/include/purescript.cc+7−3 pcc/include/purescript.hh+7−49
pcc/include/purescript.cc+7−3
@@ -108,6 +108,7 @@ any::~any() {
108108 } \
109109 valuePtr = &value; \
110110 } while (valuePtr->type != Type::Unknown); \
111+ assert(false && "Unknown value type"); \
111112 return FN(VAL);
112113
113114 template <typename T>
@@ -158,7 +159,6 @@ any::~any() {
158159 }
159160 template auto any::cast<any::vector>() const -> const vector&;
160161
161−
162162 auto any::operator()(const any& arg) const -> any {
163163 if (type == Type::Function) {
164164 return (*f)(arg);
@@ -172,6 +172,7 @@ any::~any() {
172172 }
173173 valuePtr = &value;
174174 } while (valuePtr->type != Type::Unknown);
175+ assert(false && "Unknown value type");
175176 return nullptr;
176177 }
177178
@@ -193,6 +194,7 @@ any::~any() {
193194 }
194195 valuePtr = &value;
195196 } while (valuePtr->type != Type::Unknown);
197+ assert(false && "Unknown value type");
196198 return nullptr;
197199 }
198200
@@ -220,7 +222,7 @@ any::~any() {
220222 RETURN_VALUE(Type::Vector, v, *)
221223 }
222224
223− auto any::extractPointer() const -> const void* {
225+ auto any::extractPointer() const -> void* {
224226 RETURN_VALUE(Type::Pointer, p.get(),)
225227 }
226228
@@ -235,7 +237,9 @@ any::~any() {
235237 return value;
236238 }
237239 valuePtr = &value;
238− } while (true);
240+ } while (valuePtr->type != Type::Unknown);
241+ assert(false && "Unknown value type");
242+ return a;
239243 }
240244
241245 auto any::operator[](const map_key_t& rhs) const -> const any& {
pcc/include/purescript.hh+7−49
@@ -86,11 +86,6 @@ class any {
8686 return std::make_shared<T>(std::move(arg));
8787 }
8888
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−
9489 private:
9590 union {
9691 mutable long i;
@@ -111,55 +106,30 @@ class any {
111106 any(const long val) : type(Type::Integer), i(val) {}
112107 any(const int val) : type(Type::Integer), i(val) {}
113108 any(const unsigned int val) : type(Type::Integer), i(val) {}
114−
115109 any(const double val) : type(Type::Double), d(val) {}
116−
117110 any(const char val) : type(Type::Character), c(val) {}
118111
119− any(const bool val) : type(Type::Boolean), b(val) {}
112+ template <typename T, typename = typename std::enable_if<std::is_same<bool,T>::value>::type>
113+ any(const T val) : type(Type::Boolean), b(val) {}
120114
121115 any(const string& val) : type(Type::String), s(make_shared<string>(val)) {}
122116 any(string&& val) : type(Type::String), s(make_shared<string>(std::move(val))) {}
123−
124117 any(const char val[]) : type(Type::String), s(make_shared<string>(val)) {}
125118
126− any(const shared<string>& val) : type(Type::String), s(val) {}
127− any(shared<string>&& val) : type(Type::String), s(std::move(val)) {}
128−
129119 any(const map& val) : type(Type::Map), m(make_shared<map>(val)) {}
130120 any(map&& val) : type(Type::Map), m(make_shared<map>(std::move(val))) {}
131121
132− any(const shared<map>& val) : type(Type::Map), m(val) {}
133− any(shared<map>&& val) : type(Type::Map), m(std::move(val)) {}
134−
135122 any(const vector& val) : type(Type::Vector), v(make_shared<vector>(val)) {}
136123 any(vector&& val) : type(Type::Vector), v(make_shared<vector>(std::move(val))) {}
137124
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>
146126 any(const T& val, typename std::enable_if<std::is_assignable<fn,T>::value>::type* = 0)
147127 : type(Type::Function), f(make_shared<fn>(val)) {}
148128
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−
156129 template <typename T>
157130 any(const T& val, typename std::enable_if<std::is_assignable<eff_fn,T>::value>::type* = 0)
158131 : type(Type::EffFunction), e(make_shared<eff_fn>(val)) {}
159132
160− any(const shared<thunk>& val) : type(Type::Thunk), t(val) {}
161− any(shared<thunk>&& val) : type(Type::Thunk), t(std::move(val)) {}
162−
163133 template <typename T>
164134 any(const T& val, typename std::enable_if<std::is_assignable<thunk,T>::value>::type* = 0)
165135 : type(Type::Thunk), t(make_shared<thunk>(val)) {}
@@ -168,12 +138,12 @@ class any {
168138 any(const T& val, typename std::enable_if<std::is_assignable<shared<void>,T>::value>::type* = 0)
169139 : type(Type::Pointer), p(val) {}
170140
171− any(std::nullptr_t) : type(Type::Pointer), p(nullptr) {}
172−
173141 template <typename T>
174142 any(T&& val, typename std::enable_if<std::is_assignable<shared<void>,T>::value>::type* = 0)
175143 : type(Type::Pointer), p(std::move(val)) {}
176144
145+ any(std::nullptr_t) : type(Type::Pointer), p(nullptr) {}
146+
177147 any(const any&);
178148 any(any&&);
179149
@@ -210,19 +180,7 @@ class any {
210180
211181 template <typename T>
212182 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);
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;
183+ return static_cast<typename T::element_type*>(extractPointer());
226184 }
227185
228186 auto operator()(const any&) const -> any;
@@ -243,7 +201,7 @@ class any {
243201 auto operator[](const vector::size_type) const -> const any&;
244202 auto operator[](const any&) const -> const any&;
245203
246− auto extractPointer() const -> const void*;
204+ auto extractPointer() const -> void*;
247205
248206 static auto extract_value(const any&) -> const any&;
249207