-
Notifications
You must be signed in to change notification settings - Fork 370
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
194 changed files
with
737 additions
and
177 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
#pragma once | ||
#include <functional> | ||
#include <map> | ||
#include <memory> | ||
#include <type_traits> | ||
#include <typeindex> | ||
#include <typeinfo> | ||
#include <unordered_map> | ||
namespace TaoCommon | ||
{ | ||
|
||
//对象存储器 | ||
template <typename Key, typename Value> | ||
class ObjectMap | ||
{ | ||
public: | ||
virtual ~ObjectMap() | ||
{ | ||
clear(); | ||
} | ||
void setObj(const Key &key, const Value &obj) | ||
{ | ||
m_objMap[key] = obj; | ||
} | ||
Value getObj(const Key &key) const | ||
{ | ||
auto itor = m_objMap.find(key); | ||
if (itor == m_objMap.end()) | ||
{ | ||
return nullptr; | ||
} | ||
else | ||
{ | ||
return itor->second; | ||
} | ||
} | ||
void clear() | ||
{ | ||
m_objMap.clear(); | ||
} | ||
|
||
protected: | ||
std::unordered_map<Key, Value> m_objMap; | ||
}; | ||
//智能对象存储器。自动生成key,自动管理对象。 | ||
template <typename ObjectType> | ||
class CObjectMap : public ObjectMap<std::type_index, std::shared_ptr<ObjectType>> | ||
{ | ||
public: | ||
template <typename DeriveObjectType> | ||
DeriveObjectType *getObject() const | ||
{ | ||
static_assert(std::is_base_of<ObjectType, DeriveObjectType>::value, "DeriveObjectType must be derive from ObjectType"); | ||
auto objPtr = getObj(std::type_index(typeid(std::shared_ptr<DeriveObjectType>))); | ||
return std::static_pointer_cast<DeriveObjectType>(objPtr).get(); | ||
} | ||
template <typename DeriveObjectType, typename... Args> | ||
void setObject(Args &&... args) | ||
{ | ||
static_assert(std::is_base_of<ObjectType, DeriveObjectType>::value, "DeriveObjectType must be derive from ObjectType"); | ||
auto obj = std::make_shared<DeriveObjectType>(std::forward(args...)); | ||
setObj(std::type_index(typeid(obj)), std::static_pointer_cast<ObjectType>(obj)); | ||
} | ||
void forEach(const std::function<void(ObjectType *)> &callback) const | ||
{ | ||
for (const auto &pair : m_objMap) | ||
{ | ||
callback(pair.second.get()); | ||
} | ||
} | ||
}; | ||
//优先级对象存储器。自动生成key,自动管理对象。支持按优先级处理 | ||
template <typename ObjectType> | ||
class CLevelObjectMap | ||
{ | ||
public: | ||
virtual ~CLevelObjectMap() | ||
{ | ||
clear(); | ||
} | ||
template <typename DeriveObjectType> | ||
DeriveObjectType *getObject() const | ||
{ | ||
static_assert(std::is_base_of<ObjectType, DeriveObjectType>::value, "DeriveObjectType must be derive from ObjectType"); | ||
auto index = std::type_index(typeid(std::shared_ptr<DeriveObjectType>)); | ||
|
||
for (const auto &mainPair : m_map) | ||
{ | ||
const std::unordered_map<std::type_index, std::shared_ptr<ObjectType>> &subMap = mainPair.second; | ||
|
||
auto itor = subMap.find(index); | ||
if (itor != subMap.end()) | ||
{ | ||
return std::static_pointer_cast<DeriveObjectType>(itor->second).get(); | ||
} | ||
} | ||
return nullptr; | ||
} | ||
template <typename DeriveObjectType, typename... Args> | ||
void setObject(uint32_t level, Args &&... args) | ||
{ | ||
static_assert(std::is_base_of<ObjectType, DeriveObjectType>::value, "DeriveObjectType must be derive from ObjectType"); | ||
auto obj = std::make_shared<DeriveObjectType>(args...); | ||
m_map[level][std::type_index(typeid(obj))] = std::static_pointer_cast<ObjectType>(obj); | ||
} | ||
void forEach(const std::function<void(ObjectType *)> &callback) const | ||
{ | ||
for (const auto &mainPair : m_map) | ||
{ | ||
const std::unordered_map<std::type_index, std::shared_ptr<ObjectType>> &subMap = mainPair.second; | ||
for (const auto &subPair : subMap) | ||
{ | ||
callback(subPair.second.get()); | ||
} | ||
} | ||
} | ||
void clear() | ||
{ | ||
m_map.clear(); | ||
} | ||
|
||
private: | ||
std::map<uint32_t, std::unordered_map<std::type_index, std::shared_ptr<ObjectType>>> m_map; | ||
}; | ||
} // namespace TaoCommon |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#pragma once | ||
namespace TaoCommon | ||
{ | ||
//单例模板 | ||
template <typename T> | ||
class Singleton | ||
{ | ||
public: | ||
static T &instance() | ||
{ | ||
static T t; | ||
return t; | ||
} | ||
virtual ~Singleton() {} | ||
|
||
Singleton(const Singleton &) = delete; | ||
Singleton &operator=(const Singleton &) = delete; | ||
|
||
protected: | ||
Singleton() {} | ||
}; | ||
/* | ||
使用示例: | ||
定义: | ||
class DataManager : public Singleton<DataManager> | ||
{ | ||
friend class Singleton<DataManager>; | ||
public: | ||
void loadData(); | ||
protected: | ||
DataManager(); | ||
private: | ||
}; | ||
调用: | ||
DataManager::instance().loadData(); | ||
*/ | ||
|
||
} // namespace TaoCommon |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
#pragma once | ||
#include <algorithm> | ||
#include <map> | ||
#include <vector> | ||
namespace TaoCommon | ||
{ | ||
//观察者模式,Subject-Observer。 | ||
//Subject 事件或消息的主体。模板参数为观察者类型 | ||
template <typename ObserverType> | ||
class Subject | ||
{ | ||
public: | ||
virtual ~Subject() | ||
{ | ||
m_obsList.clear(); | ||
} | ||
//订阅 | ||
void subscibe(ObserverType *obs) | ||
{ | ||
auto itor = std::find(m_obsList.begin(), m_obsList.end(), obs); | ||
if (m_obsList.end() == itor) | ||
{ | ||
m_obsList.push_back(obs); | ||
} | ||
} | ||
//取消订阅 | ||
void unSubscibe(ObserverType *obs) | ||
{ | ||
m_obsList.erase(std::remove(m_obsList.begin(), m_obsList.end(), obs)); | ||
} | ||
//发布。这里的模板参数为函数类型。 | ||
template <typename FuncType> | ||
void publish(FuncType func) | ||
{ | ||
for (auto obs : m_obsList) | ||
{ | ||
//调用回调函数,将obs作为第一个参数传递 | ||
func(obs); | ||
} | ||
} | ||
//发布。支持过滤观察者。通常用在 观察者触发消息发布时,过滤观察者自己。 | ||
template <typename FuncType> | ||
void publish(FuncType func, ObserverType *exceptObs) | ||
{ | ||
for (auto obs : m_obsList) | ||
{ | ||
//调用回调函数,将obs作为第一个参数传递 | ||
if (obs != exceptObs) | ||
{ | ||
func(obs); | ||
} | ||
} | ||
} | ||
|
||
private: | ||
std::vector<ObserverType *> m_obsList; | ||
}; | ||
|
||
//优先级观察者模式,Subject-Observer。 | ||
template <typename ObserverType> | ||
class LevelSubject | ||
{ | ||
public: | ||
virtual ~LevelSubject() | ||
{ | ||
m_obsMap.clear(); | ||
} | ||
//订阅 | ||
void subscibe(ObserverType *obs, uint32_t level) | ||
{ | ||
auto &vec = m_obsMap[level]; | ||
auto itor = std::find(vec.begin(), vec.end(), obs); | ||
if (vec.end() == itor) | ||
{ | ||
vec.push_back(obs); | ||
} | ||
} | ||
//取消订阅 | ||
void unSubscibe(ObserverType *obs) | ||
{ | ||
for (auto &obsPair : m_obsMap) | ||
{ | ||
obsPair.second.erase(std::remove(obsPair.second.begin(), obsPair.second.end(), obs)); | ||
} | ||
} | ||
//发布。这里的模板参数为函数类型。 | ||
template <typename FuncType> | ||
void publish(FuncType func) | ||
{ | ||
for (const auto &obsPair : m_obsMap) | ||
{ | ||
for (const auto &obs : obsPair.second) | ||
{ | ||
func(obs); | ||
} | ||
} | ||
} | ||
template <typename FuncType> | ||
void publish(FuncType func, ObserverType *exceptObs) | ||
{ | ||
for (const auto &obsPair : m_obsMap) | ||
{ | ||
for (const auto &obs : obsPair.second) | ||
{ | ||
if (obs != exceptObs) | ||
{ | ||
func(obs); | ||
} | ||
} | ||
} | ||
} | ||
|
||
private: | ||
std::map<uint32_t, std::vector<ObserverType *>> m_obsMap; | ||
}; | ||
} // namespace TaoCommon |
Oops, something went wrong.