Added namespace to file

main
anulax1225 ago%!(EXTRA string=1 year)
parent 46284efe28
commit 2f2a11dda1
  1. 68
      src/bakatools/file_system/file_watcher.cpp
  2. 41
      src/bakatools/file_system/file_watcher.h
  3. 61
      src/bakatools/thread/task_timer.h
  4. 118
      src/bakatools/time/time.h

@ -1,47 +1,49 @@
#include "file_watcher.h" #include "file_watcher.h"
FileWatcher::FileWatcher(std::string path, TimeSpan<Millisecond> ts) namespace Bk::Tools {
: target(path), ttm(ts) FileWatcher::FileWatcher(std::string path, TimeSpan<Millisecond> 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); 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<void (std::string, FileStatus)>& action) void FileWatcher::start(const std::function<void (std::string, FileStatus)>& action)
{
std::function<void()> task([&]()
{ {
auto it = paths.begin(); std::function<void()> task([&]()
while (it != paths.end())
{ {
if (!std::filesystem::exists(it->first)) auto it = paths.begin();
while (it != paths.end())
{ {
action(it->first, FileStatus::Deleted); if (!std::filesystem::exists(it->first))
it = paths.erase(it); {
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))
}
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()))
{ {
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; 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<std::function<void()>>(task));
ttm.start(std::make_unique<std::function<void()>>(task)); }
}
void FileWatcher::stop() { ttm.stop(); } void FileWatcher::stop() { ttm.stop(); }
bool FileWatcher::contains(std::string path) { return paths.find(path) != paths.end(); } bool FileWatcher::contains(std::string path) { return paths.find(path) != paths.end(); }
}

@ -2,26 +2,27 @@
#include <bakatools.h> #include <bakatools.h>
#include <bakatools/thread/task_timer.h> #include <bakatools/thread/task_timer.h>
namespace Bk::Tools {
enum class FileStatus
{
Created = 0,
Modified = 1,
Deleted = 2,
};
enum class FileStatus class FileWatcher
{ {
Created = 0, public:
Modified = 1, FileWatcher(std::string path, TimeSpan<Millisecond> ts);
Deleted = 2, ~FileWatcher();
};
class FileWatcher void start(const std::function<void (std::string, FileStatus)>& action);
{ void stop();
public: bool contains(std::string path);
FileWatcher(std::string path, TimeSpan<Millisecond> ts);
~FileWatcher();
void start(const std::function<void (std::string, FileStatus)>& action); private:
void stop(); std::string target;
bool contains(std::string path); std::unordered_map<std::string, std::filesystem::file_time_type> paths;
TaskTimer<Millisecond> ttm;
private: };
std::string target; }
std::unordered_map<std::string, std::filesystem::file_time_type> paths;
TaskTimer<Millisecond> ttm;
};

@ -2,38 +2,39 @@
#include <bakatools.h> #include <bakatools.h>
#include <bakatools/time/time.h> #include <bakatools/time/time.h>
namespace Bk::Tools {
template<typename P>
class TaskTimer
{
public:
TaskTimer(TimeSpan<P> ts)
: ts(ts) {}
~TaskTimer() { if (running) stop(); }
template<typename P> void start(std::unique_ptr<std::function<void()>> action)
class TaskTimer
{
public:
TaskTimer(TimeSpan<P> ts)
: ts(ts) {}
~TaskTimer() { if (running) stop(); }
void start(std::unique_ptr<std::function<void()>> action)
{
if(running) stop();
running = true;
worker = std::thread([this](std::unique_ptr<std::function<void()>> action)
{ {
std::function<void()>& task = *action; if(running) stop();
while(running) running = true;
worker = std::thread([this](std::unique_ptr<std::function<void()>> action)
{ {
std::this_thread::sleep_for(ts.interval); std::function<void()>& task = *action;
task(); while(running)
} {
}, std::move(action)); std::this_thread::sleep_for(ts.interval);
} task();
}
}, std::move(action));
}
void stop() void stop()
{ {
running = false; running = false;
worker.join(); worker.join();
} }
private: private:
bool running = false; bool running = false;
std::thread worker; std::thread worker;
TimeSpan<P> ts; TimeSpan<P> ts;
}; };
}

@ -2,75 +2,77 @@
#include <bakatoolspch.h> #include <bakatoolspch.h>
using Nanosecond = std::ratio<1,1000000000>; namespace Bk::Tools {
using Microsecond = std::ratio<1,1000000>; using Nanosecond = std::ratio<1,1000000000>;
using Millisecond = std::ratio<1,1000>; using Microsecond = std::ratio<1,1000000>;
using Second = std::ratio<1,1>; using Millisecond = std::ratio<1,1000>;
using Minute = std::ratio<60, 1>; using Second = std::ratio<1,1>;
using Hour = std::ratio<3600, 1>; using Minute = std::ratio<60, 1>;
using Day = std::ratio<84600, 1>; using Hour = std::ratio<3600, 1>;
using Day = std::ratio<84600, 1>;
using SysClock = std::chrono::system_clock; using SysClock = std::chrono::system_clock;
using SteadyClock = std::chrono::steady_clock; using SteadyClock = std::chrono::steady_clock;
template<typename P> template<typename P>
struct TimeSpan struct TimeSpan
{
TimeSpan(int interval = 0)
: interval(std::chrono::duration<long int, P>(interval)) {}
TimeSpan(std::chrono::duration<long int, P> interval)
: interval(interval) {}
template<typename T>
TimeSpan<T> get_as()
{ {
return TimeSpan<T>(std::chrono::duration_cast<std::chrono::duration<long int, T>>(interval)); TimeSpan(int interval = 0)
} : interval(std::chrono::duration<long int, P>(interval)) {}
TimeSpan(std::chrono::duration<long int, P> interval)
: interval(interval) {}
int count() { return interval.count(); } template<typename T>
TimeSpan<T> get_as()
{
return TimeSpan<T>(std::chrono::duration_cast<std::chrono::duration<long int, T>>(interval));
}
TimeSpan<P> operator-(TimeSpan<P>& time_span) int count() { return interval.count(); }
{
return TimeSpan<P>(interval - time_span.interval);
}
std::chrono::duration<long int, P> interval; TimeSpan<P> operator-(TimeSpan<P>& time_span)
}; {
return TimeSpan<P>(interval - time_span.interval);
}
template<typename C, typename P> std::chrono::duration<long int, P> interval;
struct TimePoint };
{
TimePoint() = default;
TimePoint(std::chrono::time_point<C> point)
{
this->point = std::chrono::time_point_cast<std::chrono::duration<long int, P>>(point);
}
template<typename T> template<typename C, typename P>
TimePoint<C, T> get_as() struct TimePoint
{ {
return TimePoint<C, T>(std::chrono::time_point_cast<std::chrono::duration<long int, T>>(point)); TimePoint() = default;
} TimePoint(std::chrono::time_point<C> point)
{
this->point = std::chrono::time_point_cast<std::chrono::duration<long int, P>>(point);
}
TimeSpan<P> elasped() template<typename T>
{ TimePoint<C, T> get_as()
return TimeSpan<P>(point.time_since_epoch()); {
} return TimePoint<C, T>(std::chrono::time_point_cast<std::chrono::duration<long int, T>>(point));
}
bool is_steady() TimeSpan<P> elasped()
{ {
return C::is_steady; return TimeSpan<P>(point.time_since_epoch());
} }
TimeSpan<P> operator-(TimePoint<C, P>& tp) bool is_steady()
{ {
return TimeSpan<P>(point - tp.point); return C::is_steady;
} }
static TimePoint<C, P> now() TimeSpan<P> operator-(TimePoint<C, P>& tp)
{ {
return TimePoint<C, P>(C::now()); return TimeSpan<P>(point - tp.point);
} }
static TimePoint<C, P> now()
{
return TimePoint<C, P>(C::now());
}
std::chrono::time_point<C, std::chrono::duration<long int, P>> point; std::chrono::time_point<C, std::chrono::duration<long int, P>> point;
}; };
}
Loading…
Cancel
Save