Rose
Utilities.h
1 
8 #pragma once
9 
10 #include "XDGFilePaths.h"
11 #include <iterator>
12 #include <algorithm>
13 
14 #define XSTR(arg) STR(arg)
15 #define STR(arg) #arg
16 
17 #define FILE_LOC " -- ", __FILE__, ':', __LINE__
18 
19 namespace rose {
20 
21  template<typename U, typename C>
22  bool oneFlagOf(U flag, C container) {
23  static_assert(std::is_unsigned_v<U>, "Argument flag must be unsigned type." );
24  static_assert(std::is_unsigned_v<typename C::value_type>, "Container must hold an unsigned type.");
25  return std::any_of(container.begin(), container.end(), [&flag](typename C::value_type value){
26  return flag == value;
27  });
28  }
29 
35  template<class ContainerType>
37  protected:
38  ContainerType &mContainer;
39 
40  public:
41  ReverseContainerView() = delete;
42 
48  explicit ReverseContainerView(ContainerType &container) : mContainer(container) {}
49 
54  auto begin() {
55  return std::rbegin(mContainer);
56  }
57 
62  auto end() {
63  return std::rend(mContainer);
64  }
65  };
66 
73  template<class ContainerType>
74  class ContainerView {
75  protected:
76  ContainerType &mContainer;
77 
78  public:
79  ContainerView() = delete;
80 
86  explicit ContainerView(ContainerType &container) : mContainer(container) {}
87 
92  auto begin() {
93  return std::begin(mContainer);
94  }
95 
100  auto end() {
101  return std::end(mContainer);
102  }
103  };
104 
113  template<typename Arg, typename... Args>
114  std::string StringCompositor(Arg &&arg, Args &&... args) {
115  std::stringstream out;
116  out << std::forward<Arg>(arg);
117  ((out << std::forward<Args>(args)), ...);
118  return out.str();
119  }
120 
121  class Environment {
122  protected:
123  Environment();
124 
125  std::filesystem::path mHomeDirectory;
126  std::filesystem::path mDataHome;
127  std::filesystem::path mConfigHome;
128  std::filesystem::path mCacheHome;
129  std::filesystem::path mAppResources;
130 
136  std::filesystem::path mLibResources;
137  XDGFilePaths mFilePaths{};
138 
139  std::string mAppName;
140 
141  public:
142  ~Environment() = default;
143 
144  Environment(const Environment &) = delete;
145 
146  Environment(Environment &&) = delete;
147 
148  Environment& operator=(const Environment &) = delete;
149 
150  Environment& operator=(Environment &&) = delete;
151 
152  static Environment &getEnvironment() {
153  static Environment instance{};
154  return instance;
155  }
156 
157  [[nodiscard]] const std::string& appName() const { return mAppName; }
158 
159  [[nodiscard]] const std::filesystem::path& configHome() const { return mConfigHome; }
160 
161  [[nodiscard]] const std::filesystem::path& cacheHome() const { return mCacheHome; }
162 
163  [[nodiscard]] const std::filesystem::path& dataHome() const { return mDataHome; }
164 
165  [[nodiscard]] const std::filesystem::path& appResources() const { return mAppResources; }
166 
167  template<typename Source>
168  std::filesystem::path appResourcesAppend(Source source) {
169  auto path = mAppResources;
170  return path.append(source);
171  }
172 
182  std::filesystem::path getenv_path(XDGFilePaths::XDG_Name name, const std::string &appName, bool create);
183  };
184 }
185 
std::filesystem::path mAppResources
Resources installed with the application.
Definition: Utilities.h:129
std::string mAppName
Application name as provided by the system.
Definition: Utilities.h:139
auto end()
Get the end iterator for the reverse view.
Definition: Utilities.h:62
std::string StringCompositor(Arg &&arg, Args &&... args)
Composite a pack of arguments that are streamable to a string.
Definition: Utilities.h:114
Provide a reverse view of a standard container type.
Definition: Utilities.h:36
Provide a view of a standard container type.
Definition: Utilities.h:74
std::filesystem::path mConfigHome
The user&#39;s XDG Config Home path.
Definition: Utilities.h:127
auto begin()
Get the begin iterator for the reverse view.
Definition: Utilities.h:54
std::filesystem::path mHomeDirectory
The user&#39;s home directory path.
Definition: Utilities.h:125
ContainerType & mContainer
A reference to the standard container.
Definition: Utilities.h:38
ReverseContainerView(ContainerType &container)
Constructor.
Definition: Utilities.h:48
Definition: Utilities.h:121
A class to determine and provide searching of XDG standard files paths.
Definition: XDGFilePaths.h:23
std::filesystem::path mCacheHome
The user&#39;s XDG Cache Home path.
Definition: Utilities.h:128
ContainerView(ContainerType &container)
Constructor.
Definition: Utilities.h:86
auto end()
Get the end iterator for the reverse view.
Definition: Utilities.h:100
ContainerType & mContainer
A reference to the standard container.
Definition: Utilities.h:76
ToDo: There is an issue that the initial scroll interaction is lost if the click/press lands on a Wid...
Definition: CelestialOverlay.cpp:13
std::filesystem::path mDataHome
The user&#39;s XDG Data Home path.
Definition: Utilities.h:126
std::filesystem::path mLibResources
Resources shared by applications using the library.
Definition: Utilities.h:136
auto begin()
Get the begin iterator for the reverse view.
Definition: Utilities.h:92