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 3153 : static int readTypedInt(tcpip::Storage& ret, const std::string& error = "") { 51 3153 : if (ret.readUnsignedByte() != libsumo::TYPE_INTEGER && error != "") { 52 0 : throw TraCIException(error); 53 : } 54 3153 : return ret.readInt(); 55 : } 56 : 57 462 : static int readTypedByte(tcpip::Storage& ret, const std::string& error = "") { 58 462 : if (ret.readUnsignedByte() != libsumo::TYPE_BYTE && error != "") { 59 0 : throw TraCIException(error); 60 : } 61 462 : return ret.readByte(); 62 : } 63 : 64 17570 : static double readTypedDouble(tcpip::Storage& ret, const std::string& error = "") { 65 17572 : if (ret.readUnsignedByte() != libsumo::TYPE_DOUBLE && error != "") { 66 4 : throw TraCIException(error); 67 : } 68 17568 : return ret.readDouble(); 69 : } 70 : 71 16383 : static std::string readTypedString(tcpip::Storage& ret, const std::string& error = "") { 72 16383 : if (ret.readUnsignedByte() != libsumo::TYPE_STRING && error != "") { 73 0 : throw TraCIException(error); 74 : } 75 16383 : return ret.readString(); 76 : } 77 : 78 1390 : static std::vector<std::string> readTypedStringList(tcpip::Storage& ret, const std::string& error = "") { 79 1392 : if (ret.readUnsignedByte() != libsumo::TYPE_STRINGLIST && error != "") { 80 4 : throw TraCIException(error); 81 : } 82 1388 : return ret.readStringList(); 83 : } 84 : 85 2235 : static int readCompound(tcpip::Storage& ret, int expectedSize = -1, const std::string& error = "") { 86 2235 : const int type = ret.readUnsignedByte(); 87 2235 : const int size = ret.readInt(); 88 2235 : if (error != "") { 89 261 : if (type != libsumo::TYPE_COMPOUND || (expectedSize != -1 && size != expectedSize)) { 90 0 : throw TraCIException(error); 91 : } 92 : } 93 2235 : return size; 94 : } 95 : 96 90 : static bool readBool(tcpip::Storage& ret, const std::string& error = "") { 97 90 : if (ret.readUnsignedByte() != libsumo::TYPE_UBYTE && error != "") { 98 0 : throw TraCIException(error); 99 : } 100 90 : return ret.readUnsignedByte() != 0; 101 : } 102 : 103 25 : static void readStage(tcpip::Storage& inputStorage, libsumo::TraCIStage& stage, const std::string& error = "") { 104 25 : stage.type = readTypedInt(inputStorage, error); 105 25 : stage.vType = readTypedString(inputStorage, error); 106 25 : stage.line = readTypedString(inputStorage, error); 107 25 : stage.destStop = readTypedString(inputStorage, error); 108 25 : stage.edges = readTypedStringList(inputStorage, error); 109 25 : stage.travelTime = readTypedDouble(inputStorage, error); 110 25 : stage.cost = readTypedDouble(inputStorage, error); 111 25 : stage.length = readTypedDouble(inputStorage, error); 112 25 : stage.intended = readTypedString(inputStorage, error); 113 25 : stage.depart = readTypedDouble(inputStorage, error); 114 25 : stage.departPos = readTypedDouble(inputStorage, error); 115 25 : stage.arrivalPos = readTypedDouble(inputStorage, error); 116 25 : stage.description = readTypedString(inputStorage, error); 117 25 : } 118 : 119 : 120 : static void writeTypedByte(tcpip::Storage& content, int value) { 121 5474 : content.writeUnsignedByte(libsumo::TYPE_BYTE); 122 5474 : content.writeByte(value); 123 3694 : } 124 : 125 : static void writeTypedInt(tcpip::Storage& content, int value) { 126 88444 : content.writeUnsignedByte(libsumo::TYPE_INTEGER); 127 88444 : content.writeInt(value); 128 86664 : } 129 : 130 : static void writeTypedDouble(tcpip::Storage& content, double value) { 131 121731 : content.writeUnsignedByte(libsumo::TYPE_DOUBLE); 132 121767 : content.writeDouble(value); 133 43623 : } 134 : 135 : static void writeTypedString(tcpip::Storage& content, const std::string& value) { 136 266840 : content.writeUnsignedByte(libsumo::TYPE_STRING); 137 266840 : content.writeString(value); 138 198160 : } 139 : 140 : static void writeTypedStringList(tcpip::Storage& content, const std::vector<std::string>& value) { 141 13951 : content.writeUnsignedByte(libsumo::TYPE_STRINGLIST); 142 13957 : content.writeStringList(value); 143 933 : } 144 : 145 : static void writeCompound(tcpip::Storage& content, int size) { 146 84643 : content.writeUnsignedByte(libsumo::TYPE_COMPOUND); 147 84643 : content.writeInt(size); 148 71619 : } 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 13024 : static void writeStage(tcpip::Storage& outputStorage, const libsumo::TraCIStage& stage) { 165 : writeCompound(outputStorage, 13); 166 13024 : outputStorage.writeUnsignedByte(libsumo::TYPE_INTEGER); 167 13024 : outputStorage.writeInt(stage.type); 168 13024 : writeTypedString(outputStorage, stage.vType); 169 13024 : writeTypedString(outputStorage, stage.line); 170 13024 : writeTypedString(outputStorage, stage.destStop); 171 13024 : writeTypedStringList(outputStorage, stage.edges); 172 13024 : writeTypedDouble(outputStorage, stage.travelTime); 173 13024 : writeTypedDouble(outputStorage, stage.cost); 174 13024 : writeTypedDouble(outputStorage, stage.length); 175 13024 : writeTypedString(outputStorage, stage.intended); 176 13024 : writeTypedDouble(outputStorage, stage.depart); 177 13024 : writeTypedDouble(outputStorage, stage.departPos); 178 13024 : writeTypedDouble(outputStorage, stage.arrivalPos); 179 13024 : writeTypedString(outputStorage, stage.description); 180 13024 : } 181 : 182 : }; 183 : 184 : 185 : } 186 : 187 : typedef libsumo::StorageHelper StoHelp;