Rose
Utilities.h
1 
8 #pragma once
9 
10 #include <exception>
11 #include <filesystem>
12 #include <iomanip>
13 #include <memory>
14 #include <optional>
15 #include <sstream>
16 #include <chrono>
17 #include <SDL.h>
18 #include <stack>
19 #include "Constants.h"
20 
21 namespace rose::util {
22 
23  template<typename T>
24  std::string fmtNumber(T value, int precision) {
25  std::stringstream strm{};
26  strm << std::setprecision(precision) << value;
27  return strm.str();
28  }
29 
35  template<class Iterator>
36  struct iterator_pair {
37  Iterator mBegin, mEnd;
38 
39  Iterator begin() const { return mBegin; }
40  Iterator end() const { return mEnd; }
41  };
42 
46  template<class C>
47  using iterator_t = decltype(std::begin(std::declval<C&>()));
48 
57  template<class C>
58  iterator_pair<iterator_t<C>> offset(C& container, size_t skip) {
59  return {std::next(container.begin(), skip), container.end()};
60  }
61 
70  template<typename T, size_t N>
71  std::ostream & printScreenMetric(std::ostream &strm, const std::array<T,N> &metric) {
72  strm << '(' << metric.front();
73  for (auto &i : offset(metric,1)) {
74  strm << ',' << i;
75  }
76  return strm << ')';
77  }
78 
85  template<typename T>
86  extern std::chrono::system_clock::time_point fileClockToSystemClock(T fileTimePoint) {
87  using namespace std::chrono_literals;
88  using namespace std::chrono;
89 
90  auto sysWriteTime = time_point_cast<system_clock::duration>(
91  fileTimePoint - decltype(fileTimePoint)::clock::now() +
92  system_clock::now());
93  return sysWriteTime;
94  }
95 
100  struct FontMetrics {
101  int fontAscent,
102  fontDescent,
103  fontHeight,
104  fontLineSkip;
105 
106  constexpr FontMetrics(const FontMetrics &) noexcept = default;
107  constexpr FontMetrics(FontMetrics &&) noexcept = default;
108  constexpr FontMetrics& operator=(const FontMetrics &) noexcept = default;
109  constexpr FontMetrics& operator=(FontMetrics &&) noexcept = default;
110  };
111 
117  template<class ContainerType>
119  protected:
120  ContainerType &mContainer;
121 
122  public:
123  ReverseContainerView() = delete;
124 
130  explicit ReverseContainerView(ContainerType &container, bool reverse = true) : mContainer(container) {}
131 
136  auto begin() {
137  return std::rbegin(mContainer);
138  }
139 
144  auto end() {
145  return std::rend(mContainer);
146  }
147  };
148 
154 #define XSTR(arg) STR(arg)
155 #define STR(arg) #arg
156 
157 #define FILE_LOC " -- ", __FILE__, ':', __LINE__
158 
159 
162  using DebugTuple = std::optional<std::tuple<const std::string_view, unsigned int>>;
163 
167 #define DEBUG_TUPLE(use) use ? std::make_optional(std::make_tuple(std::string_view{__FILE__},__LINE__)) : (std::nullopt)
168 
175  static bool bxor(bool a, bool b) { return b == !a; }
176 
185  template<typename Arg, typename... Args>
186  std::string StringCompositor(Arg &&arg, Args &&... args) {
187  std::stringstream out;
188  out << std::forward<Arg>(arg);
189  ((out << std::forward<Args>(args)), ...);
190  return out.str();
191  }
192 }
std::chrono::system_clock::time_point fileClockToSystemClock(T fileTimePoint)
Convert a filesystem time to a system clock time point.
Definition: Utilities.h:86
Provide a reverse view of a standard container type.
Definition: Utilities.h:118
int fontLineSkip
The size of a line advance for the font.
Definition: Utilities.h:101
auto begin()
Get the begin iterator for the reverse view.
Definition: Utilities.h:136
std::string StringCompositor(Arg &&arg, Args &&... args)
Composite a pack of arguments that are streamable to a string.
Definition: Utilities.h:186
auto end()
Get the end iterator for the reverse view.
Definition: Utilities.h:144
Constants and Enumerations.
The size metrics that pertain to a particular font.
Definition: Utilities.h:100
Utility Functions.
Definition: Math.h:14
iterator_pair< iterator_t< C > > offset(C &container, size_t skip)
Crate an iterator_pair to generate an offset sequence.
Definition: Utilities.h:58
std::ostream & printScreenMetric(std::ostream &strm, const std::array< T, N > &metric)
A function to print ScreenMetric values.
Definition: Utilities.h:71
A pair of iterators which may be used as first and last in the sequence [first .
Definition: Utilities.h:36
ReverseContainerView(ContainerType &container, bool reverse=true)
Constructor.
Definition: Utilities.h:130
std::optional< std::tuple< const std::string_view, unsigned int > > DebugTuple
A type to encode the location of an error: file name and line number.
Definition: Utilities.h:162
ContainerType & mContainer
A reference to the standard container.
Definition: Utilities.h:120
decltype(std::begin(std::declval< C & >())) iterator_t
An iterator type definition.
Definition: Utilities.h:47