demotool/filewatcher.hh

74 lines
1.8 KiB
C++

#pragma once
#ifndef REAL_BUILD
# include "externals.hh"
#endif
/*= T_FilesWatcher / T_WatchedFiles ==========================================*/
/* So we need to be able to watch for files being created, updated and deleted.
* Watching for updates & deletions is pretty easy. For files being created,
* one has to watch the parent directory. In addition, the case where a file
* is being updated by rotation (move old, write new, rm old) needs to be
* handled correctly as it is what vim does.
*/
struct T_FilesWatcher;
struct T_WatchedFiles;
using F_OnFileChanges = std::function< void( void ) >;
struct T_FilesWatcher
{
friend struct T_WatchedFiles;
T_FilesWatcher( T_FilesWatcher const& ) = delete;
T_FilesWatcher( );
T_FilesWatcher( T_FilesWatcher&& ) noexcept;
~T_FilesWatcher( );
void check( );
private:
struct T_File_ {
int wd;
std::vector< T_WatchedFiles* > watchers;
void trigger( );
};
int fd;
std::map< std::string , T_File_ > fileWatchers_;
std::map< int , std::string > inotify_;
std::vector< std::string > missing_;
std::vector< T_WatchedFiles* > watched_;
void watchFile( __rd__ std::string const& path ,
__rd__ T_WatchedFiles* const watcher );
void unwatchAll( __rd__ T_WatchedFiles* const watcher );
};
/*----------------------------------------------------------------------------*/
struct T_WatchedFiles
{
friend struct T_FilesWatcher;
T_WatchedFiles( ) = delete;
T_WatchedFiles( T_WatchedFiles const& ) = delete;
T_WatchedFiles( T_WatchedFiles&& ) noexcept;
T_WatchedFiles(
__rw__ T_FilesWatcher& watcher ,
__rd__ const F_OnFileChanges callback );
~T_WatchedFiles( );
void clear( );
bool watch( __rd__ std::string const& file );
private:
T_FilesWatcher* watcher;
const F_OnFileChanges callback;
bool triggered;
};
using P_WatchedFiles = std::unique_ptr< T_WatchedFiles >;