xref: /glogg/src/watchtower.h (revision b278d1839115e4ea1ab008ee6886473f3dccbe8b)
1 #ifndef WATCHTOWER_H
2 #define WATCHTOWER_H
3 
4 #include <memory>
5 #include <atomic>
6 #include <thread>
7 #include <mutex>
8 
9 #include "watchtowerlist.h"
10 
11 // FIXME
12 #include "inotifywatchtowerdriver.h"
13 
14 // Allow the client to register for notification on an arbitrary number
15 // of files. It will be notified to any change (creation/deletion/modification)
16 // on those files.
17 // It is passed a platform specific driver.
18 // template<typename Driver>
19 class WatchTower {
20   public:
21     // Registration object to implement RAII
22     using Registration = std::shared_ptr<void>;
23 
24     // Create an empty watchtower
25     WatchTower();
26     // Destroy the object
27     ~WatchTower();
28 
29     // Add a file to the notification list. notification will be called when
30     // the file is modified, moved or deleted.
31     // Lifetime of the notification is tied to the Registration object returned.
32     // Note the notification function is called with a mutex held and in a
33     // third party thread, beware of races!
34     Registration addFile( const std::string& file_name,
35             std::function<void()> notification );
36 
37   private:
38     // The driver (parametrised)
39     INotifyWatchTowerDriver driver_;
40 
41     // List of files/dirs observed
42     ObservedFileList observed_file_list_;
43 
44     // Protects the observed_file_list_
45     std::mutex observers_mutex_;
46 
47     // Thread
48     std::atomic_bool running_;
49     std::thread thread_;
50 
51     // Exist as long as the onject exists, to ensure observers won't try to
52     // call us if we are dead.
53     std::shared_ptr<void> heartBeat_;
54 
55     // Private member functions
56     static void removeNotification( WatchTower* watch_tower,
57             std::shared_ptr<void> notification );
58     void run();
59 };
60 
61 #endif
62