中间件底层,websocket

SerializePolymorphic.hpp 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. #ifndef INCLUDE_SF_SERIALIZEPOLYMORPHIC_HPP
  18. #define INCLUDE_SF_SERIALIZEPOLYMORPHIC_HPP
  19. namespace SF {
  20. class Archive;
  21. class I_SerializerPolymorphic
  22. {
  23. public:
  24. virtual ~I_SerializerPolymorphic() {}
  25. virtual bool invoke(void **ppvb, Archive &ar) = 0;
  26. };
  27. template<typename Base, typename Derived>
  28. class SerializerPolymorphic : public I_SerializerPolymorphic
  29. {
  30. public:
  31. SerializerPolymorphic()
  32. {}
  33. virtual bool invoke(void **ppvb, Archive &ar);
  34. };
  35. }
  36. #include <SF/Archive.hpp>
  37. #include <SF/Serializer.hpp>
  38. namespace SF {
  39. template<typename Base, typename Derived>
  40. bool SerializerPolymorphic<Base,Derived>::invoke(void **ppvb, Archive &ar)
  41. {
  42. if (ar.isWrite())
  43. {
  44. Base *pb = reinterpret_cast<Base *>(*ppvb);
  45. Derived *pd = static_cast<Derived *>(pb);
  46. ar & pd;
  47. }
  48. else if (ar.isRead())
  49. {
  50. if (ar.isFlagSet(Archive::POINTER))
  51. {
  52. Derived *pd = NULL;
  53. ar & pd;
  54. Base *pb = static_cast<Base *>(pd);
  55. *ppvb = pb;
  56. }
  57. else
  58. {
  59. Base *pb = reinterpret_cast<Base *>(*ppvb);
  60. Derived *pd = static_cast<Derived *>(pb);
  61. ar & *pd;
  62. }
  63. }
  64. return true;
  65. }
  66. }
  67. #endif // ! INCLUDE_SF_SERIALIZEPOLYMORPHIC_HPP