Gazebo Common

API Reference

5.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 GZ_COMMON_UTIL_HH_
18 #define GZ_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/Export.hh>
29 #include <gz/common/Filesystem.hh>
30 #include <gz/common/SystemPaths.hh>
31 #include <gz/common/URI.hh>
32 
34 // Defines
35 
36 #ifdef _WIN32
37 # define GZ_HOMEDIR "USERPROFILE"
38 #else
39 # define GZ_HOMEDIR "HOME"
40 #endif
41 
43 #define GZ_NANO_TO_SEC 1e-9
44 
46 #define GZ_SEC_TO_NANO 1000000000
47 
49 #define GZ_MS_TO_NANO 1000000
50 
52 #define GZ_US_TO_NANO 1000
53 
55 #define GZ_SPEED_OF_LIGHT = 299792458.0
56 
58 #define GZ_SLEEP_S(_s) (std::this_thread::sleep_for(\
59  std::chrono::seconds(_s)))
60 
62 #define GZ_SLEEP_US(_us) (std::this_thread::sleep_for(\
63  std::chrono::microseconds(_us)))
64 
66 #define GZ_SLEEP_MS(_ms) (std::this_thread::sleep_for(\
67  std::chrono::milliseconds(_ms)))
68 
70 #define GZ_SLEEP_NS(_ns) (std::this_thread::sleep_for(\
71  std::chrono::nanoseconds(_ns)))
72 
74 #define GZ_SYSTEM_TIME() (std::chrono::system_clock::now())
75 
77 #define GZ_SYSTEM_TIME_S() (std::chrono::duration_cast<std::chrono::seconds>(\
78  std::chrono::system_clock::now().time_since_epoch()).count())
79 
81 #define GZ_SYSTEM_TIME_US() (\
82  std::chrono::duration_cast<std::chrono::microseconds>(\
83  std::chrono::system_clock::now().time_since_epoch()).count())
84 
86 #define GZ_SYSTEM_TIME_MS() (\
87  std::chrono::duration_cast<std::chrono::milliseconds>(\
88  std::chrono::system_clock::now().time_since_epoch()).count())
89 
91 #define GZ_SYSTEM_TIME_NS() (\
92  std::chrono::duration_cast<std::chrono::nanoseconds>(\
93  std::chrono::system_clock::now().time_since_epoch()).count())
94 
97 #define GZ_ASSERT(_expr, _msg) assert((_msg, _expr))
98 
100 namespace gz
101 {
102  namespace common
103  {
106 
109 
115  std::string GZ_COMMON_VISIBLE systemTimeISO();
116 
119  std::string GZ_COMMON_VISIBLE systemTimeIso();
120 
125  std::string GZ_COMMON_VISIBLE timeToIso(
127 
130  std::string GZ_COMMON_VISIBLE logPath();
131 
134  void GZ_COMMON_VISIBLE addSearchPathSuffix(
135  const std::string &_suffix);
136 
140  std::string GZ_COMMON_VISIBLE findFile(const std::string &_file);
141 
147  std::string GZ_COMMON_VISIBLE findFile(const std::string &_file,
148  bool _searchLocalPath);
149 
153  std::string GZ_COMMON_VISIBLE findFilePath(const std::string &_file);
154 
163  void GZ_COMMON_VISIBLE addFindFileURICallback(
164  std::function<std::string(const URI &)> _cb);
165 
172  common::SystemPaths GZ_COMMON_VISIBLE *systemPaths();
173 
178  template<typename T>
179  std::string sha1(const T &_buffer);
180 
187  std::string GZ_COMMON_VISIBLE sha1(
188  void const *_buffer, std::size_t _byteCount);
189 
194  constexpr uint64_t GZ_COMMON_VISIBLE hash64(std::string_view _key)
195  {
196  const char *data = _key.data();
197  const auto len = _key.size();
198  const uint64_t prime = 0x100000001b3;
199  uint64_t hash = 0xcbf29ce484222325;
200 
201  for (auto i = 0u; i < len; ++i)
202  {
203  uint8_t value = static_cast<uint8_t>(data[i]);
204  hash = hash ^ value;
205  hash *= prime;
206  }
207 
208  return hash;
209  }
210 
218  bool GZ_COMMON_VISIBLE env(
219  const std::string &_name, std::string &_value);
220 
227  bool GZ_COMMON_VISIBLE env(
228  const std::string &_name, std::string &_value,
229  bool _allowEmpty);
230 
239  bool GZ_COMMON_VISIBLE setenv(
240  const std::string &_name, const std::string &_value);
241 
245  bool GZ_COMMON_VISIBLE unsetenv(const std::string &_name);
246 
249  std::string GZ_COMMON_VISIBLE uuid();
250 
255  std::vector<std::string> GZ_COMMON_VISIBLE split(
256  const std::string &_str, const std::string &_delim);
257 
260  void GZ_COMMON_VISIBLE ltrim(std::string &_s);
261 
264  void GZ_COMMON_VISIBLE rtrim(std::string &_s);
265 
268  void GZ_COMMON_VISIBLE trim(std::string &_s);
269 
273  std::string GZ_COMMON_VISIBLE ltrimmed(std::string _s);
274 
278  std::string GZ_COMMON_VISIBLE rtrimmed(std::string _s);
279 
283  std::string GZ_COMMON_VISIBLE trimmed(std::string _s);
284 
288  std::string GZ_COMMON_VISIBLE lowercase(const std::string &_in);
289 
293  std::string GZ_COMMON_VISIBLE lowercase(const char *_in);
294 
303  void GZ_COMMON_VISIBLE replaceAll(std::string &_result,
304  const std::string &_orig,
305  const std::string &_key,
306  const std::string &_replacement);
307 
316  std::string GZ_COMMON_VISIBLE replaceAll(const std::string &_orig,
317  const std::string &_key,
318  const std::string &_replacement);
319  }
320 }
321 
323 // Implementation of get_sha1
324 template<typename T>
325 std::string gz::common::sha1(const T &_buffer)
326 {
327  if (_buffer.size() == 0)
328  return gz::common::sha1(NULL, 0);
329  else
330  {
331  return gz::common::sha1(
332  &(_buffer[0]), _buffer.size() * sizeof(_buffer[0]));
333  }
334 }
335 #endif