FileChangeObserver.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #include <vector>
  2. #include <future>
  3. #include <boost/filesystem.hpp>
  4. #include <boost/asio.hpp>
  5. #include <boost/bind/bind.hpp>
  6. #include <boost/date_time/posix_time/posix_time.hpp>
  7. #ifndef _FILE_CHANGE_OBSERVER_H_
  8. #define _FILE_CHANGE_OBSERVER_H_
  9. namespace alex
  10. {
  11. class FileChangeObserver
  12. {
  13. using PathList = std::vector<boost::filesystem::path>;
  14. using DateList = std::vector<std::time_t>;
  15. public:
  16. FileChangeObserver() noexcept;
  17. FileChangeObserver(const PathList& path_list, const uint32_t timer_interval_msec, const uint32_t update_frequency) noexcept;
  18. ~FileChangeObserver() noexcept;
  19. FileChangeObserver(const FileChangeObserver&) = delete;
  20. FileChangeObserver& operator=(const FileChangeObserver&) = delete;
  21. void add_path(const boost::filesystem::path& path) noexcept;
  22. void add_paths(const PathList& path_list) noexcept;
  23. inline void set_timer_interval(const uint32_t timer_interval_msec) noexcept
  24. { m_timer_interval_msec = timer_interval_msec; }
  25. inline void set_update_frequency(const uint32_t update_frequency)
  26. { m_update_frequency = update_frequency; }
  27. void start(const std::function<void(const boost::filesystem::path&)>& notifier) noexcept;
  28. inline auto async_start(const std::function<void(const boost::filesystem::path&)>& notifier) noexcept
  29. { return std::async(std::launch::async, std::bind(&FileChangeObserver::start, this, notifier)); }
  30. private:
  31. static PathList m_working_dirs;
  32. static PathList m_files_list;
  33. static DateList m_date_list;
  34. static uint32_t m_timer_interval_msec;
  35. static uint32_t m_update_frequency;
  36. static void load_files_and_dates() noexcept;
  37. static inline void update_files_and_dates() noexcept
  38. {
  39. m_files_list.clear();
  40. m_date_list.clear();
  41. load_files_and_dates();
  42. }
  43. static void on_timer_update(const boost::system::error_code& error, boost::asio::deadline_timer* timer,
  44. const std::function<void()>& run_observer) noexcept;
  45. static void start_observing(const std::function<void(const boost::filesystem::path&)>& notifier) noexcept;
  46. };
  47. } // namespace alex
  48. #endif // _FILE_CHANGE_OBSERVER_H_