diff --git a/src/kiwano/base/ObjectBase.cpp b/src/kiwano/base/ObjectBase.cpp index af095dd6..f0fc6729 100644 --- a/src/kiwano/base/ObjectBase.cpp +++ b/src/kiwano/base/ObjectBase.cpp @@ -57,12 +57,12 @@ namespace kiwano #endif } - void * ObjectBase::GetUserData() const + const Any& ObjectBase::GetUserData() const { return user_data_; } - void ObjectBase::SetUserData(void * data) + void ObjectBase::SetUserData(Any const& data) { user_data_ = data; } diff --git a/src/kiwano/base/ObjectBase.h b/src/kiwano/base/ObjectBase.h index b3c5f0bc..fbf61547 100644 --- a/src/kiwano/base/ObjectBase.h +++ b/src/kiwano/base/ObjectBase.h @@ -36,13 +36,13 @@ namespace kiwano virtual ~ObjectBase(); - void* GetUserData() const; + const Any& GetUserData() const; - void SetUserData(void* data); + void SetUserData(Any const& data); - void SetName(String const& name); + void SetName(String const& name); - String DumpObject(); + String DumpObject(); inline String GetName() const { if (name_) return *name_; return String(); } @@ -68,7 +68,7 @@ namespace kiwano private: bool tracing_leak_; - void* user_data_; + Any user_data_; String* name_; const uint32_t id_; diff --git a/src/kiwano/core/any.hpp b/src/kiwano/core/any.hpp index c8771e63..e01f86dc 100644 --- a/src/kiwano/core/any.hpp +++ b/src/kiwano/core/any.hpp @@ -36,7 +36,7 @@ public: virtual const char* what() const override { - return "bad and cast"; + return "bad any_cast"; } }; @@ -60,12 +60,14 @@ public: template < typename _Ty, - typename _Decayed = typename std::decay<_Ty>::type, typename... _Args > any(_Args&&... args) : storage_{} { - emplace<_Decayed>(std::forward<_Args>(args)...); + using _Decayed = typename std::decay<_Ty>::type; + + reset(); + emplace_decayed<_Decayed>(std::forward<_Args>(args)...); } any(const any& rhs) : storage_{} @@ -99,13 +101,15 @@ public: } template < - typename _Decayed, + typename _Ty, typename... _Args > void emplace(_Args&&... args) { + using _Decayed = typename std::decay<_Ty>::type; + reset(); - store<_Decayed>(decayed_is_small<_Decayed>{}, std::forward<_Args>(args)...); + emplace_decayed<_Decayed>(std::forward<_Args>(args)...); } void swap(any& rhs) noexcept @@ -196,6 +200,15 @@ protected: return storage_.small_.info_; } + template < + typename _Decayed, + typename... _Args + > + inline void emplace_decayed(_Args&&... args) + { + store<_Decayed>(decayed_is_small<_Decayed>{}, std::forward<_Args>(args)...); + } + template < typename _Decayed, typename... _Args @@ -340,9 +353,9 @@ protected: } template - static void* copy_impl(void* const ptr) noexcept + static void* copy_impl(const void* const ptr) noexcept { - return ::new _Ty(*static_cast<_Ty*>(ptr)); + return ::new _Ty(*static_cast(ptr)); } destroy_func* destroy; @@ -468,7 +481,9 @@ const _Ty* any_cast(const any* const a) noexcept template _Ty any_cast(any& a) { - const auto ptr = any_cast::type>(&a); + using _Decayed = typename std::decay<_Ty>::type; + + const auto ptr = any_cast<_Decayed>(&a); if (!ptr) { throw bad_any_cast{}; @@ -479,7 +494,9 @@ _Ty any_cast(any& a) template const _Ty any_cast(const any& a) { - const auto ptr = any_cast::type>(&a); + using _Decayed = typename std::decay<_Ty>::type; + + const auto ptr = any_cast<_Decayed>(&a); if (!ptr) { throw bad_any_cast{}; @@ -490,7 +507,9 @@ const _Ty any_cast(const any& a) template _Ty any_cast(any&& a) { - _Ty* ptr = a.cast_pointer<_Ty>(); + using _Decayed = typename std::decay<_Ty>::type; + + const auto ptr = any_cast<_Decayed>(&a); if (!ptr) { throw bad_any_cast{}; diff --git a/src/kiwano/core/core.h b/src/kiwano/core/core.h index c32ba3f0..11a06e12 100644 --- a/src/kiwano/core/core.h +++ b/src/kiwano/core/core.h @@ -29,6 +29,7 @@ #include #include +#include #include #include #include @@ -69,6 +70,8 @@ namespace kiwano template using Function = kiwano::core::function<_FuncTy>; + using Any = kiwano::core::any; + using Json = kiwano::core::basic_json; } diff --git a/src/kiwano/kiwano.h b/src/kiwano/kiwano.h index 11350c57..9db500d8 100644 --- a/src/kiwano/kiwano.h +++ b/src/kiwano/kiwano.h @@ -34,13 +34,14 @@ #include #include -#include -#include +#include +#include #include #include #include #include #include +#include //