Ignition Common

API Reference

3.12.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 
192  constexpr uint64_t IGNITION_COMMON_VISIBLE hash64(std::string_view _key)
193  {
194  const char *data = _key.data();
195  const auto len = _key.size();
196  const uint64_t prime = 0x100000001b3;
197  uint64_t hash = 0xcbf29ce484222325;
198 
199  for (auto i = 0u; i < len; ++i)
200  {
201  uint8_t value = static_cast<uint8_t>(data[i]);
202  hash = hash ^ value;
203  hash *= prime;
204  }
205 
206  return hash;
207  }
208 
216  bool IGNITION_COMMON_VISIBLE env(
217  const std::string &_name, std::string &_value);
218 
225  bool IGNITION_COMMON_VISIBLE env(
226  const std::string &_name, std::string &_value,
227  bool _allowEmpty);
228 
237  bool IGNITION_COMMON_VISIBLE setenv(
238  const std::string &_name, const std::string &_value);
239 
243  bool IGNITION_COMMON_VISIBLE unsetenv(const std::string &_name);
244 
247  std::string IGNITION_COMMON_VISIBLE uuid();
248 
253  std::vector<std::string> IGNITION_COMMON_VISIBLE split(
254  const std::string &_str, const std::string &_delim);
255 
258  void IGNITION_COMMON_VISIBLE ltrim(std::string &_s);
259 
262  void IGNITION_COMMON_VISIBLE rtrim(std::string &_s);
263 
266  void IGNITION_COMMON_VISIBLE trim(std::string &_s);
267 
271  std::string IGNITION_COMMON_VISIBLE ltrimmed(std::string _s);
272 
276  std::string IGNITION_COMMON_VISIBLE rtrimmed(std::string _s);
277 
281  std::string IGNITION_COMMON_VISIBLE trimmed(std::string _s);
282 
286  std::string IGNITION_COMMON_VISIBLE lowercase(const std::string &_in);
287 
291  std::string IGNITION_COMMON_VISIBLE lowercase(const char *_in);
292 
301  void IGNITION_COMMON_VISIBLE replaceAll(std::string &_result,
302  const std::string &_orig,
303  const std::string &_key,
304  const std::string &_replacement);
305 
314  std::string IGNITION_COMMON_VISIBLE replaceAll(const std::string &_orig,
315  const std::string &_key,
316  const std::string &_replacement);
317  }
318 }
319 
321 // Implementation of get_sha1
322 template<typename T>
324 {
325  if (_buffer.size() == 0)
326  return ignition::common::sha1(NULL, 0);
327  else
328  {
329  return ignition::common::sha1(
330  &(_buffer[0]), _buffer.size() * sizeof(_buffer[0]));
331  }
332 }
333 #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:323
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:192
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.