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