Ignition Common

API Reference

3.0.0
include/ignition/common/Util.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2016 Open Source Robotics Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16 */
17 #ifndef IGNITION_COMMON_UTIL_HH_
18 #define IGNITION_COMMON_UTIL_HH_
19 
20 #include <cassert>
21 #include <memory>
22 #include <string>
23 #include <future>
24 #include <vector>
25 #include <ignition/common/Export.hh>
27 
29 // Defines
30 
31 #ifdef _WIN32
32 # define IGN_HOMEDIR "HOMEPATH"
33 #else
34 # define IGN_HOMEDIR "HOME"
35 #endif
36 
38 #define IGN_NANO_TO_SEC 1e-9
39 
41 #define IGN_SEC_TO_NANO 1000000000
42 
44 #define IGN_MS_TO_NANO 1000000
45 
47 #define IGN_US_TO_NANO 1000
48 
50 #define IGN_SPEED_OF_LIGHT = 299792458.0
51 
53 #define IGN_SLEEP_S(_s) (std::this_thread::sleep_for(\
54  std::chrono::seconds(_s)))
55 
57 #define IGN_SLEEP_US(_us) (std::this_thread::sleep_for(\
58  std::chrono::microseconds(_us)))
59 
61 #define IGN_SLEEP_MS(_ms) (std::this_thread::sleep_for(\
62  std::chrono::milliseconds(_ms)))
63 
65 #define IGN_SLEEP_NS(_ns) (std::this_thread::sleep_for(\
66  std::chrono::nanoseconds(_ns)))
67 
69 #define IGN_SYSTEM_TIME() (std::chrono::system_clock::now())
70 
72 #define IGN_SYSTEM_TIME_S() (std::chrono::duration_cast<std::chrono::seconds>(\
73  std::chrono::system_clock::now().time_since_epoch()).count())
74 
76 #define IGN_SYSTEM_TIME_US() (\
77  std::chrono::duration_cast<std::chrono::microseconds>(\
78  std::chrono::system_clock::now().time_since_epoch()).count())
79 
81 #define IGN_SYSTEM_TIME_MS() (\
82  std::chrono::duration_cast<std::chrono::milliseconds>(\
83  std::chrono::system_clock::now().time_since_epoch()).count())
84 
86 #define IGN_SYSTEM_TIME_NS() (\
87  std::chrono::duration_cast<std::chrono::nanoseconds>(\
88  std::chrono::system_clock::now().time_since_epoch()).count())
89 
92 #define IGN_ASSERT(_expr, _msg) assert((_msg, _expr))
93 
95 namespace ignition
96 {
97  namespace common
98  {
101 
104 
107  std::string IGNITION_COMMON_VISIBLE systemTimeISO();
108 
111  std::string IGNITION_COMMON_VISIBLE logPath();
112 
115  void IGNITION_COMMON_VISIBLE addSearchPathSuffix(
116  const std::string &_suffix);
117 
121  std::string IGNITION_COMMON_VISIBLE findFile(const std::string &_file);
122 
128  std::string IGNITION_COMMON_VISIBLE findFile(const std::string &_file,
129  bool _searchLocalPath);
130 
134  std::string IGNITION_COMMON_VISIBLE findFilePath(const std::string &_file);
135 
140  template<typename T>
141  std::string sha1(const T &_buffer);
142 
149  std::string IGNITION_COMMON_VISIBLE sha1(
150  void const *_buffer, std::size_t _byteCount);
151 
152  #ifdef _MSC_VER
153  #pragma warning(disable:4307)
154  #endif
155 
160  constexpr uint64_t IGNITION_COMMON_VISIBLE hash64(std::string_view _key)
161  {
162  const char *data = _key.data();
163  const auto len = _key.size();
164  const uint64_t prime = 0x100000001b3;
165  uint64_t hash = 0xcbf29ce484222325;
166 
167  for (auto i = 0u; i < len; ++i)
168  {
169  uint8_t value = data[i];
170  hash = hash ^ value;
171  hash *= prime;
172  }
173 
174  return hash;
175  }
176 
177  #ifdef _MSC_VER
178  #pragma warning(pop)
179  #endif
180 
185  bool IGNITION_COMMON_VISIBLE env(
186  const std::string &_name, std::string &_value);
187 
190  std::string IGNITION_COMMON_VISIBLE uuid();
191 
196  std::vector<std::string> IGNITION_COMMON_VISIBLE split(
197  const std::string &_str, const std::string &_delim);
198 
201  void IGNITION_COMMON_VISIBLE ltrim(std::string &_s);
202 
205  void IGNITION_COMMON_VISIBLE rtrim(std::string &_s);
206 
209  void IGNITION_COMMON_VISIBLE trim(std::string &_s);
210 
214  std::string IGNITION_COMMON_VISIBLE ltrimmed(std::string _s);
215 
219  std::string IGNITION_COMMON_VISIBLE rtrimmed(std::string _s);
220 
224  std::string IGNITION_COMMON_VISIBLE trimmed(std::string _s);
225 
229  std::string IGNITION_COMMON_VISIBLE lowercase(const std::string &_in);
230 
234  std::string IGNITION_COMMON_VISIBLE lowercase(const char *_in);
235 
244  void IGNITION_COMMON_VISIBLE replaceAll(std::string &_result,
245  const std::string &_orig,
246  const std::string &_key,
247  const std::string &_replacement);
248 
257  std::string IGNITION_COMMON_VISIBLE replaceAll(const std::string &_orig,
258  const std::string &_key,
259  const std::string &_replacement);
260  }
261 }
262 
264 // Implementation of get_sha1
265 template<typename T>
267 {
268  if (_buffer.size() == 0)
269  return ignition::common::sha1(NULL, 0);
270  else
271  {
272  return ignition::common::sha1(
273  &(_buffer[0]), _buffer.size() * sizeof(_buffer[0]));
274  }
275 }
276 #endif
std::string sha1(const T &_buffer)
Compute the SHA1 hash of an array of bytes.
Definition: include/ignition/common/Util.hh:266
std::string findFilePath(const std::string &_file)
search for a file in common::SystemPaths
std::string uuid()
Get a UUID.
void addSearchPathSuffix(const std::string &_suffix)
add path sufix to common::SystemPaths
std::string systemTimeISO()
Get the wall time as an ISO string: YYYY-MM-DDTHH:MM:SS.NS.
void replaceAll(std::string &_result, const std::string &_orig, const std::string &_key, const std::string &_replacement)
Replace all occurances of _key with _replacement.
std::string ltrimmed(std::string _s)
Copying left trim.
std::runtime_error exception
A runtime error.
Definition: include/ignition/common/Util.hh:103
void rtrim(std::string &_s)
In place right trim.
STL class.
bool env(const std::string &_name, std::string &_value)
Find the environment variable &#39;_name&#39; and return its value.
std::string logPath()
Get the log path.
Definition: include/ignition/common/Util.hh:100
Definition: include/ignition/common/Util.hh:100
constexpr uint64_t hash64(std::string_view _key)
fnv1a algorithm for 64-bit platforms.
Definition: include/ignition/common/Util.hh:160
std::string lowercase(const std::string &_in)
Transforms a string to its lowercase equivalent.
NodeTransformType
Enumeration of the transform types.
Definition: include/ignition/common/Util.hh:100
std::string findFile(const std::string &_file)
search for file in common::SystemPaths
std::vector< std::string > split(const std::string &_str, const std::string &_delim)
Splits a string into tokens.
STL class.
std::string rtrimmed(std::string _s)
Copying right trim.
std::string trimmed(std::string _s)
Copying trim from both ends.
Forward declarations for the common classes.
void trim(std::string &_s)
In place trim from both ends.
Definition: include/ignition/common/Util.hh:100
Definition: include/ignition/common/Util.hh:100
void ltrim(std::string &_s)
In place left trim.