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