@@ -83,7 +83,8 @@ class any {
86− mutable Type type = Type::Unknown;
86+ mutable Type type;
87+ mutable bool is_shared;
89 struct as_thunk {
90 struct as_thunk {
@@ -102,6 +103,7 @@ class any {
102 virtual auto operator()(const any&) const -> any = 0;
103 virtual auto operator()(const any&) const -> any = 0;
103 virtual auto operator()(const any&, const any&) const -> any = 0;
104 virtual auto operator()(const any&, const any&) const -> any = 0;
104 virtual auto operator()(const any&, const any&, const any&) const -> any = 0;
105 virtual auto operator()(const any&, const any&, const any&) const -> any = 0;
106+ virtual ~closure() {}
107 template <typename T>
109 template <typename T>
@@ -204,83 +206,129 @@ class any {
207− any(const long val) : type(Type::Integer), i(val) {}
209+ any(const long val) : type(Type::Integer), is_shared(false), i(val) {}
208− any(const int val) : type(Type::Integer), i(val) {}
210+ any(const int val) : type(Type::Integer), is_shared(false), i(val) {}
209− any(const unsigned int val) : type(Type::Integer), i(val) {}
211+ any(const unsigned int val) : type(Type::Integer), is_shared(false), i(val) {}
210− any(const double val) : type(Type::Double), d(val) {}
212+ any(const double val) : type(Type::Double), is_shared(false), d(val) {}
211− any(const char val) : type(Type::Character), c(val) {}
213+ any(const char val) : type(Type::Character), is_shared(false), c(val) {}
213 template <typename T, typename = typename std::enable_if<std::is_same<bool,T>::value>::type>
215 template <typename T, typename = typename std::enable_if<std::is_same<bool,T>::value>::type>
214− any(const T val) : type(Type::Boolean), b(val) {}
216+ any(const T val) : type(Type::Boolean), is_shared(false), b(val) {}
216 template <size_t N>
218 template <size_t N>
217− any(const char (&val)[N]) : type(Type::StringLiteral), r(val) {}
219+ any(const char (&val)[N]) : type(Type::StringLiteral), is_shared(false), r(val) {}
218− any(char * val) : type(Type::String), s(make_shared<std::string>(val)) {}
220+ any(char * val) : type(Type::String), is_shared(true), s(make_shared<std::string>(val)) {}
220− any(const std::string& val) : type(Type::String), s(make_shared<std::string>(val)) {}
222+ any(const std::string& val) : type(Type::String), is_shared(true), s(make_shared<std::string>(val)) {}
221− any(std::string&& val) : type(Type::String), s(make_shared<std::string>(std::move(val))) {}
223+ any(std::string&& val) : type(Type::String), is_shared(true), s(make_shared<std::string>(std::move(val))) {}
223− any(const shared<std::string>& val) : type(Type::String), s(val) {}
225+ any(const shared<std::string>& val) : type(Type::String), is_shared(true), s(val) {}
224− any(shared<std::string>&& val) noexcept : type(Type::String), s(std::move(val)) {}
226+ any(shared<std::string>&& val) noexcept : type(Type::String), is_shared(true), s(std::move(val)) {}
226− any(const map& val) : type(Type::Map), m(make_shared<map>(val)) {}
228+ any(const map& val) : type(Type::Map), is_shared(true), m(make_shared<map>(val)) {}
227− any(map&& val) noexcept : type(Type::Map), m(make_shared<map>(std::move(val))) {}
229+ any(map&& val) noexcept : type(Type::Map), is_shared(true), m(make_shared<map>(std::move(val))) {}
229− any(const data& val) : type(Type::Data), v(make_shared<data>(val)) {}
231+ any(const data& val) : type(Type::Data), is_shared(true), v(make_shared<data>(val)) {}
230− any(data&& val) noexcept : type(Type::Data), v(make_shared<data>(std::move(val))) {}
232+ any(data&& val) noexcept : type(Type::Data), is_shared(true), v(make_shared<data>(std::move(val))) {}
232− any(const array& val) : type(Type::Array), a(make_shared<array>(val)) {}
234+ any(const array& val) : type(Type::Array), is_shared(true), a(make_shared<array>(val)) {}
233− any(array&& val) noexcept : type(Type::Array), a(make_shared<array>(std::move(val))) {}
235+ any(array&& val) noexcept : type(Type::Array), is_shared(true), a(make_shared<array>(std::move(val))) {}
235 template <typename T>
237 template <typename T>
236 any(const T& val, typename std::enable_if<std::is_convertible<T,fn>::value>::type* = 0)
238 any(const T& val, typename std::enable_if<std::is_convertible<T,fn>::value>::type* = 0)
237− : type(Type::Function), f(val) {}
239+ : type(Type::Function), is_shared(false), f(val) {}
239 template <typename T, typename = typename std::enable_if<!std::is_same<any,T>::value &&
241 template <typename T, typename = typename std::enable_if<!std::is_same<any,T>::value &&
240 !std::is_convertible<T,fn>::value>::type>
242 !std::is_convertible<T,fn>::value>::type>
241 any(const T& val, typename std::enable_if<std::is_assignable<std::function<any(const any&)>,T>::value>::type* = 0)
243 any(const T& val, typename std::enable_if<std::is_assignable<std::function<any(const any&)>,T>::value>::type* = 0)
242− : type(Type::Closure), l(make_shared<_closure<T>>(val)) {}
244+ : type(Type::Closure), is_shared(true), l(make_shared<_closure<T>>(val)) {}
244 template <typename T, typename = typename std::enable_if<!std::is_same<any,T>::value &&
246 template <typename T, typename = typename std::enable_if<!std::is_same<any,T>::value &&
245 !std::is_convertible<T,fn>::value>::type>
247 !std::is_convertible<T,fn>::value>::type>
246 any(const T& val, typename std::enable_if<std::is_assignable<std::function<any(const any&,
248 any(const T& val, typename std::enable_if<std::is_assignable<std::function<any(const any&,
247 const any&)>,T>::value>::type* = 0)
249 const any&)>,T>::value>::type* = 0)
248− : type(Type::Closure), l(make_shared<_closure2<T>>(val)) {}
250+ : type(Type::Closure), is_shared(true), l(make_shared<_closure2<T>>(val)) {}
250 template <typename T, typename = typename std::enable_if<!std::is_same<any,T>::value &&
252 template <typename T, typename = typename std::enable_if<!std::is_same<any,T>::value &&
251 !std::is_convertible<T,fn>::value>::type>
253 !std::is_convertible<T,fn>::value>::type>
252 any(const T& val, typename std::enable_if<std::is_assignable<std::function<any(const any&,
254 any(const T& val, typename std::enable_if<std::is_assignable<std::function<any(const any&,
253 const any&,
255 const any&,
254 const any&)>,T>::value>::type* = 0)
256 const any&)>,T>::value>::type* = 0)
255− : type(Type::Closure), l(make_shared<_closure3<T>>(val)) {}
257+ : type(Type::Closure), is_shared(true), l(make_shared<_closure3<T>>(val)) {}
257 template <typename T>
259 template <typename T>
258 any(const T& val, typename std::enable_if<std::is_assignable<std::function<any()>,T>::value>::type* = 0)
260 any(const T& val, typename std::enable_if<std::is_assignable<std::function<any()>,T>::value>::type* = 0)
259− : type(Type::EffFunction), e(make_shared<_eff_fn<T>>(val)) {}
261+ : type(Type::EffFunction), is_shared(true), e(make_shared<_eff_fn<T>>(val)) {}
261 template <typename T>
263 template <typename T>
262 any(const T& val, typename std::enable_if<std::is_convertible<T,thunk>::value>::type* = 0)
264 any(const T& val, typename std::enable_if<std::is_convertible<T,thunk>::value>::type* = 0)
263− : type(Type::Thunk), t(val) {}
265+ : type(Type::Thunk), is_shared(false), t(val) {}
265 template <typename T>
267 template <typename T>
266 any(const T& val, typename std::enable_if<std::is_assignable<shared<void>,T>::value>::type* = 0)
268 any(const T& val, typename std::enable_if<std::is_assignable<shared<void>,T>::value>::type* = 0)
267− : type(Type::Pointer), p(val) {}
269+ : type(Type::Pointer), is_shared(true), p(val) {}
269 template <typename T>
271 template <typename T>
270 any(T&& val, typename std::enable_if<std::is_assignable<shared<void>,T>::value>::type* = 0) noexcept
272 any(T&& val, typename std::enable_if<std::is_assignable<shared<void>,T>::value>::type* = 0) noexcept
271− : type(Type::Pointer), p(std::move(val)) {}
273+ : type(Type::Pointer), is_shared(true), p(std::move(val)) {}
273− any(std::nullptr_t) : type(Type::Pointer), p(nullptr) {}
275+ any(std::nullptr_t) : type(Type::Pointer), is_shared(true), p(nullptr) {}
275− any(const any&);
277+ private:
276− any(any&&) noexcept;
278+
279+ auto copy(const any& other) const -> void {
281+ new (&p) shared<void>(other.p);
278− auto operator=(const any&) -> any&;
287+ auto move(any& other) const noexcept -> void {
289+ new (&p) shared<void>(std::move(other.p));
295+ auto del() noexcept -> void {
303+ any(const any& other) : type(other.type), is_shared(other.is_shared) {
307+ any(any&& other) noexcept : type(other.type), is_shared(other.is_shared) {
279− auto operator=(any&) noexcept -> any&;
311+ auto operator=(const any& rhs) -> any& {
314+ is_shared = rhs.is_shared;
280− auto operator=(any&&) noexcept -> any&;
319+ auto operator=(any&& rhs) noexcept -> any& {
322+ is_shared = rhs.is_shared;
282 any() = delete;
327 any() = delete;
283− ~any();
329+ ~any() {
285 template <typename T>
333 template <typename T>
286 auto cast() const -> typename std::enable_if<std::is_same<T, long>::value, T>::type;
334 auto cast() const -> typename std::enable_if<std::is_same<T, long>::value, T>::type;