CernVM-FS  2.13.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
pointer.h
Go to the documentation of this file.
1 
5 #ifndef CVMFS_UTIL_POINTER_H_
6 #define CVMFS_UTIL_POINTER_H_
7 
8 #include <cstdlib>
9 
10 #include "util/single_copy.h"
11 
12 #ifdef CVMFS_NAMESPACE_GUARD
13 namespace CVMFS_NAMESPACE_GUARD {
14 #endif
15 
20 template<typename T>
21 struct IsPointer {
22  static const bool value = false;
23 };
24 template<typename T>
25 struct IsPointer<T *> {
26  static const bool value = true;
27 };
28 
29 
30 template<class T, class DerivedT>
32  public:
33  inline UniquePtrBase() : ref_(NULL) { }
34  inline explicit UniquePtrBase(T *ref) : ref_(ref) { }
35  inline ~UniquePtrBase() { Free(); }
36 
37  inline T *operator->() const { return ref_; }
38  // NOLINTNEXTLINE(misc-unconventional-assign-operator)
39  inline DerivedT &operator=(T *ref) {
40  if (ref_ != ref) {
41  Free();
42  ref_ = ref;
43  }
44  return *(static_cast<DerivedT *>(this));
45  }
46  inline T *weak_ref() const { return ref_; }
47  inline bool IsValid() const { return (ref_ != NULL); }
48  inline T *Release() {
49  T *r = ref_;
50  ref_ = NULL;
51  return r;
52  }
53  inline void Destroy() {
54  Free();
55  ref_ = NULL;
56  }
57 
58  protected:
59  void Free() { static_cast<DerivedT *>(this)->Free(); }
60  T *ref_;
61 };
62 
63 
64 template<class T>
65 class UniquePtr : public UniquePtrBase<T, UniquePtr<T> > {
66  friend class UniquePtrBase<T, UniquePtr<T> >;
67 
68  private:
70 
71  public:
72  using BaseT::operator=;
73  inline UniquePtr() : BaseT(NULL) { }
74  inline explicit UniquePtr(T *ref) : BaseT(ref) { }
75  inline T &operator*() const { return *BaseT::ref_; }
76 
77  protected:
78  void Free() { delete BaseT::ref_; }
79 };
80 
81 
82 template<>
83 class UniquePtr<void> : public UniquePtrBase<void, UniquePtr<void> > {
84  private:
86 
87  public:
88  friend class UniquePtrBase<void, UniquePtr<void> >;
89  using BaseT::operator=;
90  inline UniquePtr() : BaseT(NULL) { }
91  inline explicit UniquePtr(void *ref) : BaseT(ref) { }
92 
93  protected:
94  void Free() {
95  if (IsValid()) {
96  free(BaseT::ref_);
97  }
98  }
99 };
100 
101 template<>
102 class UniquePtr<unsigned char>
103  : public UniquePtrBase<unsigned char, UniquePtr<unsigned char> > {
104  private:
106 
107  public:
108  friend class UniquePtrBase<unsigned char, UniquePtr<unsigned char> >;
109  using BaseT::operator=;
110  inline UniquePtr() : BaseT(NULL) { }
111  inline explicit UniquePtr(unsigned char *ref) : BaseT(ref) { }
112 
113  protected:
114  void Free() {
115  if (IsValid()) {
116  free(BaseT::ref_);
117  }
118  }
119 };
120 
121 
122 #ifdef CVMFS_NAMESPACE_GUARD
123 } // namespace CVMFS_NAMESPACE_GUARD
124 #endif
125 
126 #endif // CVMFS_UTIL_POINTER_H_
T * weak_ref() const
Definition: pointer.h:46
T * operator->() const
Definition: pointer.h:37
UniquePtr(T *ref)
Definition: pointer.h:74
UniquePtr()
Definition: pointer.h:73
void Free()
Definition: pointer.h:78
DerivedT & operator=(T *ref)
Definition: pointer.h:39
~UniquePtrBase()
Definition: pointer.h:35
static const bool value
Definition: pointer.h:22
T & operator*() const
Definition: pointer.h:75
UniquePtr(unsigned char *ref)
Definition: pointer.h:111
bool IsValid() const
Definition: pointer.h:47
UniquePtr(void *ref)
Definition: pointer.h:91
void Free()
Definition: pointer.h:59
UniquePtrBase< T, UniquePtr< T > > BaseT
Definition: pointer.h:69
T * Release()
Definition: pointer.h:48
UniquePtrBase< void, UniquePtr< void > > BaseT
Definition: pointer.h:85
UniquePtrBase()
Definition: pointer.h:33
void Destroy()
Definition: pointer.h:53
UniquePtrBase(T *ref)
Definition: pointer.h:34
UniquePtrBase< unsigned char, UniquePtr< unsigned char > > BaseT
Definition: pointer.h:105