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_entry_created_t = std::function<void(const std::string&, const fs::path&, const std::vector<fs::path>&, bool)>;
19 using on_entry_modified_t = std::function<void(const std::string&, const fs::path&, const std::vector<fs::path>&, bool)>;
20 using on_entry_removed_t = std::function<void(const std::string&, const fs::path&, const std::vector<fs::path>&)>;
21 using on_entry_renamed_t = std::function<void(const std::string&, const rename_pair_t&, const std::vector<rename_pair_t>&)>;
22
31 using mapping_t = std::unordered_map<std::string, mapping>;
32
33 syncer() = default;
34 ~syncer();
35
36 //-----------------------------------------------------------------------------
37 // Name : set_mapping ()
44 //-----------------------------------------------------------------------------
45 void set_mapping(const std::string& ref_ext,
46 const std::vector<std::string>& synced_ext,
47 on_entry_created_t on_entry_created,
48 on_entry_modified_t on_entry_modified,
49 on_entry_removed_t on_entry_removed,
50 on_entry_renamed_t on_entry_renamed);
51
52 void set_directory_mapping(on_entry_created_t on_entry_created,
53 on_entry_modified_t on_entry_modified,
54 on_entry_removed_t on_entry_removed,
55 on_entry_renamed_t on_entry_renamed);
56
57 //-----------------------------------------------------------------------------
58 // Name : sync ()
64 //-----------------------------------------------------------------------------
65 void sync(const fs::path& reference_dir, const fs::path& synced_dir);
66
67 //-----------------------------------------------------------------------------
68 // Name : unsync ()
72 //-----------------------------------------------------------------------------
73 void unsync();
74
75private:
76 auto get_mapping(const std::string& ext) -> mapping;
77 auto get_on_created_callback(const std::string& ext) -> on_entry_created_t;
78 auto get_on_modified_callback(const std::string& ext) -> on_entry_modified_t;
79 auto get_on_removed_callback(const std::string& ext) -> on_entry_removed_t;
80 auto get_on_renamed_callback(const std::string& ext) -> on_entry_renamed_t;
81 //-----------------------------------------------------------------------------
82 // Name : get_synced_directory ()
86 //-----------------------------------------------------------------------------
87 auto get_synced_directory(const fs::path& path) -> fs::path;
88
89 //-----------------------------------------------------------------------------
90 // Name : get_path_protocols ()
94 //-----------------------------------------------------------------------------
95 auto get_synced_entries(const fs::path& path, bool is_directory) -> std::vector<fs::path>;
96
97 //-----------------------------------------------------------------------------
98 // Name : get_watch_path ()
102 //-----------------------------------------------------------------------------
103 auto get_watch_path() -> fs::path;
104
106 std::mutex mutex_;
108 mapping_t mapping_;
110 fs::path reference_dir_;
112 fs::path synced_dir_;
113
114 std::atomic<std::uint64_t> watch_id_ = {0};
115};
116
144} // 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:18
std::function< void(const std::string &, const fs::path &, const std::vector< fs::path > &, bool)> on_entry_modified_t
Definition syncer.h:19
void unsync()
Stops syncing.
Definition syncer.cpp:53
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:24
std::function< void(const std::string &, const rename_pair_t &, const std::vector< rename_pair_t > &)> on_entry_renamed_t
Definition syncer.h:21
void sync(const fs::path &reference_dir, const fs::path &synced_dir)
Start syncing the synced_dir with reference to the reference_dir i.e changes that occur in the refere...
Definition syncer.cpp:90
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:40
std::unordered_map< std::string, mapping > mapping_t
Definition syncer.h:31
std::function< void(const std::string &, const fs::path &, const std::vector< fs::path > &)> on_entry_removed_t
Definition syncer.h:20
Definition cache.hpp:11
std::vector< std::string > extensions
Definition syncer.h:25
on_entry_removed_t on_entry_removed
Definition syncer.h:28
on_entry_modified_t on_entry_modified
Definition syncer.h:27
on_entry_renamed_t on_entry_renamed
Definition syncer.h:29
on_entry_created_t on_entry_created
Definition syncer.h:26