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