-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathFileWatcher.h
146 lines (118 loc) · 3.97 KB
/
FileWatcher.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/**
Main header for the FileWatcher class. Declares all implementation
classes to reduce compilation overhead.
@author James Wynn
@date 4/15/2009
Copyright (c) 2009 James Wynn ([email protected])
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#ifndef _FW_FILEWATCHER_H_
#define _FW_FILEWATCHER_H_
#pragma once
#include <string>
#include <stdexcept>
namespace FW
{
/// Type for a string
typedef std::string String;
/// Type for a watch id
typedef unsigned long WatchID;
// forward declarations
class FileWatcherImpl;
class FileWatchListener;
/// Base exception class
/// @class Exception
class Exception : public std::runtime_error
{
public:
Exception(const String& message)
: std::runtime_error(message)
{}
};
/// Exception thrown when a file is not found.
/// @class FileNotFoundException
class FileNotFoundException : public Exception
{
public:
FileNotFoundException()
: Exception("File not found")
{}
FileNotFoundException(const String& filename)
: Exception("File not found (" + filename + ")")
{}
};
/// Actions to listen for. Rename will send two events, one for
/// the deletion of the old file, and one for the creation of the
/// new file.
namespace Actions
{
enum Action
{
/// Sent when a file is created or renamed
Add = 1,
/// Sent when a file is deleted or renamed
Delete = 2,
/// Sent when a file is modified
Modified = 4
};
};
typedef Actions::Action Action;
/// Listens to files and directories and dispatches events
/// to notify the parent program of the changes.
/// @class FileWatcher
class FileWatcher
{
public:
///
///
FileWatcher();
FileWatcher(FileWatcher&& other) noexcept;
FileWatcher(const FileWatcher& other) = delete;
FileWatcher& operator=(FileWatcher&& other) noexcept;
FileWatcher& operator=(const FileWatcher& other) = delete;
///
///
virtual ~FileWatcher();
/// Add a directory watch
/// @exception FileNotFoundException Thrown when the requested directory does not exist
WatchID addWatch(const String& directory, FileWatchListener* watcher);
/// Remove a directory watch. This is a brute force search O(nlogn).
void removeWatch(const String& directory);
/// Remove a directory watch. This is a map lookup O(logn).
void removeWatch(WatchID watchid);
/// Updates the watcher. Must be called often.
void update();
private:
/// The implementation
FileWatcherImpl* mImpl;
};//end FileWatcher
/// Basic interface for listening for file events.
/// @class FileWatchListener
class FileWatchListener
{
public:
FileWatchListener() {}
virtual ~FileWatchListener() {}
/// Handles the action file action
/// @param watchid The watch id for the directory
/// @param dir The directory
/// @param filename The filename that was accessed (not full path)
/// @param action Action that was performed
virtual void handleFileAction(WatchID watchid, const String& dir, const String& filename, Action action) = 0;
};//class FileWatchListener
};//namespace FW
#endif//_FW_FILEWATCHER_H_