Coverage Report

Created: 2022-01-22 13:19

/Users/buildslave/jenkins/workspace/coverage/llvm-project/lldb/source/Host/macosx/cfcpp/CFCReleaser.h
Line
Count
Source (jump to first uncovered line)
1
//===-- CFCReleaser.h -------------------------------------------*- C++ -*-===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
9
#ifndef LLDB_SOURCE_HOST_MACOSX_CFCPP_CFCRELEASER_H
10
#define LLDB_SOURCE_HOST_MACOSX_CFCPP_CFCRELEASER_H
11
12
#include <CoreFoundation/CoreFoundation.h>
13
14
#ifdef __cplusplus
15
16
#include <cassert>
17
18
// Templatized CF helper class that can own any CF pointer and will
19
// call CFRelease() on any valid pointer it owns unless that pointer is
20
// explicitly released using the release() member function. This class
21
// is designed to mimic the std::auto_ptr<T> class and has all of the
22
// same functions. The one thing to watch out for is the
23
// CFCReleaser<T>::release() function won't actually CFRelease any owned
24
// pointer, it is designed to relinquish ownership of the pointer just
25
// like std:auto_ptr<T>::release() does.
26
template <class T> class CFCReleaser {
27
public:
28
  // Constructor that takes a pointer to a CF object that is
29
  // to be released when this object goes out of scope
30
3.91k
  CFCReleaser(T ptr = NULL) : _ptr(ptr) {}
CFCReleaser<__CFBundle*>::CFCReleaser(__CFBundle*)
Line
Count
Source
30
2
  CFCReleaser(T ptr = NULL) : _ptr(ptr) {}
CFCReleaser<__CFData const*>::CFCReleaser(__CFData const*)
Line
Count
Source
30
6
  CFCReleaser(T ptr = NULL) : _ptr(ptr) {}
Unexecuted instantiation: CFCReleaser<__CFWriteStream*>::CFCReleaser(__CFWriteStream*)
CFCReleaser<__CFString const*>::CFCReleaser(__CFString const*)
Line
Count
Source
30
8
  CFCReleaser(T ptr = NULL) : _ptr(ptr) {}
CFCReleaser<__CFUUID const*>::CFCReleaser(__CFUUID const*)
Line
Count
Source
30
1.29k
  CFCReleaser(T ptr = NULL) : _ptr(ptr) {}
CFCReleaser<__CFDictionary const*>::CFCReleaser(__CFDictionary const*)
Line
Count
Source
30
6
  CFCReleaser(T ptr = NULL) : _ptr(ptr) {}
CFCReleaser<__CFURL const*>::CFCReleaser(__CFURL const*)
Line
Count
Source
30
2.59k
  CFCReleaser(T ptr = NULL) : _ptr(ptr) {}
Unexecuted instantiation: CFCReleaser<__CFArray const*>::CFCReleaser(__CFArray const*)
31
32
  // Copy constructor
33
  //
34
  // Note that copying a CFCReleaser will not transfer
35
  // ownership of the contained pointer, but it will bump its
36
  // reference count. This is where this class differs from
37
  // std::auto_ptr.
38
0
  CFCReleaser(const CFCReleaser &rhs) : _ptr(rhs.get()) {
39
0
    if (get())
40
0
      ::CFRetain(get());
41
0
  }
Unexecuted instantiation: CFCReleaser<__CFData const*>::CFCReleaser(CFCReleaser<__CFData const*> const&)
Unexecuted instantiation: CFCReleaser<__CFString const*>::CFCReleaser(CFCReleaser<__CFString const*> const&)
42
43
  // The destructor will release the pointer that it contains
44
  // if it has a valid pointer.
45
3.91k
  virtual ~CFCReleaser() { reset(); }
CFCReleaser<__CFBundle*>::~CFCReleaser()
Line
Count
Source
45
2
  virtual ~CFCReleaser() { reset(); }
CFCReleaser<__CFData const*>::~CFCReleaser()
Line
Count
Source
45
6
  virtual ~CFCReleaser() { reset(); }
Unexecuted instantiation: CFCReleaser<__CFWriteStream*>::~CFCReleaser()
CFCReleaser<__CFString const*>::~CFCReleaser()
Line
Count
Source
45
8
  virtual ~CFCReleaser() { reset(); }
CFCReleaser<__CFUUID const*>::~CFCReleaser()
Line
Count
Source
45
1.29k
  virtual ~CFCReleaser() { reset(); }
CFCReleaser<__CFDictionary const*>::~CFCReleaser()
Line
Count
Source
45
6
  virtual ~CFCReleaser() { reset(); }
CFCReleaser<__CFURL const*>::~CFCReleaser()
Line
Count
Source
45
2.59k
  virtual ~CFCReleaser() { reset(); }
Unexecuted instantiation: CFCReleaser<__CFArray const*>::~CFCReleaser()
46
47
  // Assignment operator.
48
  //
49
  // Note that assigning one CFCReleaser to another will
50
  // not transfer ownership of the contained pointer, but it
51
  // will bump its reference count. This is where this class
52
  // differs from std::auto_ptr.
53
  CFCReleaser &operator=(const CFCReleaser<T> &rhs) {
54
    if (this != &rhs) {
55
      // Replace our owned pointer with the new one
56
      reset(rhs.get());
57
      // Retain the current pointer that we own
58
      if (get())
59
        ::CFRetain(get());
60
    }
61
    return *this;
62
  }
63
64
  // Get the address of the contained type in case it needs
65
  // to be passed to a function that will fill in a pointer
66
  // value. The function currently will assert if _ptr is not
67
  // NULL because the only time this method should be used is
68
  // if another function will modify the contents, and we
69
  // could leak a pointer if this is not NULL. If the
70
  // assertion fires, check the offending code, or call
71
  // reset() prior to using the "ptr_address()" member to make
72
  // sure any owned objects has CFRelease called on it.
73
  // I had to add the "enforce_null" bool here because some
74
  // API's require the pointer address even though they don't change it.
75
0
  T *ptr_address(bool enforce_null = true) {
76
0
    if (enforce_null)
77
0
      assert(_ptr == NULL);
78
0
    return &_ptr;
79
0
  }
80
81
  // Access the pointer itself
82
5.23k
  T get() { return _ptr; }
CFCReleaser<__CFBundle*>::get()
Line
Count
Source
82
2
  T get() { return _ptr; }
Unexecuted instantiation: CFCReleaser<__CFWriteStream*>::get()
CFCReleaser<__CFUUID const*>::get()
Line
Count
Source
82
2.59k
  T get() { return _ptr; }
CFCReleaser<__CFDictionary const*>::get()
Line
Count
Source
82
18
  T get() { return _ptr; }
CFCReleaser<__CFData const*>::get()
Line
Count
Source
82
6
  T get() { return _ptr; }
CFCReleaser<__CFURL const*>::get()
Line
Count
Source
82
2.60k
  T get() { return _ptr; }
CFCReleaser<__CFString const*>::get()
Line
Count
Source
82
12
  T get() { return _ptr; }
Unexecuted instantiation: CFCReleaser<__CFArray const*>::get()
83
84
2
  const T get() const { return _ptr; }
CFCReleaser<__CFBundle*>::get() const
Line
Count
Source
84
2
  const T get() const { return _ptr; }
Unexecuted instantiation: CFCReleaser<__CFData const*>::get() const
Unexecuted instantiation: CFCReleaser<__CFString const*>::get() const
85
86
  // Set a new value for the pointer and CFRelease our old
87
  // value if we had a valid one.
88
5.22k
  void reset(T ptr = NULL) {
89
5.22k
    if ((_ptr != NULL) && 
(ptr != _ptr)2.62k
)
90
2.62k
      ::CFRelease(_ptr);
91
5.22k
    _ptr = ptr;
92
5.22k
  }
CFCReleaser<__CFBundle*>::reset(__CFBundle*)
Line
Count
Source
88
6
  void reset(T ptr = NULL) {
89
6
    if ((_ptr != NULL) && 
(ptr != _ptr)2
)
90
2
      ::CFRelease(_ptr);
91
6
    _ptr = ptr;
92
6
  }
CFCReleaser<__CFData const*>::reset(__CFData const*)
Line
Count
Source
88
6
  void reset(T ptr = NULL) {
89
6
    if ((_ptr != NULL) && (ptr != _ptr))
90
6
      ::CFRelease(_ptr);
91
6
    _ptr = ptr;
92
6
  }
Unexecuted instantiation: CFCReleaser<__CFWriteStream*>::reset(__CFWriteStream*)
CFCReleaser<__CFString const*>::reset(__CFString const*)
Line
Count
Source
88
16
  void reset(T ptr = NULL) {
89
16
    if ((_ptr != NULL) && 
(ptr != _ptr)8
)
90
8
      ::CFRelease(_ptr);
91
16
    _ptr = ptr;
92
16
  }
CFCReleaser<__CFUUID const*>::reset(__CFUUID const*)
Line
Count
Source
88
1.29k
  void reset(T ptr = NULL) {
89
1.29k
    if ((_ptr != NULL) && (ptr != _ptr))
90
1.29k
      ::CFRelease(_ptr);
91
1.29k
    _ptr = ptr;
92
1.29k
  }
CFCReleaser<__CFDictionary const*>::reset(__CFDictionary const*)
Line
Count
Source
88
6
  void reset(T ptr = NULL) {
89
6
    if ((_ptr != NULL) && (ptr != _ptr))
90
6
      ::CFRelease(_ptr);
91
6
    _ptr = ptr;
92
6
  }
CFCReleaser<__CFURL const*>::reset(__CFURL const*)
Line
Count
Source
88
3.89k
  void reset(T ptr = NULL) {
89
3.89k
    if ((_ptr != NULL) && 
(ptr != _ptr)1.30k
)
90
1.30k
      ::CFRelease(_ptr);
91
3.89k
    _ptr = ptr;
92
3.89k
  }
Unexecuted instantiation: CFCReleaser<__CFArray const*>::reset(__CFArray const*)
93
94
  // Release ownership without calling CFRelease. This class
95
  // is designed to mimic std::auto_ptr<T>, so the release
96
  // method releases ownership of the contained pointer
97
  // and does NOT call CFRelease.
98
  T release() {
99
    T tmp = _ptr;
100
    _ptr = NULL;
101
    return tmp;
102
  }
103
104
private:
105
  T _ptr;
106
};
107
108
#endif // #ifdef __cplusplus
109
#endif // LLDB_SOURCE_HOST_MACOSX_CFCPP_CFCRELEASER_H