Ignition Common

API Reference

3.9.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 <chrono>
26 #include <ignition/common/Export.hh>
29 #include <ignition/common/URI.hh>
30 
32 // Defines
33 
34 #ifdef _WIN32
35 # define IGN_HOMEDIR "USERPROFILE"
36 #else
37 # define IGN_HOMEDIR "HOME"
38 #endif
39 
41 #define IGN_NANO_TO_SEC 1e-9
42 
44 #define IGN_SEC_TO_NANO 1000000000
45 
47 #define IGN_MS_TO_NANO 1000000
48 
50 #define IGN_US_TO_NANO 1000
51 
53 #define IGN_SPEED_OF_LIGHT = 299792458.0
54 
56 #define IGN_SLEEP_S(_s) (std::this_thread::sleep_for(\
57  std::chrono::seconds(_s)))
58 
60 #define IGN_SLEEP_US(_us) (std::this_thread::sleep_for(\
61  std::chrono::microseconds(_us)))
62 
64 #define IGN_SLEEP_MS(_ms) (std::this_thread::sleep_for(\
65  std::chrono::milliseconds(_ms)))
66 
68 #define IGN_SLEEP_NS(_ns) (std::this_thread::sleep_for(\
69  std::chrono::nanoseconds(_ns)))
70 
72 #define IGN_SYSTEM_TIME() (std::chrono::system_clock::now())
73 
75 #define IGN_SYSTEM_TIME_S() (std::chrono::duration_cast<std::chrono::seconds>(\
76  std::chrono::system_clock::now().time_since_epoch()).count())
77 
79 #define IGN_SYSTEM_TIME_US() (\
80  std::chrono::duration_cast<std::chrono::microseconds>(\
81  std::chrono::system_clock::now().time_since_epoch()).count())
82 
84 #define IGN_SYSTEM_TIME_MS() (\
85  std::chrono::duration_cast<std::chrono::milliseconds>(\
86  std::chrono::system_clock::now().time_since_epoch()).count())
87 
89 #define IGN_SYSTEM_TIME_NS() (\
90  std::chrono::duration_cast<std::chrono::nanoseconds>(\
91  std::chrono::system_clock::now().time_since_epoch()).count())
92 
95 #define IGN_ASSERT(_expr, _msg) assert((_msg, _expr))
96 
98 namespace ignition
99 {
100  namespace common
101  {
104 
107 
113  std::string IGNITION_COMMON_VISIBLE systemTimeISO();
114 
117  std::string IGNITION_COMMON_VISIBLE systemTimeIso();
118 
123  std::string IGNITION_COMMON_VISIBLE timeToIso(
125 
128  std::string IGNITION_COMMON_VISIBLE logPath();
129 
132  void IGNITION_COMMON_VISIBLE addSearchPathSuffix(
133  const std::string &_suffix);
134 
138  std::string IGNITION_COMMON_VISIBLE findFile(const std::string &_file);
139 
145  std::string IGNITION_COMMON_VISIBLE findFile(const std::string &_file,
146  bool _searchLocalPath);
147 
151  std::string IGNITION_COMMON_VISIBLE findFilePath(const std::string &_file);
152 
161  void IGNITION_COMMON_VISIBLE addFindFileURICallback(
162  std::function<std::string(const URI &)> _cb);
163 
170  common::SystemPaths IGNITION_COMMON_VISIBLE *systemPaths();
171 
176  template<typename T>
177  std::string sha1(const T &_buffer);
178 
185  std::string IGNITION_COMMON_VISIBLE sha1(
186  void const *_buffer, std::size_t _byteCount);
187 
188  #ifdef _MSC_VER
189  #pragma warning(disable:4307)
190  #endif
191 
196  constexpr uint64_t IGNITION_COMMON_VISIBLE hash64(std::string_view _key)
197  {
198  const char *data = _key.data();
199  const auto len = _key.size();
200  const uint64_t prime = 0x100000001b3;
201  uint64_t hash = 0xcbf29ce484222325;
202 
203  for (auto i = 0u; i < len; ++i)
204  {
205  uint8_t value = data[i];
206  hash = hash ^ value;
207  hash *= prime;
208  }
209 
210  return hash;
211  }
212 
213  #ifdef _MSC_VER
214  #pragma warning(pop)
215  #endif
216 
224  bool IGNITION_COMMON_VISIBLE env(
225  const std::string &_name, std::string &_value);
226 
233  bool IGNITION_COMMON_VISIBLE env(
234  const std::string &_name, std::string &_value,
235  bool _allowEmpty);
236 
245  bool IGNITION_COMMON_VISIBLE setenv(
246  const std::string &_name, const std::string &_value);
247 
251  bool IGNITION_COMMON_VISIBLE unsetenv(const std::string &_name);
252 
255  std::string IGNITION_COMMON_VISIBLE uuid();
256 
261  std::vector<std::string> IGNITION_COMMON_VISIBLE split(
262  const std::string &_str, const std::string &_delim);
263 
266  void IGNITION_COMMON_VISIBLE ltrim(std::string &_s);
267 
270  void IGNITION_COMMON_VISIBLE rtrim(std::string &_s);
271 
274  void IGNITION_COMMON_VISIBLE trim(std::string &_s);
275 
279  std::string IGNITION_COMMON_VISIBLE ltrimmed(std::string _s);
280 
284  std::string IGNITION_COMMON_VISIBLE rtrimmed(std::string _s);
285 
289  std::string IGNITION_COMMON_VISIBLE trimmed(std::string _s);
290 
294  std::string IGNITION_COMMON_VISIBLE lowercase(const std::string &_in);
295 
299  std::string IGNITION_COMMON_VISIBLE lowercase(const char *_in);
300 
309  void IGNITION_COMMON_VISIBLE replaceAll(std::string &_result,
310  const std::string &_orig,
311  const std::string &_key,
312  const std::string &_replacement);
313 
322  std::string IGNITION_COMMON_VISIBLE replaceAll(const std::string &_orig,
323  const std::string &_key,
324  const std::string &_replacement);
325  }
326 }
327 
329 // Implementation of get_sha1
330 template<typename T>
332 {
333  if (_buffer.size() == 0)
334  return ignition::common::sha1(NULL, 0);
335  else
336  {
337  return ignition::common::sha1(
338  &(_buffer[0]), _buffer.size() * sizeof(_buffer[0]));
339  }
340 }
341 #endif
bool setenv(const std::string &_name, const std::string &_value)
Set the environment variable &#39;_name&#39;.
std::string sha1(const T &_buffer)
Compute the SHA1 hash of an array of bytes.
Definition: include/ignition/common/Util.hh:331
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 suffix to common::SystemPaths
std::string systemTimeISO()
Please use systemTimeIso() as this function will be deprecated in Ignition Common 4...
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.
bool unsetenv(const std::string &_name)
Unset the environment variable &#39;_name&#39;.
common::SystemPaths * systemPaths()
Get a pointer to the global system paths that is used by all the findFile functions. The returned instance has global shared state for a given process. Care should be taken when manipulating global system paths Caller should not assume ownership of the pointer.
std::runtime_error exception
A runtime error.
Definition: include/ignition/common/Util.hh:106
std::string systemTimeIso()
Get the wall time as an ISO string: YYYY-MM-DDTHH:MM:SS.NS.
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.
A complete URI.
Definition: URI.hh:263
Definition: include/ignition/common/Util.hh:103
Functions to handle getting system paths, keeps track of:
Definition: SystemPaths.hh:51
Definition: include/ignition/common/Util.hh:103
constexpr uint64_t hash64(std::string_view _key)
fnv1a algorithm for 64-bit platforms.
Definition: include/ignition/common/Util.hh:196
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:103
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.
std::string timeToIso(const std::chrono::time_point< std::chrono::system_clock > &_time)
Converts a time point to an ISO string: YYYY-MM-DDTHH:MM:SS.NS.
STL class.
void addFindFileURICallback(std::function< std::string(const URI &)> _cb)
Add a callback to use when findFile() can&#39;t find a file that is a valid URI. The callback should retu...
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:103
Definition: include/ignition/common/Util.hh:103
void ltrim(std::string &_s)
In place left trim.