Add UserData

This commit is contained in:
Nomango 2019-10-13 23:43:15 +08:00
parent 225e703be4
commit f7fd54ef1f
5 changed files with 141 additions and 1 deletions

View File

@ -90,6 +90,7 @@
<ClInclude Include="..\..\src\kiwano\utils\LocalStorage.h" /> <ClInclude Include="..\..\src\kiwano\utils\LocalStorage.h" />
<ClInclude Include="..\..\src\kiwano\utils\FileSystem.h" /> <ClInclude Include="..\..\src\kiwano\utils\FileSystem.h" />
<ClInclude Include="..\..\src\kiwano\utils\ResourceCache.h" /> <ClInclude Include="..\..\src\kiwano\utils\ResourceCache.h" />
<ClInclude Include="..\..\src\kiwano\utils\UserData.h" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="..\..\src\kiwano\2d\action\Action.cpp" /> <ClCompile Include="..\..\src\kiwano\2d\action\Action.cpp" />
@ -149,6 +150,7 @@
<ClCompile Include="..\..\src\kiwano\utils\LocalStorage.cpp" /> <ClCompile Include="..\..\src\kiwano\utils\LocalStorage.cpp" />
<ClCompile Include="..\..\src\kiwano\utils\FileSystem.cpp" /> <ClCompile Include="..\..\src\kiwano\utils\FileSystem.cpp" />
<ClCompile Include="..\..\src\kiwano\utils\ResourceCache.cpp" /> <ClCompile Include="..\..\src\kiwano\utils\ResourceCache.cpp" />
<ClCompile Include="..\..\src\kiwano\utils\UserData.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup Label="ProjectConfigurations"> <ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32"> <ProjectConfiguration Include="Debug|Win32">

View File

@ -297,6 +297,9 @@
<ClInclude Include="..\..\src\kiwano\core\any.hpp"> <ClInclude Include="..\..\src\kiwano\core\any.hpp">
<Filter>core</Filter> <Filter>core</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\..\src\kiwano\utils\UserData.h">
<Filter>utils</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="..\..\src\kiwano\ui\Button.cpp"> <ClCompile Include="..\..\src\kiwano\ui\Button.cpp">
@ -470,5 +473,8 @@
<ClCompile Include="..\..\src\kiwano\utils\FileSystem.cpp"> <ClCompile Include="..\..\src\kiwano\utils\FileSystem.cpp">
<Filter>utils</Filter> <Filter>utils</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="..\..\src\kiwano\utils\UserData.cpp">
<Filter>utils</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -319,7 +319,7 @@ protected:
} }
protected: protected:
static const auto ANY_SMALL_SPACE_SIZE = 16U; static const auto ANY_SMALL_SPACE_SIZE = 8U;
template <typename _Ty> template <typename _Ty>
struct decayed_is_small : public std::bool_constant<sizeof(_Ty) <= ANY_SMALL_SPACE_SIZE> struct decayed_is_small : public std::bool_constant<sizeof(_Ty) <= ANY_SMALL_SPACE_SIZE>

View File

@ -0,0 +1,73 @@
// Copyright (c) 2018-2019 Kiwano - Nomango
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#include <kiwano/utils/UserData.h>
namespace kiwano
{
Any UserData::Get(String const& key, Any const& default_data) const
{
auto iter = data_.find(key);
if (iter != data_.end())
{
return iter->second;
}
return default_data;
}
void UserData::Set(String const& key, Any const& data)
{
data_.insert(std::make_pair(key, data));
}
void UserData::Set(Pair<String, Any> const& pair)
{
data_.insert(pair);
}
void UserData::Set(std::initializer_list<Pair<String, Any>> const& list)
{
for (const auto& pair : list)
{
data_.insert(pair);
}
}
void UserData::Set(DataMap const& map)
{
data_ = map;
}
bool UserData::Contains(String const& key) const
{
return data_.count(key) != 0;
}
const UnorderedMap<String, Any>& UserData::GetData() const
{
return data_;
}
void UserData::Clear()
{
data_.clear();
}
}

View File

@ -0,0 +1,59 @@
// Copyright (c) 2018-2019 Kiwano - Nomango
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#pragma once
#include <kiwano/core/core.h>
namespace kiwano
{
// UserData is a simple database for user
class KGE_API UserData
: public Singleton<UserData>
{
KGE_DECLARE_SINGLETON(UserData);
public:
using DataMap = UnorderedMap<String, Any>;
Any Get(String const& key, Any const& default_data = Any()) const;
void Set(String const& key, Any const& data);
void Set(Pair<String, Any> const& pair);
void Set(std::initializer_list<Pair<String, Any>> const& list);
void Set(DataMap const& map);
bool Contains(String const& key) const;
const DataMap& GetData() const;
void Clear();
private:
UserData() = default;
UserData(const UserData&) = default;
UserData& operator=(const UserData&) = default;
private:
DataMap data_;
};
}