Xpace
sharedimpl.h
Go to the documentation of this file.
1 
2 /**********************************************************//**
3  **
4  ** @file base/sharedimpl.h
5  **
6  ** Copyright (C) 2012 Xpace, LLC. All rights reserved
7  **
8  ** www.xpace.net
9  **
10  **************************************************************/
11 
12 
13 #ifndef XPACE_SHAREDIMPL_H
14 #define XPACE_SHAREDIMPL_H
15 
16 #ifdef _MSC_VER
17 // MS warns when you "export" a template definition
18 #pragma warning(disable: 4251)
19 #endif
20 
21 #if TRACE_SHARE
22 #include <cstdio>
23 #include <typeinfo>
24 #endif
25 
26 #include "base/sharedvoid.h"
27 
28 namespace Xpace
29 {
30  /// placeholder class
32  {
33  public:
34  _impl_ref
35  () :
36  ref(0)
37  {
38  };
39 
40  _impl_ref
41  (const _impl_ref&) :
42  ref(0)
43  {
44  };
45 
46  virtual ~_impl_ref
47  ()
48  {
49  }
50 
51  private:
52  friend class SharedVoidPointer;
53  int ref;
54 
55  _impl_ref& operator=
56  (const _impl_ref&);
57  };
58 
59  /// The base class for all shared implementations
61  {
62  public :
63  virtual ~SharedImpl
64  ()
65  = 0;
66  virtual SharedImpl* clone
67  ()
68  const
69  = 0;
70 
71  #ifndef NDEBUG
72  static
73  void test
74  ();
75  #endif
76  };
77 
78  /// A pointer to a shared class object
79  /// Copying the pointer calls T::clone()
80  /// T must inherit SharedImpl
81  template <typename T, bool cached = false>
83  {
84  public:
85  /// @param p the object to be wrapped
86  explicit SharedImplPointer
87  (T* p);
89  ();
91  (const SharedImplPointer<T, cached> &rhs);
92  SharedImplPointer& operator=
93  (const SharedImplPointer<T, cached> &rhs);
95  ();
96 
97  SharedImplPointer& operator=
98  (T* p);
99 
100  /// @return true if pointer is null
101  bool operator!
102  ()
103  const;
104 
105  /// @return a writable pointer
106  T* data
107  ();
108 
109  /// @return a non-writable pointer
110  const T* data
111  ()
112  const;
113 
114  /// @return a non-writable pointer (even if object is non-const)
115  const T* constData
116  ()
117  const;
118 
119  /// @return a writable pointer
120  T* operator->
121  ()
122  {
123  return data();
124  };
125 
126  /// @return a non-writable pointer
127  const T* operator->
128  ()
129  const
130  {
131  return constData();
132  };
133 
134  #ifndef NDEBUG
135  int getRefCount
136  ()
137  const;
138  #endif
139 
140  private:
142 
143  void _trace
144  (const char*)
145  #ifndef TRACE_SHARE
146  {
147  }
148  #endif
149  ;
150  };
151 
152  /// A wrapper around a shared class object
153  /// Used to store objects in containers (e.g. QCache)
154  template<typename T>
156  {
157  public:
160  si(s)
161  {
162  }
163 
165  ()
166  {
167  return si;
168  }
169 
170  private:
172  };
173 
174  // ============================================================
175  // ============================================================
176  // ============================================================
177 
178  #if TRACE_SHARE
179  extern FILE* _trace_file;
180 
181  template <typename T>
183  (const char* str)
184  {
185  if (!_trace_file)
186  fopen_s(&_trace_file, "_TRACE_SHARE_", "wt");
187  fprintf(_trace_file, "Impl %s: (%s) %p %u\n", str, typeid(T).name(), constData(), getRefCount());
188  fflush(_trace_file);
189  }
190  #endif
191 
192  template <typename T, bool cached>
193  inline
195  (T* p) :
196  v(p)
197  {
198  _trace("Ctor");
199  }
200 
201  template <typename T, bool cached>
202  inline
204  ()
205  {
206  }
207 
208  template <typename T, bool cached>
209  inline
212  v(rhs.v)
213  {
214  _trace("Copy");
215  }
216 
217  template <typename T, bool cached>
218  inline
220  ()
221  {
222  _trace("Dtor");
223  }
224 
225  template <typename T, bool cached>
226  inline
229  {
230  _trace("operator=");
231  v = rhs.v;
232  return *this;
233  }
234 
235  template <typename T, bool cached>
236  inline
237  SharedImplPointer<T, cached>& SharedImplPointer<T, cached>::operator=
238  (T* p)
239  {
240  _trace("operator=");
241  v = p;
242  return *this;
243  }
244 
245  template <typename T, bool cached>
246  inline
247  bool SharedImplPointer<T, cached>::operator!
248  ()
249  const
250  {
251  return !v;
252  }
253 
254  template <typename T, bool cached>
255  inline
257  ()
258  {
259  return static_cast<T*>(v.data(cached));
260  }
261 
262  template <typename T, bool cached>
263  inline
265  ()
266  const
267  {
268  return static_cast<const T*>(v.constData());
269  }
270 
271  template <typename T, bool cached>
272  inline
274  ()
275  const
276  {
277  return static_cast<const T*>(v.constData());
278  }
279 
280  #ifndef NDEBUG
281  template <typename T, bool cached>
282  inline
284  ()
285  const
286  {
287  return v.getRefCount();
288  }
289  #endif
290 
291  #define DECLARE_IMPL_COMMON(className, cached) \
292  public : \
293  class Impl; \
294  className() {}; \
295  friend class Impl; \
296  SharedImplPointer<Impl, cached> si; \
297  className(Impl* i) : si(i) {}; \
298  Impl* impl() \
299  { return static_cast<Impl*>(si.data()); }; \
300  const Impl* impl() const \
301  { return static_cast<const Impl*>(si.data()); }; \
302  const Impl* constImpl() const \
303  { return static_cast<const Impl*>(si.constData()); };
304 
305  #define DECLARE_IMPL(className) \
306  DECLARE_IMPL_COMMON(className, false)
307 
308  #define DECLARE_IMPL_BASE(className, base) \
309  DECLARE_IMPL_COMMON(className, false) \
310  className(const className& rhs) : \
311  base(), si(rhs.si) {}
312 
313  #define DECLARE_CACHED_IMPL(className) \
314  DECLARE_IMPL_COMMON(className, true) \
315  className(const className& rhs) : \
316  si(rhs.si) {}
317 
318  #define DECLARE_CACHED_IMPL_BASE(className, base) \
319  DECLARE_IMPL_COMMON(className, true) \
320  className(const className& rhs) : \
321  base(), si(rhs.si) {}
322 
323  #define DECLARE_CONFIG \
324  virtual const Configuration& getConfig() const override;
325 
326  #define DEFINE_CONFIG(className) \
327  String className::getConfig() const \
328  { return constImpl()->getConfig().toString(); }
329 };
330 
331 #endif
A low-level data holder.
Definition: types_c.h:82
int getRefCount() const
Definition: sharedimpl.h:284
A wrapper around a shared class object Used to store objects in containers (e.g.
Definition: sharedimpl.h:155
A pointer to a shared class object Copying the pointer calls T::clone() T must inherit SharedImpl...
Definition: sharedimpl.h:82
const T * constData() const
Definition: sharedimpl.h:274
The base class for all shared implementations.
Definition: sharedimpl.h:60
const Xpace_Char16 * name
Sink callbacks for table data.
Definition: table_c.h:141
Copyright (C) 2012 Xpace, LLC.
placeholder class
Definition: sharedimpl.h:31
uint const Xpace_Char8 * data
Definition: table_c.h:180
Xpace project main namespace
Definition: datetime.h:18
an anonymous pointer, used only in conjunction with SharedImplPointer
Definition: sharedvoid.h:21

current as of Wed Jun 10 2026 12:00:05