Ignition Common

API Reference

3.5.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>
28 #include <ignition/common/URI.hh>
29 
31 // Defines
32 
33 #ifdef _WIN32
34 # define IGN_HOMEDIR "HOMEPATH"
35 #else
36 # define IGN_HOMEDIR "HOME"
37 #endif
38 
40 #define IGN_NANO_TO_SEC 1e-9
41 
43 #define IGN_SEC_TO_NANO 1000000000
44 
46 #define IGN_MS_TO_NANO 1000000
47 
49 #define IGN_US_TO_NANO 1000
50 
52 #define IGN_SPEED_OF_LIGHT = 299792458.0
53 
55 #define IGN_SLEEP_S(_s) (std::this_thread::sleep_for(\
56  std::chrono::seconds(_s)))
57 
59 #define IGN_SLEEP_US(_us) (std::this_thread::sleep_for(\
60  std::chrono::microseconds(_us)))
61 
63 #define IGN_SLEEP_MS(_ms) (std::this_thread::sleep_for(\
64  std::chrono::milliseconds(_ms)))
65 
67 #define IGN_SLEEP_NS(_ns) (std::this_thread::sleep_for(\
68  std::chrono::nanoseconds(_ns)))
69 
71 #define IGN_SYSTEM_TIME() (std::chrono::system_clock::now())
72 
74 #define IGN_SYSTEM_TIME_S() (std::chrono::duration_cast<std::chrono::seconds>(\
75  std::chrono::system_clock::now().time_since_epoch()).count())
76 
78 #define IGN_SYSTEM_TIME_US() (\
79  std::chrono::duration_cast<std::chrono::microseconds>(\
80  std::chrono::system_clock::now().time_since_epoch()).count())
81 
83 #define IGN_SYSTEM_TIME_MS() (\
84  std::chrono::duration_cast<std::chrono::milliseconds>(\
85  std::chrono::system_clock::now().time_since_epoch()).count())
86 
88 #define IGN_SYSTEM_TIME_NS() (\
89  std::chrono::duration_cast<std::chrono::nanoseconds>(\
90  std::chrono::system_clock::now().time_since_epoch()).count())
91 
94 #define IGN_ASSERT(_expr, _msg) assert((_msg, _expr))
95 
97 namespace ignition
98 {
99  namespace common
100  {
103 
106 
112  std::string IGNITION_COMMON_VISIBLE systemTimeISO();
113 
116  std::string IGNITION_COMMON_VISIBLE systemTimeIso();
117 
122  std::string IGNITION_COMMON_VISIBLE timeToIso(
124 
127  std::string IGNITION_COMMON_VISIBLE logPath();
128 
131  void IGNITION_COMMON_VISIBLE addSearchPathSuffix(
132  const std::string &_suffix);
133 
137  std::string IGNITION_COMMON_VISIBLE findFile(const std::string &_file);
138 
144  std::string IGNITION_COMMON_VISIBLE findFile(const std::string &_file,
145  bool _searchLocalPath);
146 
150  std::string IGNITION_COMMON_VISIBLE findFilePath(const std::string &_file);
151 
160  void IGNITION_COMMON_VISIBLE addFindFileURICallback(
161  std::function<std::string(const URI &)> _cb);
162 
167  template<typename T>
168  std::string sha1(const T &_buffer);
169 
176  std::string IGNITION_COMMON_VISIBLE sha1(
177  void const *_buffer, std::size_t _byteCount);
178 
179  #ifdef _MSC_VER
180  #pragma warning(disable:4307)
181  #endif
182 
187  constexpr uint64_t IGNITION_COMMON_VISIBLE hash64(std::string_view _key)
188  {
189  const char *data = _key.data();
190  const auto len = _key.size();
191  const uint64_t prime = 0x100000001b3;
192  uint64_t hash = 0xcbf29ce484222325;
193 
194  for (auto i = 0u; i < len; ++i)
195  {
196  uint8_t value = data[i];
197  hash = hash ^ value;
198  hash *= prime;
199  }
200 
201  return hash;
202  }
203 
204  #ifdef _MSC_VER
205  #pragma warning(pop)
206  #endif
207 
212  bool IGNITION_COMMON_VISIBLE env(
213  const std::string &_name, std::string &_value);
214 
217  std::string IGNITION_COMMON_VISIBLE uuid();
218 
223  std::vector<std::string> IGNITION_COMMON_VISIBLE split(
224  const std::string &_str, const std::string &_delim);
225 
228  void IGNITION_COMMON_VISIBLE ltrim(std::string &_s);
229 
232  void IGNITION_COMMON_VISIBLE rtrim(std::string &_s);
233 
236  void IGNITION_COMMON_VISIBLE trim(std::string &_s);
237 
241  std::string IGNITION_COMMON_VISIBLE ltrimmed(std::string _s);
242 
246  std::string IGNITION_COMMON_VISIBLE rtrimmed(std::string _s);
247 
251  std::string IGNITION_COMMON_VISIBLE trimmed(std::string _s);
252 
256  std::string IGNITION_COMMON_VISIBLE lowercase(const std::string &_in);
257 
261  std::string IGNITION_COMMON_VISIBLE lowercase(const char *_in);
262 
271  void IGNITION_COMMON_VISIBLE replaceAll(std::string &_result,
272  const std::string &_orig,
273  const std::string &_key,
274  const std::string &_replacement);
275 
284  std::string IGNITION_COMMON_VISIBLE replaceAll(const std::string &_orig,
285  const std::string &_key,
286  const std::string &_replacement);
287  }
288 }
289 
291 // Implementation of get_sha1
292 template<typename T>
294 {
295  if (_buffer.size() == 0)
296  return ignition::common::sha1(NULL, 0);
297  else
298  {
299  return ignition::common::sha1(
300  &(_buffer[0]), _buffer.size() * sizeof(_buffer[0]));
301  }
302 }
303 #endif
std::string sha1(const T &_buffer)
Compute the SHA1 hash of an array of bytes.
Definition: include/ignition/common/Util.hh:293
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.
std::runtime_error exception
A runtime error.
Definition: include/ignition/common/Util.hh:105
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:256
Definition: include/ignition/common/Util.hh:102
Definition: include/ignition/common/Util.hh:102
constexpr uint64_t hash64(std::string_view _key)
fnv1a algorithm for 64-bit platforms.
Definition: include/ignition/common/Util.hh:187
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:102
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:102
Definition: include/ignition/common/Util.hh:102
void ltrim(std::string &_s)
In place left trim.