SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ToString.h
Go to the documentation of this file.
1 /****************************************************************************/
9 // -------------------
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo-sim.org/
12 // Copyright (C) 2001-2013 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 /****************************************************************************/
22 #ifndef ToString_h
23 #define ToString_h
24 
25 
26 // ===========================================================================
27 // included modules
28 // ===========================================================================
29 #ifdef _MSC_VER
30 #include <windows_config.h>
31 #else
32 #include <config.h>
33 #endif
34 
35 #include <sstream>
36 #include <string>
37 #include <iomanip>
40 #include "StdDefs.h"
41 
42 
43 // ===========================================================================
44 // class definitions
45 // ===========================================================================
50 template <class T>
51 inline std::string toString(const T& t, std::streamsize accuracy = OUTPUT_ACCURACY) {
52  std::ostringstream oss;
53  oss.setf(std::ios::fixed , std::ios::floatfield);
54  oss << std::setprecision(accuracy);
55  oss << t;
56  return oss.str();
57 }
58 
59 
60 template <>
61 inline std::string toString<SumoXMLTag>(const SumoXMLTag& tag, std::streamsize accuracy) {
62  UNUSED_PARAMETER(accuracy);
64 }
65 
66 
67 template <>
68 inline std::string toString<SumoXMLAttr>(const SumoXMLAttr& attr, std::streamsize accuracy) {
69  UNUSED_PARAMETER(accuracy);
71 }
72 
73 
74 template <>
75 inline std::string toString<SumoXMLNodeType>(const SumoXMLNodeType& nodeType, std::streamsize accuracy) {
76  UNUSED_PARAMETER(accuracy);
78 }
79 
80 
81 template <>
82 inline std::string toString<SumoXMLEdgeFunc>(const SumoXMLEdgeFunc& edgeFunc, std::streamsize accuracy) {
83  UNUSED_PARAMETER(accuracy);
85 }
86 
87 
88 template <>
89 inline std::string toString<SUMOVehicleClass>(const SUMOVehicleClass& vClass, std::streamsize accuracy) {
90  UNUSED_PARAMETER(accuracy);
91  return SumoVehicleClassStrings.getString(vClass);
92 }
93 
94 
95 template <>
96 inline std::string toString<LaneSpreadFunction>(const LaneSpreadFunction& lsf, std::streamsize accuracy) {
97  UNUSED_PARAMETER(accuracy);
99 }
100 
101 
102 template <>
103 inline std::string toString<LinkState>(const LinkState& linkState, std::streamsize accuracy) {
104  UNUSED_PARAMETER(accuracy);
105  return SUMOXMLDefinitions::LinkStates.getString(linkState);
106 }
107 
108 template <>
109 inline std::string toString<LinkDirection>(const LinkDirection& linkDir, std::streamsize accuracy) {
110  UNUSED_PARAMETER(accuracy);
112 }
113 
114 template <>
115 inline std::string toString<TrafficLightType>(const TrafficLightType& type, std::streamsize accuracy) {
116  UNUSED_PARAMETER(accuracy);
118 }
119 
120 template <>
121 inline std::string toString<LaneChangeModel>(const LaneChangeModel& model, std::streamsize accuracy) {
122  UNUSED_PARAMETER(accuracy);
124 }
125 
126 
127 template <typename V>
128 inline std::string toString(const std::vector<V*>& v, std::streamsize accuracy = OUTPUT_ACCURACY) {
129  UNUSED_PARAMETER(accuracy);
130  std::ostringstream oss;
131  for (typename std::vector<V*>::const_iterator it = v.begin(); it != v.end(); ++it) {
132  if (it != v.begin()) {
133  oss << " ";
134  }
135  oss << (*it)->getID();
136  }
137  return oss.str();
138 }
139 
140 
141 template <typename T, typename T_BETWEEN>
142 inline std::string joinToString(const std::vector<T>& v, const T_BETWEEN& between, std::streamsize accuracy = OUTPUT_ACCURACY) {
143  std::ostringstream oss;
144  bool connect = false;
145  for (typename std::vector<T>::const_iterator it = v.begin(); it != v.end(); ++it) {
146  if (connect) {
147  oss << toString(between, accuracy);
148  } else {
149  connect = true;
150  }
151  oss << toString(*it, accuracy);
152  }
153  return oss.str();
154 }
155 
156 
157 template <>
158 inline std::string toString(const std::vector<int>& v, std::streamsize accuracy) {
159  return joinToString(v, " ", accuracy);
160 }
161 
162 
163 template <>
164 inline std::string toString(const std::vector<SUMOReal>& v, std::streamsize accuracy) {
165  return joinToString(v, " ", accuracy);
166 }
167 
168 
169 template <typename T, typename T_BETWEEN>
170 inline std::string joinToString(const std::set<T>& s, const T_BETWEEN& between, std::streamsize accuracy = OUTPUT_ACCURACY) {
171  std::ostringstream oss;
172  bool connect = false;
173  for (typename std::set<T>::const_iterator it = s.begin(); it != s.end(); ++it) {
174  if (connect) {
175  oss << toString(between, accuracy);
176  } else {
177  connect = true;
178  }
179  oss << toString(*it, accuracy);
180  }
181  return oss.str();
182 }
183 
184 template <>
185 inline std::string toString(const std::set<std::string>& v, std::streamsize) {
186  return joinToString(v, " ");
187 }
188 
189 template <typename KEY, typename VAL, typename T_BETWEEN, typename T_BETWEEN_KEYVAL>
190 inline std::string joinToString(const std::map<KEY, VAL>& s, const T_BETWEEN& between, const T_BETWEEN_KEYVAL& between_keyval, std::streamsize accuracy = OUTPUT_ACCURACY) {
191  std::ostringstream oss;
192  bool connect = false;
193  for (typename std::map<KEY, VAL>::const_iterator it = s.begin(); it != s.end(); ++it) {
194  if (connect) {
195  oss << toString(between, accuracy);
196  } else {
197  connect = true;
198  }
199  oss << toString(it->first, accuracy) << between_keyval << toString(it->second, accuracy);
200  }
201  return oss.str();
202 }
203 
204 template <>
205 inline std::string toString(const std::map<std::string, std::string>& v, std::streamsize) {
206  return joinToString(v, ", ", ":");
207 }
208 
209 
210 #endif
211 
212 /****************************************************************************/
213