watchdog.h
Go to the documentation of this file.00001 #ifndef WATCHDOG_H
00002 #define WATCHDOG_H
00003
00009 #include <config.h>
00010 #include <string>
00011 #include <set>
00012 #include <map>
00013 #include <QObject>
00014 #include <QString>
00015
00016 #include <util/forall.h>
00017
00018 namespace util
00019 {
00020
00030 class FileObject : public QObject
00031 {
00032 Q_OBJECT
00033
00034 public:
00040 FileObject(QObject *parent = 0) : QObject(parent) {}
00046 FileObject(std::string fn, QObject *parent = 0) : QObject(parent), filename(fn) {}
00052 FileObject(QString fn, QObject *parent = 0) : QObject(parent), filename(fn.toStdString()) {}
00058 FileObject(const FileObject& copy, QObject *parent = 0) : QObject(parent), filename(copy.filename) {}
00067 FileObject& operator=(const FileObject& other)
00068 { if(other.filename != filename) { filename = other.filename; } return *this; }
00069
00070 #ifdef USE_CXX0X
00071 FileObject(FileObject&& other, QObject * parent = 0)
00072 : QObject(parent)
00073 , filename(std::move(other.filename))
00074 { }
00075
00076 FileObject& operator=(FileObject&& other)
00077 {
00078 filename = std::move(other.filename);
00079 return *this;
00080 }
00081 #endif
00082
00086 virtual void reread() = 0;
00087
00091 std::string getFilename() { return filename; }
00092
00099 void setFilename(std::string fn);
00100
00107 void setFilename(QString fn) { setFilename(fn.toStdString()); }
00108
00115 void setFilename(const char* fn) { setFilename(std::string(fn)); }
00116
00117 void update();
00118
00119 signals:
00123 void modified();
00124
00125 protected:
00126 std::string filename;
00127 };
00128
00134 class WatchDog : public QObject
00135 {
00136 Q_OBJECT
00137
00138 public:
00144 WatchDog(QObject* m);
00145
00149 bool guarded(FileObject *obj) const;
00150 bool guarded(FileObject& obj) const { return guarded(&obj); }
00151
00155 void addObject(FileObject* obj);
00159 void addObject(FileObject& obj) { addObject(&obj); }
00166 void changeFilename(FileObject* obj, std::string fn);
00170 void removeObject(FileObject *obj);
00174 void removeObject(FileObject &obj) { removeObject(&obj); }
00175
00184 bool watch(const std::set<std::string>& filenames);
00185
00186 signals:
00187 void registerFile(std::string);
00188 void unregisterFile(std::string);
00189
00190 protected:
00194 QObject *model;
00198 std::set<FileObject*> fileObjects;
00202 std::map<std::string, std::set<FileObject*> > names;
00203 };
00204
00205 }
00206 #endif // WATCHDOG_H
00207