Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/GraphCtrl/GraphEvent/GEvent.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ class GEvent : public GEventObject {
std::mutex async_destroy_lock_;

friend class GEventManager;
friend class GStorage;
friend class GPipeline;
};

using GEventPtr = GEvent *;
Expand Down
3 changes: 2 additions & 1 deletion src/GraphCtrl/GraphEvent/GEventManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,11 @@ class GEventManager : public GEventObject,
CGRAPH_NO_ALLOWED_COPY(GEventManager)

private:
std::unordered_map<std::string, GEventPtr> events_map_; // event 管理类
std::unordered_map<std::string, GEventPtr> events_map_ {}; // event 管理类

friend class CAllocator;
friend class GPipeline;
friend class GStorage;

private:
CStatus __create_4py(GEventPtr event, const std::string& key);
Expand Down
6 changes: 3 additions & 3 deletions src/GraphCtrl/GraphPipeline/GPipeline.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,9 @@ class GPipeline : public GPipelineObject,
template<typename TGroup,
c_enable_if_t<std::is_base_of<GGroup, TGroup>::value, int> = 0>
TGroup* createGGroup(const GElementPtrArr &elements,
const GElementPtrSet &depends = std::initializer_list<GElementPtr>(),
const std::string &name = CGRAPH_EMPTY,
CSize loop = CGRAPH_DEFAULT_LOOP_TIMES);
const GElementPtrSet &depends = std::initializer_list<GElementPtr>(),
const std::string &name = CGRAPH_EMPTY,
CSize loop = CGRAPH_DEFAULT_LOOP_TIMES);

