Horizon
util.hpp
1 #pragma once
2 #include "common/common.hpp"
3 #include "nlohmann/json_fwd.hpp"
4 #include <string>
5 #include <vector>
6 #include <functional>
7 
8 namespace horizon {
9 using json = nlohmann::json;
10 void save_json_to_file(const std::string &filename, const json &j);
11 json load_json_from_file(const std::string &filename);
12 int orientation_to_angle(Orientation o);
13 std::string get_exe_dir();
14 void allow_set_foreground_window(int pid);
15 std::string coord_to_string(const Coordf &c, bool delta = false);
16 std::string dim_to_string(int64_t x, bool with_sign = true);
17 std::string angle_to_string(int angle, bool pos_only = true);
18 
19 int64_t round_multiple(int64_t x, int64_t mul);
20 
21 template <typename T, typename U> std::vector<T> dynamic_cast_vector(const std::vector<U> &cin)
22 {
23  std::vector<T> out;
24  out.reserve(cin.size());
25  std::transform(cin.begin(), cin.end(), std::back_inserter(out), [](auto x) { return dynamic_cast<T>(x); });
26  return out;
27 }
28 
29 template <typename Map, typename F> static void map_erase_if(Map &m, F pred)
30 {
31  for (typename Map::iterator i = m.begin(); (i = std::find_if(i, m.end(), pred)) != m.end(); m.erase(i++))
32  ;
33 }
34 
35 bool endswith(const std::string &haystack, const std::string &needle);
36 
37 template <typename T> int sgn(T val)
38 {
39  return (T(0) < val) - (val < T(0));
40 }
41 
42 int strcmp_natural(const std::string &a, const std::string &b);
43 void create_config_dir();
44 std::string get_config_dir();
45 
46 void replace_backslash(std::string &path);
47 json json_from_resource(const std::string &rsrc);
48 bool compare_files(const std::string &filename_a, const std::string &filename_b);
49 void find_files_recursive(const std::string &base_path, std::function<void(const std::string &)> cb,
50  const std::string &path = "");
51 
52 Color color_from_json(const json &j);
53 json color_to_json(const Color &c);
54 
55 } // namespace horizon
a class to store JSON values
Definition: json.hpp:161
Definition: block.cpp:9
basic_json<> json
default JSON class
Definition: json_fwd.hpp:61