Skip to content

Commit

Permalink
move app to examples
Browse files Browse the repository at this point in the history
  • Loading branch information
jared2020 committed Jun 29, 2020
1 parent 5483b00 commit d7a9cca
Show file tree
Hide file tree
Showing 194 changed files with 737 additions and 177 deletions.
2 changes: 2 additions & 0 deletions .qmake.conf
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ TaoQuick_BUILD_TREE = $$PWD/build

TaoQuick_RUN_TREE = $$PWD/bin

TaoQuick_3RDPARTY_TREE = $$PWD/3rdparty

TaoQuick_VERSION_MAJOR = 0.3
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
#include <QJsonDocument>
#include <QJsonObject>
#include <QString>
#include "Logger/logger.h"
namespace TaoCommon
{
static bool readFile(const QString &filePath, QByteArray &content)
{
QFile file(filePath);
if (!file.open(QFile::ReadOnly))
{
qWarning() << "open file " << filePath << "failed:" << file.errorString();
LOG_WARN << "open file " << filePath << "failed:" << file.errorString();
return false;
}
content = file.readAll();
Expand All @@ -25,7 +26,7 @@ namespace TaoCommon
doc = QJsonDocument::fromJson(data, &err);
if (doc.isNull())
{
qWarning() << "parse json failed:" << err.errorString();
LOG_WARN << "parse json failed:" << err.errorString();
return false;
}
return true;
Expand Down Expand Up @@ -83,7 +84,7 @@ namespace TaoCommon
QFile file(filePath);
if (!file.open(QFile::WriteOnly))
{
qWarning() << "open file " << filePath << "failed:" << file.errorString();
LOG_WARN << "open file " << filePath << "failed:" << file.errorString();
return false;
}
file.write(content);
Expand Down
125 changes: 125 additions & 0 deletions 3rdparty/TaoCommon/Common/objectmap.h
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
42 changes: 42 additions & 0 deletions 3rdparty/TaoCommon/Common/singleton.h
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
116 changes: 116 additions & 0 deletions 3rdparty/TaoCommon/Common/subject.h
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
Loading

0 comments on commit d7a9cca

Please sign in to comment.