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 <sdf/sdf_config.h>
29 #include "sdf/system_util.hh"
30 #include "sdf/Error.hh"
31 
32 #if defined(__GNUC__) || defined(__clang__)
33 #define SDF_DEPRECATED(version) __attribute__((deprecated))
34 #define SDF_FORCEINLINE __attribute__((always_inline))
35 #elif defined(_MSC_VER)
36 #define SDF_DEPRECATED(version)
37 #define SDF_FORCEINLINE __forceinline
38 #else
39 #define SDF_DEPRECATED(version)
40 #define SDF_FORCEINLINE
41 #endif
42 
43 #if defined(__GNUC__)
44 # define SDF_SUPPRESS_DEPRECATED_BEGIN \
45  _Pragma("GCC diagnostic push") \
46  _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"")
47 # define SDF_SUPPRESS_DEPRECATED_END _Pragma("GCC diagnostic pop")
48 #elif defined(__clang__)
49 # define SDF_SUPPRESS_DEPRECATED_BEGIN \
50  _Pragma("clang diagnostic push") \
51  _Pragma("clang diagnostic ignored \"-Wdeprecated-declarations\"")
52 # define SDF_SUPPRESS_DEPRECATED_END _Pragma("clang diagnostic pop")
53 #else
54 # define SDF_SUPPRESS_DEPRECATED_BEGIN
55 # define SDF_SUPPRESS_DEPRECATED_END
56 #endif
57 
58 namespace sdf
59 {
60  // Inline bracket to help doxygen filtering.
61  inline namespace SDF_VERSION_NAMESPACE {
62  //
63 
64  const std::string kSdfScopeDelimiter = "::";
65 
71  std::vector<std::string> split(const std::string &_str,
72  const std::string &_splitter);
73 
78  std::string trim(const char *_in);
79 
84  std::string trim(const std::string &_in);
85 
90  template<typename T>
91  inline bool equal(const T &_a, const T &_b,
92  const T &_epsilon = 1e-6f)
93  {
94  return std::fabs(_a - _b) <= _epsilon;
95  }
96 
98  using Errors = std::vector<Error>;
99 
104  SDFORMAT_VISIBLE std::ostream &operator<<(
105  std::ostream &_out, const sdf::Errors &_errs);
106 
109  {
116  public: Color(float _r = 0.0f, float _g = 0.0f,
117  float _b = 0.0f, float _a = 1.0f) SDF_DEPRECATED(6.0)
118  : r(_r), g(_g), b(_b), a(_a)
119  {}
120 
125  public: friend std::ostream &operator<< (std::ostream &_out,
126  const Color &_pt)
127  {
128  _out << _pt.r << " " << _pt.g << " " << _pt.b << " " << _pt.a;
129  return _out;
130  }
131 
135  public: friend std::istream &operator>> (std::istream &_in, Color &_pt)
136  {
137  // Skip white spaces
138  _in.setf(std::ios_base::skipws);
139  _in >> _pt.r >> _pt.g >> _pt.b >> _pt.a;
140  return _in;
141  }
142 
146  public: bool operator ==(const Color &_clr) const
147  {
148  return equal(this->r, _clr.r) &&
149  equal(this->g, _clr.g) &&
150  equal(this->b, _clr.b) &&
151  equal(this->a, _clr.a);
152  }
153 
155  public: float r;
156 
158  public: float g;
159 
161  public: float b;
162 
164  public: float a;
165  };
166 
170  {
172  public: Time()
173  : sec(0), nsec(0)
174  {
175  }
176 
180  public: Time(int32_t _sec, int32_t _nsec)
181  : sec(_sec), nsec(_nsec)
182  {
183  }
184 
189  public: friend std::ostream &operator<<(std::ostream &_out,
190  const Time &_time)
191  {
192  _out << _time.sec << " " << _time.nsec;
193  return _out;
194  }
195 
200  public: friend std::istream &operator>>(std::istream &_in,
201  Time &_time)
202  {
203  // Skip white spaces
204  _in.setf(std::ios_base::skipws);
205  _in >> _time.sec >> _time.nsec;
206  return _in;
207  }
208 
212  public: bool operator ==(const Time &_time) const
213  {
214  return this->sec == _time.sec && this->nsec == _time.nsec;
215  }
216 
218  public: int32_t sec;
219 
221  public: int32_t nsec;
222  };
223 
226  {
227  public: double mass;
228  };
229 
233  std::string SDFORMAT_VISIBLE lowercase(const std::string &_in);
234 
240  std::pair<std::string, std::string> SplitName(
241  const std::string &_absoluteName);
242 
249  std::string JoinName(
250  const std::string &_scopeName, const std::string &_localName);
251  }
252 }
253 #endif
const std::string kSdfScopeDelimiter
Definition: Types.hh:64
SDFORMAT_VISIBLE std::string trim(const std::string &_in)
Trim leading and trailing whitespace from a string.
friend std::ostream & operator<<(std::ostream &_out, const Time &_time)
Stream insertion operator.
Definition: Types.hh:189
std::string SDFORMAT_VISIBLE lowercase(const std::string &_in)
Transforms a string to its lowercase equivalent.
friend std::istream & operator>>(std::istream &_in, Time &_time)
Stream extraction operator.
Definition: Types.hh:200
Defines a color.
Definition: Types.hh:108
int32_t nsec
Nanoseconds.
Definition: Types.hh:221
Time(int32_t _sec, int32_t _nsec)
Constructor.
Definition: Types.hh:180
double mass
Definition: Types.hh:227
float g
Green value.
Definition: Types.hh:158
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:91
SDFORMAT_VISIBLE std::vector< std::string > split(const std::string &_str, const std::string &_splitter)
Split a string using the delimiter in splitter.
A class for inertial information about a link.
Definition: Types.hh:225
std::vector< Error > Errors
A vector of Error.
Definition: Types.hh:98
SDFORMAT_VISIBLE std::ostream & operator<<(std::ostream &_out, const sdf::Errors &_errs)
Output operator for a collection of errors.
float r
Red value.
Definition: Types.hh:155
Time()
Constructor.
Definition: Types.hh:172
float b
Blue value.
Definition: Types.hh:161
SDFORMAT_VISIBLE std::pair< std::string, std::string > SplitName(const std::string &_absoluteName)
Split a name into a two strings based on the &#39;::&#39; delimeter.
Color(float _r=0.0f, float _g=0.0f, float _b=0.0f, float _a=1.0f)
Constructor.
Definition: Types.hh:116
#define SDFORMAT_VISIBLE
Use to represent "symbol visible" if supported.
Definition: system_util.hh:41
float a
Alpha value.
Definition: Types.hh:164
A Time class, can be used to hold wall- or sim-time.
Definition: Types.hh:169
SDFORMAT_VISIBLE std::string JoinName(const std::string &_scopeName, const std::string &_localName)
Join two strings with the &#39;::&#39; delimiter.
int32_t sec
Seconds.
Definition: Types.hh:218
namespace for Simulation Description Format parser
Definition: Actor.hh:33
#define SDF_DEPRECATED(version)
Definition: Types.hh:39