From 2f2a11dda1a596d40c18a34f39eabae36e0f342b Mon Sep 17 00:00:00 2001 From: anulax1225 Date: Sun, 24 Mar 2024 19:17:30 +0100 Subject: [PATCH] Added namespace to file --- src/bakatools/file_system/file_watcher.cpp | 68 ++++++------ src/bakatools/file_system/file_watcher.h | 41 +++---- src/bakatools/thread/task_timer.h | 61 +++++------ src/bakatools/time/time.h | 118 +++++++++++---------- 4 files changed, 147 insertions(+), 141 deletions(-) diff --git a/src/bakatools/file_system/file_watcher.cpp b/src/bakatools/file_system/file_watcher.cpp index 4adc842..2c083c3 100644 --- a/src/bakatools/file_system/file_watcher.cpp +++ b/src/bakatools/file_system/file_watcher.cpp @@ -1,47 +1,49 @@ #include "file_watcher.h" -FileWatcher::FileWatcher(std::string path, TimeSpan ts) -: target(path), ttm(ts) -{ - for (const std::filesystem::directory_entry& file : std::filesystem::recursive_directory_iterator(target)) - paths[file.path().string()] = std::filesystem::last_write_time(file); -} +namespace Bk::Tools { + FileWatcher::FileWatcher(std::string path, TimeSpan ts) + : target(path), ttm(ts) + { + for (const std::filesystem::directory_entry& file : std::filesystem::recursive_directory_iterator(target)) + paths[file.path().string()] = std::filesystem::last_write_time(file); + } -FileWatcher::~FileWatcher() { stop(); } + FileWatcher::~FileWatcher() { stop(); } -void FileWatcher::start(const std::function& action) -{ - std::function task([&]() + void FileWatcher::start(const std::function& action) { - auto it = paths.begin(); - while (it != paths.end()) + std::function task([&]() { - if (!std::filesystem::exists(it->first)) + auto it = paths.begin(); + while (it != paths.end()) { - action(it->first, FileStatus::Deleted); - it = paths.erase(it); + if (!std::filesystem::exists(it->first)) + { + action(it->first, FileStatus::Deleted); + it = paths.erase(it); + } + else it++; } - else it++; - } - for (const std::filesystem::directory_entry& file : std::filesystem::recursive_directory_iterator(target)) - { - auto current_file_last_write_time = std::filesystem::last_write_time(file); - if (contains(file.path().string())) + for (const std::filesystem::directory_entry& file : std::filesystem::recursive_directory_iterator(target)) { - if(paths[file.path().string()] != current_file_last_write_time) { + auto current_file_last_write_time = std::filesystem::last_write_time(file); + if (contains(file.path().string())) + { + if(paths[file.path().string()] != current_file_last_write_time) { + paths[file.path().string()] = current_file_last_write_time; + action(file.path().string(), FileStatus::Modified); + } + } else + { paths[file.path().string()] = current_file_last_write_time; - action(file.path().string(), FileStatus::Modified); + action(file.path().string(), FileStatus::Created); } - } else - { - paths[file.path().string()] = current_file_last_write_time; - action(file.path().string(), FileStatus::Created); } - } - }); - ttm.start(std::make_unique>(task)); -} + }); + ttm.start(std::make_unique>(task)); + } -void FileWatcher::stop() { ttm.stop(); } + void FileWatcher::stop() { ttm.stop(); } -bool FileWatcher::contains(std::string path) { return paths.find(path) != paths.end(); } \ No newline at end of file + bool FileWatcher::contains(std::string path) { return paths.find(path) != paths.end(); } +} \ No newline at end of file diff --git a/src/bakatools/file_system/file_watcher.h b/src/bakatools/file_system/file_watcher.h index 6def1e0..9c05b68 100644 --- a/src/bakatools/file_system/file_watcher.h +++ b/src/bakatools/file_system/file_watcher.h @@ -2,26 +2,27 @@ #include #include +namespace Bk::Tools { + enum class FileStatus + { + Created = 0, + Modified = 1, + Deleted = 2, + }; -enum class FileStatus -{ - Created = 0, - Modified = 1, - Deleted = 2, -}; + class FileWatcher + { + public: + FileWatcher(std::string path, TimeSpan ts); + ~FileWatcher(); -class FileWatcher -{ - public: - FileWatcher(std::string path, TimeSpan ts); - ~FileWatcher(); + void start(const std::function& action); + void stop(); + bool contains(std::string path); - void start(const std::function& action); - void stop(); - bool contains(std::string path); - - private: - std::string target; - std::unordered_map paths; - TaskTimer ttm; -}; \ No newline at end of file + private: + std::string target; + std::unordered_map paths; + TaskTimer ttm; + }; +} \ No newline at end of file diff --git a/src/bakatools/thread/task_timer.h b/src/bakatools/thread/task_timer.h index ac8419d..6778b28 100644 --- a/src/bakatools/thread/task_timer.h +++ b/src/bakatools/thread/task_timer.h @@ -2,38 +2,39 @@ #include #include +namespace Bk::Tools { + template + class TaskTimer + { + public: + TaskTimer(TimeSpan

