coral
A C++ library for distributed co-simulation
util.hpp
Go to the documentation of this file.
1 
10 #ifndef CORAL_UTIL_HPP
11 #define CORAL_UTIL_HPP
12 
13 #include <cstdint>
14 #include <string>
15 #include <utility>
16 #include <vector>
17 
18 #include "coral/config.h"
19 #include "boost/filesystem/path.hpp"
20 #include "boost/noncopyable.hpp"
21 
22 
23 namespace coral
24 {
25 
27 namespace util
28 {
29 
30 
32 void EncodeUint16(std::uint16_t source, char target[2]);
33 
34 
36 void EncodeUint32(std::uint32_t source, char target[4]);
37 
38 
40 void EncodeUint64(std::uint64_t source, char target[8]);
41 
42 
44 std::uint16_t DecodeUint16(const char source[2]);
45 
46 
48 std::uint32_t DecodeUint32(const char source[4]);
49 
50 
52 std::uint64_t DecodeUint64(const char source[8]);
53 
54 
62 int ArrayStringCmp(const char* array, size_t length, const char* stringz);
63 
64 
67 
68 
78 std::string RandomString(size_t size, const char* charSet);
79 
87 
88 
97 template<typename T1, typename T2>
98 T1 MoveAndReplace(T1& variable, const T2& replacement)
99 {
100  auto tmp = std::move(variable);
101  variable = replacement;
102  return std::move(tmp);
103 }
104 
109 template<typename T>
110 T MoveAndReplace(T& variable)
111 {
112  return MoveAndReplace(variable, T());
113 }
114 
115 
133 template<typename F>
134 void LastCall(F& f)
135 {
136  F tmp;
137  swap(f, tmp);
138  tmp();
139 }
140 
141 template<typename F, typename... Args>
142 void LastCall(F& f, Args&&... args)
143 {
144  F tmp;
145  swap(f, tmp);
146  tmp(std::forward<Args>(args)...);
147 }
148 
149 
150 template<typename Action>
151 class ScopeGuard : boost::noncopyable
152 {
153 public:
154  explicit ScopeGuard(Action action) : m_active(true), m_action(action) { }
155 
156  ScopeGuard(ScopeGuard&& other)
157  : m_active(MoveAndReplace(other.m_active, false)),
158  m_action(std::move(other.m_action))
159  { }
160 
161  ScopeGuard& operator=(ScopeGuard&& other)
162  {
163  m_active = MoveAndReplace(other.m_active, false);
164  m_action = std::move(other.m_action);
165  }
166 
167  ~ScopeGuard() { if (m_active) m_action(); }
168 
169 private:
170  bool m_active;
171  Action m_action;
172 };
173 
188 template<typename Action>
189 ScopeGuard<Action> OnScopeExit(Action action) { return ScopeGuard<Action>(action); }
190 
191 
199 void SpawnProcess(
200  const std::string& program,
201  const std::vector<std::string>& args);
202 
203 
208 boost::filesystem::path ThisExePath();
209 
210 
211 }} // namespace
212 #endif // header guard
void EncodeUint64(std::uint64_t source, char target[8])
Encodes a 64-bit unsigned integer using little-endian byte order.
void SpawnProcess(const std::string &program, const std::vector< std::string > &args)
Starts a new process.
void EncodeUint16(std::uint16_t source, char target[2])
Encodes a 16-bit unsigned integer using little-endian byte order.
std::string RandomUUID()
Returns a string that contains a random UUID.
std::string Timestamp()
Returns the current UTC time in the ISO 8601 "basic" format.
void LastCall(F &f)
Calls the given function(-like object), but only after swapping it with a default-constructed one...
Definition: util.hpp:134
STL class.
boost::filesystem::path ThisExePath()
Returns the path of the current executable.
std::uint64_t DecodeUint64(const char source[8])
Decodes a 64-bit unsigned integer using little-endian byte order.
ScopeGuard< Action > OnScopeExit(Action action)
Scope guard.
Definition: util.hpp:189
Definition: variable_io.hpp:28
T1 MoveAndReplace(T1 &variable, const T2 &replacement)
Moves a value, replacing it with another one.
Definition: util.hpp:98
T move(T...args)
std::uint16_t DecodeUint16(const char source[2])
Decodes a 16-bit unsigned integer using little-endian byte order.
std::string RandomString(size_t size, const char *charSet)
Creates a random string.
std::uint32_t DecodeUint32(const char source[4])
Decodes a 32-bit unsigned integer using little-endian byte order.
void EncodeUint32(std::uint32_t source, char target[4])
Encodes a 32-bit unsigned integer using little-endian byte order.
int ArrayStringCmp(const char *array, size_t length, const char *stringz)
Given a character array and its length, compares it lexicographically to a zero-terminated string...