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 <vector>
26 
27 #include <sdf/sdf_config.h>
28 #include "sdf/system_util.hh"
29 #include "sdf/Error.hh"
30 
31 #if defined(__GNUC__) || defined(__clang__)
32 #define SDF_DEPRECATED(version) __attribute__((deprecated))
33 #define SDF_FORCEINLINE __attribute__((always_inline))
34 #elif defined(_MSC_VER)
35 #define SDF_DEPRECATED(version)
36 #define SDF_FORCEINLINE __forceinline
37 #else
38 #define SDF_DEPRECATED(version)
39 #define SDF_FORCEINLINE
40 #endif
41 
42 #if defined(__GNUC__)
43 # define SDF_SUPPRESS_DEPRECATED_BEGIN \
44  _Pragma("GCC diagnostic push") \
45  _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"")
46 # define SDF_SUPPRESS_DEPRECATED_END _Pragma("GCC diagnostic pop")
47 #elif defined(__clang__)
48 # define SDF_SUPPRESS_DEPRECATED_BEGIN \
49  _Pragma("clang diagnostic push") \
50  _Pragma("clang diagnostic ignored \"-Wdeprecated-declarations\"")
51 # define SDF_SUPPRESS_DEPRECATED_END _Pragma("clang diagnostic pop")
52 #else
53 # define SDF_SUPPRESS_DEPRECATED_BEGIN
54 # define SDF_SUPPRESS_DEPRECATED_END
55 #endif
56 
57 namespace sdf
58 {
59  // Inline bracket to help doxygen filtering.
60  inline namespace SDF_VERSION_NAMESPACE {
61  //
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 
99  {
106  public: Color(float _r = 0.0f, float _g = 0.0f,
107  float _b = 0.0f, float _a = 1.0f) SDF_DEPRECATED(6.0)
108  : r(_r), g(_g), b(_b), a(_a)
109  {}
110 
115  public: friend std::ostream &operator<< (std::ostream &_out,
116  const Color &_pt)
117  {
118  _out << _pt.r << " " << _pt.g << " " << _pt.b << " " << _pt.a;
119  return _out;
120  }
121 
125  public: friend std::istream &operator>> (std::istream &_in, Color &_pt)
126  {
127  // Skip white spaces
128  _in.setf(std::ios_base::skipws);
129  _in >> _pt.r >> _pt.g >> _pt.b >> _pt.a;
130  return _in;
131  }
132 
136  public: bool operator ==(const Color &_clr) const
137  {
138  return equal(this->r, _clr.r) &&
139  equal(this->g, _clr.g) &&
140  equal(this->b, _clr.b) &&
141  equal(this->a, _clr.a);
142  }
143 
145  public: float r;
146 
148  public: float g;
149 
151  public: float b;
152 
154  public: float a;
155  };
156 
160  {
162  public: Time()
163  : sec(0), nsec(0)
164  {
165  }
166 
170  public: Time(int32_t _sec, int32_t _nsec)
171  : sec(_sec), nsec(_nsec)
172  {
173  }
174 
179  public: friend std::ostream &operator<<(std::ostream &_out,
180  const Time &_time)
181  {
182  _out << _time.sec << " " << _time.nsec;
183  return _out;
184  }
185 
190  public: friend std::istream &operator>>(std::istream &_in,
191  Time &_time)
192  {
193  // Skip white spaces
194  _in.setf(std::ios_base::skipws);
195  _in >> _time.sec >> _time.nsec;
196  return _in;
197  }
198 
202  public: bool operator ==(const Time &_time) const
203  {
204  return this->sec == _time.sec && this->nsec == _time.nsec;
205  }
206 
208  public: int32_t sec;
209 
211  public: int32_t nsec;
212  };
213 
216  {
217  public: double mass;
218  };
219 
223  std::string SDFORMAT_VISIBLE lowercase(const std::string &_in);
224  }
225 }
226 #endif
int32_t nsec
Nanoseconds.
Definition: Types.hh:211
std::string SDFORMAT_VISIBLE lowercase(const std::string &_in)
Transforms a string to its lowercase equivalent.
SDFORMAT_VISIBLE std::string trim(const std::string &_in)
Trim leading and trailing whitespace from a string.
int32_t sec
Seconds.
Definition: Types.hh:208
Time(int32_t _sec, int32_t _nsec)
Constructor.
Definition: Types.hh:170
friend std::ostream & operator<<(std::ostream &_out, const Time &_time)
Stream insertion operator.
Definition: Types.hh:179
SDFORMAT_VISIBLE std::vector< std::string > split(const std::string &_str, const std::string &_splitter)
Split a string using the delimiter in splitter.
float g
Green value.
Definition: Types.hh:148
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
Color(float _r=0.0f, float _g=0.0f, float _b=0.0f, float _a=1.0f)
Constructor.
Definition: Types.hh:106
double mass
Definition: Types.hh:217
std::ostream & operator<<(std::ostream &os, ParamStreamer< T > s)
Definition: Param.hh:76
float r
Red value.
Definition: Types.hh:145
#define SDFORMAT_VISIBLE
Use to represent "symbol visible" if supported.
Definition: system_util.hh:48
A Time class, can be used to hold wall- or sim-time.
Definition: Types.hh:159
float b
Blue value.
Definition: Types.hh:151
Time()
Constructor.
Definition: Types.hh:162
friend std::istream & operator>>(std::istream &_in, Time &_time)
Stream extraction operator.
Definition: Types.hh:190
A class for inertial information about a link.
Definition: Types.hh:215
namespace for Simulation Description Format parser
Definition: Actor.hh:32
Defines a color.
Definition: Types.hh:98
std::vector< Error > Errors
A vector of Error.
Definition: Types.hh:95
#define SDF_DEPRECATED(version)
Definition: Types.hh:38
float a
Alpha value.
Definition: Types.hh:154