Choreonoid  1.8
FloatingNumberString.h
Go to the documentation of this file.
1 
5 #ifndef CNOID_UTIL_FLOATING_NUMBER_STRING_H
6 #define CNOID_UTIL_FLOATING_NUMBER_STRING_H
7 
8 #include <string>
9 #include <fmt/format.h>
10 
11 #ifdef _MSC_VER
12 #if !defined(INFINITY)
13 # define INFINITY (DBL_MAX+DBL_MAX)
14 #endif
15 #if !defined(NAN)
16 # define NAN (INFINITY-INFINITY)
17 #endif
18 #else
19 #include <cmath>
20 #endif
21 
22 namespace cnoid {
23 
25 {
26  double v;
27  std::string s;
28 
29 public:
31  v = 0.0;
32  s = "0";
33  }
34 
35  FloatingNumberString(const std::string& value)
36  : s(value) {
37  char* p;
38  double nv = strtod(value.c_str(), &p);
39  if(p != value.c_str()){
40  v = nv;
41  s = value;
42  }
43  }
44 
47  }
48 
49  bool set(const std::string& value){
50  char* p;
51  double nv = strtod(value.c_str(), &p);
52  if(p != value.c_str()){
53  v = nv;
54  s = value;
55  return true;
56  }
57  return false;
58  }
59 
60 
62  s = rhs.s;
63  v = rhs.v;
64  return *this;
65  }
66 
67  FloatingNumberString& operator=(const std::string& rhs){
68  set(rhs);
69  return *this;
70  }
71 
73  v = rhs;
74  s = fmt::format("{:g}", rhs);
75  return *this;
76  }
77 
78  bool setPositiveValue(const std::string& value){
79  char* p;
80  double nv = strtod(value.c_str(), &p);
81  if(p != value.c_str() && nv > 0.0){
82  v = nv;
83  s = value;
84  return true;
85  }
86  return false;
87  }
88 
89  bool setNonNegativeValue(const std::string& value){
90  char* p;
91  double nv = strtod(value.c_str(), &p);
92  if(p != value.c_str() && nv >= 0.0){
93  v = nv;
94  s = value;
95  return true;
96  }
97  return false;
98  }
99 
100  operator std::string() const {
101  return s;
102  }
103 
104  const std::string& string() const {
105  return s;
106  }
107 
108  double value() const {
109  return v;
110  }
111 };
112 
113 }
114 
115 #endif
cnoid::FloatingNumberString::string
const std::string & string() const
Definition: FloatingNumberString.h:104
cnoid::FloatingNumberString::FloatingNumberString
FloatingNumberString(const std::string &value)
Definition: FloatingNumberString.h:35
cnoid::FloatingNumberString::FloatingNumberString
FloatingNumberString()
Definition: FloatingNumberString.h:30
cnoid::FloatingNumberString::setNonNegativeValue
bool setNonNegativeValue(const std::string &value)
Definition: FloatingNumberString.h:89
cnoid::strtod
double strtod(const char *nptr, char **endptr)
Definition: strtofloat.h:19
cnoid::FloatingNumberString::value
double value() const
Definition: FloatingNumberString.h:108
cnoid::FloatingNumberString::set
bool set(const std::string &value)
Definition: FloatingNumberString.h:49
cnoid::FloatingNumberString
Definition: FloatingNumberString.h:24
cnoid::FloatingNumberString::setPositiveValue
bool setPositiveValue(const std::string &value)
Definition: FloatingNumberString.h:78
cnoid
Definition: AbstractSceneLoader.h:11
cnoid::FloatingNumberString::FloatingNumberString
FloatingNumberString(double value)
Definition: FloatingNumberString.h:45
cnoid::FloatingNumberString::operator=
FloatingNumberString & operator=(double rhs)
Definition: FloatingNumberString.h:72
cnoid::FloatingNumberString::operator=
FloatingNumberString & operator=(const FloatingNumberString &rhs)
Definition: FloatingNumberString.h:61
cnoid::FloatingNumberString::operator=
FloatingNumberString & operator=(const std::string &rhs)
Definition: FloatingNumberString.h:67