Add a class for monitoring changes in files
[libs/core.git] / source / fs / filemonitor.cpp
1 #include "filemonitor.h"
2 #include "filemonitor_platform.h"
3
4 using namespace std;
5
6 namespace Msp {
7 namespace FS {
8
9 FileMonitor::FileMonitor():
10         priv(new Private(*this)),
11         event_disp(0)
12 { }
13
14 FileMonitor::~FileMonitor()
15 {
16         delete priv;
17 }
18
19 void FileMonitor::use_event_dispatcher(IO::EventDispatcher &ed)
20 {
21         if(event_disp)
22                 throw logic_error("event_disp!=0");
23
24         event_disp = &ed;
25         platform_use_event_dispatcher();
26 }
27
28 void FileMonitor::add_file(const FS::Path &path)
29 {
30         MonitoredFile file;
31         file.path = path;
32         file.modified = false;
33         prepare_file(file);
34         files.push_back(file);
35 }
36
37 void FileMonitor::remove_file(const FS::Path &path)
38 {
39         for(vector<MonitoredFile>::iterator i=files.begin(); i!=files.end(); ++i)
40                 if(i->path==path)
41                 {
42                         cleanup_file(*i);
43                         if(&*i!=&files.back())
44                                 *i = files.back();
45                         files.pop_back();
46                         break;
47                 }
48 }
49
50 } // namespace FS
51 } // namespace Msp