Directory: | cvmfs/ |
---|---|
File: | cvmfs/util/pointer.h |
Date: | 2025-02-02 02:34:22 |
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 | 1437796 | inline explicit UniquePtrBase(T *ref) : ref_(ref) { } | |
31 | 820213 | inline ~UniquePtrBase() { Free(); } | |
32 | |||
33 | 6176689 | inline T* operator->() const { return ref_; } | |
34 | // NOLINTNEXTLINE(misc-unconventional-assign-operator) | ||
35 | 257370 | inline DerivedT& operator=(T *ref) { | |
36 |
2/2✓ Branch 0 taken 254646 times.
✓ Branch 1 taken 182 times.
|
257370 | if (ref_ != ref) { |
37 | 256988 | Free(); | |
38 | 256949 | ref_ = ref; | |
39 | } | ||
40 | 257331 | return *(static_cast<DerivedT*>(this)); | |
41 | } | ||
42 | 791296 | inline T* weak_ref() const { return ref_; } | |
43 | 800682 | inline bool IsValid() const { return (ref_ != NULL); } | |
44 | 104365 | inline T* Release() { T* r = ref_; ref_ = NULL; return r; } | |
45 | 251333 | inline void Destroy() { Free(); ref_ = NULL; } | |
46 | |||
47 | protected: | ||
48 | 2436812 | void Free() { | |
49 | 2436812 | static_cast<DerivedT*>(this)->Free(); | |
50 | 2440252 | } | |
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 | 8256 | inline UniquePtr() : BaseT(NULL) { } | |
64 | 1173563 | inline explicit UniquePtr(T *ref) : BaseT(ref) { } | |
65 | 100042 | inline T& operator*() const { return *BaseT::ref_; } | |
66 | protected: | ||
67 |
2/2✓ Branch 0 taken 708659 times.
✓ Branch 1 taken 610231 times.
|
1686858 | 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 |