GCC Code Coverage Report | |||||||||||||||||||||
|
|||||||||||||||||||||
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 |
322715 |
inline explicit UniquePtrBase(T *ref) : ref_(ref) { } |
|
31 |
322701 |
inline ~UniquePtrBase() { Free(); } |
|
32 |
|||
33 |
1672 |
inline operator bool() const { return IsValid(); } |
|
34 |
765141 |
inline T* operator->() const { return ref_; } |
|
35 |
14540 |
inline operator T*() { return ref_; } |
|
36 |
11001 |
inline DerivedT& operator=(T *ref) { |
|
37 |
✓✓✓✗ ✓✗✓✗ ✓✗✓✗ ✓✗✓✗ ✓✗✓✗ ✓✗✓✗ ✓✗✓✗ ✓✗✗✗ ✓✗ |
11001 |
if (ref_ != ref) { |
38 |
10998 |
Free(); |
|
39 |
10998 |
ref_ = ref; |
|
40 |
} |
||
41 |
11001 |
return *(static_cast<DerivedT*>(this)); |
|
42 |
} |
||
43 |
540125 |
inline T* weak_ref() const { return ref_; } |
|
44 |
231538 |
inline bool IsValid() const { return (ref_ != NULL); } |
|
45 |
102427 |
inline T* Release() { T* r = ref_; ref_ = NULL; return r; } |
|
46 |
5 |
inline void Destroy() { Free(); ref_ = NULL; } |
|
47 |
|||
48 |
protected: |
||
49 |
333700 |
void Free() { |
|
50 |
333700 |
static_cast<DerivedT*>(this)->Free(); |
|
51 |
333690 |
} |
|
52 |
T *ref_; |
||
53 |
}; |
||
54 |
|||
55 |
|||
56 |
template <class T> |
||
57 |
315870 |
class UniquePtr : public UniquePtrBase<T, UniquePtr<T> > { |
|
58 |
friend class UniquePtrBase<T, UniquePtr<T> >; |
||
59 |
|||
60 |
private: |
||
61 |
typedef UniquePtrBase<T, UniquePtr<T> > BaseT; |
||
62 |
public: |
||
63 |
using BaseT::operator=; |
||
64 |
4629 |
inline UniquePtr() : BaseT(NULL) { } |
|
65 |
311255 |
inline explicit UniquePtr(T *ref) : BaseT(ref) { } |
|
66 |
100034 |
inline T& operator*() const { return *BaseT::ref_; } |
|
67 |
protected: |
||
68 |
✓✓✓✓ ✓✓✓✓ ✓✓✗✓ ✓✓✓✓ ✓✓✓✗ ✓✓✓✓ ✓✓✗✓ ✓✗✓✓ ✓✓✓✓ ✓✓✓✓ ✓✓✓✓ ✓✓✓✓ ✓✓✓✗ ✗ |
320048 |
void Free() { delete BaseT::ref_; } |
69 |
}; |
||
70 |
|||
71 |
|||
72 |
template <> |
||
73 |
6829 |
class UniquePtr<void> : public UniquePtrBase<void, UniquePtr<void> > { |
|
74 |
private: |
||
75 |
typedef UniquePtrBase<void, UniquePtr<void> > BaseT; |
||
76 |
public: |
||
77 |
friend class UniquePtrBase<void, UniquePtr<void> >; |
||
78 |
using BaseT::operator=; |
||
79 |
6826 |
inline UniquePtr() : BaseT(NULL) { } |
|
80 |
3 |
inline explicit UniquePtr(void *ref) : BaseT(ref) { } |
|
81 |
protected: |
||
82 |
13650 |
void Free() { |
|
83 |
✓✓ | 13650 |
if (IsValid()) { |
84 |
6828 |
free(BaseT::ref_); |
|
85 |
} |
||
86 |
13649 |
} |
|
87 |
}; |
||
88 |
|||
89 |
template <> |
||
90 |
class UniquePtr<unsigned char> |
||
91 |
2 |
: public UniquePtrBase<unsigned char, UniquePtr<unsigned char> > { |
|
92 |
private: |
||
93 |
typedef UniquePtrBase<unsigned char, UniquePtr<unsigned char> > BaseT; |
||
94 |
public: |
||
95 |
friend class UniquePtrBase<unsigned char, UniquePtr<unsigned char> >; |
||
96 |
using BaseT::operator=; |
||
97 |
inline UniquePtr() : BaseT(NULL) { } |
||
98 |
2 |
inline explicit UniquePtr(unsigned char *ref) : BaseT(ref) { } |
|
99 |
protected: |
||
100 |
2 |
void Free() { |
|
101 |
✓✗ | 2 |
if (IsValid()) { |
102 |
2 |
free(BaseT::ref_); |
|
103 |
} |
||
104 |
2 |
} |
|
105 |
}; |
||
106 |
|||
107 |
|||
108 |
#ifdef CVMFS_NAMESPACE_GUARD |
||
109 |
} // namespace CVMFS_NAMESPACE_GUARD |
||
110 |
#endif |
||
111 |
|||
112 |
#endif // CVMFS_UTIL_POINTER_H_ |
Generated by: GCOVR (Version 4.1) |