6#ifndef YARP_CONFIG_NUMERIC_H
7#define YARP_CONFIG_NUMERIC_H
9#define YARP_HAS_SYS_TYPES_H
11#if defined(YARP_HAS_SYS_TYPES_H)
12# include <sys/types.h>
30#define YARP_LITTLE_ENDIAN
32#define YARP_HAS_FLOAT128_T
33#define YARP_FLOAT32_IS_IEC559 1
34#define YARP_FLOAT64_IS_IEC559 1
35#define YARP_FLOAT128_IS_IEC559 1
45#if defined(float128_t)
50#define YARP_FLT_EXP_DIG 3
51#define YARP_DBL_EXP_DIG 4
52#define YARP_LDBL_EXP_DIG 5
67#if defined(YARP_HAS_FLOAT128_T)
68# define YARP_DOUBLE_TO_STRING_MAX_LENGTH (16 + DECIMAL_DIG + YARP_LDBL_EXP_DIG)
70# define YARP_DOUBLE_TO_STRING_MAX_LENGTH (16 + DECIMAL_DIG + YARP_DBL_EXP_DIG)
79#if defined(YARP_HAS_FLOAT128_T)
91constexpr const T&
clamp(
const T& v,
const T& lo,
const T& hi )
94 return (v < lo) ? lo : (hi < v) ? hi : v;
97template<
class T,
class Compare>
98constexpr const T&
clamp(
const T& v,
const T& lo,
const T& hi, Compare comp )
101 return comp(v, lo) ? lo : comp(hi, v) ? hi : v;
111template <
typename IntegerType,
113 std::is_integral<IntegerType>::value &&
114 !std::is_same<IntegerType, bool>::value,
bool> =
true>
123template <
typename BoolType,
124 std::enable_if_t<std::is_same<BoolType, bool>::value,
bool> =
true>
127 return {x ?
"true" :
"false"};
133template <
typename FloatingType,
134 std::enable_if_t<std::is_floating_point<FloatingType>::value,
bool> =
true>
139 std::string str(buf);
146 struct lconv* lc = localeconv();
147 size_t offset = str.find(lc->decimal_point);
148 if (offset != std::string::npos) {
150 }
else if (str.find(
'e') == std::string::npos && str !=
"inf" && str !=
"-inf" && str !=
"nan") {
159template <
typename IntegerType,
161 std::is_integral<IntegerType>::value &&
162 std::is_signed<IntegerType>::value &&
163 !std::is_same<IntegerType, bool>::value,
bool> =
true>
164inline IntegerType
from_string(
const std::string& src, IntegerType defaultValue =
static_cast<IntegerType
>(0))
166 char *endptr =
nullptr;
167 const char* startptr = src.c_str();
168 static constexpr int base = 10;
171 auto ret = std::strtoll(startptr, &endptr, base);
178 if (endptr == startptr) {
183 if (endptr != startptr + src.size()) {
188 if (
ret < std::numeric_limits<IntegerType>::min() ||
ret > std::numeric_limits<IntegerType>::max()) {
193 return static_cast<IntegerType
>(
ret);
199template <
typename IntegerType,
201 std::is_integral<IntegerType>::value &&
202 !std::is_signed<IntegerType>::value &&
203 !std::is_same<IntegerType, bool>::value,
bool> =
true>
204inline IntegerType
from_string(
const std::string& src, IntegerType defaultValue =
static_cast<IntegerType
>(0))
206 char *endptr =
nullptr;
207 const char* startptr = src.c_str();
208 static constexpr int base = 10;
211 auto ret = std::strtoull(startptr, &endptr, base);
218 if (endptr == startptr) {
223 if (endptr != startptr + src.size()) {
228 if (
ret > std::numeric_limits<IntegerType>::max()) {
233 if (
ret > std::numeric_limits<long long>::max() && std::strtoll(startptr, &endptr, base) < 0) {
238 return static_cast<IntegerType
>(
ret);
245template <
typename BoolType,
246 std::enable_if_t<std::is_same<BoolType, bool>::value,
bool> =
true>
247inline BoolType
from_string(
const std::string& src, BoolType defaultValue =
false)
249 if (src ==
"1") {
return true; }
250 if (src ==
"true") {
return true; }
251 if (src ==
"True") {
return true; }
252 if (src ==
"TRUE") {
return true; }
253 if (src ==
"yes") {
return true; }
254 if (src ==
"Yes") {
return true; }
255 if (src ==
"YES") {
return true; }
256 if (src ==
"on") {
return true; }
257 if (src ==
"On") {
return true; }
258 if (src ==
"ON") {
return true; }
260 if (src ==
"0") {
return false; }
261 if (src ==
"false") {
return false; }
262 if (src ==
"False") {
return false; }
263 if (src ==
"FALSE") {
return false; }
264 if (src ==
"no") {
return false; }
265 if (src ==
"No") {
return false; }
266 if (src ==
"NO") {
return false; }
267 if (src ==
"off") {
return false; }
268 if (src ==
"Off") {
return false; }
269 if (src ==
"OFF") {
return false; }
277template <
typename FloatingType,
278 std::enable_if_t<std::is_floating_point<FloatingType>::value,
bool> =
true>
279inline FloatingType
from_string(
const std::string& src, FloatingType defaultValue =
static_cast<FloatingType
>(0.0))
282 return std::numeric_limits<FloatingType>::infinity();
285 return -std::numeric_limits<FloatingType>::infinity();
288 return std::numeric_limits<FloatingType>::quiet_NaN();
292 std::string src_c = src;
293 size_t offset = src.find(
'.');
294 if (offset != std::string::npos) {
295 struct lconv* lc = localeconv();
296 src_c[offset] = lc->decimal_point[0];
299 char *endptr =
nullptr;
300 const char* startptr = src_c.c_str();
301 auto ret =
static_cast<FloatingType
>(strtod(startptr, &endptr));
303 if (endptr == startptr) {
308 if (endptr != startptr + src.size()) {
316template <
typename IntegerType,
318 std::is_integral<IntegerType>::value &&
319 sizeof(IntegerType) != 1 &&
320 !std::is_same<IntegerType, bool>::value,
bool> =
true>
323 std::stringstream stream;
324 stream << std::hex << i;
329template <
typename IntegerType,
330 std::enable_if_t<
sizeof(IntegerType) == 1,
bool> =
true>
335 return to_hex_string<int>(
static_cast<int>(
static_cast<uint8_t
>(i)));
345#ifndef YARP_NO_DEPRECATED
354#define YARP_INT32_FMT PRId32
355#define YARP_INT64_FMT PRId64
std::string to_hex_string(IntegerType i)
IntegerType from_string(const std::string &src, IntegerType defaultValue=static_cast< IntegerType >(0))
std::string to_string(FloatingType x)
std::string to_string(IntegerType x)
constexpr const T & clamp(const T &v, const T &lo, const T &hi)
The main, catch-all namespace for YARP.
yarp::conf::float32_t YARP_FLOAT32
#define YARP_DOUBLE_TO_STRING_MAX_LENGTH
yarp::conf::ssize_t YARP_SSIZE_T
yarp::conf::float64_t YARP_FLOAT64
#define YARP_DEPRECATED_TYPEDEF_MSG(x)