GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/util/pointer.h
Date: 2024-04-21 02:33:16
Exec Total Coverage
Lines: 30 30 100.0%
Branches: 7 8 87.5%

Line Branch Exec Source
1 /**
2 * This file is part of the CernVM File System.
3 */
4
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
16 /**
17 * Type Trait:
18 * "Static" assertion that a template parameter is a pointer
19 */
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>
27 class UniquePtrBase : SingleCopy {
28 public:
29 inline UniquePtrBase() : ref_(NULL) {}
30 2438176 inline explicit UniquePtrBase(T *ref) : ref_(ref) { }
31 1320462 inline ~UniquePtrBase() { Free(); }
32
33 7869010 inline T* operator->() const { return ref_; }
34 // NOLINTNEXTLINE(misc-unconventional-assign-operator)
35 257407 inline DerivedT& operator=(T *ref) {
36
2/2
✓ Branch 0 taken 254685 times.
✓ Branch 1 taken 186 times.
257407 if (ref_ != ref) {
37 257023 Free();
38 256958 ref_ = ref;
39 }
40 257342 return *(static_cast<DerivedT*>(this));
41 }
42 791527 inline T* weak_ref() const { return ref_; }
43 1300938 inline bool IsValid() const { return (ref_ != NULL); }
44 104388 inline T* Release() { T* r = ref_; ref_ = NULL; return r; }
45 251345 inline void Destroy() { Free(); ref_ = NULL; }
46
47 protected:
48 3437826 void Free() {
49 3437826 static_cast<DerivedT*>(this)->Free();
50 3440602 }
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:
60 typedef UniquePtrBase<T, UniquePtr<T> > BaseT;
61 public:
62 using BaseT::operator=;
63 8231 inline UniquePtr() : BaseT(NULL) { }
64 2173893 inline explicit UniquePtr(T *ref) : BaseT(ref) { }
65 100034 inline T& operator*() const { return *BaseT::ref_; }
66 protected:
67
2/2
✓ Branch 0 taken 1208910 times.
✓ Branch 1 taken 610578 times.
2687680 void Free() { delete BaseT::ref_; }
68 };
69
70
71 template <>
72 class UniquePtr<void> : public UniquePtrBase<void, UniquePtr<void> > {
73 private:
74 typedef UniquePtrBase<void, UniquePtr<void> > BaseT;
75 public:
76 friend class UniquePtrBase<void, UniquePtr<void> >;
77 using BaseT::operator=;
78 1 inline UniquePtr() : BaseT(NULL) { }
79 3 inline explicit UniquePtr(void *ref) : BaseT(ref) { }
80 protected:
81 4 void Free() {
82
2/2
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 1 times.
4 if (IsValid()) {
83 3 free(BaseT::ref_);
84 }
85 4 }
86 };
87
88 template <>
89 class UniquePtr<unsigned char>
90 : public UniquePtrBase<unsigned char, UniquePtr<unsigned char> > {
91 private:
92 typedef UniquePtrBase<unsigned char, UniquePtr<unsigned char> > BaseT;
93 public:
94 friend class UniquePtrBase<unsigned char, UniquePtr<unsigned char> >;
95 using BaseT::operator=;
96 inline UniquePtr() : BaseT(NULL) { }
97 3 inline explicit UniquePtr(unsigned char *ref) : BaseT(ref) { }
98 protected:
99 3 void Free() {
100
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
3 if (IsValid()) {
101 3 free(BaseT::ref_);
102 }
103 3 }
104 };
105
106
107 #ifdef CVMFS_NAMESPACE_GUARD
108 } // namespace CVMFS_NAMESPACE_GUARD
109 #endif
110
111 #endif // CVMFS_UTIL_POINTER_H_
112