123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- #ifndef MODULES_INFERENCE_INCLUDE_REFLEX_OBJECT_H_
- #define MODULES_INFERENCE_INCLUDE_REFLEX_OBJECT_H_
- #include <functional>
- #include <map>
- #include <memory>
- #include <string>
- #define DECLARE_REFLEX_OBJECT(Class) \
- public: \
- static cnstream::ClassInfo<cnstream::ReflexObject> sclass_info; \
- \
- protected: \
- const cnstream::ClassInfo<cnstream::ReflexObject>& class_info() const;
- #define IMPLEMENT_REFLEX_OBJECT(Class) \
- cnstream::ClassInfo<ReflexObject> Class::sclass_info(std::string(#Class), \
- cnstream::ObjectConstructor<cnstream::ReflexObject>([]() { \
- return reinterpret_cast<cnstream::ReflexObject*>(new Class); \
- }), \
- true); \
- const cnstream::ClassInfo<cnstream::ReflexObject>& Class::class_info() const { return sclass_info; }
- #define DECLARE_REFLEX_OBJECT_EX(Class, BaseType) \
- public: \
- static cnstream::ClassInfo<BaseType> sclass_info; \
- \
- protected: \
- const cnstream::ClassInfo<BaseType>& class_info() const;
- #define IMPLEMENT_REFLEX_OBJECT_EX(Class, BaseType) \
- cnstream::ClassInfo<BaseType> Class::sclass_info( \
- std::string(#Class), \
- cnstream::ObjectConstructor<BaseType>([]() { return reinterpret_cast<BaseType*>(new Class); }), true); \
- const cnstream::ClassInfo<BaseType>& Class::class_info() const { return sclass_info; }
- namespace cnstream {
- template <typename T>
- using ObjectConstructor = std::function<T*()>;
- template <typename T>
- class ClassInfo {
- public:
- ClassInfo(const std::string& name, const ObjectConstructor<T>& constructor, bool regist = false);
- T* CreateObject() const;
- std::string name() const;
- bool Register() const;
- const ObjectConstructor<T>& constructor() const;
- private:
- std::string name_;
- ObjectConstructor<T> constructor_;
- };
- class ReflexObject {
- public:
- static ReflexObject* CreateObject(const std::string& name);
- static bool Register(const ClassInfo<ReflexObject>& info);
- virtual ~ReflexObject() = 0;
- #ifdef UNIT_TEST
- static void Remove(const std::string& name);
- #endif
- };
- template <typename T>
- class ReflexObjectEx : public ReflexObject {
- public:
- static T* CreateObject(const std::string& name);
- static bool Register(const ClassInfo<T>& info);
- virtual ~ReflexObjectEx() = 0;
- };
- template <typename T>
- ClassInfo<T>::ClassInfo(const std::string& name, const ObjectConstructor<T>& constructor, bool regist)
- : name_(name), constructor_(constructor) {
- if (regist) {
- Register();
- }
- }
- template <typename T>
- inline std::string ClassInfo<T>::name() const {
- return name_;
- }
- template <typename T>
- inline const ObjectConstructor<T>& ClassInfo<T>::constructor() const {
- return constructor_;
- }
- template <typename T>
- inline bool ClassInfo<T>::Register() const {
- return ReflexObjectEx<T>::Register(*this);
- }
- template <typename T>
- T* ClassInfo<T>::CreateObject() const {
- if (NULL != constructor_) {
- return constructor_();
- }
- return nullptr;
- }
- template <typename T>
- T* ReflexObjectEx<T>::CreateObject(const std::string& name) {
- auto ptr = ReflexObject::CreateObject(name);
- if (nullptr == ptr) return nullptr;
- T* ret = dynamic_cast<T*>(ptr);
- if (nullptr == ret) {
- delete ptr;
- return nullptr;
- }
- return ret;
- }
- template <typename T>
- bool ReflexObjectEx<T>::Register(const ClassInfo<T>& info) {
-
- ObjectConstructor<ReflexObject> base_constructor = NULL;
- if (info.constructor() != NULL) {
- base_constructor = [info]() { return static_cast<ReflexObject*>(info.constructor()()); };
- }
- ClassInfo<ReflexObject> base_info(info.name(), base_constructor);
- return ReflexObject::Register(base_info);
- }
- template <typename T>
- ReflexObjectEx<T>::~ReflexObjectEx() {}
- }
- #endif
|