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 
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 template <>
179 inline std::string toString(const std::set<std::string>& v, std::streamsize accuracy) {
180  return joinToString(v, " ", accuracy);
181 }
182 
183 
184 #endif
185 
186 /****************************************************************************/
187 
SumoXMLTag
Numbers representing SUMO-XML - element names.
std::string toString< LinkDirection >(const LinkDirection &linkDir, std::streamsize accuracy)
Definition: ToString.h:109
static StringBijection< SumoXMLNodeType > NodeTypes
SUMOVehicleClass
Definition of vehicle classes to differ between different lane usage and authority types...
static StringBijection< LaneSpreadFunction > LaneSpreadFunctions
const std::string & getString(const T key)
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
static StringBijection< LinkState > LinkStates
#define UNUSED_PARAMETER(x)
Definition: StdDefs.h:36
#define OUTPUT_ACCURACY
Definition: config.h:168
LinkDirection
The different directions a link between two lanes may take (or a stream between two edges)...
static StringBijection< LinkDirection > LinkDirections
StringBijection< SUMOVehicleClass > SumoVehicleClassStrings(sumoVehicleClassStringInitializer, SVC_CUSTOM2)
std::string toString< TrafficLightType >(const TrafficLightType &type, std::streamsize accuracy)
Definition: ToString.h:115
static StringBijection< TrafficLightType > TrafficLightTypes
LinkState
The right-of-way state of a link between two lanes used when constructing a NBTrafficLightLogic, in MSLink and GNEInternalLane.
std::string toString< SumoXMLTag >(const SumoXMLTag &tag, std::streamsize accuracy)
Definition: ToString.h:61
std::string toString(const T &t, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:51
std::string toString< SumoXMLEdgeFunc >(const SumoXMLEdgeFunc &edgeFunc, std::streamsize accuracy)
Definition: ToString.h:82
SumoXMLNodeType
Numbers representing special SUMO-XML-attribute values for representing node- (junction-) types used ...
static StringBijection< int > Attrs
The names of SUMO-XML attributes for use in netbuild.
std::string toString< LinkState >(const LinkState &linkState, std::streamsize accuracy)
Definition: ToString.h:103
std::string toString< SUMOVehicleClass >(const SUMOVehicleClass &vClass, std::streamsize accuracy)
Definition: ToString.h:89
SumoXMLEdgeFunc
Numbers representing special SUMO-XML-attribute values for representing edge functions used in netbui...
LaneSpreadFunction
Numbers representing special SUMO-XML-attribute values Information how the edge&#39;s lateral offset shal...
std::string toString< SumoXMLAttr >(const SumoXMLAttr &attr, std::streamsize accuracy)
Definition: ToString.h:68
std::string joinToString(const std::vector< T > &v, const T_BETWEEN &between, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:136
static StringBijection< int > Tags
The names of SUMO-XML elements for use in netbuild.
static StringBijection< SumoXMLEdgeFunc > EdgeFunctions
std::string toString< LaneSpreadFunction >(const LaneSpreadFunction &lsf, std::streamsize accuracy)
Definition: ToString.h:96
std::string toString< SumoXMLNodeType >(const SumoXMLNodeType &nodeType, std::streamsize accuracy)
Definition: ToString.h:75
TrafficLightType