update kiwano::core::any

This commit is contained in:
Nomango 2019-10-13 17:45:58 +08:00
parent 720d1fd6e0
commit 225e703be4
5 changed files with 42 additions and 19 deletions

View File

@ -57,12 +57,12 @@ namespace kiwano
#endif #endif
} }
void * ObjectBase::GetUserData() const const Any& ObjectBase::GetUserData() const
{ {
return user_data_; return user_data_;
} }
void ObjectBase::SetUserData(void * data) void ObjectBase::SetUserData(Any const& data)
{ {
user_data_ = data; user_data_ = data;
} }

View File

@ -36,13 +36,13 @@ namespace kiwano
virtual ~ObjectBase(); 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(); } inline String GetName() const { if (name_) return *name_; return String(); }
@ -68,7 +68,7 @@ namespace kiwano
private: private:
bool tracing_leak_; bool tracing_leak_;
void* user_data_; Any user_data_;
String* name_; String* name_;
const uint32_t id_; const uint32_t id_;

View File

@ -36,7 +36,7 @@ public:
virtual const char* what() const override virtual const char* what() const override
{ {
return "bad and cast"; return "bad any_cast";
} }
}; };
@ -60,12 +60,14 @@ public:
template < template <
typename _Ty, typename _Ty,
typename _Decayed = typename std::decay<_Ty>::type,
typename... _Args typename... _Args
> >
any(_Args&&... args) : storage_{} 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_{} any(const any& rhs) : storage_{}
@ -99,13 +101,15 @@ public:
} }
template < template <
typename _Decayed, typename _Ty,
typename... _Args typename... _Args
> >
void emplace(_Args&&... args) void emplace(_Args&&... args)
{ {
using _Decayed = typename std::decay<_Ty>::type;
reset(); reset();
store<_Decayed>(decayed_is_small<_Decayed>{}, std::forward<_Args>(args)...); emplace_decayed<_Decayed>(std::forward<_Args>(args)...);
} }
void swap(any& rhs) noexcept void swap(any& rhs) noexcept
@ -196,6 +200,15 @@ protected:
return storage_.small_.info_; 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 < template <
typename _Decayed, typename _Decayed,
typename... _Args typename... _Args
@ -340,9 +353,9 @@ protected:
} }
template <typename _Ty> template <typename _Ty>
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<const _Ty*>(ptr));
} }
destroy_func* destroy; destroy_func* destroy;
@ -468,7 +481,9 @@ const _Ty* any_cast(const any* const a) noexcept
template <typename _Ty> template <typename _Ty>
_Ty any_cast(any& a) _Ty any_cast(any& a)
{ {
const auto ptr = any_cast<std::decay<_Ty>::type>(&a); using _Decayed = typename std::decay<_Ty>::type;
const auto ptr = any_cast<_Decayed>(&a);
if (!ptr) if (!ptr)
{ {
throw bad_any_cast{}; throw bad_any_cast{};
@ -479,7 +494,9 @@ _Ty any_cast(any& a)
template <typename _Ty> template <typename _Ty>
const _Ty any_cast(const any& a) const _Ty any_cast(const any& a)
{ {
const auto ptr = any_cast<std::decay<_Ty>::type>(&a); using _Decayed = typename std::decay<_Ty>::type;
const auto ptr = any_cast<_Decayed>(&a);
if (!ptr) if (!ptr)
{ {
throw bad_any_cast{}; throw bad_any_cast{};
@ -490,7 +507,9 @@ const _Ty any_cast(const any& a)
template <typename _Ty> template <typename _Ty>
_Ty any_cast(any&& a) _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) if (!ptr)
{ {
throw bad_any_cast{}; throw bad_any_cast{};

View File

@ -29,6 +29,7 @@
#include <kiwano/core/vector.hpp> #include <kiwano/core/vector.hpp>
#include <kiwano/core/string.hpp> #include <kiwano/core/string.hpp>
#include <kiwano/core/any.hpp>
#include <kiwano/core/intrusive_list.hpp> #include <kiwano/core/intrusive_list.hpp>
#include <kiwano/core/intrusive_ptr.hpp> #include <kiwano/core/intrusive_ptr.hpp>
#include <kiwano/core/noncopyable.hpp> #include <kiwano/core/noncopyable.hpp>
@ -69,6 +70,8 @@ namespace kiwano
template <typename _FuncTy> template <typename _FuncTy>
using Function = kiwano::core::function<_FuncTy>; using Function = kiwano::core::function<_FuncTy>;
using Any = kiwano::core::any;
using Json = kiwano::core::basic_json<kiwano::Map, kiwano::Vector, kiwano::String, using Json = kiwano::core::basic_json<kiwano::Map, kiwano::Vector, kiwano::String,
int, double, bool, std::allocator>; int, double, bool, std::allocator>;
} }

View File

@ -34,13 +34,14 @@
#include <kiwano/core/vector.hpp> #include <kiwano/core/vector.hpp>
#include <kiwano/core/string.hpp> #include <kiwano/core/string.hpp>
#include <kiwano/core/core.h> #include <kiwano/core/any.hpp>
#include <kiwano/core/Function.hpp> #include <kiwano/core/function.hpp>
#include <kiwano/core/intrusive_list.hpp> #include <kiwano/core/intrusive_list.hpp>
#include <kiwano/core/intrusive_ptr.hpp> #include <kiwano/core/intrusive_ptr.hpp>
#include <kiwano/core/noncopyable.hpp> #include <kiwano/core/noncopyable.hpp>
#include <kiwano/core/singleton.hpp> #include <kiwano/core/singleton.hpp>
#include <kiwano/core/basic_json.hpp> #include <kiwano/core/basic_json.hpp>
#include <kiwano/core/core.h>
// //