@@ -36,7 +36,7 @@
3636#include <string>
3737#include <array>
3838#include <deque>
39−#include <unordered_map>
39+#include <map>
4040#include <utility>
4141#include <stdexcept>
4242#include <iso646.h> // mostly for MS Visual Studio compiler
@@ -80,9 +80,13 @@ static constexpr size_t unknown_size = 64;
8080//
8181class any {
8282
83+ private:
84+ static constexpr auto _managed_ = 0x10;
85+
8386 public:
8487 enum class Tag {
85− Thunk = 0x10,
88+ Unknown,
89+ Thunk,
8690 Integer,
8791 Double,
8892 Character,
@@ -90,26 +94,31 @@ class any {
9094 Function,
9195 EffFunction,
9296 RawPointer,
93− String,
94− Map,
97+ String = _managed_,
98+ Dictionary,
9599 Data,
96100 Array,
97101 Record,
98102 Closure,
99103 EffClosure,
100− Pointer
104+ Pointer,
105+ Invalid
101106 };
102107
103108 private:
104109 mutable Tag tag;
105110
111+ inline static auto isManaged(const Tag tag) -> decltype(_managed_) {
112+ return static_cast<decltype(_managed_)>(tag) & _managed_;
113+ }
114+ static_assert(static_cast<decltype(_managed_)>(Tag::Invalid) & _managed_, "outside of range");
115+
106116 public:
107117 struct as_thunk {
108118 };
109119 static constexpr as_thunk unthunk = as_thunk{};
110120
111121 using dict_pair = std::pair<const Private::Symbol, const any>;
112−
113122 template <size_t N>
114123 using dict = std::array<const dict_pair, N>;
115124
@@ -117,7 +126,14 @@ class any {
117126 using data = std::array<const any, N>;
118127
119128 using array = std::deque<any WITH_ALLOCATOR(any)>;
120− using record = std::unordered_map<std::string, any WITH_ALLOCATOR_PAIR(std::string, any)>;
129+
130+ struct cstr_cmp {
131+ auto operator ()(const char *, const char *) const -> bool;
132+ };
133+ using record = std::map<const char *,
134+ any,
135+ cstr_cmp
136+ WITH_ALLOCATOR_PAIR(const char *, any)>;
121137
122138 using fn = auto (*)(const any&) -> any;
123139 using eff_fn = auto (*)() -> any;
@@ -158,20 +174,16 @@ class any {
158174
159175 private:
160176 union {
161− mutable thunk t;
162− mutable int i;
163− mutable double d;
164− mutable char32_t c;
165− mutable bool b;
166− mutable fn f;
167− mutable eff_fn e;
168− mutable void * v;
169− mutable managed<string> s;
170− mutable managed<array> a;
171− mutable managed<record> r;
172− mutable managed<Closure> l;
173− mutable managed<EffClosure> k;
174− mutable managed<void> p;
177+ mutable thunk t;
178+ mutable int i;
179+ mutable double d;
180+ mutable char32_t c;
181+ mutable bool b;
182+ mutable fn f;
183+ mutable eff_fn e;
184+ mutable void * v;
185+ mutable uint64_t u;
186+ mutable managed<void> p;
175187 };
176188
177189 public:
@@ -188,24 +200,24 @@ class any {
188200 template <typename T, typename = typename std::enable_if<std::is_same<bool,T>::value>::type>
189201 any(const T val) noexcept : tag(Tag::Boolean), b(val) {}
190202
191− any(const char * val) : tag(Tag::String), s(make_managed_and_finalized<string>(val)) {}
192− any(const string& val) : tag(Tag::String), s(make_managed_and_finalized<string>(val)) {}
193− any(string&& val) noexcept : tag(Tag::String), s(make_managed_and_finalized<string>(std::move(val))) {}
203+ any(const char * val) : tag(Tag::String), p(make_managed_and_finalized<string>(val)) {}
204+ any(const string& val) : tag(Tag::String), p(make_managed_and_finalized<string>(val)) {}
205+ any(string&& val) noexcept : tag(Tag::String), p(make_managed_and_finalized<string>(std::move(val))) {}
194206
195− any(const managed<string>& val) noexcept : tag(Tag::String), s(val) {}
196− any(managed<string>&& val) noexcept : tag(Tag::String), s(std::move(val)) {}
207+ any(const managed<string>& val) noexcept : tag(Tag::String), p(val) {}
208+ any(managed<string>&& val) noexcept : tag(Tag::String), p(std::move(val)) {}
197209
198210 template <size_t N>
199− any(dict<N>&& val) noexcept : tag(Tag::Map), p(make_managed<dict<N>>(std::move(val))) {}
211+ any(dict<N>&& val) noexcept : tag(Tag::Dictionary), p(make_managed<dict<N>>(std::move(val))) {}
200212
201213 template <size_t N>
202214 any(data<N>&& val) noexcept : tag(Tag::Data), p(make_managed<data<N>>(std::move(val))) {}
203215
204− any(const array& val) : tag(Tag::Array), a(make_managed<array>(val)) {}
205− any(array&& val) noexcept : tag(Tag::Array), a(make_managed<array>(std::move(val))) {}
216+ any(const array& val) : tag(Tag::Array), p(make_managed<array>(val)) {}
217+ any(array&& val) noexcept : tag(Tag::Array), p(make_managed<array>(std::move(val))) {}
206218
207− any(const record& val) : tag(Tag::Record), r(make_managed<record>(val)) {}
208− any(record&& val) noexcept : tag(Tag::Record), r(make_managed<record>(std::move(val))) {}
219+ any(const record& val) : tag(Tag::Record), p(make_managed<record>(val)) {}
220+ any(record&& val) noexcept : tag(Tag::Record), p(make_managed<record>(std::move(val))) {}
209221
210222 template <typename T>
211223 any(const T& val, typename std::enable_if<std::is_convertible<T,fn>::value>::type* = 0) noexcept
@@ -214,7 +226,7 @@ class any {
214226 template <typename T, typename = typename std::enable_if<!std::is_same<any,T>::value &&
215227 !std::is_convertible<T,fn>::value>::type>
216228 any(const T& val, typename std::enable_if<std::is_assignable<std::function<any(const any&)>,T>::value>::type* = 0)
217− : tag(Tag::Closure), l(make_managed<Closure_<T>>(val)) {}
229+ : tag(Tag::Closure), p(make_managed<Closure_<T>>(val)) {}
218230
219231 template <typename T>
220232 any(const T& val, typename std::enable_if<std::is_convertible<T,eff_fn>::value>::type* = 0) noexcept
@@ -224,7 +236,7 @@ class any {
224236 typename = typename std::enable_if<!std::is_same<any,T>::value &&
225237 !std::is_convertible<T,eff_fn>::value>::type>
226238 any(const T& val, typename std::enable_if<std::is_assignable<std::function<any()>,T>::value>::type* = 0)
227− : tag(Tag::EffClosure), k(make_managed<EffClosure_<T>>(val)) {}
239+ : tag(Tag::EffClosure), p(make_managed<EffClosure_<T>>(val)) {}
228240
229241 template <typename T>
230242 any(const T& val, typename std::enable_if<std::is_convertible<T,thunk>::value>::type* = 0) noexcept
@@ -249,80 +261,56 @@ class any {
249261
250262#if !defined(USE_GC)
251263 private:
252− template <typename T,
253− typename U,
254− typename = typename std::enable_if<!std::is_reference<T>::value>::type>
255− static constexpr auto move_if_rvalue(U&& u) noexcept -> U&& {
256− return static_cast<U&&>(u);
257− }
258−
259− template <typename T,
260− typename U,
261− typename = typename std::enable_if<std::is_reference<T>::value>::type>
262− static constexpr auto move_if_rvalue(U& u) noexcept -> U& {
263− return static_cast<U&>(u);
264+ auto destruct() const noexcept -> void {
265+ if (isManaged(tag)) {
266+ p.~managed<void>();
267+ }
264268 }
265269
266− template <typename T>
267− auto assign(T&& other) const noexcept -> void {
268− switch (other.tag) {
269− case Tag::Thunk: t = other.t; break;
270− case Tag::Integer: i = other.i; break;
271− case Tag::Double: d = other.d; break;
272− case Tag::Character: c = other.c; break;
273− case Tag::Boolean: b = other.b; break;
274− case Tag::Function: f = other.f; break;
275− case Tag::EffFunction: e = other.e; break;
276− case Tag::RawPointer: v = other.v; break;
277−
278− case Tag::String: new (&s) managed<string>(move_if_rvalue<T>(other.s)); break;
279− case Tag::Array: new (&a) managed<array>(move_if_rvalue<T>(other.a)); break;
280− case Tag::Record: new (&r) managed<record>(move_if_rvalue<T>(other.r)); break;
281− case Tag::Closure: new (&l) managed<Closure>(move_if_rvalue<T>(other.l)); break;
282− case Tag::EffClosure: new (&k) managed<EffClosure>(move_if_rvalue<T>(other.k)); break;
283− case Tag::Map:
284− case Tag::Data:
285− case Tag::Pointer: new (&p) managed<void>(move_if_rvalue<T>(other.p)); break;
286−
287− default: assert(false && "Bad 'any' tag"); break;
270+ static_assert(sizeof(u) >= sizeof(t), "size assumption");
271+ static_assert(sizeof(u) >= sizeof(i), "size assumption");
272+ static_assert(sizeof(u) >= sizeof(d), "size assumption");
273+ static_assert(sizeof(u) >= sizeof(c), "size assumption");
274+ static_assert(sizeof(u) >= sizeof(b), "size assumption");
275+ static_assert(sizeof(u) >= sizeof(f), "size assumption");
276+ static_assert(sizeof(u) >= sizeof(e), "size assumption");
277+ static_assert(sizeof(u) >= sizeof(v), "size assumption");
278+
279+ auto copyAssign(const any& other) const noexcept -> void {
280+ if (isManaged(other.tag)) {
281+ new (&p) managed<void>(other.p);
282+ } else {
283+ u = other.u;
288284 }
289285 }
290−
291− auto destruct() -> void {
292− switch (tag) {
293− case Tag::String: s.~managed<string>(); break;
294− case Tag::Array: a.~managed<array>(); break;
295− case Tag::Record: r.~managed<record>(); break;
296− case Tag::Closure: l.~managed<Closure>(); break;
297− case Tag::EffClosure: k.~managed<EffClosure>(); break;
298− case Tag::Map:
299− case Tag::Data:
300− case Tag::Pointer: p.~managed<void>(); break;
301−
302− default: break;
286+ auto moveAssign(any& other) const noexcept -> void {
287+ if (isManaged(other.tag)) {
288+ new (&p) managed<void>(std::move(other.p));
289+ } else {
290+ u = other.u;
303291 }
304292 }
305293
306294 public:
307295 any(const any& other) noexcept : tag(other.tag) {
308− assign(other);
296+ copyAssign(other);
309297 }
310298
311299 any(any&& other) noexcept : tag(other.tag) {
312− assign(std::move(other));
300+ moveAssign(other);
313301 }
314302
315303 auto operator=(const any& rhs) noexcept -> any& {
316304 destruct();
317305 tag = rhs.tag;
318− assign(rhs);
306+ copyAssign(rhs);
319307 return *this;
320308 }
321309
322310 auto operator=(any&& rhs) noexcept -> any& {
323311 destruct();
324312 tag = rhs.tag;
325− assign(std::move(rhs));
313+ moveAssign(rhs);
326314 return *this;
327315 }
328316
@@ -350,7 +338,7 @@ class any {
350338 operator const record&() const;
351339
352340 auto operator[](const size_t) const -> const any&;
353− auto at(const std::string&) const -> const any&;
341+ auto at(const char *) const -> const any&;
354342 auto size() const -> size_t;
355343 auto empty() const -> bool;
356344 auto contains(const Private::Symbol) const -> bool;
@@ -429,9 +417,19 @@ namespace Private {
429417 template <typename T, typename U=void>
430418 struct TagHelper {};
431419
420+ template <>
421+ struct TagHelper<string> {
422+ static constexpr any::Tag tag = any::Tag::String;
423+ };
424+
425+ template <>
426+ struct TagHelper<const char *> {
427+ static constexpr any::Tag tag = any::Tag::String;
428+ };
429+
432430 template <size_t N>
433431 struct TagHelper<any::dict<N>> {
434− static constexpr any::Tag tag = any::Tag::Map;
432+ static constexpr any::Tag tag = any::Tag::Dictionary;
435433 };
436434
437435 template <size_t N>
@@ -439,6 +437,16 @@ namespace Private {
439437 static constexpr any::Tag tag = any::Tag::Data;
440438 };
441439
440+ template <>
441+ struct TagHelper<any::array> {
442+ static constexpr any::Tag tag = any::Tag::Array;
443+ };
444+
445+ template <>
446+ struct TagHelper<any::record> {
447+ static constexpr any::Tag tag = any::Tag::Record;
448+ };
449+
442450 template <typename T>
443451 struct TagHelper<T> {
444452 static constexpr any::Tag tag = any::Tag::Pointer;
@@ -458,19 +466,8 @@ inline auto cast(const any& a) ->
458466 return a;
459467}
460468
461−template <typename T>
462−inline auto cast(const any& a) ->
463− typename std::enable_if<std::is_same<T, string>::value ||
464− std::is_same<T, any::array>::value ||
465− std::is_same<T, any::record>::value, const T&>::type {
466− return a;
467−}
468−
469469template <typename T,
470− typename = typename std::enable_if<std::is_class<T>::value &&
471− !std::is_same<T,string>::value &&
472− !std::is_same<T,any::array>::value &&
473− !std::is_same<T,any::record>::value>::type>
470+ typename = typename std::enable_if<std::is_class<T>::value>::type>
474471inline auto cast(const any& a) -> T& {
475472 return *static_cast<T*>(a.extractPointer(IF_DEBUG(Private::TagHelper<T>::tag)));
476473}