mrs_lib
Various reusable classes, functions and utilities for use in MRS projects
Loading...
Searching...
No Matches
utils.h
Go to the documentation of this file.
1// clang: MatousFormat
8#ifndef UTILS_H
9#define UTILS_H
10
11#include <iterator>
12#include <vector>
13#include <sstream>
14#include <atomic>
15
16namespace mrs_lib
17{
18 /* containerToString() function //{ */
19
29 template <typename Iterator>
30 std::string containerToString(const Iterator begin, const Iterator end, const std::string& delimiter = ", ")
31 {
32 bool first = true;
33 std::ostringstream output;
34 for (Iterator it = begin; it != end; it++)
35 {
36 if (!first)
37 output << delimiter;
38 output << *it;
39 first = false;
40 }
41 return output.str();
42 }
43
53 template <typename Iterator>
54 std::string containerToString(const Iterator begin, const Iterator end, const char* delimiter)
55 {
56 return containerToString(begin, end, std::string(delimiter));
57 }
58
67 template <typename Container>
68 std::string containerToString(const Container& cont, const std::string& delimiter = ", ")
69 {
70 return containerToString(std::begin(cont), std::end(cont), delimiter);
71 }
72
81 template <typename Container>
82 std::string containerToString(const Container& cont, const char* delimiter = ", ")
83 {
84 return containerToString(std::begin(cont), std::end(cont), std::string(delimiter));
85 }
86
87 //}
88
89 /* remove_const() function //{ */
90
99 template <typename T>
100 typename T::iterator remove_const(const typename T::const_iterator& it, T& cont)
101 {
102 typename T::iterator ret = cont.begin();
103 std::advance(ret, std::distance((typename T::const_iterator)ret, it));
104 return ret;
105 }
106
107 //}
108
115 {
116
117 public:
124 AtomicScopeFlag(std::atomic<bool>& in);
130
131 private:
132 std::atomic<bool>& variable;
133 };
134
135 // branchless, templated, more efficient version of the signum function
136 // taken from https://stackoverflow.com/questions/1903954/is-there-a-standard-sign-function-signum-sgn-in-c-c
137 template <typename T>
138 int signum(T val)
139 {
140 return (T(0) < val) - (val < T(0));
141 }
142
143} // namespace mrs_lib
144
145#endif
Convenience class for automatically setting and unsetting an atomic boolean based on the object's sco...
Definition utils.h:115
~AtomicScopeFlag()
The destructor. Resets the variable given in the constructor to false.
Definition utils.cpp:12
All mrs_lib functions, classes, variables and definitions are contained in this namespace.
Definition attitude_converter.h:24
T::iterator remove_const(const typename T::const_iterator &it, T &cont)
Convenience class for removing const-ness from a container iterator.
Definition utils.h:100
std::string containerToString(const Iterator begin, const Iterator end, const std::string &delimiter=", ")
Convenience function for converting container ranges to strings (e.g. for printing).
Definition utils.h:30