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