Unravel Engine C++ Reference
Loading...
Searching...
No Matches
syncer.h
Go to the documentation of this file.
1#pragma once
2
3#include "filesystem.h"
4#include <atomic>
5#include <functional>
6#include <mutex>
7#include <string>
8#include <unordered_map>
9#include <vector>
10
11namespace fs
12{
13class syncer
14{
15public:
16 using rename_pair_t = std::pair<fs::path, fs::path>;
17
18 using on_sync_progress_t = std::function<void(size_t completed, size_t total, const std::string& current_job)>;
19 using on_entry_created_t = std::function<void(const std::string&, const fs::path&, const std::vector<fs::path>&, bool)>;
20 using on_entry_modified_t = std::function<void(const std::string&, const fs::path&, const std::vector<fs::path>&, bool)>;
21 using on_entry_removed_t = std::function<void(const std::string&, const fs::path&, const std::vector<fs::path>&)>;
22 using on_entry_renamed_t = std::function<void(const std::string&, const rename_pair_t&, const std::vector<rename_pair_t>&)>;
23
32 using mapping_t = std::unordered_map<std::string, mapping>;
33
34 syncer() = default;
35 ~syncer();
36
37 //-----------------------------------------------------------------------------
38 // Name : set_mapping ()
45 //-----------------------------------------------------------------------------
46 void set_mapping(const std::string& ref_ext,
47 const std::vector<std::string>& synced_ext,
48 on_entry_created_t on_entry_created,
49 on_entry_modified_t on_entry_modified,
50 on_entry_removed_t on_entry_removed,
51 on_entry_renamed_t on_entry_renamed);
52
53 void set_directory_mapping(on_entry_created_t on_entry_created,
54 on_entry_modified_t on_entry_modified,
55 on_entry_removed_t on_entry_removed,
56 on_entry_renamed_t on_entry_renamed);
57
58 //-----------------------------------------------------------------------------
59 // Name : sync ()
65 //-----------------------------------------------------------------------------
66 void sync(const fs::path& reference_dir, const fs::path& synced_dir, const on_sync_progress_t& on_progress = nullptr);
67
68 //-----------------------------------------------------------------------------
69 // Name : unsync ()
73 //-----------------------------------------------------------------------------
74 void unsync();
75
76private:
77 auto get_mapping(const std::string& ext) -> mapping;
78 auto get_on_created_callback(const std::string& ext) -> on_entry_created_t;
79 auto get_on_modified_callback(const std::string& ext) -> on_entry_modified_t;
80 auto get_on_removed_callback(const std::string& ext) -> on_entry_removed_t;
81 auto get_on_renamed_callback(const std::string& ext) -> on_entry_renamed_t;
82 //-----------------------------------------------------------------------------
83 // Name : get_synced_directory ()
87 //-----------------------------------------------------------------------------
88 auto get_synced_directory(const fs::path& path) -> fs::path;
89
90 //-----------------------------------------------------------------------------
91 // Name : get_path_protocols ()
95 //-----------------------------------------------------------------------------
96 auto get_synced_entries(const fs::path& path, bool is_directory) -> std::vector<fs::path>;
97
98 //-----------------------------------------------------------------------------
99 // Name : get_watch_path ()
103 //-----------------------------------------------------------------------------
104 auto get_watch_path() -> fs::path;
105
107 std::mutex mutex_;
109 mapping_t mapping_;
111 fs::path reference_dir_;
113 fs::path synced_dir_;
114
115 std::atomic<std::uint64_t> watch_id_ = {0};
116};
117
145} // namespace fs
syncer()=default
std::pair< fs::path, fs::path > rename_pair_t
Definition syncer.h:16
std::function< void(const std::string &, const fs::path &, const std::vector< fs::path > &, bool)> on_entry_created_t
Definition syncer.h:19
std::function< void(const std::string &, const fs::path &, const std::vector< fs::path > &, bool)> on_entry_modified_t
Definition syncer.h:20
void unsync()
Stops syncing.
Definition syncer.cpp:107
void sync(const fs::path &reference_dir, const fs::path &synced_dir, const on_sync_progress_t &on_progress=nullptr)
Start syncing the synced_dir with reference to the reference_dir i.e changes that occur in the refere...
Definition syncer.cpp:148
void set_mapping(const std::string &ref_ext, const std::vector< std::string > &synced_ext, on_entry_created_t on_entry_created, on_entry_modified_t on_entry_modified, on_entry_removed_t on_entry_removed, on_entry_renamed_t on_entry_renamed)
Remaps a specific extension of the reference directory to extensions of the synced directory....
Definition syncer.cpp:78
std::function< void(const std::string &, const rename_pair_t &, const std::vector< rename_pair_t > &)> on_entry_renamed_t
Definition syncer.h:22
void set_directory_mapping(on_entry_created_t on_entry_created, on_entry_modified_t on_entry_modified, on_entry_removed_t on_entry_removed, on_entry_renamed_t on_entry_renamed)
Definition syncer.cpp:94
std::unordered_map< std::string, mapping > mapping_t
Definition syncer.h:32
std::function< void(const std::string &, const fs::path &, const std::vector< fs::path > &)> on_entry_removed_t
Definition syncer.h:21
std::function< void(size_t completed, size_t total, const std::string &current_job)> on_sync_progress_t
Definition syncer.h:18
Definition cache.hpp:11
std::vector< std::string > extensions
Definition syncer.h:26
on_entry_removed_t on_entry_removed
Definition syncer.h:29
on_entry_modified_t on_entry_modified
Definition syncer.h:28
on_entry_renamed_t on_entry_renamed
Definition syncer.h:30
on_entry_created_t on_entry_created
Definition syncer.h:27