ts) + : ts(ts) {} + ~TaskTimer() { if (running) stop(); } -template -class TaskTimer -{ - public: - TaskTimer(TimeSpan

ts) - : ts(ts) {} - ~TaskTimer() { if (running) stop(); } - - void start(std::unique_ptr> action) - { - if(running) stop(); - running = true; - worker = std::thread([this](std::unique_ptr> action) + void start(std::unique_ptr> action) { - std::function& task = *action; - while(running) + if(running) stop(); + running = true; + worker = std::thread([this](std::unique_ptr> action) { - std::this_thread::sleep_for(ts.interval); - task(); - } - }, std::move(action)); - } + std::function& task = *action; + while(running) + { + std::this_thread::sleep_for(ts.interval); + task(); + } + }, std::move(action)); + } - void stop() - { - running = false; - worker.join(); - } + void stop() + { + running = false; + worker.join(); + } - private: - bool running = false; - std::thread worker; - TimeSpan

ts; -}; \ No newline at end of file + private: + bool running = false; + std::thread worker; + TimeSpan

ts; + }; +} \ No newline at end of file diff --git a/src/bakatools/time/time.h b/src/bakatools/time/time.h index 3c03fd1..22948b9 100644 --- a/src/bakatools/time/time.h +++ b/src/bakatools/time/time.h @@ -2,75 +2,77 @@ #include -using Nanosecond = std::ratio<1,1000000000>; -using Microsecond = std::ratio<1,1000000>; -using Millisecond = std::ratio<1,1000>; -using Second = std::ratio<1,1>; -using Minute = std::ratio<60, 1>; -using Hour = std::ratio<3600, 1>; -using Day = std::ratio<84600, 1>; +namespace Bk::Tools { + using Nanosecond = std::ratio<1,1000000000>; + using Microsecond = std::ratio<1,1000000>; + using Millisecond = std::ratio<1,1000>; + using Second = std::ratio<1,1>; + using Minute = std::ratio<60, 1>; + using Hour = std::ratio<3600, 1>; + using Day = std::ratio<84600, 1>; -using SysClock = std::chrono::system_clock; -using SteadyClock = std::chrono::steady_clock; + using SysClock = std::chrono::system_clock; + using SteadyClock = std::chrono::steady_clock; -template -struct TimeSpan -{ - TimeSpan(int interval = 0) - : interval(std::chrono::duration(interval)) {} - TimeSpan(std::chrono::duration interval) - : interval(interval) {} - - template - TimeSpan get_as() + template + struct TimeSpan { - return TimeSpan(std::chrono::duration_cast>(interval)); - } + TimeSpan(int interval = 0) + : interval(std::chrono::duration(interval)) {} + TimeSpan(std::chrono::duration interval) + : interval(interval) {} - int count() { return interval.count(); } + template + TimeSpan get_as() + { + return TimeSpan(std::chrono::duration_cast>(interval)); + } - TimeSpan

operator-(TimeSpan

& time_span) - { - return TimeSpan

(interval - time_span.interval); - } + int count() { return interval.count(); } - std::chrono::duration interval; -}; + TimeSpan

operator-(TimeSpan

& time_span) + { + return TimeSpan

(interval - time_span.interval); + } -template -struct TimePoint -{ - TimePoint() = default; - TimePoint(std::chrono::time_point point) - { - this->point = std::chrono::time_point_cast>(point); - } + std::chrono::duration interval; + }; - template - TimePoint get_as() + template + struct TimePoint { - return TimePoint(std::chrono::time_point_cast>(point)); - } + TimePoint() = default; + TimePoint(std::chrono::time_point point) + { + this->point = std::chrono::time_point_cast>(point); + } - TimeSpan

elasped() - { - return TimeSpan

(point.time_since_epoch()); - } + template + TimePoint get_as() + { + return TimePoint(std::chrono::time_point_cast>(point)); + } - bool is_steady() - { - return C::is_steady; - } + TimeSpan

elasped() + { + return TimeSpan

(point.time_since_epoch()); + } - TimeSpan

operator-(TimePoint& tp) - { - return TimeSpan

(point - tp.point); - } + bool is_steady() + { + return C::is_steady; + } - static TimePoint now() - { - return TimePoint(C::now()); - } + TimeSpan

operator-(TimePoint& tp) + { + return TimeSpan

(point - tp.point); + } + + static TimePoint now() + { + return TimePoint(C::now()); + } - std::chrono::time_point> point; -}; \ No newline at end of file + std::chrono::time_point> point; + }; +} \ No newline at end of file