LEMUR Packages: ompl_lemur or_lemur pr_bgl prpy_lemur
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Groups Pages
string_map.h
Go to the documentation of this file.
1 
9 namespace pr_bgl
10 {
11 
12 inline void stringify_from_x(std::string & repr, const double & val)
13 {
14  char buf[2048];
15  // invariant: min DOESNT WORK, max DOES WORK
16  // validate invariants
17  int min = 0;
18  sprintf(buf, "%.*f", min, val);
19  if (val == strtod(buf,0))
20  {
21  repr = std::string(buf);
22  return;
23  }
24  // what is it at 1?
25  sprintf(buf, "%.*f", 1, val);
26  int max = sizeof(buf)-strlen(buf);
27  sprintf(buf, "%.*f", max, val);
28  if (val != strtod(buf,0))
29  {
30  printf("stringify_from_x invariant failed!\n");
31  abort();
32  }
33  // binary search
34  for (;;)
35  {
36  int diff = max - min;
37  if (diff == 1)
38  break;
39  int test = min + diff/2;
40  sprintf(buf, "%.*f", test, val);
41  if (val == strtod(buf,0))
42  max = test;
43  else
44  min = test;
45  }
46  sprintf(buf, "%.*f", max, val);
47  repr = std::string(buf);
48  return;
49 }
50 inline void stringify_to_x(const std::string & repr, double & val)
51 {
52  val = atof(repr.c_str());
53 }
54 
55 
56 inline void stringify_from_x(std::string & repr, const int & val)
57 {
58  char buf[2048];
59  sprintf(buf, "%d", val);
60  repr = std::string(buf);
61 }
62 inline void stringify_to_x(const std::string & repr, int & val)
63 {
64  val = atoi(repr.c_str());
65 }
66 
67 
68 inline void stringify_from_x(std::string & repr, const long unsigned int & val)
69 {
70  char buf[2048];
71  sprintf(buf, "%lu", val);
72  repr = std::string(buf);
73 }
74 inline void stringify_to_x(const std::string & repr, long unsigned int & val)
75 {
76  val = atoi(repr.c_str());
77 }
78 
79 
80 inline void stringify_from_x(std::string & repr, const bool & val)
81 {
82  if (val)
83  repr = "true";
84  else
85  repr = "false";
86 }
87 inline void stringify_to_x(const std::string & repr, bool & val)
88 {
89  if (repr == "true")
90  val = true;
91  else if (repr == "true")
92  val = false;
93  else
94  throw std::runtime_error("parse error!");
95 }
96 
97 
107 template <class PropMap>
109 {
110 public:
111  typedef typename boost::property_traits<PropMap>::category category;
112  typedef typename boost::property_traits<PropMap>::key_type key_type;
113  typedef std::string value_type;
114  typedef std::string reference;
115  PropMap prop_map;
116  string_map(PropMap prop_map) : prop_map(prop_map) {}
117 };
118 
119 template <class PropMap>
120 string_map<PropMap> make_string_map(PropMap prop_map)
121 {
122  return string_map<PropMap>(prop_map);
123 }
124 
125 template <class PropMap>
126 inline std::string
127 get(const string_map<PropMap> & map, const typename string_map<PropMap>::key_type & k)
128 {
129  std::string repr;
130  stringify_from_x(repr, get(map.prop_map,k));
131  return repr;
132 }
133 
134 template <class PropMap>
135 inline void
136 put(const string_map<PropMap> & map, const typename string_map<PropMap>::key_type & k, const std::string repr)
137 {
138  typename boost::property_traits<PropMap>::value_type val;
139  stringify_to_x(repr, val);
140  put(map.prop_map, k, val);
141 }
142 
143 } // namespace pr_bgl
Wraps a property as a string map.
Definition: string_map.h:108