Skip to content

SourceWatcher.hpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#pragma once

#include "doxide.hpp"

#include <filesystem>

/**
 * Source Watcher.
 *
 * Watches for changes of source files. The files to watch are specified by a glob pattern.
 *
 * @ingroup developer
 */
class SourceWatcher {
public:
  /**
   * Constructor for a list of patterns.
   *
   * @param patterns
   */
  SourceWatcher(std::list<std::string> patterns);

  /**
   * Constructor for a single pattern.
   */
  SourceWatcher(std::string pattern);

  /**
   * Check if any of the watched files changed since the last call to `changed()` or `diff()`.
   */
  bool changed();

  /**
   * Check if any of the watched files changed since the last call to `changed()` or `diff()`.
   *
   * @return A tuple containing sets of added, modified and deleted file paths.
   */
  std::tuple<std::unordered_set<std::filesystem::path>,
             std::unordered_set<std::filesystem::path>,
             std::unordered_set<std::filesystem::path>> diff();

  /**
   * The current source files.
   *
   */
  std::unordered_set<std::filesystem::path> filenames();

private:
  /**
   * The patterns to watch.
   */
  std::list<std::string> patterns;

  /**
   * The tracked file paths and their last modification time.
   */
  std::unordered_map<std::filesystem::path, std::filesystem::file_time_type> tracked_files;
};