中间件底层,websocket

Registry.hpp 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. //******************************************************************************
  2. // RCF - Remote Call Framework
  3. //
  4. // Copyright (c) 2005 - 2020, Delta V Software. All rights reserved.
  5. // http://www.deltavsoft.com
  6. //
  7. // RCF is distributed under dual licenses - closed source or GPL.
  8. // Consult your particular license for conditions of use.
  9. //
  10. // If you have not purchased a commercial license, you are using RCF
  11. // under GPL terms.
  12. //
  13. // Version: 3.2
  14. // Contact: support <at> deltavsoft.com
  15. //
  16. //******************************************************************************
  17. /// @file
  18. #ifndef INCLUDE_SF_REGISTRY_HPP
  19. #define INCLUDE_SF_REGISTRY_HPP
  20. #include <map>
  21. #include <string>
  22. #include <typeinfo>
  23. #include <memory>
  24. #include <RCF/Export.hpp>
  25. #include <RCF/ThreadLibrary.hpp>
  26. #include <RCF/Tools.hpp>
  27. namespace SF {
  28. typedef RCF::ReadWriteMutex ReadWriteMutex;
  29. typedef RCF::ReadLock ReadLock;
  30. typedef RCF::WriteLock WriteLock;
  31. class I_SerializerPolymorphic;
  32. class I_SerializerAny;
  33. class RCF_EXPORT Registry : Noncopyable
  34. {
  35. private:
  36. Registry();
  37. typedef std::string Rtti;
  38. typedef std::map<std::string, Rtti> TypenameToRtti;
  39. typedef std::map<Rtti, std::string> RttiToTypename;
  40. typedef std::map<
  41. std::pair<Rtti, Rtti>,
  42. std::shared_ptr<I_SerializerPolymorphic> >
  43. RttiToSerializerPolymorphic;
  44. typedef std::map<
  45. Rtti,
  46. std::shared_ptr<I_SerializerAny> >
  47. RttiToSerializerAny;
  48. TypenameToRtti mTypenameToRtti;
  49. RttiToTypename mRttiToTypename;
  50. RttiToSerializerPolymorphic mRttiToSerializerPolymorphic;
  51. RttiToSerializerAny mRttiToSerializerAny;
  52. ReadWriteMutex mReadWriteMutex;
  53. public:
  54. friend void initRegistrySingleton();
  55. static Registry &getSingleton();
  56. static Registry *getSingletonPtr();
  57. template<typename Type>
  58. void registerAny(Type *);
  59. template<typename Type>
  60. void registerType(Type *, const std::string &typeName);
  61. template<typename Base, typename Derived>
  62. void registerBaseAndDerived(Base *, Derived *);
  63. template<typename Base>
  64. I_SerializerPolymorphic &getSerializerPolymorphic(
  65. Base *,
  66. const std::string &derivedTypeName);
  67. template<typename T>
  68. std::string getTypeName()
  69. {
  70. return getTypeName( (T *) 0);
  71. }
  72. template<typename Type>
  73. void registerAny()
  74. {
  75. registerAny( (Type *) 0);
  76. }
  77. template<typename Type>
  78. void registerType(const std::string &typeName)
  79. {
  80. registerType( (Type *) 0);
  81. }
  82. template<typename Base, typename Derived>
  83. void registerBaseAndDerived()
  84. {
  85. registerBaseAndDerived( (Base *) 0, (Derived *) 0);
  86. }
  87. template<typename Base>
  88. I_SerializerPolymorphic &getSerializerPolymorphic(
  89. const std::string &derivedTypeName)
  90. {
  91. return getSerializerPolymorphic( (Base *) 0, derivedTypeName);
  92. }
  93. I_SerializerAny * getAnySerializer(const std::string &which);
  94. bool isTypeRegistered(const std::string &typeName);
  95. bool isTypeRegistered(const std::type_info &ti);
  96. template<typename T>
  97. std::string getTypeName(T *)
  98. {
  99. return getTypeName(typeid(T));
  100. }
  101. std::string getTypeName(const std::type_info &ti);
  102. void clear();
  103. };
  104. template<typename Type>
  105. inline void registerAny(Type *)
  106. {
  107. Registry::getSingleton().registerAny( (Type *) 0);
  108. }
  109. template<typename Type>
  110. inline void registerType(Type *, const std::string &typeName)
  111. {
  112. Registry::getSingleton().registerType( (Type *) 0, typeName);
  113. }
  114. template<typename Base, typename Derived>
  115. inline void registerBaseAndDerived( Base *, Derived *)
  116. {
  117. Registry::getSingleton().registerBaseAndDerived(
  118. (Base *) 0,
  119. (Derived *) 0);
  120. }
  121. } // namespace SF
  122. #include <SF/SerializePolymorphic.hpp>
  123. //#include <SF/SerializeAny.hpp>
  124. #include <SF/Serializer.hpp>
  125. namespace SF {
  126. /// Register a type T with SF runtime. When serializing polymorphic objects of
  127. /// type T, typeName will be written to the archive along with the serialized representation
  128. /// of T, to allow correct instantiation of type T when deserializing through a base type.
  129. template<typename T>
  130. void registerType(const std::string &typeName)
  131. {
  132. Registry::getSingleton().registerType( (T *) 0, typeName);
  133. }
  134. /// Register a base/derived relationship with the SF runtime. Allows SF to locate the correct
  135. /// serialization function for objects of type Derived, when Derived objects are serialized
  136. /// through a Base pointer.
  137. template<typename Base, typename Derived>
  138. void registerBaseAndDerived()
  139. {
  140. Registry::getSingleton().registerBaseAndDerived(
  141. (Base *) 0,
  142. (Derived *) 0);
  143. }
  144. template<typename T>
  145. class SerializerAny;
  146. template<typename Type>
  147. void Registry::registerAny(Type *)
  148. {
  149. WriteLock lock(mReadWriteMutex);
  150. RCF_UNUSED_VARIABLE(lock);
  151. Rtti typeRtti = typeid(Type).name();
  152. if ( mRttiToTypename.find(typeRtti) == mRttiToTypename.end() )
  153. {
  154. RCF::Exception e(RCF::RcfError_SfTypeRegistration, typeRtti);
  155. RCF_THROW(e);
  156. }
  157. mRttiToSerializerAny[typeRtti].reset(new SerializerAny<Type>());
  158. }
  159. template<typename Type>
  160. void Registry::registerType(Type *, const std::string &typeName)
  161. {
  162. WriteLock lock(mReadWriteMutex);
  163. RCF_UNUSED_VARIABLE(lock);
  164. Rtti typeRtti = typeid(Type).name();
  165. mRttiToTypename[typeRtti] = typeName;
  166. mTypenameToRtti[typeName] = typeRtti;
  167. // Instantiate Type's serialize function so we can register the
  168. // base/derived info.
  169. // NB: release build optimizers had better not eliminate this.
  170. //if (0)
  171. //{
  172. // serialize( *((Archive *) NULL), *((Type *) NULL), 0);
  173. //}
  174. }
  175. template<typename Base, typename Derived>
  176. void Registry::registerBaseAndDerived(Base *, Derived *)
  177. {
  178. WriteLock lock(mReadWriteMutex);
  179. RCF_UNUSED_VARIABLE(lock);
  180. Rtti baseRtti = typeid(Base).name();
  181. Rtti derivedRtti = typeid(Derived).name();
  182. std::pair<Rtti, Rtti> baseDerivedRtti(baseRtti, derivedRtti);
  183. mRttiToSerializerPolymorphic[baseDerivedRtti].reset(
  184. new SerializerPolymorphic<Base,Derived>);
  185. }
  186. template<typename Base>
  187. I_SerializerPolymorphic &Registry::getSerializerPolymorphic(
  188. Base *,
  189. const std::string &derivedTypeName)
  190. {
  191. ReadLock lock(mReadWriteMutex);
  192. RCF_UNUSED_VARIABLE(lock);
  193. Rtti baseRtti = typeid(Base).name();
  194. Rtti derivedRtti = mTypenameToRtti[derivedTypeName];
  195. std::pair<Rtti, Rtti> baseDerivedRtti(baseRtti, derivedRtti);
  196. if (
  197. mRttiToSerializerPolymorphic.find(baseDerivedRtti)
  198. == mRttiToSerializerPolymorphic.end())
  199. {
  200. RCF::Exception e( RCF::RcfError_SfBaseDerivedRegistration,
  201. baseRtti,
  202. derivedRtti);
  203. RCF_THROW(e);
  204. }
  205. return *mRttiToSerializerPolymorphic[ baseDerivedRtti ];
  206. }
  207. } // namespace SF
  208. #endif // ! INCLUDE_SF_REGISTRY_HPP