/**
* 在图中注册一个 GElement信息
Expand Down
49 changes: 44 additions & 5 deletions src/GraphCtrl/GraphPipeline/_GStroage/GStorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ UREFL_CREATE_STRUCT_TRAIT_INFO(_GElementStorage,
UREFL_DECLARE_FIELD(_GElementStorage, children_, 14)
)

UREFL_CREATE_STRUCT_TRAIT_INFO(_GEventStorage,
UREFL_DECLARE_FIELD(_GEventStorage, key_, 1),
UREFL_DECLARE_FIELD(_GEventStorage, event_clz_name_, 2)
)

UREFL_CREATE_STRUCT_TRAIT_INFO(UThreadPoolConfig,
UREFL_DECLARE_FIELD(UThreadPoolConfig, default_thread_size_, 1),
UREFL_DECLARE_FIELD(UThreadPoolConfig, secondary_thread_size_, 2),
Expand All @@ -58,7 +63,8 @@ UREFL_CREATE_STRUCT_TRAIT_INFO(UThreadPoolConfig,

UREFL_CREATE_STRUCT_TRAIT_INFO(_GPipelineStorage,
UREFL_DECLARE_FIELD(_GPipelineStorage, element_storages_, 1),
UREFL_DECLARE_FIELD(_GPipelineStorage, thread_pool_config_, 2)
UREFL_DECLARE_FIELD(_GPipelineStorage, event_storages_, 2),
UREFL_DECLARE_FIELD(_GPipelineStorage, thread_pool_config_, 3)
)

CGRAPH_INTERNAL_NAMESPACE_END
Expand All @@ -71,12 +77,18 @@ CGRAPH_INTERNAL_NAMESPACE_END
*/
CStatus GStorage::save(GPipelinePtr pipeline, const std::string& path) {
CGRAPH_FUNCTION_BEGIN
CGRAPH_ASSERT_NOT_NULL(pipeline);
CGRAPH_ASSERT_NOT_NULL(pipeline, pipeline->event_manager_);

_GPipelineStorage storage {};
for (const auto* cur : pipeline->repository_.elements_) {
storage.element_storages_.emplace_back(_GElementStorage(cur));
storage.element_storages_.emplace_back(cur);
}
for (const auto& event : pipeline->event_manager_->events_map_) {
const std::string& key = event.first;
const std::string& clz = typeid(*event.second).name();
storage.event_storages_.emplace_back(key, clz);
}

storage.thread_pool_config_ = pipeline->schedule_.config_;

auto refl = internal::UReflection<_GPipelineStorage>();
Expand Down Expand Up @@ -147,10 +159,23 @@ CStatus GStorage::loadBuffer(GPipelinePtr pipeline, char* buffer, CSize size) {
auto refl = internal::UReflection<_GPipelineStorage>();
refl.read(storage, (CUCharPtr)buffer, size);

status += loadElement(pipeline, storage);
status += loadEvent(pipeline, storage);
CGRAPH_FUNCTION_CHECK_STATUS

pipeline->setUniqueThreadPoolConfig(storage.thread_pool_config_);

CGRAPH_FUNCTION_END
}

CStatus GStorage::loadElement(GPipelinePtr pipeline, const _GPipelineStorage& storage) {
CGRAPH_FUNCTION_BEGIN
CGRAPH_ASSERT_NOT_NULL(pipeline)

std::map<std::string, GElementPtr> eleCache{}; // 用于存放所有的构建好的 element 信息
// 恢复所有的 element 的信息
for (const auto& cur : storage.element_storages_) {
auto element = dynamic_cast<GElementPtr>(GStorageFactory::createByType(cur.clz_name_.c_str()));
auto element = dynamic_cast<GElementPtr>(GStorageFactory::createByType(cur.clz_name_));
CGRAPH_RETURN_ERROR_STATUS_BY_CONDITION(!element,
cur.name_ + " element type do not register, please check")
CGRAPH_RETURN_ERROR_STATUS_BY_CONDITION(element->element_type_ != cur.element_type_,
Expand Down Expand Up @@ -191,7 +216,21 @@ CStatus GStorage::loadBuffer(GPipelinePtr pipeline, char* buffer, CSize size) {
}
}
}
pipeline->setUniqueThreadPoolConfig(storage.thread_pool_config_);

CGRAPH_FUNCTION_END
}


CStatus GStorage::loadEvent(GPipelinePtr pipeline, const _GPipelineStorage& storage) {
CGRAPH_FUNCTION_BEGIN
CGRAPH_ASSERT_NOT_NULL(pipeline, pipeline->event_manager_, pipeline->param_manager_)

pipeline->event_manager_->param_manager_ = pipeline->param_manager_;
for (const auto& es : storage.event_storages_) {
GEventPtr event = dynamic_cast<GEventPtr>(GStorageFactory::createByType(es.event_clz_name_));
CGRAPH_ASSERT_NOT_NULL(event)
pipeline->event_manager_->events_map_[es.key_] = event;
}

CGRAPH_FUNCTION_END
}
Expand Down
5 changes: 5 additions & 0 deletions src/GraphCtrl/GraphPipeline/_GStroage/GStorage.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#if __cplusplus >= 201703L

#include "GStorageDefine.h"
#include "../../GraphObject.h"

CGRAPH_NAMESPACE_BEGIN
Expand Down Expand Up @@ -49,6 +50,10 @@ class GStorage : public GraphObject {
*/
static CStatus loadBuffer(GPipeline* pipeline, char* buffer, CSize size);

static CStatus loadElement(GPipeline* pipeline, const _GPipelineStorage& storage);

static CStatus loadEvent(GPipeline* pipeline, const _GPipelineStorage& storage);

friend class GPipeline;
};

Expand Down
15 changes: 14 additions & 1 deletion src/GraphCtrl/GraphPipeline/_GStroage/GStorageDefine.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

#include "../../GraphDefine.h"
#include "../../GraphElement/GElementInclude.h"
#include "../../GraphEvent/GEventInclude.h"

CGRAPH_NAMESPACE_BEGIN

Expand Down Expand Up @@ -71,6 +72,7 @@ struct _GElementStorage : public CStruct {
element->setVisible(visible_);
element->setLevel(level_);
element->setTimeout(timeout_, timeout_strategy_);
element->session_ = session_;
if (element->isGNode()) {
element->setMacro(is_marco_);
}
Expand All @@ -81,9 +83,20 @@ struct _GElementStorage : public CStruct {
friend class GStorage;
};

struct _GEventStorage : public CStruct {
explicit _GEventStorage() = default;
explicit _GEventStorage(const std::string& key, const std::string& clz) {
key_ = key;
event_clz_name_ = clz;
}

std::string key_ {};
std::string event_clz_name_ {};
};

struct _GPipelineStorage : public CStruct {
std::vector<_GElementStorage> element_storages_ {}; // 记录pipeline中所有element 的信息
std::vector<_GElementStorage> element_storages_ {}; // 记录pipeline中所有 element 的信息
std::vector<_GEventStorage> event_storages_ {}; // 记录pipeline中所有 event 的信息
UThreadPoolConfig thread_pool_config_ {}; // 记录线程池配置信息
};

Expand Down
2 changes: 1 addition & 1 deletion src/GraphCtrl/GraphPipeline/_GStroage/GStorageFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ CGRAPH_NAMESPACE_BEGIN

std::map<std::string, std::function<GraphObject*()>> GStorageFactory::meta_types_ {};

GraphObject* GStorageFactory::createByType(const char* typeName) {
GraphObject* GStorageFactory::createByType(const std::string& typeName) {
const auto& it = meta_types_.find(typeName);
return (it == meta_types_.end()) ? nullptr : it->second();
}
Expand Down
4 changes: 2 additions & 2 deletions src/GraphCtrl/GraphPipeline/_GStroage/GStorageFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class GStorageFactory : public GraphObject {
|| std::is_base_of<GEvent, T>::value, int> = 0>
static CVoid registerMetaType() {
const char* key = typeid(T).name();
meta_types_[key] = []() { return new T(); };
meta_types_[key] = []() { return CAllocator::safeMallocCObject<T>(); };
}

protected:
Expand All @@ -45,7 +45,7 @@ class GStorageFactory : public GraphObject {
* @param typeName
* @return
*/
static GraphObject* createByType(const char* typeName);
static GraphObject* createByType(const std::string& typeName);

explicit GStorageFactory() = default;
~GStorageFactory() override = default;
Expand Down
Loading