Coverage Report

Created: 2026-06-15 07:03

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/CMake/Source/cmVariableWatch.h
Line
Count
Source
1
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2
   file LICENSE.rst or https://cmake.org/licensing for details.  */
3
#pragma once
4
5
#include "cmConfigure.h" // IWYU pragma: keep
6
7
#include <map>
8
#include <memory>
9
#include <string>
10
#include <vector>
11
12
class cmMakefile;
13
14
/** \class cmVariableWatch
15
 * \brief Helper class for watching of variable accesses.
16
 *
17
 * Calls function when variable is accessed
18
 */
19
class cmVariableWatch
20
{
21
public:
22
  /**
23
   * Different access types.
24
   */
25
  enum AccessType : unsigned
26
  {
27
    VARIABLE_READ_ACCESS,
28
    UNKNOWN_VARIABLE_READ_ACCESS,
29
    UNKNOWN_VARIABLE_DEFINED_ACCESS,
30
    VARIABLE_MODIFIED_ACCESS,
31
    VARIABLE_REMOVED_ACCESS,
32
    NO_ACCESS
33
  };
34
35
  using WatchMethod = void (*)(std::string const&, AccessType, void*,
36
                               char const*, cmMakefile const*);
37
  using DeleteData = void (*)(void*);
38
39
  cmVariableWatch();
40
  ~cmVariableWatch();
41
42
  /**
43
   * Add watch to the variable
44
   */
45
  bool AddWatch(std::string const& variable, WatchMethod method,
46
                void* client_data = nullptr, DeleteData delete_data = nullptr);
47
  void RemoveWatch(std::string const& variable, WatchMethod method,
48
                   void* client_data = nullptr);
49
50
  /**
51
   * This method is called when variable is accessed
52
   */
53
  bool VariableAccessed(std::string const& variable, AccessType accessType,
54
                        char const* newValue, cmMakefile const* mf) const;
55
56
  /**
57
   * Return the access as string
58
   */
59
  static std::string const& GetAccessAsString(AccessType accessType);
60
61
protected:
62
  struct Pair
63
  {
64
    WatchMethod Method = nullptr;
65
    void* ClientData = nullptr;
66
    DeleteData DeleteDataCall = nullptr;
67
    ~Pair()
68
2
    {
69
2
      if (this->DeleteDataCall && this->ClientData) {
70
0
        this->DeleteDataCall(this->ClientData);
71
0
      }
72
2
    }
73
2
    Pair() = default;
74
    Pair(Pair const&) = delete;
75
    Pair& operator=(Pair const&) = delete;
76
  };
77
78
  using VectorOfPairs = std::vector<std::shared_ptr<Pair>>;
79
  using StringToVectorOfPairs = std::map<std::string, VectorOfPairs>;
80
81
  StringToVectorOfPairs WatchMap;
82
};