Sane C++ Libraries
C++ Platform Abstraction Libraries
FileSystemWatcher.h
1// Copyright (c) Stefano Cristiano
2// SPDX-License-Identifier: MIT
3#pragma once
4
5#include "../Async/Async.h" // AsyncLoopWakeUp
6#include "../Containers/IntrusiveDoubleLinkedList.h"
7#include "../Foundation/Function.h"
8#include "../Foundation/OpaqueObject.h"
9#include "../Foundation/Result.h"
10#include "../Strings/SmallString.h"
11#include "../Threading/Threading.h" // EventObject
12
13namespace SC
14{
15struct FileSystemWatcher;
16struct String;
17} // namespace SC
18
21
24
45{
46 private:
47 struct Internal;
48 struct ThreadRunnerInternal;
49 struct ThreadRunnerDefinition
50 {
51 static constexpr int MaxWatchablePaths = 1024;
52 static constexpr int Windows =
53 (2 * MaxWatchablePaths) * sizeof(void*) + sizeof(uint64_t) + sizeof(Thread) + sizeof(Action);
54 static constexpr int Apple = sizeof(void*);
55 static constexpr int Default = sizeof(Thread) + sizeof(void*) * 2;
56
57 static constexpr size_t Alignment = alignof(void*);
58
59 using Object = ThreadRunnerInternal;
60 };
61
62 struct FolderWatcherInternal;
63 struct FolderWatcherSizes
64 {
65 static constexpr int MaxNumberOfSubdirs = 128; // Max number of subfolders tracked in a watcher
66 static constexpr int MaxChangesBufferSize = 1024;
67 static constexpr int Windows =
68 MaxChangesBufferSize + sizeof(void*) + sizeof(FileDescriptor) + sizeof(AsyncFilePoll);
69 static constexpr int Apple = sizeof(void*);
70 static constexpr int Default =
71 sizeof(Array<int64_t, MaxNumberOfSubdirs>) + sizeof(Vector<char>) + sizeof(void*);
72
73 static constexpr size_t Alignment = alignof(void*);
74
75 using Object = FolderWatcherInternal;
76 };
77 struct InternalDefinition
78 {
79 static constexpr int Windows = 3 * sizeof(void*);
80 static constexpr int Apple = 43 * sizeof(void*) + sizeof(Mutex);
81 static constexpr int Default = sizeof(void*) * 4;
82
83 static constexpr size_t Alignment = alignof(void*);
84
85 using Object = Internal;
86 };
87
89
90 InternalOpaque internal;
91
92 public:
95 enum class Operation
96 {
97 Modified,
99 };
100
103 {
107
112 SC::Result getFullPath(String& bufferString, StringView& outFullPath) const;
113
114 private:
115 friend struct Internal;
116#if SC_PLATFORM_APPLE
117 StringView fullPath;
118#endif
119 };
120
125 {
127
131
132 private:
133 friend struct FileSystemWatcher;
135 FileSystemWatcher* parent = nullptr;
136 FolderWatcher* next = nullptr;
137 FolderWatcher* prev = nullptr;
139
141 };
142
145 {
146
147 private:
148 friend struct FileSystemWatcher;
149 AsyncEventLoop* eventLoop = nullptr;
150#if SC_PLATFORM_APPLE
151 AsyncLoopWakeUp eventLoopAsync = {};
152 EventObject eventObject = {};
153#elif SC_PLATFORM_LINUX
154 AsyncFilePoll asyncPoll = {};
155#endif
156 };
157
160
164 [[nodiscard]] Result init(ThreadRunner& runner);
165
170 [[nodiscard]] Result init(EventLoopRunner& runner, AsyncEventLoop& eventLoop);
171
174 [[nodiscard]] Result close();
175
181 [[nodiscard]] Result watch(FolderWatcher& watcher, StringView path);
182
183 private:
184 friend decltype(internal);
185 friend decltype(FolderWatcher::internal);
187};
188
unsigned long long uint64_t
Platform independent (8) bytes unsigned int.
Definition: PrimitiveTypes.h:42
A contiguous sequence of elements kept inside its inline storage.
Definition: Array.h:43
Asynchronous I/O (files, sockets, timers, processes, fs events, threads wake-up) (see Async) AsyncEve...
Definition: Async.h:933
Starts an handle polling operation.
Definition: Async.h:883
Starts a wake-up operation, allowing threads to execute callbacks on loop thread.
Definition: Async.h:346
An automatically reset event object to synchronize two threads.
Definition: Threading.h:174
Wraps an OS File descriptor to read and write to and from it.
Definition: FileDescriptor.h:57
Delivers notifications using Async (SC::AsyncEventLoop).
Definition: FileSystemWatcher.h:145
Represents a single folder being watched.
Definition: FileSystemWatcher.h:125
Function< void(const Notification &)> notifyCallback
Function that will be called on a notification.
Definition: FileSystemWatcher.h:126
Result stopWatching()
Stop watching this directory.
Notification holding type and path.
Definition: FileSystemWatcher.h:103
SC::Result getFullPath(String &bufferString, StringView &outFullPath) const
Get the full path of the file being watched.
StringView basePath
Reference to the watched directory.
Definition: FileSystemWatcher.h:104
Operation operation
Notification type.
Definition: FileSystemWatcher.h:106
StringView relativePath
Relative path of the file being notified from basePath
Definition: FileSystemWatcher.h:105
Notifies about events (add, remove, rename, modified) on files and directories.
Definition: FileSystemWatcher.h:45
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 watch(FolderWatcher &watcher, StringView path)
Starts watching a single directory, calling FolderWatcher::notifyCallback on file events.
Result init(EventLoopRunner &runner, AsyncEventLoop &eventLoop)
Setup watcher to receive async notifications on SC::AsyncEventLoop.
Operation
Specifies the event classes.
Definition: FileSystemWatcher.h:96
@ 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.
Wraps function pointers, member functions and lambdas without ever allocating.
Definition: Function.h:50
An Intrusive Double Linked List.
Definition: IntrusiveDoubleLinkedList.h:22
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
String with compile time configurable inline storage (small string optimization)
Definition: SmallString.h:21
A non-modifiable owning string with associated encoding.
Definition: String.h:30
Non-owning view over a range of characters with UTF Encoding.
Definition: StringView.h:47
A native OS thread.
Definition: Threading.h:118
A contiguous sequence of heap allocated elements.
Definition: Vector.h:51