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 71 : static std::shared_ptr<tcpip::Storage> toStorage(const TraCIResult& v) {
34 : std::shared_ptr<tcpip::Storage> result = std::make_shared<tcpip::Storage>();
35 71 : result->writeUnsignedByte(v.getType());
36 71 : switch (v.getType()) {
37 : case TYPE_STRING:
38 21 : result->writeString(v.getString());
39 21 : break;
40 : case TYPE_DOUBLE:
41 50 : result->writeDouble(((const TraCIDouble&)v).value);
42 : break;
43 : default:
44 : // Error!
45 : break;
46 : }
47 71 : return result;
48 : }
49 :
50 3294 : static int readTypedInt(tcpip::Storage& ret, const std::string& error = "") {
51 3294 : if (ret.readUnsignedByte() != libsumo::TYPE_INTEGER && error != "") {
52 0 : throw TraCIException(error);
53 : }
54 3294 : 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 17982 : static double readTypedDouble(tcpip::Storage& ret, const std::string& error = "") {
65 17982 : if (ret.readUnsignedByte() != libsumo::TYPE_DOUBLE && error != "") {
66 4 : throw TraCIException(error);
67 : }
68 17980 : return ret.readDouble();
69 : }
70 :
71 16796 : static std::string readTypedString(tcpip::Storage& ret, const std::string& error = "") {
72 16796 : if (ret.readUnsignedByte() != libsumo::TYPE_STRING && error != "") {
73 0 : throw TraCIException(error);
74 : }
75 16796 : return ret.readString();
76 : }
77 :
78 1503 : static std::vector<std::string> readTypedStringList(tcpip::Storage& ret, const std::string& error = "") {
79 1503 : if (ret.readUnsignedByte() != libsumo::TYPE_STRINGLIST && error != "") {
80 4 : throw TraCIException(error);
81 : }
82 1501 : return ret.readStringList();
83 : }
84 :
85 2301 : static int readCompound(tcpip::Storage& ret, int expectedSize = -1, const std::string& error = "") {
86 2301 : const int type = ret.readUnsignedByte();
87 2301 : const int size = ret.readInt();
88 2301 : if (error != "") {
89 205 : if (type != libsumo::TYPE_COMPOUND || (expectedSize != -1 && size != expectedSize)) {
90 0 : throw TraCIException(error);
91 : }
92 : }
93 2301 : 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 28 : static void readStage(tcpip::Storage& inputStorage, libsumo::TraCIStage& stage, const std::string& error = "") {
104 28 : stage.type = readTypedInt(inputStorage, error);
105 28 : stage.vType = readTypedString(inputStorage, error);
106 28 : stage.line = readTypedString(inputStorage, error);
107 28 : stage.destStop = readTypedString(inputStorage, error);
108 28 : stage.edges = readTypedStringList(inputStorage, error);
109 28 : stage.travelTime = readTypedDouble(inputStorage, error);
110 28 : stage.cost = readTypedDouble(inputStorage, error);
111 28 : stage.length = readTypedDouble(inputStorage, error);
112 28 : stage.intended = readTypedString(inputStorage, error);
113 28 : stage.depart = readTypedDouble(inputStorage, error);
114 28 : stage.departPos = readTypedDouble(inputStorage, error);
115 28 : stage.arrivalPos = readTypedDouble(inputStorage, error);
116 28 : stage.description = readTypedString(inputStorage, error);
117 28 : }
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 49572 : content.writeUnsignedByte(libsumo::TYPE_INTEGER);
127 49572 : content.writeInt(value);
128 48240 : }
129 :
130 : static void writeTypedDouble(tcpip::Storage& content, double value) {
131 68115 : content.writeUnsignedByte(libsumo::TYPE_DOUBLE);
132 68169 : content.writeDouble(value);
133 30417 : }
134 :
135 : static void writeTypedString(tcpip::Storage& content, const std::string& value) {
136 136572 : content.writeUnsignedByte(libsumo::TYPE_STRING);
137 136572 : content.writeString(value);
138 102448 : }
139 :
140 : static void writeTypedStringList(tcpip::Storage& content, const std::vector<std::string>& value) {
141 6981 : content.writeUnsignedByte(libsumo::TYPE_STRINGLIST);
142 6990 : content.writeStringList(value);
143 698 : }
144 :
145 : static void writeCompound(tcpip::Storage& content, int size) {
146 45564 : content.writeUnsignedByte(libsumo::TYPE_COMPOUND);
147 45564 : content.writeInt(size);
148 39272 : }
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 6292 : static void writeStage(tcpip::Storage& outputStorage, const libsumo::TraCIStage& stage) {
165 : writeCompound(outputStorage, 13);
166 6292 : outputStorage.writeUnsignedByte(libsumo::TYPE_INTEGER);
167 6292 : outputStorage.writeInt(stage.type);
168 6292 : writeTypedString(outputStorage, stage.vType);
169 6292 : writeTypedString(outputStorage, stage.line);
170 6292 : writeTypedString(outputStorage, stage.destStop);
171 6292 : writeTypedStringList(outputStorage, stage.edges);
172 6292 : writeTypedDouble(outputStorage, stage.travelTime);
173 6292 : writeTypedDouble(outputStorage, stage.cost);
174 6292 : writeTypedDouble(outputStorage, stage.length);
175 6292 : writeTypedString(outputStorage, stage.intended);
176 6292 : writeTypedDouble(outputStorage, stage.depart);
177 6292 : writeTypedDouble(outputStorage, stage.departPos);
178 6292 : writeTypedDouble(outputStorage, stage.arrivalPos);
179 6292 : writeTypedString(outputStorage, stage.description);
180 6292 : }
181 :
182 : };
183 :
184 :
185 : }
186 :
187 : typedef libsumo::StorageHelper StoHelp;
|