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.sourceforge.net/
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 
121 template <typename V>
122 inline std::string toString(const std::vector<V*>& v, std::streamsize accuracy = OUTPUT_ACCURACY) {
123  UNUSED_PARAMETER(accuracy);
124  std::ostringstream oss;
125  for (typename std::vector<V*>::const_iterator it = v.begin(); it != v.end(); ++it) {
126  if (it != v.begin()) {
127  oss << " ";
128  }
129  oss << (*it)->getID();
130  }
131  return oss.str();
132 }
133 
134 
135 template <typename T, typename T_BETWEEN>
136 inline std::string joinToString(const std::vector<T>& v, const T_BETWEEN& between, std::streamsize accuracy = OUTPUT_ACCURACY) {
137  std::ostringstream oss;
138  bool connect = false;
139  for (typename std::vector<T>::const_iterator it = v.begin(); it != v.end(); ++it) {
140  if (connect) {
141  oss << toString(between, accuracy);
142  } else {
143  connect = true;
144  }
145  oss << toString(*it, accuracy);
146  }
147  return oss.str();
148 }
149 
150 
151 template <>
152 inline std::string toString(const std::vector<int>& v, std::streamsize accuracy) {
153  return joinToString(v, " ", accuracy);
154 }
155 
156 
157 template <>
158 inline std::string toString(const std::vector<SUMOReal>& v, std::streamsize accuracy) {
159  return joinToString(v, " ", accuracy);
160 }
161 
162 
163 template <typename T, typename T_BETWEEN>
164 inline std::string joinToString(const std::set<T>& s, const T_BETWEEN& between, std::streamsize accuracy = OUTPUT_ACCURACY) {
165  std::ostringstream oss;
166  bool connect = false;
167  for (typename std::set<T>::const_iterator it = s.begin(); it != s.end(); ++it) {
168  if (connect) {
169  oss << toString(between, accuracy);
170  } else {
171  connect = true;
172  }
173  oss << toString(*it, accuracy);
174  }
175  return oss.str();
176 }
177 
178 #endif
179 
180 /****************************************************************************/
181