Sane C++ Libraries
C++ Platform Abstraction Libraries
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
FileSystemWatcher.h
1// Copyright (c) Stefano Cristiano
2// SPDX-License-Identifier: MIT
3#pragma once
4
5#include "../Async/Async.h" // AsyncLoopWakeUp
6#include "../Foundation/Function.h"
7#include "../Foundation/OpaqueObject.h"
8#include "../Foundation/Result.h"
9#include "../Threading/Threading.h" // EventObject
10
11namespace SC
12{
13
16
19
40{
41 private:
42 struct Internal;
43 struct ThreadRunnerInternal;
44 struct ThreadRunnerDefinition
45 {
46 static constexpr int MaxWatchablePaths = 1024;
47 static constexpr int Windows =
48 (2 * MaxWatchablePaths) * sizeof(void*) + sizeof(uint64_t) + sizeof(Thread) + sizeof(Action);
49 static constexpr int Apple = sizeof(void*);
50 static constexpr int Linux = sizeof(Thread) + sizeof(void*) * 2;
51 static constexpr int Default = Linux;
52
53 static constexpr size_t Alignment = alignof(void*);
54
55 using Object = ThreadRunnerInternal;
56 };
57
58 struct FolderWatcherInternal;
59 struct FolderWatcherSizes
60 {
61 static constexpr int MaxNumberOfSubdirs = 128; // Max number of subfolders tracked in a watcher
62 static constexpr int MaxChangesBufferSize = 1024;
63 static constexpr int Windows =
64 MaxChangesBufferSize + sizeof(void*) + sizeof(FileDescriptor) + sizeof(AsyncFilePoll);
65 static constexpr int Apple = sizeof(void*);
66 static constexpr int Linux = 1056;
67 static constexpr int Default = Linux;
68
69 static constexpr size_t Alignment = alignof(void*);
70
71 using Object = FolderWatcherInternal;
72 };
73 struct InternalDefinition
74 {
75 static constexpr int Windows = 3 * sizeof(void*);
76 static constexpr int Apple = 43 * sizeof(void*) + sizeof(Mutex);
77 static constexpr int Linux = sizeof(void*) * 4;
78 static constexpr int Default = Linux;
79
80 static constexpr size_t Alignment = alignof(void*);
81
82 using Object = Internal;
83 };
84
86
87 InternalOpaque internal;
88
89 public:
92 enum class Operation
93 {
94 Modified,
96 };
97
100 {
104
109
110 private:
111 friend struct Internal;
112#if SC_PLATFORM_APPLE
113 StringSpan fullPath;
114#endif
115 };
116
121 {
123
127
129 void setDebugName(const char* debugName);
130
131 private:
132 friend struct FileSystemWatcher;
133 friend struct IntrusiveDoubleLinkedList<FolderWatcher>;
134 FileSystemWatcher* parent = nullptr;
135 FolderWatcher* next = nullptr;
136 FolderWatcher* prev = nullptr;
137
138 StringPath path;
139
141 };
142
145 {
146
147 private:
148 friend struct FileSystemWatcher;
149 AsyncEventLoop* eventLoop = nullptr;
150#if SC_PLATFORM_APPLE
151 AsyncLoopWakeUp asyncWakeUp = {};
152 EventObject eventObject = {};
153#elif SC_PLATFORM_LINUX
154 AsyncFilePoll asyncPoll = {};
155#endif
156 };
157
160
165
171
175
182
183 private:
184 friend decltype(internal);
185 friend decltype(FolderWatcher::internal);
186 IntrusiveDoubleLinkedList<FolderWatcher> watchers;
187};
188
190} // namespace SC
unsigned long long uint64_t
Platform independent (8) bytes unsigned int.
Definition PrimitiveTypes.h:42
Asynchronous I/O (files, sockets, timers, processes, fs events, threads wake-up) (see Async) AsyncEve...
Definition Async.h:1187
Starts an handle polling operation.
Definition Async.h:820
Starts a wake-up operation, allowing threads to execute callbacks on loop thread.
Definition Async.h:339
An automatically reset event object to synchronize two threads.
Definition Threading.h:174
Open, read and write to/from a file descriptor (like a file or pipe).
Definition File.h:76
Delivers notifications using Async (SC::AsyncEventLoop).
Definition FileSystemWatcher.h:145
Represents a single folder being watched.
Definition FileSystemWatcher.h:121
Function< void(const Notification &)> notifyCallback
Function that will be called on a notification.
Definition FileSystemWatcher.h:122
void setDebugName(const char *debugName)
Sets debug name for AsyncFilePoll used on Windows (used only for debug purposes)
Result stopWatching()
Stop watching this directory.
Notification holding type and path.
Definition FileSystemWatcher.h:100
StringSpan relativePath
Relative path of the file being notified from basePath
Definition FileSystemWatcher.h:102
Operation operation
Notification type.
Definition FileSystemWatcher.h:103
StringSpan basePath
Reference to the watched directory.
Definition FileSystemWatcher.h:101
SC::Result getFullPath(StringPath &path) const
Get the full path of the file being watched.
Notifies about events (add, remove, rename, modified) on files and directories.
Definition FileSystemWatcher.h:40
Result close()
Stops all watchers and frees the ThreadRunner or EventLoopRunner passed in init.
Result init(ThreadRunner &runner)
Setup watcher to receive notifications from a background thread.
Result init(EventLoopRunner &runner, AsyncEventLoop &eventLoop)
Setup watcher to receive async notifications on SC::AsyncEventLoop.
Operation
Specifies the event classes.
Definition FileSystemWatcher.h:93
@ Modified
A file or directory has been modified in its contents and/or timestamp.
@ AddRemoveRename
A file or directory has been added, removed or renamed.
Result watch(FolderWatcher &watcher, StringSpan path)
Starts watching a single directory, calling FolderWatcher::notifyCallback on file events.
A native OS mutex to synchronize access to shared resources.
Definition Threading.h:28
An ascii string used as boolean result. SC_TRY macro forwards errors to caller.
Definition Result.h:12
Pre-sized char array holding enough space to represent a file system path.
Definition StringPath.h:10
An read-only view over a string (to avoid including Strings library when parsing is not needed).
Definition StringSpan.h:31
A native OS thread.
Definition Threading.h:118