| Directory: | cvmfs/ |
|---|---|
| File: | cvmfs/util/pointer.h |
| Date: | 2025-11-09 02:35:23 |
| Exec | Total | Coverage | |
|---|---|---|---|
| Lines: | 34 | 34 | 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 { | ||
| 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> | ||
| 31 | class UniquePtrBase : SingleCopy { | ||
| 32 | public: | ||
| 33 | inline UniquePtrBase() : ref_(NULL) { } | ||
| 34 | 40408298 | inline explicit UniquePtrBase(T *ref) : ref_(ref) { } | |
| 35 | 24843974 | inline ~UniquePtrBase() { Free(); } | |
| 36 | |||
| 37 | 170646159 | inline T *operator->() const { return ref_; } | |
| 38 | // NOLINTNEXTLINE(misc-unconventional-assign-operator) | ||
| 39 | 7495158 | inline DerivedT &operator=(T *ref) { | |
| 40 |
2/2✓ Branch 0 taken 7404955 times.
✓ Branch 1 taken 5980 times.
|
7495158 | if (ref_ != ref) { |
| 41 | 7482908 | Free(); | |
| 42 | 7480124 | ref_ = ref; | |
| 43 | } | ||
| 44 | 7492374 | return *(static_cast<DerivedT *>(this)); | |
| 45 | } | ||
| 46 | 22949405 | inline T *weak_ref() const { return ref_; } | |
| 47 | 22783563 | inline bool IsValid() const { return (ref_ != NULL); } | |
| 48 | 4717558 | inline T *Release() { | |
| 49 | 4720719 | T *r = ref_; | |
| 50 | 4720719 | ref_ = NULL; | |
| 51 | 4720719 | return r; | |
| 52 | } | ||
| 53 | 7283031 | inline void Destroy() { | |
| 54 | 7283031 | Free(); | |
| 55 | 7290223 | ref_ = NULL; | |
| 56 | 7290223 | } | |
| 57 | |||
| 58 | protected: | ||
| 59 | 69530472 | 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: | ||
| 69 | typedef UniquePtrBase<T, UniquePtr<T> > BaseT; | ||
| 70 | |||
| 71 | public: | ||
| 72 | using BaseT::operator=; | ||
| 73 | 271396 | inline UniquePtr() : BaseT(NULL) { } | |
| 74 | 32702638 | inline explicit UniquePtr(T *ref) : BaseT(ref) { } | |
| 75 | 4601402 | inline T &operator*() const { return *BaseT::ref_; } | |
| 76 | |||
| 77 | protected: | ||
| 78 |
2/2✓ Branch 0 taken 19960479 times.
✓ Branch 1 taken 19411558 times.
|
47771102 | void Free() { delete BaseT::ref_; } |
| 79 | }; | ||
| 80 | |||
| 81 | |||
| 82 | template<> | ||
| 83 | class UniquePtr<void> : public UniquePtrBase<void, UniquePtr<void> > { | ||
| 84 | private: | ||
| 85 | typedef UniquePtrBase<void, UniquePtr<void> > BaseT; | ||
| 86 | |||
| 87 | public: | ||
| 88 | friend class UniquePtrBase<void, UniquePtr<void> >; | ||
| 89 | using BaseT::operator=; | ||
| 90 | 29 | inline UniquePtr() : BaseT(NULL) { } | |
| 91 | 65 | inline explicit UniquePtr(void *ref) : BaseT(ref) { } | |
| 92 | |||
| 93 | protected: | ||
| 94 | 91 | void Free() { | |
| 95 |
2/2✓ Branch 1 taken 62 times.
✓ Branch 2 taken 29 times.
|
91 | if (IsValid()) { |
| 96 | 62 | free(BaseT::ref_); | |
| 97 | } | ||
| 98 | 91 | } | |
| 99 | }; | ||
| 100 | |||
| 101 | template<> | ||
| 102 | class UniquePtr<unsigned char> | ||
| 103 | : public UniquePtrBase<unsigned char, UniquePtr<unsigned char> > { | ||
| 104 | private: | ||
| 105 | typedef UniquePtrBase<unsigned char, UniquePtr<unsigned char> > BaseT; | ||
| 106 | |||
| 107 | public: | ||
| 108 | friend class UniquePtrBase<unsigned char, UniquePtr<unsigned char> >; | ||
| 109 | using BaseT::operator=; | ||
| 110 | inline UniquePtr() : BaseT(NULL) { } | ||
| 111 | 95 | inline explicit UniquePtr(unsigned char *ref) : BaseT(ref) { } | |
| 112 | |||
| 113 | protected: | ||
| 114 | 95 | void Free() { | |
| 115 |
1/2✓ Branch 1 taken 95 times.
✗ Branch 2 not taken.
|
95 | if (IsValid()) { |
| 116 | 95 | free(BaseT::ref_); | |
| 117 | } | ||
| 118 | 95 | } | |
| 119 | }; | ||
| 120 | |||
| 121 | |||
| 122 | #ifdef CVMFS_NAMESPACE_GUARD | ||
| 123 | } // namespace CVMFS_NAMESPACE_GUARD | ||
| 124 | #endif | ||
| 125 | |||
| 126 | #endif // CVMFS_UTIL_POINTER_H_ | ||
| 127 |