Line data Source code
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 : /****************************************************************************/
14 : /// @file StorageHelper.h
15 : /// @author Michael Behrisch
16 : /// @date 2020-12-17
17 : ///
18 : // Functions for reading, writing and converting TraCIResults to tcpip::Storage
19 : /****************************************************************************/
20 : #pragma once
21 : #include <config.h>
22 : #include <foreign/tcpip/storage.h>
23 : #include <libsumo/TraCIDefs.h>
24 :
25 :
26 : // ===========================================================================
27 : // class definitions
28 : // ===========================================================================
29 : namespace libsumo {
30 :
31 : class StorageHelper {
32 : public:
33 72 : static std::shared_ptr<tcpip::Storage> toStorage(const TraCIResult& v) {
34 : std::shared_ptr<tcpip::Storage> result = std::make_shared<tcpip::Storage>();
35 72 : result->writeUnsignedByte(v.getType());
36 72 : switch (v.getType()) {
37 : case TYPE_STRING:
38 21 : result->writeString(v.getString());
39 21 : break;
40 : case TYPE_DOUBLE:
41 51 : result->writeDouble(((const TraCIDouble&)v).value);
42 : break;
43 : default:
44 : // Error!
45 : break;
46 : }
47 72 : return result;
48 : }
49 :
50 3286 : static int readTypedInt(tcpip::Storage& ret, const std::string& error = "") {
51 3286 : if (ret.readUnsignedByte() != libsumo::TYPE_INTEGER && error != "") {
52 0 : throw TraCIException(error);
53 : }
54 3286 : return ret.readInt();
55 : }
56 :
57 458 : static int readTypedByte(tcpip::Storage& ret, const std::string& error = "") {
58 458 : if (ret.readUnsignedByte() != libsumo::TYPE_BYTE && error != "") {
59 0 : throw TraCIException(error);
60 : }
61 458 : return ret.readByte();
62 : }
63 :
64 17922 : static double readTypedDouble(tcpip::Storage& ret, const std::string& error = "") {
65 17922 : if (ret.readUnsignedByte() != libsumo::TYPE_DOUBLE && error != "") {
66 4 : throw TraCIException(error);
67 : }
68 17920 : return ret.readDouble();
69 : }
70 :
71 16749 : static std::string readTypedString(tcpip::Storage& ret, const std::string& error = "") {
72 16749 : if (ret.readUnsignedByte() != libsumo::TYPE_STRING && error != "") {
73 0 : throw TraCIException(error);
74 : }
75 16749 : return ret.readString();
76 : }
77 :
78 1497 : static std::vector<std::string> readTypedStringList(tcpip::Storage& ret, const std::string& error = "") {
79 1497 : if (ret.readUnsignedByte() != libsumo::TYPE_STRINGLIST && error != "") {
80 4 : throw TraCIException(error);
81 : }
82 1495 : return ret.readStringList();
83 : }
84 :
85 2282 : static int readCompound(tcpip::Storage& ret, int expectedSize = -1, const std::string& error = "") {
86 2282 : const int type = ret.readUnsignedByte();
87 2282 : const int size = ret.readInt();
88 2282 : if (error != "") {
89 205 : if (type != libsumo::TYPE_COMPOUND || (expectedSize != -1 && size != expectedSize)) {
90 0 : throw TraCIException(error);
91 : }
92 : }
93 2282 : return size;
94 : }
95 :
96 96 : static bool readBool(tcpip::Storage& ret, const std::string& error = "") {
97 96 : if (ret.readUnsignedByte() != libsumo::TYPE_UBYTE && error != "") {
98 0 : throw TraCIException(error);
99 : }
100 96 : return ret.readUnsignedByte() != 0;
101 : }
102 :
103 22 : static void readStage(tcpip::Storage& inputStorage, libsumo::TraCIStage& stage, const std::string& error = "") {
104 22 : stage.type = readTypedInt(inputStorage, error);
105 22 : stage.vType = readTypedString(inputStorage, error);
106 22 : stage.line = readTypedString(inputStorage, error);
107 22 : stage.destStop = readTypedString(inputStorage, error);
108 22 : stage.edges = readTypedStringList(inputStorage, error);
109 22 : stage.travelTime = readTypedDouble(inputStorage, error);
110 22 : stage.cost = readTypedDouble(inputStorage, error);
111 22 : stage.length = readTypedDouble(inputStorage, error);
112 22 : stage.intended = readTypedString(inputStorage, error);
113 22 : stage.depart = readTypedDouble(inputStorage, error);
114 22 : stage.departPos = readTypedDouble(inputStorage, error);
115 22 : stage.arrivalPos = readTypedDouble(inputStorage, error);
116 22 : stage.description = readTypedString(inputStorage, error);
117 22 : }
118 :
119 :
120 : static void writeTypedByte(tcpip::Storage& content, int value) {
121 5039 : content.writeUnsignedByte(libsumo::TYPE_BYTE);
122 5039 : content.writeByte(value);
123 3707 : }
124 :
125 : static void writeTypedInt(tcpip::Storage& content, int value) {
126 49562 : content.writeUnsignedByte(libsumo::TYPE_INTEGER);
127 49562 : content.writeInt(value);
128 48230 : }
129 :
130 : static void writeTypedDouble(tcpip::Storage& content, double value) {
131 68019 : content.writeUnsignedByte(libsumo::TYPE_DOUBLE);
132 68061 : content.writeDouble(value);
133 30321 : }
134 :
135 : static void writeTypedString(tcpip::Storage& content, const std::string& value) {
136 136482 : content.writeUnsignedByte(libsumo::TYPE_STRING);
137 136482 : content.writeString(value);
138 102368 : }
139 :
140 : static void writeTypedStringList(tcpip::Storage& content, const std::vector<std::string>& value) {
141 6981 : content.writeUnsignedByte(libsumo::TYPE_STRINGLIST);
142 6988 : content.writeStringList(value);
143 698 : }
144 :
145 : static void writeCompound(tcpip::Storage& content, int size) {
146 45482 : content.writeUnsignedByte(libsumo::TYPE_COMPOUND);
147 45482 : content.writeInt(size);
148 39192 : }
149 :
150 38 : static void writePolygon(tcpip::Storage& content, const libsumo::TraCIPositionVector& shape) {
151 38 : content.writeUnsignedByte(libsumo::TYPE_POLYGON);
152 38 : if (shape.value.size() <= 255) {
153 37 : content.writeUnsignedByte((int)shape.value.size());
154 : } else {
155 1 : content.writeUnsignedByte(0);
156 1 : content.writeInt((int)shape.value.size());
157 : }
158 465 : for (const libsumo::TraCIPosition& pos : shape.value) {
159 427 : content.writeDouble(pos.x);
160 427 : content.writeDouble(pos.y);
161 : }
162 38 : }
163 :
164 6290 : static void writeStage(tcpip::Storage& outputStorage, const libsumo::TraCIStage& stage) {
165 : writeCompound(outputStorage, 13);
166 6290 : outputStorage.writeUnsignedByte(libsumo::TYPE_INTEGER);
167 6290 : outputStorage.writeInt(stage.type);
168 6290 : writeTypedString(outputStorage, stage.vType);
169 6290 : writeTypedString(outputStorage, stage.line);
170 6290 : writeTypedString(outputStorage, stage.destStop);
171 6290 : writeTypedStringList(outputStorage, stage.edges);
172 6290 : writeTypedDouble(outputStorage, stage.travelTime);
173 6290 : writeTypedDouble(outputStorage, stage.cost);
174 6290 : writeTypedDouble(outputStorage, stage.length);
175 6290 : writeTypedString(outputStorage, stage.intended);
176 6290 : writeTypedDouble(outputStorage, stage.depart);
177 6290 : writeTypedDouble(outputStorage, stage.departPos);
178 6290 : writeTypedDouble(outputStorage, stage.arrivalPos);
179 6290 : writeTypedString(outputStorage, stage.description);
180 6290 : }
181 :
182 : };
183 :
184 :
185 : }
186 :
187 : typedef libsumo::StorageHelper StoHelp;
|