Sese Framework  2.3.0
A cross-platform framework
Loading...
Searching...
No Matches
FileNotifier.h
Go to the documentation of this file.
1// Copyright 2024 libsese
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
20
21#pragma once
22
23#include "sese/Config.h"
24#include "sese/util/ErrorCode.h"
25#include "sese/util/Result.h"
26
27#include <atomic>
28#ifdef __APPLE__
29#include <tuple>
30#else
31#include "sese/thread/Thread.h"
32#endif
33
34namespace sese::system {
35
38 virtual ~FileNotifyOption() = default;
39
41 virtual void onCreate(std::string_view name) = 0;
42
44 virtual void onMove(std::string_view src_name, std::string_view dst_name) = 0;
45
47 virtual void onModify(std::string_view name) = 0;
48
50 virtual void onDelete(std::string_view name) = 0;
51};
52
57public:
58 using Ptr = std::unique_ptr<FileNotifier>;
59
64 static FileNotifier::Ptr create(const std::string &path, FileNotifyOption *option) noexcept;
65
66 static Result<Ptr, ErrorCode> createEx(const std::string &path, FileNotifyOption *option) noexcept;
67
68 virtual ~FileNotifier() noexcept;
69
71 void loopNonblocking() noexcept;
72
74 void shutdown() noexcept;
75
76private:
77 FileNotifier() = default;
78
79#ifdef WIN32
80 void *file_handle = nullptr;
81 void *overlapped = nullptr;
82 std::atomic_bool is_shutdown = false;
83 FileNotifyOption *option = nullptr;
84 Thread::Ptr th = nullptr;
85#elif __linux__
86 int inotify_fd = -1;
87 int watch_fd = -1;
88 std::atomic_bool is_shutdown = false;
89 FileNotifyOption *option = nullptr;
90 Thread::Ptr th = nullptr;
91#elif __APPLE__
92 void *stream = nullptr;
93 void *queue = nullptr;
94#endif
95};
96} // namespace sese::system