Remote Call Framework 3.3
SerializeSmartPtr.hpp
1 
2 //******************************************************************************
3 // RCF - Remote Call Framework
4 //
5 // Copyright (c) 2005 - 2022, Delta V Software. All rights reserved.
6 // https://www.deltavsoft.com
7 //
8 // RCF is distributed under dual licenses - closed source or GPL.
9 // Consult your particular license for conditions of use.
10 //
11 // If you have not purchased a commercial license, you are using RCF under GPL terms.
12 //
13 // Version: 3.3
14 // Contact: support <at> deltavsoft.com
15 //
16 //******************************************************************************
17 
18 #ifndef INCLUDE_SF_SERIALIZESMARTPTR_HPP
19 #define INCLUDE_SF_SERIALIZESMARTPTR_HPP
20 
21 #include <SF/Archive.hpp>
22 #include <SF/Stream.hpp>
23 
24 namespace SF {
25 
26  // 1. Non-ref counted smart pointer. SmartPtr<T> must support reset() and operator->().
27 
28  template<typename T, typename SmartPtrT>
29  inline void serializeSimpleSmartPtr(SmartPtrT **ppt, SF::Archive &ar)
30  {
31  if (ar.isRead())
32  {
33  if (ar.isFlagSet(Archive::POINTER))
34  {
35  *ppt = new SmartPtrT();
36  }
37  T *pt = NULL;
38  ar & pt;
39  (**ppt).reset(pt);
40  }
41  else if (ar.isWrite())
42  {
43  T *pt = NULL;
44  if (*ppt && (**ppt).get())
45  {
46  pt = (**ppt).operator->();
47  }
48  ar & pt;
49  }
50  }
51 
52 
53 #define SF_SERIALIZE_SIMPLE_SMARTPTR( SmartPtr ) \
54  template<typename T> \
55  inline bool invokeCustomSerializer(SmartPtr<T> **ppt, Archive &ar, int) \
56  { \
57  serializeSimpleSmartPtr<T>(ppt, ar); \
58  return true; \
59  }
60 
61  // 2. Ref counted smart pointer. Must support operator=(), operator->(), and get().
62 
63  template<typename T, typename SmartPtrT>
64  inline void serializeRefCountedSmartPtr(SmartPtrT **ppt, SF::Archive &ar)
65  {
66  if (ar.isRead())
67  {
68  if (ar.isFlagSet(Archive::POINTER))
69  {
70  *ppt = new SmartPtrT;
71  }
72  T *pt = NULL;
73  ar & pt;
74 
75  ContextRead &ctx = ar.getIstream()->getTrackingContext();
76  if (!ctx.getEnabled())
77  {
78  // No pointer tracking.
79  **ppt = SmartPtrT(pt);
80  }
81  else
82  {
83  // Pointer tracking enabled, so some extra gymnastics involved.
84  void *pv = NULL;
85  if (pt && ctx.getEnabled() && ctx.query((void *)pt, typeid(SmartPtrT), pv))
86  {
87  SmartPtrT *ps_prev = reinterpret_cast<SmartPtrT *>(pv);
88  **ppt = *ps_prev;
89  }
90  else if (pt)
91  {
92  if (ctx.getEnabled())
93  {
94  ctx.add((void *)pt, typeid(SmartPtrT), *ppt);
95  }
96  **ppt = SmartPtrT(pt);
97  }
98  }
99  }
100  else /*if (ar.isWrite())*/
101  {
102  T *pt = NULL;
103  if (*ppt)
104  {
105  pt = (**ppt).get();
106  }
107  ar & pt;
108  }
109  }
110 
111 #define SF_SERIALIZE_REFCOUNTED_SMARTPTR( SmartPtr ) \
112  template<typename T> \
113  inline bool invokeCustomSerializer(SmartPtr<T> **ppt, Archive &ar, int) \
114  { \
115  serializeRefCountedSmartPtr<T>(ppt, ar); \
116  return true; \
117  }
118 
119 } // namespace SF
120 
121 #endif // ! INCLUDE_SF_SERIALIZERSMARTPTR_HPP
Represents an archive, in which serialized objects are stored.
Definition: Archive.hpp:31
Definition: ByteBuffer.hpp:188
bool isWrite() const
Returns true if this archive is being written to.
bool isRead() const
Returns true if this archive is being read from.