CeresEngine 0.2.0
A game development framework
Loading...
Searching...
No Matches
FileWatcher.hpp
Go to the documentation of this file.
1
2//
3// CeresEngine - A game development framework
4//
5// Created by Rogiel Sulzbach.
6// Copyright (c) 2018-2023 Rogiel Sulzbach. All rights reserved.
7//
8
9#pragma once
10
12
15
20
21#include <functional>
22#include <memory>
23#include <vector>
24
25namespace CeresEngine {
26
28 enum class FileEvent {
29 Created = 0x01,
30 Removed = 0x02,
31 Modified = 0x04,
32 AttributeChanged = 0x08,
33
35 };
36
39
112
115 friend class FileWatcher;
116
117 protected:
121
122 public:
124 virtual ~IFileWatcher();
125
126 public:
128 [[nodiscard]] virtual IFileSystem* getFileSystem() const = 0;
129
131 virtual void start() = 0;
132
134 virtual void stop() = 0;
135
136 protected:
138 void onFileEvent(const FileHandle& handle, FileEvent event);
139 };
140
144 friend class FileWatcher;
145
146 public:
147 FileEventHandler() = default;
148 virtual ~FileEventHandler() = default;
149
150 protected:
152 virtual void onFileEvent(const FileHandle& handle, FileEvent event);
153
156 virtual void onFileCreated(const FileHandle& handle);
157
160 virtual void onFileRemoved(const FileHandle& handle);
161
164 virtual void onFileModified(const FileHandle& handle);
165
168 virtual void onFileAttributesChanged(const FileHandle& handle);
169 };
170
173 public:
175 using EventFunc = std::function<void(const FileHandle&, FileEvent)>;
176
177 private:
180
181 public:
183
187 explicit FunctionalFileEventHandler(EventFunc&& funcFileEvent);
188
190
191 protected:
193 void onFileEvent(const FileHandle& handle, FileEvent event) final;
194 };
195
196} // namespace CeresEngine
#define CE_EXPLICIT(EXPR)
Definition Macros.hpp:413
A context for function object execution.
Definition ExecutionContext.hpp:90
A handler that is informed about file system events.
Definition FileWatcher.hpp:143
virtual void onFileEvent(const FileHandle &handle, FileEvent event)
virtual void onFileModified(const FileHandle &handle)
Called when a file or directory has been modified.
virtual void onFileRemoved(const FileHandle &handle)
Called when a file or directory has been removed.
virtual ~FileEventHandler()=default
virtual void onFileAttributesChanged(const FileHandle &handle)
Called when file attributes have been modified.
virtual void onFileCreated(const FileHandle &handle)
Called when a file or directory has been created.
Handle for a file or directory.
Definition FileHandle.hpp:46
Watcher that reports on changes of files or directories.
Definition FileWatcher.hpp:41
FileWatcher(ExecutionContext &executionContext, const FileHandle &directory, const FileEvent &events=FileEvent::Default, bool recursive=true)
Creates a new watcher for the given directory.
void onFileEvent(const FileHandle &handle, FileEvent event)
Called on file event.
SPtr< IFileWatcher > ImplementationType
Definition FileWatcher.hpp:43
IFileSystem * getFileSystem() const
Gets file system that this watcher watches for.
Vector< UPtr< FunctionalFileEventHandler > > mOwnEventHandlers
Functional event handlers that are owned by the file watcher.
Definition FileWatcher.hpp:53
void start()
Starts the file watching operation.
void addHandler(FileEventHandler &eventHandler)
Adds an event handler.
std::function< void(const FileHandle &, FileEvent)> EventFunc
Callback function for file system events.
Definition FileWatcher.hpp:57
void removeHandler(const FileEventHandler &eventHandler)
Remove an event handler.
ImplementationType mImplementation
The file handle implementation.
Definition FileWatcher.hpp:47
void stop()
Stops the file watching operation.
Vector< FileEventHandler * > mEventHandlers
List of registered file event handlers.
Definition FileWatcher.hpp:50
A FileEventHandler that calls a function or a lambda.
Definition FileWatcher.hpp:172
FunctionalFileEventHandler(EventFunc &&funcFileEvent)
Creates a new FunctionalFileEventHandler.
std::function< void(const FileHandle &, FileEvent)> EventFunc
Callback function type for file system events.
Definition FileWatcher.hpp:175
EventFunc mFuncFileEvent
A Callback function for file system events.
Definition FileWatcher.hpp:179
Interface for accessing file systems.
Definition FileSystem.hpp:28
Interface for file watcher implementations.
Definition FileWatcher.hpp:114
virtual IFileSystem * getFileSystem() const =0
Gets file system that this watcher watches for.
FileWatcher * mFileWatcher
File watcher that owns the implementation.
Definition FileWatcher.hpp:120
void onFileEvent(const FileHandle &handle, FileEvent event)
Called on file event.
virtual void start()=0
Starts the file watching operation.
virtual void stop()=0
Stops the file watching operation.
Definition Application.hpp:19
std::shared_ptr< T > SPtr
SPtr is a smart pointer that retains shared ownership of an object through a pointer.
Definition SmartPtr.hpp:37
FileEvent
Type of event on the file system.
Definition FileWatcher.hpp:28
@ Created
A file or directory has been created.
@ AttributeChanged
Attributes on a file or directory have been modified.
@ Modified
A file or directory has been modified.
std::vector< T, ScopedAllocatorAdaptor< StdAllocator< T, RawAllocator > > > Vector
Vector is a sequence container that encapsulates dynamic size arrays.
Definition Vector.hpp:17
constexpr size_t hash(const T &v)
Generates a hash for the provided type.
Definition Hash.hpp:25