中间件底层,websocket

Serializer.hpp 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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_SERIALIZER_HPP
  19. #define INCLUDE_SF_SERIALIZER_HPP
  20. #include <RCF/Exception.hpp>
  21. #include <RCF/Export.hpp>
  22. #include <RCF/MemStream.hpp>
  23. #include <RCF/TypeTraits.hpp>
  24. #include <SF/Archive.hpp>
  25. #include <SF/I_Stream.hpp>
  26. #include <SF/SerializeFundamental.hpp>
  27. #include <SF/SfNew.hpp>
  28. #include <RCF/Tools.hpp>
  29. namespace SF {
  30. // Generic serializer, subclassed by all other serializers.
  31. class RCF_EXPORT SerializerBase : Noncopyable
  32. {
  33. private:
  34. void invokeRead(Archive &ar);
  35. void invokeWrite(Archive &ar);
  36. // Following are overridden to provide type-specific operations.
  37. virtual std::string getTypeName() = 0;
  38. virtual void newObject(Archive &ar) = 0;
  39. virtual bool isDerived() = 0;
  40. virtual std::string getDerivedTypeName() = 0;
  41. virtual void getSerializerPolymorphic(const std::string &derivedTypeName) = 0;
  42. virtual void invokeSerializerPolymorphic(SF::Archive &) = 0;
  43. virtual void serializeContents(Archive &ar) = 0;
  44. virtual void addToInputContext(IStream *, const UInt32 &) = 0;
  45. virtual bool queryInputContext(IStream *, const UInt32 &) = 0;
  46. virtual void addToOutputContext(OStream *, UInt32 &) = 0;
  47. virtual bool queryOutputContext(OStream *, UInt32 &) = 0;
  48. virtual void setFromId() = 0;
  49. virtual void setToNull() = 0;
  50. virtual bool isNull() = 0;
  51. virtual bool isNonAtomic() = 0;
  52. public:
  53. SerializerBase();
  54. virtual ~SerializerBase();
  55. void invoke(Archive &ar);
  56. };
  57. //---------------------------------------------------------------------
  58. // Type-specific serializers
  59. // These pragmas concern Serializer<T>::newObject, but needs to be up here, probably because Serializer<T> is a template
  60. #ifdef _MSC_VER
  61. #pragma warning( push )
  62. #pragma warning( disable : 4675 ) // warning C4675: resolved overload was found by argument-dependent lookup
  63. #pragma warning( disable : 4702 ) // warning C4702: unreachable code
  64. #endif
  65. class I_SerializerPolymorphic;
  66. template<typename T>
  67. class Serializer : public SerializerBase
  68. {
  69. public:
  70. Serializer(T ** ppt);
  71. private:
  72. typedef ObjectId IdT;
  73. T ** ppt;
  74. I_SerializerPolymorphic * pf;
  75. IdT id;
  76. std::string getTypeName();
  77. void newObject(Archive &ar);
  78. bool isDerived();
  79. std::string getDerivedTypeName();
  80. void getSerializerPolymorphic(const std::string &derivedTypeName);
  81. void invokeSerializerPolymorphic(SF::Archive &ar);
  82. void serializeContents(Archive &ar);
  83. void addToInputContext(SF::IStream *stream, const UInt32 &nid);
  84. bool queryInputContext(SF::IStream *stream, const UInt32 &nid);
  85. void addToOutputContext(SF::OStream *stream, UInt32 &nid);
  86. bool queryOutputContext(SF::OStream *stream, UInt32 &nid);
  87. void setFromId();
  88. void setToNull();
  89. bool isNull();
  90. bool isNonAtomic();
  91. };
  92. #ifdef _MSC_VER
  93. #pragma warning( pop )
  94. #endif
  95. template<typename PPT>
  96. struct GetIndirection
  97. {
  98. typedef typename RCF::RemovePointer<PPT>::type PT;
  99. typedef typename RCF::IsPointer<PPT>::type is_single;
  100. typedef typename RCF::IsPointer<PT>::type is_double;
  101. typedef
  102. typename RCF::If<
  103. is_double,
  104. RCF::Int<2>,
  105. typename RCF::If<
  106. is_single,
  107. RCF::Int<1>,
  108. RCF::Int<0>
  109. >::type
  110. >::type Level;
  111. typedef
  112. typename RCF::RemoveCv<
  113. typename RCF::RemovePointer<
  114. typename RCF::RemoveCv<
  115. typename RCF::RemovePointer<
  116. typename RCF::RemoveCv<PPT>::type
  117. >::type
  118. >::type
  119. >::type
  120. >::type Base;
  121. };
  122. template<typename T>
  123. inline void invokeCustomSerializer(
  124. T **ppt,
  125. Archive &ar,
  126. int)
  127. {
  128. static_assert(!RCF::IsPointer<T>::value, "Incorrect serialization code.");
  129. Serializer<T>(ppt).invoke(ar);
  130. }
  131. template<typename U, typename T>
  132. inline void invokeSerializer(
  133. U *,
  134. T *,
  135. RCF::Int<0> *,
  136. const U &u,
  137. Archive &ar)
  138. {
  139. static_assert(!RCF::IsPointer<T>::value, "Incorrect serialization code.");
  140. T *pt = const_cast<T *>(&u);
  141. invokeCustomSerializer( (T **) (&pt), ar, 0);
  142. }
  143. template<typename U, typename T>
  144. inline void invokeSerializer(
  145. U *,
  146. T *,
  147. RCF::Int<1> *,
  148. const U &u,
  149. Archive &ar)
  150. {
  151. static_assert(!RCF::IsPointer<T>::value, "Incorrect serialization code.");
  152. invokeCustomSerializer( (T **) (&u), ar, 0);
  153. }
  154. template<typename U, typename T>
  155. inline void invokeSerializer(
  156. U *,
  157. T *,
  158. RCF::Int<2> *,
  159. const U &u,
  160. Archive &ar)
  161. {
  162. static_assert(!RCF::IsPointer<T>::value, "Incorrect serialization code.");
  163. invokeCustomSerializer( (T**) (u), ar, 0);
  164. }
  165. template<typename U>
  166. inline void invokeSerializer(U u, Archive &ar)
  167. {
  168. typedef typename GetIndirection<U>::Level Level;
  169. typedef typename GetIndirection<U>::Base T;
  170. static_assert(!RCF::IsPointer<T>::value, "Incorrect serialization code.");
  171. invokeSerializer( (U *) 0, (T *) 0, (Level *) 0, u, ar);
  172. }
  173. template<typename U>
  174. inline void invokePtrSerializer(U u, Archive &ar)
  175. {
  176. typedef typename GetIndirection<U>::Level Level;
  177. const int levelOfIndirection = Level::value;
  178. static_assert( levelOfIndirection == 1 || levelOfIndirection == 2, "Incorrect value from GetIndirection<>.");
  179. const bool isPointer = (levelOfIndirection == 2);
  180. //#ifdef _MSC_VER
  181. //#pragma warning(push)
  182. //#pragma warning(disable: 6326) // warning C6326: Potential comparison of a constant with another constant.
  183. //#endif
  184. ar.setFlag( SF::Archive::POINTER, isPointer );
  185. //#ifdef _MSC_VER
  186. //#pragma warning(pop)
  187. //#endif
  188. invokeSerializer(u, ar);
  189. }
  190. // Forward declaration.
  191. template<typename T>
  192. Archive & operator&(
  193. Archive & archive,
  194. const T & t);
  195. template<typename T>
  196. inline void serializeEnum(SF::Archive &ar, T &t)
  197. {
  198. int runtimeVersion = ar.getRuntimeVersion();
  199. if (runtimeVersion >= 2)
  200. {
  201. ar & SF::Archive::Flag(SF::Archive::NO_BEGIN_END);
  202. }
  203. if (ar.isRead())
  204. {
  205. std::int32_t n = 0;
  206. ar & n;
  207. t = T(n);
  208. }
  209. else /* if (ar.isWrite())) */
  210. {
  211. std::int32_t n = t;
  212. ar & n;
  213. }
  214. }
  215. template<typename T>
  216. inline void serializeInternal(Archive &archive, T &t)
  217. {
  218. // A compiler error here indicates that the class T has not implemented
  219. // an internal SF serialization function. Here are a few situations in
  220. // which this can happen:
  221. // * No serialization function was provided at all, in which case one
  222. // needs to be written (either internal or external).
  223. //
  224. // * The intention was to provide an external SF serialization function,
  225. // but the external serialization function definition is incorrect and
  226. // hence not visible to the framework.
  227. // * The class is a Protocol Buffers generated class, and the intention
  228. // was to serialize it as such, in which case RCF_FEATURE_PROTOBUF=1
  229. // needs to be defined.
  230. t.serialize(archive);
  231. }
  232. template<typename T>
  233. inline void serializeFundamentalOrNot(
  234. Archive & archive,
  235. T & t,
  236. RCF::TrueType *)
  237. {
  238. serializeFundamental(archive, t);
  239. }
  240. template<typename T>
  241. inline void serializeFundamentalOrNot(
  242. Archive & archive,
  243. T & t,
  244. RCF::FalseType *)
  245. {
  246. serializeInternal(archive, t);
  247. }
  248. template<typename T>
  249. inline void serializeEnumOrNot(
  250. Archive & archive,
  251. T & t,
  252. RCF::TrueType *)
  253. {
  254. serializeEnum(archive, t);
  255. }
  256. template<typename T>
  257. inline void serializeEnumOrNot(
  258. Archive & archive,
  259. T & t,
  260. RCF::FalseType *)
  261. {
  262. typedef typename RCF::IsFundamental<T>::type type;
  263. serializeFundamentalOrNot(archive, t, (type *) NULL);
  264. }
  265. template<typename T>
  266. inline void serialize(
  267. Archive & archive,
  268. T & t)
  269. {
  270. typedef typename std::is_enum<T>::type type;
  271. serializeEnumOrNot(archive, t, (type *) NULL);
  272. }
  273. template<typename T>
  274. inline void serialize_vc6(
  275. Archive & archive,
  276. T & t,
  277. const unsigned int)
  278. {
  279. serialize(archive, t);
  280. }
  281. template<typename T>
  282. inline void preserialize(
  283. Archive & archive,
  284. T *& pt,
  285. const unsigned int)
  286. {
  287. static_assert(!RCF::IsPointer<T>::value, "Incorrect serialization code.");
  288. typedef typename RCF::RemoveCv<T>::type U;
  289. serialize_vc6(archive, (U &) *pt, static_cast<const unsigned int>(0) );
  290. }
  291. template<typename T>
  292. Archive & operator&(
  293. Archive & archive,
  294. const T & t)
  295. {
  296. invokePtrSerializer(&t, archive);
  297. return archive;
  298. }
  299. template<typename T, typename U>
  300. inline void serializeAs(Archive & ar, T &t)
  301. {
  302. if (ar.isWrite())
  303. {
  304. U u = static_cast<U>(t);
  305. ar & u;
  306. }
  307. else
  308. {
  309. U u;
  310. ar & u;
  311. t = static_cast<T>(u);
  312. }
  313. }
  314. /// Instructs RCF to serialize enum class EnumType, as a BaseType object.
  315. #define SF_SERIALIZE_ENUM_CLASS(EnumType, BaseType) \
  316. void serialize(SF::Archive & ar, EnumType & e) \
  317. { \
  318. SF::serializeAs<EnumType, BaseType>(ar, e); \
  319. }
  320. }
  321. #include <SF/Registry.hpp>
  322. #include <SF/SerializePolymorphic.hpp>
  323. #include <SF/Stream.hpp>
  324. namespace SF {
  325. template<typename T>
  326. Serializer<T>::Serializer(T **ppt) :
  327. ppt(ppt),
  328. pf(),
  329. id()
  330. {}
  331. template<typename T>
  332. std::string Serializer<T>::getTypeName()
  333. {
  334. return SF::Registry::getSingleton().getTypeName( (T *) 0);
  335. }
  336. #ifdef _MSC_VER
  337. #pragma warning( push )
  338. #pragma warning( disable: 4702 )
  339. #endif
  340. template<typename T>
  341. void Serializer<T>::newObject(Archive &ar)
  342. {
  343. *ppt = sfNew((T *) NULL, (T **) NULL, ar);
  344. }
  345. #ifdef _MSC_VER
  346. #pragma warning( pop )
  347. #endif
  348. template<typename T>
  349. bool Serializer<T>::isDerived()
  350. {
  351. static const bool isFundamental = RCF::IsFundamental<T>::value;
  352. if RCF_CONSTEXPR(!isFundamental)
  353. {
  354. if ( *ppt && typeid(T) != typeid(**ppt) )
  355. {
  356. if ( !SF::Registry::getSingleton().isTypeRegistered(typeid(**ppt)) )
  357. {
  358. RCF::Exception e(RCF::RcfError_SfTypeRegistration, typeid(**ppt).name());
  359. RCF_THROW(e);
  360. }
  361. return true;
  362. }
  363. }
  364. return false;
  365. }
  366. template<typename T>
  367. std::string Serializer<T>::getDerivedTypeName()
  368. {
  369. return SF::Registry::getSingleton().getTypeName( typeid(**ppt) );
  370. }
  371. template<typename T>
  372. void Serializer<T>::getSerializerPolymorphic(
  373. const std::string &derivedTypeName)
  374. {
  375. pf = & SF::Registry::getSingleton().getSerializerPolymorphic(
  376. (T *) 0,
  377. derivedTypeName);
  378. }
  379. template<typename T>
  380. void Serializer<T>::invokeSerializerPolymorphic(SF::Archive &ar)
  381. {
  382. RCF_ASSERT(pf);
  383. void **ppvb = (void **) (ppt); // not even reinterpret_cast wants to touch this
  384. pf->invoke(ppvb, ar);
  385. }
  386. template<typename T>
  387. void Serializer<T>::serializeContents(Archive &ar)
  388. {
  389. preserialize(ar, *ppt, 0);
  390. }
  391. template<typename T>
  392. void Serializer<T>::addToInputContext(SF::IStream *stream, const UInt32 &nid)
  393. {
  394. ContextRead &ctx = stream->getTrackingContext();
  395. ctx.add(nid, IdT( (void *) (*ppt), &typeid(T)));
  396. }
  397. template<typename T>
  398. bool Serializer<T>::queryInputContext(SF::IStream *stream, const UInt32 &nid)
  399. {
  400. ContextRead &ctx = stream->getTrackingContext();
  401. return ctx.query(nid, id);
  402. }
  403. template<typename T>
  404. void Serializer<T>::addToOutputContext(SF::OStream *stream, UInt32 &nid)
  405. {
  406. ContextWrite &ctx = stream->getTrackingContext();
  407. ctx.add( IdT( (void *) *ppt, &typeid(T)), nid);
  408. }
  409. template<typename T>
  410. bool Serializer<T>::queryOutputContext(SF::OStream *stream, UInt32 &nid)
  411. {
  412. ContextWrite &ctx = stream->getTrackingContext();
  413. return ctx.query( IdT( (void *) *ppt, &typeid(T)), nid);
  414. }
  415. template<typename T>
  416. void Serializer<T>::setFromId()
  417. {
  418. *ppt = reinterpret_cast<T *>(id.first);
  419. }
  420. template<typename T>
  421. void Serializer<T>::setToNull()
  422. {
  423. *ppt = NULL;
  424. }
  425. template<typename T>
  426. bool Serializer<T>::isNull()
  427. {
  428. return *ppt == NULL;
  429. }
  430. template<typename T>
  431. bool Serializer<T>::isNonAtomic()
  432. {
  433. bool isFundamental = RCF::IsFundamental<T>::value;
  434. return !isFundamental;
  435. }
  436. } // namespace SF
  437. #endif // ! INCLUDE_SF_SERIALIZER_HPP