Gazebo Common

API Reference

4.7.0
include/gz/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 <chrono>
22 #include <future>
23 #include <memory>
24 #include <string>
25 #include <thread>
26 #include <vector>
27 
28 #include <gz/common/config.hh>
29 #include <gz/common/Export.hh>
30 #include <gz/common/Filesystem.hh>
31 #include <gz/common/SystemPaths.hh>
32 #include <gz/common/URI.hh>
33 
35 // Defines
36 
37 #ifdef _WIN32
38 # define IGN_HOMEDIR "USERPROFILE"
39 #else
40 # define IGN_HOMEDIR "HOME"
41 #endif
42 
44 #define IGN_NANO_TO_SEC 1e-9
45 
47 #define IGN_SEC_TO_NANO 1000000000
48 
50 #define IGN_MS_TO_NANO 1000000
51 
53 #define IGN_US_TO_NANO 1000
54 
56 #define IGN_SPEED_OF_LIGHT = 299792458.0
57 
59 #define IGN_SLEEP_S(_s) (std::this_thread::sleep_for(\
60  std::chrono::seconds(_s)))
61 
63 #define IGN_SLEEP_US(_us) (std::this_thread::sleep_for(\
64  std::chrono::microseconds(_us)))
65 
67 #define IGN_SLEEP_MS(_ms) (std::this_thread::sleep_for(\
68  std::chrono::milliseconds(_ms)))
69 
71 #define IGN_SLEEP_NS(_ns) (std::this_thread::sleep_for(\
72  std::chrono::nanoseconds(_ns)))
73 
75 #define IGN_SYSTEM_TIME() (std::chrono::system_clock::now())
76 
78 #define IGN_SYSTEM_TIME_S() (std::chrono::duration_cast<std::chrono::seconds>(\
79  std::chrono::system_clock::now().time_since_epoch()).count())
80 
82 #define IGN_SYSTEM_TIME_US() (\
83  std::chrono::duration_cast<std::chrono::microseconds>(\
84  std::chrono::system_clock::now().time_since_epoch()).count())
85 
87 #define IGN_SYSTEM_TIME_MS() (\
88  std::chrono::duration_cast<std::chrono::milliseconds>(\
89  std::chrono::system_clock::now().time_since_epoch()).count())
90 
92 #define IGN_SYSTEM_TIME_NS() (\
93  std::chrono::duration_cast<std::chrono::nanoseconds>(\
94  std::chrono::system_clock::now().time_since_epoch()).count())
95 
98 #define IGN_ASSERT(_expr, _msg) assert((_msg, _expr))
99 
101 namespace ignition
102 {
103  namespace common
104  {
107 
110 
116  std::string IGNITION_COMMON_VISIBLE systemTimeISO();
117 
120  std::string IGNITION_COMMON_VISIBLE systemTimeIso();
121 
126  std::string IGNITION_COMMON_VISIBLE timeToIso(
128 
131  std::string IGNITION_COMMON_VISIBLE logPath();
132 
135  void IGNITION_COMMON_VISIBLE addSearchPathSuffix(
136  const std::string &_suffix);
137 
141  std::string IGNITION_COMMON_VISIBLE findFile(const std::string &_file);
142 
148  std::string IGNITION_COMMON_VISIBLE findFile(const std::string &_file,
149  bool _searchLocalPath);
150 
154  std::string IGNITION_COMMON_VISIBLE findFilePath(const std::string &_file);
155 
164  void IGNITION_COMMON_VISIBLE addFindFileURICallback(
165  std::function<std::string(const URI &)> _cb);
166 
173  common::SystemPaths IGNITION_COMMON_VISIBLE *systemPaths();
174 
179  template<typename T>
180  std::string sha1(const T &_buffer);
181 
188  std::string IGNITION_COMMON_VISIBLE sha1(
189  void const *_buffer, std::size_t _byteCount);
190 
195  constexpr uint64_t IGNITION_COMMON_VISIBLE hash64(std::string_view _key)
196  {
197  const char *data = _key.data();
198  const auto len = _key.size();
199  const uint64_t prime = 0x100000001b3;
200  uint64_t hash = 0xcbf29ce484222325;
201 
202  for (auto i = 0u; i < len; ++i)
203  {
204  uint8_t value = static_cast<uint8_t>(data[i]);
205  hash = hash ^ value;
206  hash *= prime;
207  }
208 
209  return hash;
210  }
211 
219  bool IGNITION_COMMON_VISIBLE env(
220  const std::string &_name, std::string &_value);
221 
228  bool IGNITION_COMMON_VISIBLE env(
229  const std::string &_name, std::string &_value,
230  bool _allowEmpty);
231 
240  bool IGNITION_COMMON_VISIBLE setenv(
241  const std::string &_name, const std::string &_value);
242 
246  bool IGNITION_COMMON_VISIBLE unsetenv(const std::string &_name);
247 
250  std::string IGNITION_COMMON_VISIBLE uuid();
251 
256  std::vector<std::string> IGNITION_COMMON_VISIBLE split(
257  const std::string &_str, const std::string &_delim);
258 
261  void IGNITION_COMMON_VISIBLE ltrim(std::string &_s);
262 
265  void IGNITION_COMMON_VISIBLE rtrim(std::string &_s);
266 
269  void IGNITION_COMMON_VISIBLE trim(std::string &_s);
270 
274  std::string IGNITION_COMMON_VISIBLE ltrimmed(std::string _s);
275 
279  std::string IGNITION_COMMON_VISIBLE rtrimmed(std::string _s);
280 
284  std::string IGNITION_COMMON_VISIBLE trimmed(std::string _s);
285 
289  std::string IGNITION_COMMON_VISIBLE lowercase(const std::string &_in);
290 
294  std::string IGNITION_COMMON_VISIBLE lowercase(const char *_in);
295 
304  void IGNITION_COMMON_VISIBLE replaceAll(std::string &_result,
305  const std::string &_orig,
306  const std::string &_key,
307  const std::string &_replacement);
308 
317  std::string IGNITION_COMMON_VISIBLE replaceAll(const std::string &_orig,
318  const std::string &_key,
319  const std::string &_replacement);
320  }
321 }
322 
324 // Implementation of get_sha1
325 template<typename T>
327 {
328  if (_buffer.size() == 0)
329  return ignition::common::sha1(NULL, 0);
330  else
331  {
332  return ignition::common::sha1(
333  &(_buffer[0]), _buffer.size() * sizeof(_buffer[0]));
334  }
335 }
336 #endif
std::string sha1(const T &_buffer)
Compute the SHA1 hash of an array of bytes.
Definition: include/gz/common/Util.hh:326
NodeTransformType
Enumeration of the transform types.
Definition: include/gz/common/Util.hh:106
Forward declarations for the common classes.
std::string findFile(const std::string &_file)
search for file in common::SystemPaths
STL class.
std::vector< std::string > split(const std::string &_str, const std::string &_delim)
Splits a string into tokens.
constexpr uint64_t hash64(std::string_view _key)
fnv1a algorithm for 64-bit platforms.
Definition: include/gz/common/Util.hh:195
std::string lowercase(const std::string &_in)
Transforms a string to its lowercase equivalent.
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.
@ TRANSLATE
Definition: include/gz/common/Util.hh:106
@ MATRIX
Definition: include/gz/common/Util.hh:106
std::string trimmed(std::string _s)
Copying trim from both ends.
void addFindFileURICallback(std::function< std::string(const URI &)> _cb)
Add a callback to use when findFile() can't find a file that is a valid URI. The callback should retu...
std::string rtrimmed(std::string _s)
Copying right trim.
@ ROTATE
Definition: include/gz/common/Util.hh:106
@ SCALE
Definition: include/gz/common/Util.hh:106
void trim(std::string &_s)
In place trim from both ends.
A complete URI which has the following components:
Definition: gz/common/URI.hh:407
bool setenv(const std::string &_name, const std::string &_value)
Set the environment variable '_name'.
Functions to handle getting system paths, keeps track of:
Definition: gz/common/SystemPaths.hh:52
void ltrim(std::string &_s)
In place left trim.
std::string findFilePath(const std::string &_file)
search for a file in common::SystemPaths
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::string uuid()
Get a UUID.
void addSearchPathSuffix(const std::string &_suffix)
add path suffix to common::SystemPaths
bool unsetenv(const std::string &_name)
Unset the environment variable '_name'.
std::string systemTimeISO()
Please use systemTimeIso() as this function will be deprecated in Ignition Common 4....
std::runtime_error exception
A runtime error.
Definition: include/gz/common/Util.hh:109
bool env(const std::string &_name, std::string &_value)
Find the environment variable '_name' and return its value.
common::SystemPaths * systemPaths()
Get a pointer to the global system paths that is used by all the findFile functions....
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.
std::string logPath()
Get the log path.