CernVM-FS  2.10.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 { static const bool value = false; };
22 template<typename T>
23 struct IsPointer<T*> { static const bool value = true; };
24 
25 
26 template <class T, class DerivedT>
28  public:
29  inline UniquePtrBase() : ref_(NULL) {}
30  inline explicit UniquePtrBase(T *ref) : ref_(ref) { }
31  inline ~UniquePtrBase() { Free(); }
32 
33  inline T* operator->() const { return ref_; }
34  // NOLINTNEXTLINE(misc-unconventional-assign-operator)
35  inline DerivedT& operator=(T *ref) {
36  if (ref_ != ref) {
37  Free();
38  ref_ = ref;
39  }
40  return *(static_cast<DerivedT*>(this));
41  }
42  inline T* weak_ref() const { return ref_; }
43  inline bool IsValid() const { return (ref_ != NULL); }
44  inline T* Release() { T* r = ref_; ref_ = NULL; return r; }
45  inline void Destroy() { Free(); ref_ = NULL; }
46 
47  protected:
48  void Free() {
49  static_cast<DerivedT*>(this)->Free();
50  }
51  T *ref_;
52 };
53 
54 
55 template <class T>
56 class UniquePtr : public UniquePtrBase<T, UniquePtr<T> > {
57  friend class UniquePtrBase<T, UniquePtr<T> >;
58 
59  private:
61  public:
62  using BaseT::operator=;
63  inline UniquePtr() : BaseT(NULL) { }
64  inline explicit UniquePtr(T *ref) : BaseT(ref) { }
65  inline T& operator*() const { return *BaseT::ref_; }
66  protected:
67  void Free() { delete BaseT::ref_; }
68 };
69 
70 
71 template <>
72 class UniquePtr<void> : public UniquePtrBase<void, UniquePtr<void> > {
73  private:
75  public:
76  friend class UniquePtrBase<void, UniquePtr<void> >;
77  using BaseT::operator=;
78  inline UniquePtr() : BaseT(NULL) { }
79  inline explicit UniquePtr(void *ref) : BaseT(ref) { }
80  protected:
81  void Free() {
82  if (IsValid()) {
83  free(BaseT::ref_);
84  }
85  }
86 };
87 
88 template <>
89 class UniquePtr<unsigned char>
90  : public UniquePtrBase<unsigned char, UniquePtr<unsigned char> > {
91  private:
93  public:
94  friend class UniquePtrBase<unsigned char, UniquePtr<unsigned char> >;
95  using BaseT::operator=;
96  inline UniquePtr() : BaseT(NULL) { }
97  inline explicit UniquePtr(unsigned char *ref) : BaseT(ref) { }
98  protected:
99  void Free() {
100  if (IsValid()) {
101  free(BaseT::ref_);
102  }
103  }
104 };
105 
106 
107 #ifdef CVMFS_NAMESPACE_GUARD
108 } // namespace CVMFS_NAMESPACE_GUARD
109 #endif
110 
111 #endif // CVMFS_UTIL_POINTER_H_
T * weak_ref() const
Definition: pointer.h:42
T * operator->() const
Definition: pointer.h:33
UniquePtr(T *ref)
Definition: pointer.h:64
UniquePtr()
Definition: pointer.h:63
void Free()
Definition: pointer.h:67
DerivedT & operator=(T *ref)
Definition: pointer.h:35
~UniquePtrBase()
Definition: pointer.h:31
static const bool value
Definition: pointer.h:21
T & operator*() const
Definition: pointer.h:65
UniquePtr(unsigned char *ref)
Definition: pointer.h:97
bool IsValid() const
Definition: pointer.h:43
UniquePtr(void *ref)
Definition: pointer.h:79
void Free()
Definition: pointer.h:48
UniquePtrBase< T, UniquePtr< T > > BaseT
Definition: pointer.h:60
T * Release()
Definition: pointer.h:44
UniquePtrBase< void, UniquePtr< void > > BaseT
Definition: pointer.h:74
UniquePtrBase()
Definition: pointer.h:29
void Destroy()
Definition: pointer.h:45
UniquePtrBase(T *ref)
Definition: pointer.h:30
UniquePtrBase< unsigned char, UniquePtr< unsigned char > > BaseT
Definition: pointer.h:92