Magic_Game/src/kiwano/core/common.h

153 lines
4.6 KiB
C
Raw Normal View History

2019-04-11 14:40:54 +08:00
// Copyright (c) 2016-2018 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
2019-10-10 15:09:38 +08:00
#include <set>
#include <map>
#include <list>
#include <queue>
2019-10-14 10:43:11 +08:00
#include <stack>
2019-10-10 15:09:38 +08:00
#include <unordered_set>
#include <unordered_map>
#include <sstream>
2019-12-17 20:47:55 +08:00
#include <3rd-party/OuterC/oc/oc.h>
#include <kiwano/macros.h>
2019-04-11 14:40:54 +08:00
namespace kiwano
{
2019-12-25 18:07:57 +08:00
/// \~chinese
/// @brief <20>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2019-12-17 20:47:55 +08:00
using String = oc::wstring;
2019-08-13 21:16:38 +08:00
2019-12-25 18:07:57 +08:00
/// \~chinese
/// @brief <20>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD>
using StringStream = std::wstringstream;
2019-12-25 18:07:57 +08:00
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2019-08-13 21:16:38 +08:00
template <typename _Ty, typename... _Args>
2019-12-17 20:47:55 +08:00
using Vector = oc::vector<_Ty, _Args...>;
2019-12-25 18:07:57 +08:00
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2019-08-13 21:16:38 +08:00
template <typename _Ty, typename... _Args>
using List = std::list<_Ty, _Args...>;
2019-12-25 18:07:57 +08:00
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2019-08-13 21:16:38 +08:00
template <typename _Ty, typename... _Args>
using Queue = std::queue<_Ty, _Args...>;
2019-12-25 18:07:57 +08:00
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2019-08-13 21:16:38 +08:00
template <typename _Ty, typename... _Args>
using Set = std::set<_Ty, _Args...>;
2019-12-25 18:07:57 +08:00
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2019-08-13 21:16:38 +08:00
template <typename _Ty1, typename _Ty2>
using Pair = std::pair<_Ty1, _Ty2>;
2019-12-25 18:07:57 +08:00
/// \~chinese
/// @brief <20><><EFBFBD>򼯺<EFBFBD><F2BCAFBA><EFBFBD><EFBFBD><EFBFBD>
2019-08-13 21:16:38 +08:00
template <typename _Ty, typename... _Args>
using UnorderedSet = std::unordered_set<_Ty, _Args...>;
2019-12-25 18:07:57 +08:00
/// \~chinese
/// @brief ջ<><D5BB><EFBFBD><EFBFBD>
2019-10-14 10:43:11 +08:00
template <typename _Ty, typename... _Args>
using Stack = std::stack<_Ty, _Args...>;
2019-12-25 18:07:57 +08:00
/// \~chinese
/// @brief <20>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2019-08-13 21:16:38 +08:00
template <typename _Kty, typename _Ty, typename... _Args>
using Map = std::map<_Kty, _Ty, _Args...>;
2019-12-25 18:07:57 +08:00
/// \~chinese
/// @brief <20>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2019-08-13 21:16:38 +08:00
template <typename _Kty, typename _Ty, typename... _Args>
using UnorderedMap = std::unordered_map<_Kty, _Ty, _Args...>;
2019-08-13 21:16:38 +08:00
2019-12-25 18:07:57 +08:00
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>װ<EFBFBD><D7B0>
/// @par
/// ʹ<>ú<EFBFBD><C3BA><EFBFBD><EFBFBD><EFBFBD>װ<EFBFBD><D7B0><EFBFBD><EFBFBD><EFBFBD>Դ<EFBFBD><D4B4><EFBFBD><E6A1A2><EFBFBD>ƺ͵<C6BA><CDB5><EFBFBD><EFBFBD>κοɵ<CEBF><C9B5><EFBFBD>Ŀ<EFBFBD>ʾ<EAA3AC><CABE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>£<EFBFBD>
/// @code
/// Function<bool(int)> func1 = StaticFunc; // bool StaticFunc(int x);
/// Function<bool(int)> func2 = Closure(&t, &T::Func); // bool T::Func(int x);
/// Function<bool(int)> func3 = T::StaticFunc; // static bool T::StaticFunc(int x);
/// Function<bool(int)> func4 = [](int x) -> bool {}; // Lambda function
/// Function<bool(int)> func5 = std::bind(&T::Func, &t); // std::bind
/// Function<bool(int)> func5 = Callable(); // Callable objects: struct Callable { bool operator()(int x) {} };
/// @endcode
2019-08-13 21:16:38 +08:00
template <typename _FuncTy>
2019-12-17 20:47:55 +08:00
using Function = oc::function<_FuncTy>;
2019-08-13 21:16:38 +08:00
2019-12-25 18:07:57 +08:00
/// \~chinese
/// @brief <20><>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD>
2019-12-17 20:47:55 +08:00
using Any = oc::any;
2019-10-13 17:45:58 +08:00
2019-12-25 18:07:57 +08:00
/// \~chinese
/// @brief JSON<4F><4E><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2019-12-17 20:47:55 +08:00
using Json = oc::basic_json<Map, Vector, String, int, double, bool, std::allocator>;
2019-10-30 23:12:18 +08:00
2019-12-25 18:07:57 +08:00
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD>ģ<EFBFBD><C4A3>
2019-10-30 23:12:18 +08:00
template <typename _Ty>
2019-12-17 20:47:55 +08:00
using Singleton = oc::singleton<_Ty>;
2019-10-30 23:12:18 +08:00
2019-12-25 18:07:57 +08:00
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2019-10-30 23:12:18 +08:00
template <typename _Ty>
2019-12-17 20:47:55 +08:00
using IntrusiveList = oc::intrusive_list<_Ty>;
2019-10-30 23:12:18 +08:00
2019-12-25 18:07:57 +08:00
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA>
2019-10-30 23:12:18 +08:00
template <typename _Ty>
2019-12-17 20:47:55 +08:00
using IntrusiveListItem = oc::intrusive_list_item<_Ty>;
2019-08-13 21:16:38 +08:00
2019-12-25 18:07:57 +08:00
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8>
2019-12-17 20:47:55 +08:00
template <typename _Ty, typename _RefProxyTy>
using IntrusivePtr = oc::intrusive_ptr<_Ty, _RefProxyTy>;
2019-12-25 18:07:57 +08:00
/// \~chinese
/// @brief <20><><EFBFBD>ɿ<EFBFBD><C9BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2019-12-17 20:47:55 +08:00
using Noncopyable = oc::noncopyable;
2019-12-25 18:07:57 +08:00
/// \~chinese
/// @brief <20>հ<EFBFBD><D5B0><EFBFBD><EFBFBD><EFBFBD>
2019-12-17 20:47:55 +08:00
template<typename _Ty, typename _Uty, typename _Ret, typename... _Args>
inline Function<_Ret(_Args...)> Closure(_Uty* ptr, _Ret(_Ty::* func)(_Args...))
2019-08-13 21:16:38 +08:00
{
2019-12-17 20:47:55 +08:00
return oc::closure(ptr, func);
}
2019-12-25 18:07:57 +08:00
/// \~chinese
/// @brief <20>հ<EFBFBD><D5B0><EFBFBD><EFBFBD><EFBFBD>
2019-12-17 20:47:55 +08:00
template<typename _Ty, typename _Uty, typename _Ret, typename... _Args>
inline Function<_Ret(_Args...)> Closure(_Uty* ptr, _Ret(_Ty::* func)(_Args...) const)
2019-08-13 21:16:38 +08:00
{
2019-12-17 20:47:55 +08:00
return oc::closure(ptr, func);
2019-08-13 21:16:38 +08:00
}
}