Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
StorageHelper.h
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2001-2024 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 bool readBool(tcpip::Storage& ret, const std::string& error = "") {
97 if (ret.readUnsignedByte() != libsumo::TYPE_UBYTE && error != "") {
98 throw TraCIException(error);
99 }
100 return ret.readUnsignedByte() != 0;
101 }
102
103 static void readStage(tcpip::Storage& inputStorage, libsumo::TraCIStage& stage, const std::string& error = "") {
104 stage.type = readTypedInt(inputStorage, error);
105 stage.vType = readTypedString(inputStorage, error);
106 stage.line = readTypedString(inputStorage, error);
107 stage.destStop = readTypedString(inputStorage, error);
108 stage.edges = readTypedStringList(inputStorage, error);
109 stage.travelTime = readTypedDouble(inputStorage, error);
110 stage.cost = readTypedDouble(inputStorage, error);
111 stage.length = readTypedDouble(inputStorage, error);
112 stage.intended = readTypedString(inputStorage, error);
113 stage.depart = readTypedDouble(inputStorage, error);
114 stage.departPos = readTypedDouble(inputStorage, error);
115 stage.arrivalPos = readTypedDouble(inputStorage, error);
116 stage.description = readTypedString(inputStorage, error);
117 }
118
119
120 static void writeTypedByte(tcpip::Storage& content, int value) {
122 content.writeByte(value);
123 }
124
125 static void writeTypedInt(tcpip::Storage& content, int value) {
127 content.writeInt(value);
128 }
129
130 static void writeTypedDouble(tcpip::Storage& content, double value) {
132 content.writeDouble(value);
133 }
134
135 static void writeTypedString(tcpip::Storage& content, const std::string& value) {
137 content.writeString(value);
138 }
139
140 static void writeTypedStringList(tcpip::Storage& content, const std::vector<std::string>& value) {
142 content.writeStringList(value);
143 }
144
145 static void writeCompound(tcpip::Storage& content, int size) {
147 content.writeInt(size);
148 }
149
150 static void writePolygon(tcpip::Storage& content, const libsumo::TraCIPositionVector& shape) {
152 if (shape.value.size() <= 255) {
153 content.writeUnsignedByte((int)shape.value.size());
154 } else {
155 content.writeUnsignedByte(0);
156 content.writeInt((int)shape.value.size());
157 }
158 for (const libsumo::TraCIPosition& pos : shape.value) {
159 content.writeDouble(pos.x);
160 content.writeDouble(pos.y);
161 }
162 }
163
164 static void writeStage(tcpip::Storage& outputStorage, const libsumo::TraCIStage& stage) {
165 writeCompound(outputStorage, 13);
167 outputStorage.writeInt(stage.type);
168 writeTypedString(outputStorage, stage.vType);
169 writeTypedString(outputStorage, stage.line);
170 writeTypedString(outputStorage, stage.destStop);
171 writeTypedStringList(outputStorage, stage.edges);
172 writeTypedDouble(outputStorage, stage.travelTime);
173 writeTypedDouble(outputStorage, stage.cost);
174 writeTypedDouble(outputStorage, stage.length);
175 writeTypedString(outputStorage, stage.intended);
176 writeTypedDouble(outputStorage, stage.depart);
177 writeTypedDouble(outputStorage, stage.departPos);
178 writeTypedDouble(outputStorage, stage.arrivalPos);
179 writeTypedString(outputStorage, stage.description);
180 }
181
182};
183
184
185}
186
libsumo::StorageHelper StoHelp
static int readTypedByte(tcpip::Storage &ret, const std::string &error="")
static void writeTypedDouble(tcpip::Storage &content, double value)
static int readCompound(tcpip::Storage &ret, int expectedSize=-1, const std::string &error="")
static bool readBool(tcpip::Storage &ret, const std::string &error="")
static void writePolygon(tcpip::Storage &content, const libsumo::TraCIPositionVector &shape)
static std::vector< std::string > readTypedStringList(tcpip::Storage &ret, const std::string &error="")
static std::shared_ptr< tcpip::Storage > toStorage(const TraCIResult &v)
static int readTypedInt(tcpip::Storage &ret, const std::string &error="")
static void writeCompound(tcpip::Storage &content, int size)
static std::string readTypedString(tcpip::Storage &ret, const std::string &error="")
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="")
static double readTypedDouble(tcpip::Storage &ret, const std::string &error="")
An error which allows to continue.
Definition TraCIDefs.h:144
std::string intended
id of the intended vehicle for public transport ride
Definition TraCIDefs.h:582
int type
The type of stage (walking, driving, ...)
Definition TraCIDefs.h:566
std::string destStop
The id of the destination stop.
Definition TraCIDefs.h:572
double length
length in m
Definition TraCIDefs.h:580
double travelTime
duration of the stage in seconds
Definition TraCIDefs.h:576
double departPos
position on the lane when starting the stage
Definition TraCIDefs.h:586
std::string description
arbitrary description string
Definition TraCIDefs.h:590
std::string line
The line or the id of the vehicle type.
Definition TraCIDefs.h:570
double cost
effort needed
Definition TraCIDefs.h:578
double depart
intended depart time for public transport ride or INVALID_DOUBLE_VALUE
Definition TraCIDefs.h:584
std::vector< std::string > edges
The sequence of edges to travel.
Definition TraCIDefs.h:574
double arrivalPos
position on the lane when ending the stage
Definition TraCIDefs.h:588
std::string vType
The vehicle type when using a private car or bike.
Definition TraCIDefs.h:568
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_UBYTE
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 2D or 3D-position, for 2D positions z == INVALID_DOUBLE_VALUE.
Definition TraCIDefs.h:178
A list of positions.
Definition TraCIDefs.h:234
std::vector< TraCIPosition > value
Definition TraCIDefs.h:244
virtual std::string getString() const
Definition TraCIDefs.h:167
virtual int getType() const
Definition TraCIDefs.h:170