Types.hh
Go to the documentation of this file.
1 /*
2  * Copyright 2011 Nate Koenig
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 SDFORMAT_TYPES_HH_
18 #define SDFORMAT_TYPES_HH_
19 
20 #include <algorithm>
21 #include <cmath>
22 #include <cstdint>
23 #include <sstream>
24 #include <string>
25 #include <utility>
26 #include <vector>
27 
28 #include <gz/utils/NeverDestroyed.hh>
29 #include <sdf/sdf_config.h>
30 #include "sdf/system_util.hh"
31 #include "sdf/Error.hh"
32 
33 namespace sdf
34 {
35  // Inline bracket to help doxygen filtering.
36  inline namespace SDF_VERSION_NAMESPACE {
37  //
38 
39  namespace internal
40  {
45  SDFORMAT_VISIBLE const std::string &SdfScopeDelimiter();
46  } // namespace internal
47 
48  constexpr std::string_view kScopeDelimiter{"::"};
49 
50  // Deprecated because it violates the Google Style Guide as it is not
51  // trivially destructible. Please use sdf::kScopeDelimiter instead.
52  GZ_DEPRECATED(14)
53  inline const std::string &kSdfScopeDelimiter = internal::SdfScopeDelimiter();
54 
57  constexpr char kSdfStringSource[] = "<data-string>";
58 
61  constexpr char kUrdfStringSource[] = "<urdf-string>";
62 
68  std::vector<std::string> split(const std::string &_str,
69  const std::string &_splitter);
70 
75  std::string trim(const char *_in);
76 
81  std::string trim(const std::string &_in);
82 
87  template<typename T>
88  inline bool equal(const T &_a, const T &_b,
89  const T &_epsilon = 1e-6f)
90  {
91  return std::fabs(_a - _b) <= _epsilon;
92  }
93 
95  using Errors = std::vector<Error>;
96 
102  std::ostream &_out, const sdf::Errors &_errs);
103 
107  {
109  public: Time()
110  : sec(0), nsec(0)
111  {
112  }
113 
117  public: Time(int32_t _sec, int32_t _nsec)
118  : sec(_sec), nsec(_nsec)
119  {
120  }
121 
126  public: friend std::ostream &operator<<(std::ostream &_out,
127  const Time &_time)
128  {
129  _out << _time.sec << " " << _time.nsec;
130  return _out;
131  }
132 
137  public: friend std::istream &operator>>(std::istream &_in,
138  Time &_time)
139  {
140  // Skip white spaces
141  _in.setf(std::ios_base::skipws);
142  _in >> _time.sec >> _time.nsec;
143  return _in;
144  }
145 
149  public: bool operator ==(const Time &_time) const
150  {
151  return this->sec == _time.sec && this->nsec == _time.nsec;
152  }
153 
155  public: int32_t sec;
156 
158  public: int32_t nsec;
159  };
160 
162  class SDFORMAT_VISIBLE GZ_DEPRECATED(13) Inertia
163  {
164  public: double mass;
165  };
166 
170  std::string SDFORMAT_VISIBLE lowercase(const std::string &_in);
171 
177  std::pair<std::string, std::string> SplitName(
178  const std::string &_absoluteName);
179 
186  std::string JoinName(
187  const std::string &_scopeName, const std::string &_localName);
188  }
189 }
190 #endif
A Time class, can be used to hold wall- or sim-time.
Definition: Types.hh:107
int32_t sec
Seconds.
Definition: Types.hh:155
friend std::istream & operator>>(std::istream &_in, Time &_time)
Stream extraction operator.
Definition: Types.hh:137
friend std::ostream & operator<<(std::ostream &_out, const Time &_time)
Stream insertion operator.
Definition: Types.hh:126
Time(int32_t _sec, int32_t _nsec)
Constructor.
Definition: Types.hh:117
int32_t nsec
Nanoseconds.
Definition: Types.hh:158
Time()
Constructor.
Definition: Types.hh:109
GZ_SDFORMAT_VISIBLE const std::string & SdfScopeDelimiter()
Initializes the scope delimiter as a function-local static variable so it can be used to initialize k...
bool equal(const T &_a, const T &_b, const T &_epsilon=1e-6f)
check if two values are equal, within a tolerance
Definition: Types.hh:88
constexpr char kUrdfStringSource[]
The source path replacement if the urdf was parsed from a string, instead of a file.
Definition: Types.hh:61
class GZ_SDFORMAT_VISIBLE GZ_DEPRECATED(13) Inertia
A class for inertial information about a link.
Definition: Types.hh:162
constexpr GZ_DEPRECATED(14) inline const std char kSdfStringSource[]
The source path replacement if it was parsed from a string, instead of a file.
Definition: Types.hh:57
GZ_SDFORMAT_VISIBLE std::string trim(const char *_in)
Trim leading and trailing whitespace from a string.
std::ostream & operator<<(std::ostream &os, ParamStreamer< T > s)
Definition: Param.hh:94
GZ_SDFORMAT_VISIBLE std::pair< std::string, std::string > SplitName(const std::string &_absoluteName)
Split a name into a two strings based on the '::' delimeter.
GZ_SDFORMAT_VISIBLE std::vector< std::string > split(const std::string &_str, const std::string &_splitter)
Split a string using the delimiter in splitter.
std::string GZ_SDFORMAT_VISIBLE lowercase(const std::string &_in)
Transforms a string to its lowercase equivalent.
constexpr std::string_view kScopeDelimiter
Definition: Types.hh:48
std::vector< Error > Errors
A vector of Error.
Definition: Types.hh:95
GZ_SDFORMAT_VISIBLE std::string JoinName(const std::string &_scopeName, const std::string &_localName)
Join two strings with the '::' delimiter.
namespace for Simulation Description Format parser
Definition: Actor.hh:35
#define SDFORMAT_VISIBLE
Use to represent "symbol visible" if supported.
Definition: system_util.hh:25