From ec77a143191a9f2c54631e6ddefcde9188525622 Mon Sep 17 00:00:00 2001 From: zhaohe Date: Sun, 10 Sep 2023 17:39:02 +0800 Subject: [PATCH] add zservice container --- .../iflytop_cancmd_server.hpp | 2 +- .../components/zcanreceiver/zcanreceiverhost.hpp | 2 +- .../zservice_container/zservice_container.cpp | 8 + .../zservice_container/zservice_container.hpp | 204 +++++++++++++++++++++ src/iflytop/core/core.hpp | 3 + 5 files changed, 217 insertions(+), 2 deletions(-) create mode 100644 src/iflytop/core/components/zservice_container/zservice_container.cpp create mode 100644 src/iflytop/core/components/zservice_container/zservice_container.hpp diff --git a/src/iflytop/components/iflytop_cancmd_server/iflytop_cancmd_server.hpp b/src/iflytop/components/iflytop_cancmd_server/iflytop_cancmd_server.hpp index 2d144d4..0e50fb7 100644 --- a/src/iflytop/components/iflytop_cancmd_server/iflytop_cancmd_server.hpp +++ b/src/iflytop/components/iflytop_cancmd_server/iflytop_cancmd_server.hpp @@ -13,10 +13,10 @@ #include #include +#include "iflytop/core/core.hpp" #include "iflytop/core/basic/nlohmann/json.hpp" #include "iflytop/core/driver/socketcan/socket_can.hpp" #include "iflytop/core/spdlogfactory/logger.hpp" -#include "zservice_container/zservice_container.hpp" /** * @brief diff --git a/src/iflytop/components/zcanreceiver/zcanreceiverhost.hpp b/src/iflytop/components/zcanreceiver/zcanreceiverhost.hpp index 3c215dc..84dfa8c 100644 --- a/src/iflytop/components/zcanreceiver/zcanreceiverhost.hpp +++ b/src/iflytop/components/zcanreceiver/zcanreceiverhost.hpp @@ -10,7 +10,7 @@ #include "iflytop/core/core.hpp" #include "iflytop/core/driver/socketcan/socket_can.hpp" #include "iflytop/core/spdlogfactory/logger.hpp" -#include "zservice_container/zservice_container.hpp" + namespace iflytop { diff --git a/src/iflytop/core/components/zservice_container/zservice_container.cpp b/src/iflytop/core/components/zservice_container/zservice_container.cpp new file mode 100644 index 0000000..4f93ceb --- /dev/null +++ b/src/iflytop/core/components/zservice_container/zservice_container.cpp @@ -0,0 +1,8 @@ +#include "zservice_container.hpp" +using namespace iflytop; +using namespace std; + +ServiceContrainer& ServiceContrainer::get() { + static ServiceContrainer serviceContrainer; + return serviceContrainer; +} \ No newline at end of file diff --git a/src/iflytop/core/components/zservice_container/zservice_container.hpp b/src/iflytop/core/components/zservice_container/zservice_container.hpp new file mode 100644 index 0000000..6846ea3 --- /dev/null +++ b/src/iflytop/core/components/zservice_container/zservice_container.hpp @@ -0,0 +1,204 @@ +// +// Created by zwsd +// + +#pragma once +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/** + * @brief + * + * service: ServiceContrainer + * + * 监听事件: + * 依赖状态: + * 依赖服务: + * 作用: + * + */ +// std::shared_ptr buf(new Buffer()); +// ZserviceContainerAny any(buf); +// std::shared_ptr b = any.Get>(); +// assert(buf.get() == b.get()); +namespace iflytop { +using namespace std; + +class ZserviceContainerAny { + public: + ZserviceContainerAny() : content_(nullptr) {} + ~ZserviceContainerAny() { delete content_; } + + template + explicit ZserviceContainerAny(const ValueType& value) : content_(new Holder(value)) {} + + ZserviceContainerAny(const ZserviceContainerAny& other) + : content_(other.content_ ? other.content_->clone() : nullptr) {} + + public: + ZserviceContainerAny& swap(ZserviceContainerAny& rhs) { + std::swap(content_, rhs.content_); + return *this; + } + + template + ZserviceContainerAny& operator=(const ValueType& rhs) { + ZserviceContainerAny(rhs).swap(*this); + return *this; + } + + ZserviceContainerAny& operator=(const ZserviceContainerAny& rhs) { + ZserviceContainerAny(rhs).swap(*this); + return *this; + } + + bool IsEmpty() const { return !content_; } + + const std::type_info& GetType() const { return content_ ? content_->GetType() : typeid(void); } + + template + ValueType operator()() const { + return Get(); + } + + template + ValueType Get() const { + if (GetType() == typeid(ValueType)) { + return static_cast*>(content_)->held_; + } else { + return ValueType(); + } + } + + protected: + class ZPlaceHolder { + public: + virtual ~ZPlaceHolder() {} + + public: + virtual const std::type_info& GetType() const = 0; + virtual ZPlaceHolder* clone() const = 0; + }; + + template + class Holder : public ZPlaceHolder { + public: + Holder(const ValueType& value) : held_(value) {} + + virtual const std::type_info& GetType() const { return typeid(ValueType); } + + virtual ZPlaceHolder* clone() const { return new Holder(held_); } + + ValueType held_; + }; + + protected: + ZPlaceHolder* content_; + template + friend ValueType* zany_cast(ZserviceContainerAny*); +}; + +template +ValueType* zany_cast(ZserviceContainerAny* any) { + if (any && any->GetType() == typeid(ValueType)) { + return &(static_cast*>(any->content_)->held_); + } + + return nullptr; +} + +template +const ValueType* zany_cast(const ZserviceContainerAny* any) { + return zany_cast(const_cast(any)); +} + +template +ValueType zany_cast(const ZserviceContainerAny& any) { + const ValueType* result = zany_cast(&any); + assert(result); + + if (!result) { + return ValueType(); + } + + return *result; +} + +class ServiceContrainer { + map services; + ServiceContrainer(){}; + + public: + static ServiceContrainer& get(); + + template + void regAndInitializeService(shared_ptr object) { + if (object == nullptr) { + return; + } + object->initialize(); + type_index index = typeid(T); + services[index] = ZserviceContainerAny(object); + }; + template + void regService(shared_ptr object) { + if (object == nullptr) { + return; + } + type_index index = typeid(T); + services[index] = ZserviceContainerAny(object); + }; + template + shared_ptr getService() { + type_index index = typeid(T); + auto result = services.find(index); + if (result == services.end()) { + return nullptr; + } + return result->second.Get>(); + }; + + template + void getToService(shared_ptr& service) { + type_index index = typeid(T); + auto result = services.find(index); + if (result == services.end()) { + service = nullptr; + } + service = result->second.Get>(); + }; +}; + +#define GET_SERVICE(type) (ServiceContrainer::get().getService()) +#define GET_TO_SERVICE(value) \ + ServiceContrainer::get().getToService(value); \ + if (value == nullptr) { \ + logger->error("[{}:{}]GET_TO_SERVICE({}) fail", __FILE__, __LINE__, #value); \ + exit(-1); \ + }; + +#define BUILD_AND_REG_SERRVICE(type, ...) \ + logger->info("build {}.....", #type); \ + shared_ptr type##_val(new type(__VA_ARGS__)); \ + ServiceContrainer::get().regService(type##_val); + +#define REG_SERRVICE(type,object) \ + shared_ptr type##_val(object); \ + ServiceContrainer::get().regService(type##_val); + +#define BUILD_AND_REG_MOCK_SERRVICE(type, mocktype, ...) \ + logger->info("build {}.....", #type); \ + shared_ptr type##_val(new mocktype(__VA_ARGS__)); \ + ServiceContrainer::get().regService(type##_val); + +} // namespace iflytop \ No newline at end of file diff --git a/src/iflytop/core/core.hpp b/src/iflytop/core/core.hpp index 2220817..ebb208f 100644 --- a/src/iflytop/core/core.hpp +++ b/src/iflytop/core/core.hpp @@ -20,6 +20,7 @@ #include "iflytop/core/error/error_code.hpp" #include "iflytop/core/spdlogfactory/logger.hpp" #include "iflytop/core/thread/thread.hpp" +#include "iflytop/core/components/fileutils.hpp" // #include "iflytop/core/components/sha256/sha256.hpp" @@ -28,6 +29,8 @@ #include "iflytop/core/utils/uuid/uuid.hpp" #include "iflytop/core/basic/ds/binary.hpp" #include "iflytop/core/components/jobs/work_queue.hpp" +#include "iflytop/core/linuxcoreutils/linuxcoreutils.hpp" +#include "iflytop/core/components/zservice_container/zservice_container.hpp" #define ZARRAYSIZE(a) (sizeof(a) / sizeof(a[0])) \ No newline at end of file