tc_dyn_object.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /**
  2. * Tencent is pleased to support the open source community by making Tars available.
  3. *
  4. * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved.
  5. *
  6. * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
  7. * in compliance with the License. You may obtain a copy of the License at
  8. *
  9. * https://opensource.org/licenses/BSD-3-Clause
  10. *
  11. * Unless required by applicable law or agreed to in writing, software distributed
  12. * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  13. * CONDITIONS OF ANY KIND, either express or implied. See the License for the
  14. * specific language governing permissions and limitations under the License.
  15. */
  16. #ifndef __TC_DYN_OBJECT_H
  17. #define __TC_DYN_OBJECT_H
  18. #include <iostream>
  19. namespace tars
  20. {
  21. /////////////////////////////////////////////////
  22. /**
  23. * @file tc_dyn_object.h
  24. * @brief 动态生成类.
  25. */
  26. /////////////////////////////////////////////////
  27. /**
  28. * @brief 动态生成类
  29. */
  30. class TC_DYN_Object;
  31. struct TC_DYN_RuntimeClass;
  32. struct TC_DYN_RuntimeClass
  33. {
  34. const char* m_lpszClassName;
  35. int m_nObjectSize;
  36. TC_DYN_Object* (* m_pfnCreateObject)();
  37. TC_DYN_RuntimeClass* m_pBaseClass;
  38. TC_DYN_RuntimeClass* m_pNextClass;
  39. static TC_DYN_RuntimeClass* pFirstClass;
  40. TC_DYN_Object* createObject();
  41. static TC_DYN_RuntimeClass* load(const char *szClassName);
  42. };
  43. struct TC_DYN_Init
  44. {
  45. TC_DYN_Init(TC_DYN_RuntimeClass* pNewClass)
  46. {
  47. pNewClass->m_pNextClass = TC_DYN_RuntimeClass::pFirstClass;
  48. TC_DYN_RuntimeClass::pFirstClass = pNewClass;
  49. }
  50. };
  51. class TC_DYN_Object
  52. {
  53. public:
  54. TC_DYN_Object(){};
  55. virtual ~TC_DYN_Object(){};
  56. virtual TC_DYN_RuntimeClass* GetRuntimeClass() const;
  57. bool isKindOf(const TC_DYN_RuntimeClass* pClass) const;
  58. private:
  59. TC_DYN_Object(const TC_DYN_Object& objectSrc);
  60. void operator=(const TC_DYN_Object& objectSrc);
  61. public:
  62. static TC_DYN_RuntimeClass classTC_DYN_Object;
  63. };
  64. #define RUNTIME_CLASS(class_name) ((TC_DYN_RuntimeClass*)(&class_name::class##class_name))
  65. #define DECLARE_DYNCREATE(class_name) \
  66. public: \
  67. static TC_DYN_RuntimeClass class##class_name; \
  68. virtual TC_DYN_RuntimeClass* GetRuntimeClass() const; \
  69. static TC_DYN_Object* createObject();
  70. #define IMPLEMENT_DYNCREATE(class_name, base_class_name) \
  71. TC_DYN_Object* class_name::createObject() \
  72. { return new class_name; } \
  73. TC_DYN_RuntimeClass class_name::class##class_name = { \
  74. #class_name, \
  75. sizeof(class_name), \
  76. &class_name::createObject, \
  77. RUNTIME_CLASS(base_class_name), \
  78. NULL }; \
  79. static TC_DYN_Init _init_##class_name(&class_name::class##class_name); \
  80. TC_DYN_RuntimeClass* class_name::GetRuntimeClass() const \
  81. { return RUNTIME_CLASS(class_name); }
  82. #define TC_DYN_CreateObject(class_name) \
  83. (TC_DYN_RuntimeClass::load(class_name) == NULL ? NULL : TC_DYN_RuntimeClass::load(class_name)->createObject())
  84. }
  85. #endif