Eclipse SUMO - Simulation of Urban MObility
StorageHelper.h
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3// Copyright (C) 2001-2022 German Aerospace Center (DLR) and others.
4// This program and the accompanying materials are made available under the
5// terms of the Eclipse Public License 2.0 which is available at
6// https://www.eclipse.org/legal/epl-2.0/
7// This Source Code may also be made available under the following Secondary
8// Licenses when the conditions for such availability set forth in the Eclipse
9// Public License 2.0 are satisfied: GNU General Public License, version 2
10// or later which is available at
11// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13/****************************************************************************/
18// Functions for reading, writing and converting TraCIResults to tcpip::Storage
19/****************************************************************************/
20#pragma once
21#include <config.h>
23#include <libsumo/TraCIDefs.h>
24
25
26// ===========================================================================
27// class definitions
28// ===========================================================================
29namespace libsumo {
30
32public:
33 static std::shared_ptr<tcpip::Storage> toStorage(const TraCIResult& v) {
34 std::shared_ptr<tcpip::Storage> result = std::make_shared<tcpip::Storage>();
35 result->writeUnsignedByte(v.getType());
36 switch (v.getType()) {
37 case TYPE_STRING:
38 result->writeString(v.getString());
39 break;
40 case TYPE_DOUBLE:
41 result->writeDouble(((const TraCIDouble&)v).value);
42 break;
43 default:
44 // Error!
45 break;
46 }
47 return result;
48 }
49
50 static int readTypedInt(tcpip::Storage& ret, const std::string& error = "") {
51 if (ret.readUnsignedByte() != libsumo::TYPE_INTEGER && error != "") {
52 throw TraCIException(error);
53 }
54 return ret.readInt();
55 }
56
57 static int readTypedByte(tcpip::Storage& ret, const std::string& error = "") {
58 if (ret.readUnsignedByte() != libsumo::TYPE_BYTE && error != "") {
59 throw TraCIException(error);
60 }
61 return ret.readByte();
62 }
63
64 static double readTypedDouble(tcpip::Storage& ret, const std::string& error = "") {
65 if (ret.readUnsignedByte() != libsumo::TYPE_DOUBLE && error != "") {
66 throw TraCIException(error);
67 }
68 return ret.readDouble();
69 }
70
71 static std::string readTypedString(tcpip::Storage& ret, const std::string& error = "") {
72 if (ret.readUnsignedByte() != libsumo::TYPE_STRING && error != "") {
73 throw TraCIException(error);
74 }
75 return ret.readString();
76 }
77
78 static std::vector<std::string> readTypedStringList(tcpip::Storage& ret, const std::string& error = "") {
79 if (ret.readUnsignedByte() != libsumo::TYPE_STRINGLIST && error != "") {
80 throw TraCIException(error);
81 }
82 return ret.readStringList();
83 }
84
85 static int readCompound(tcpip::Storage& ret, int expectedSize = -1, const std::string& error = "") {
86 const int type = ret.readUnsignedByte();
87 const int size = ret.readInt();
88 if (error != "") {
89 if (type != libsumo::TYPE_COMPOUND || (expectedSize != -1 && size != expectedSize)) {
90 throw TraCIException(error);
91 }
92 }
93 return size;
94 }
95
96 static void readStage(tcpip::Storage& inputStorage, libsumo::TraCIStage& stage, const std::string& error = "") {
97 stage.type = readTypedInt(inputStorage, error);
98 stage.vType = readTypedString(inputStorage, error);
99 stage.line = readTypedString(inputStorage, error);
100 stage.destStop = readTypedString(inputStorage, error);
101 stage.edges = readTypedStringList(inputStorage, error);
102 stage.travelTime = readTypedDouble(inputStorage, error);
103 stage.cost = readTypedDouble(inputStorage, error);
104 stage.length = readTypedDouble(inputStorage, error);
105 stage.intended = readTypedString(inputStorage, error);
106 stage.depart = readTypedDouble(inputStorage, error);
107 stage.departPos = readTypedDouble(inputStorage, error);
108 stage.arrivalPos = readTypedDouble(inputStorage, error);
109 stage.description = readTypedString(inputStorage, error);
110 }
111
112
113 static void writeTypedByte(tcpip::Storage& content, int value) {
115 content.writeByte(value);
116 }
117
118 static void writeTypedInt(tcpip::Storage& content, int value) {
120 content.writeInt(value);
121 }
122
123 static void writeTypedDouble(tcpip::Storage& content, double value) {
125 content.writeDouble(value);
126 }
127
128 static void writeTypedString(tcpip::Storage& content, const std::string& value) {
130 content.writeString(value);
131 }
132
133 static void writeTypedStringList(tcpip::Storage& content, const std::vector<std::string>& value) {
135 content.writeStringList(value);
136 }
137
138 static void writeCompound(tcpip::Storage& content, int size) {
140 content.writeInt(size);
141 }
142
143 static void writePolygon(tcpip::Storage& content, const libsumo::TraCIPositionVector& shape) {
145 if (shape.value.size() <= 255) {
146 content.writeUnsignedByte((int)shape.value.size());
147 } else {
148 content.writeUnsignedByte(0);
149 content.writeInt((int)shape.value.size());
150 }
151 for (const libsumo::TraCIPosition& pos : shape.value) {
152 content.writeDouble(pos.x);
153 content.writeDouble(pos.y);
154 }
155 }
156
157 static void writeStage(tcpip::Storage& outputStorage, const libsumo::TraCIStage& stage) {
158 writeCompound(outputStorage, 13);
160 outputStorage.writeInt(stage.type);
161 writeTypedString(outputStorage, stage.vType);
162 writeTypedString(outputStorage, stage.line);
163 writeTypedString(outputStorage, stage.destStop);
164 writeTypedStringList(outputStorage, stage.edges);
165 writeTypedDouble(outputStorage, stage.travelTime);
166 writeTypedDouble(outputStorage, stage.cost);
167 writeTypedDouble(outputStorage, stage.length);
168 writeTypedString(outputStorage, stage.intended);
169 writeTypedDouble(outputStorage, stage.depart);
170 writeTypedDouble(outputStorage, stage.departPos);
171 writeTypedDouble(outputStorage, stage.arrivalPos);
172 writeTypedString(outputStorage, stage.description);
173 }
174
175};
176
177
178}
179
libsumo::StorageHelper StoHelp
static int readTypedByte(tcpip::Storage &ret, const std::string &error="")
Definition: StorageHelper.h:57
static void writeTypedDouble(tcpip::Storage &content, double value)
static int readCompound(tcpip::Storage &ret, int expectedSize=-1, const std::string &error="")
Definition: StorageHelper.h:85
static void writePolygon(tcpip::Storage &content, const libsumo::TraCIPositionVector &shape)
static std::vector< std::string > readTypedStringList(tcpip::Storage &ret, const std::string &error="")
Definition: StorageHelper.h:78
static std::shared_ptr< tcpip::Storage > toStorage(const TraCIResult &v)
Definition: StorageHelper.h:33
static int readTypedInt(tcpip::Storage &ret, const std::string &error="")
Definition: StorageHelper.h:50
static void writeCompound(tcpip::Storage &content, int size)
static std::string readTypedString(tcpip::Storage &ret, const std::string &error="")
Definition: StorageHelper.h:71
static void writeTypedInt(tcpip::Storage &content, int value)
static void writeTypedStringList(tcpip::Storage &content, const std::vector< std::string > &value)
static void writeTypedByte(tcpip::Storage &content, int value)
static void writeStage(tcpip::Storage &outputStorage, const libsumo::TraCIStage &stage)
static void writeTypedString(tcpip::Storage &content, const std::string &value)
static void readStage(tcpip::Storage &inputStorage, libsumo::TraCIStage &stage, const std::string &error="")
Definition: StorageHelper.h:96
static double readTypedDouble(tcpip::Storage &ret, const std::string &error="")
Definition: StorageHelper.h:64
An error which allows to continue.
Definition: TraCIDefs.h:138
std::string intended
id of the intended vehicle for public transport ride
Definition: TraCIDefs.h:545
int type
The type of stage (walking, driving, ...)
Definition: TraCIDefs.h:529
std::string destStop
The id of the destination stop.
Definition: TraCIDefs.h:535
double length
length in m
Definition: TraCIDefs.h:543
double travelTime
duration of the stage in seconds
Definition: TraCIDefs.h:539
double departPos
position on the lane when starting the stage
Definition: TraCIDefs.h:549
std::string description
arbitrary description string
Definition: TraCIDefs.h:553
std::string line
The line or the id of the vehicle type.
Definition: TraCIDefs.h:533
double cost
effort needed
Definition: TraCIDefs.h:541
double depart
intended depart time for public transport ride or INVALID_DOUBLE_VALUE
Definition: TraCIDefs.h:547
std::vector< std::string > edges
The sequence of edges to travel.
Definition: TraCIDefs.h:537
double arrivalPos
position on the lane when ending the stage
Definition: TraCIDefs.h:551
std::string vType
The vehicle type when using a private car or bike.
Definition: TraCIDefs.h:531
virtual std::string readString()
Definition: storage.cpp:180
virtual void writeString(const std::string &s)
Definition: storage.cpp:197
virtual void writeInt(int)
Definition: storage.cpp:321
virtual void writeDouble(double)
Definition: storage.cpp:354
virtual int readUnsignedByte()
Definition: storage.cpp:155
virtual void writeStringList(const std::vector< std::string > &s)
Definition: storage.cpp:247
virtual void writeUnsignedByte(int)
Definition: storage.cpp:165
virtual void writeByte(int)
Definition: storage.cpp:140
virtual int readByte()
Definition: storage.cpp:128
virtual std::vector< std::string > readStringList()
Definition: storage.cpp:211
virtual double readDouble()
Definition: storage.cpp:362
virtual int readInt()
Definition: storage.cpp:311
TRACI_CONST int TYPE_COMPOUND
TRACI_CONST int TYPE_POLYGON
TRACI_CONST int TYPE_STRINGLIST
TRACI_CONST int TYPE_INTEGER
TRACI_CONST int TYPE_DOUBLE
TRACI_CONST int TYPE_BYTE
TRACI_CONST int TYPE_STRING
A 3D-position.
Definition: TraCIDefs.h:172
A list of positions.
Definition: TraCIDefs.h:215
std::vector< TraCIPosition > value
Definition: TraCIDefs.h:225
virtual std::string getString() const
Definition: TraCIDefs.h:161
virtual int getType() const
Definition: TraCIDefs.h:164