reflex_object.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*************************************************************************
  2. * Copyright (C) [2019] by Cambricon, Inc. All rights reserved
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  13. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  15. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. * THE SOFTWARE.
  19. *************************************************************************/
  20. #ifndef MODULES_INFERENCE_INCLUDE_REFLEX_OBJECT_H_
  21. #define MODULES_INFERENCE_INCLUDE_REFLEX_OBJECT_H_
  22. #include <functional>
  23. #include <map>
  24. #include <memory>
  25. #include <string>
  26. #define DECLARE_REFLEX_OBJECT(Class) \
  27. public: \
  28. static cnstream::ClassInfo<cnstream::ReflexObject> sclass_info; \
  29. \
  30. protected: \
  31. const cnstream::ClassInfo<cnstream::ReflexObject>& class_info() const;
  32. #define IMPLEMENT_REFLEX_OBJECT(Class) \
  33. cnstream::ClassInfo<ReflexObject> Class::sclass_info(std::string(#Class), \
  34. cnstream::ObjectConstructor<cnstream::ReflexObject>([]() { \
  35. return reinterpret_cast<cnstream::ReflexObject*>(new Class); \
  36. }), \
  37. true); \
  38. const cnstream::ClassInfo<cnstream::ReflexObject>& Class::class_info() const { return sclass_info; }
  39. #define DECLARE_REFLEX_OBJECT_EX(Class, BaseType) \
  40. public: \
  41. static cnstream::ClassInfo<BaseType> sclass_info; \
  42. \
  43. protected: \
  44. const cnstream::ClassInfo<BaseType>& class_info() const;
  45. #define IMPLEMENT_REFLEX_OBJECT_EX(Class, BaseType) \
  46. cnstream::ClassInfo<BaseType> Class::sclass_info( \
  47. std::string(#Class), \
  48. cnstream::ObjectConstructor<BaseType>([]() { return reinterpret_cast<BaseType*>(new Class); }), true); \
  49. const cnstream::ClassInfo<BaseType>& Class::class_info() const { return sclass_info; }
  50. namespace cnstream {
  51. /*****************************************
  52. * [T]: The return type for reflection object.
  53. *****************************************/
  54. template <typename T>
  55. using ObjectConstructor = std::function<T*()>;
  56. template <typename T>
  57. class ClassInfo {
  58. public:
  59. ClassInfo(const std::string& name, const ObjectConstructor<T>& constructor, bool regist = false);
  60. T* CreateObject() const;
  61. std::string name() const;
  62. bool Register() const;
  63. const ObjectConstructor<T>& constructor() const;
  64. private:
  65. std::string name_;
  66. ObjectConstructor<T> constructor_;
  67. }; // class classinfo
  68. class ReflexObject {
  69. public:
  70. static ReflexObject* CreateObject(const std::string& name);
  71. static bool Register(const ClassInfo<ReflexObject>& info);
  72. virtual ~ReflexObject() = 0;
  73. #ifdef UNIT_TEST
  74. static void Remove(const std::string& name);
  75. #endif
  76. }; // class reflexobject<void>
  77. template <typename T>
  78. class ReflexObjectEx : public ReflexObject {
  79. public:
  80. static T* CreateObject(const std::string& name);
  81. static bool Register(const ClassInfo<T>& info);
  82. virtual ~ReflexObjectEx() = 0;
  83. }; // class reflectobject
  84. template <typename T>
  85. ClassInfo<T>::ClassInfo(const std::string& name, const ObjectConstructor<T>& constructor, bool regist)
  86. : name_(name), constructor_(constructor) {
  87. if (regist) {
  88. Register();
  89. }
  90. }
  91. template <typename T>
  92. inline std::string ClassInfo<T>::name() const {
  93. return name_;
  94. }
  95. template <typename T>
  96. inline const ObjectConstructor<T>& ClassInfo<T>::constructor() const {
  97. return constructor_;
  98. }
  99. template <typename T>
  100. inline bool ClassInfo<T>::Register() const {
  101. return ReflexObjectEx<T>::Register(*this);
  102. }
  103. template <typename T>
  104. T* ClassInfo<T>::CreateObject() const {
  105. if (NULL != constructor_) {
  106. return constructor_();
  107. }
  108. return nullptr;
  109. }
  110. template <typename T>
  111. T* ReflexObjectEx<T>::CreateObject(const std::string& name) {
  112. auto ptr = ReflexObject::CreateObject(name);
  113. if (nullptr == ptr) return nullptr;
  114. T* ret = dynamic_cast<T*>(ptr);
  115. if (nullptr == ret) {
  116. delete ptr;
  117. return nullptr;
  118. }
  119. return ret;
  120. }
  121. template <typename T>
  122. bool ReflexObjectEx<T>::Register(const ClassInfo<T>& info) {
  123. // build base ClassInfo(ClassInfo<ReflexObjectEx>)
  124. ObjectConstructor<ReflexObject> base_constructor = NULL;
  125. if (info.constructor() != NULL) {
  126. base_constructor = [info]() { return static_cast<ReflexObject*>(info.constructor()()); };
  127. }
  128. ClassInfo<ReflexObject> base_info(info.name(), base_constructor);
  129. return ReflexObject::Register(base_info);
  130. }
  131. template <typename T>
  132. ReflexObjectEx<T>::~ReflexObjectEx() {}
  133. } // namespace cnstream
  134. #endif // MODULES_INFERENCE_INCLUDE_REFLEX_OBJECT_HPP_