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>
22 #include <foreign/tcpip/storage.h>
23 #include <libsumo/TraCIDefs.h>
24 
25 
26 // ===========================================================================
27 // class definitions
28 // ===========================================================================
29 namespace libsumo {
30 
32 public:
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 
97  static void writeTypedByte(tcpip::Storage& content, int value) {
99  content.writeByte(value);
100  }
101 
102  static void writeTypedInt(tcpip::Storage& content, int value) {
104  content.writeInt(value);
105  }
106 
107  static void writeTypedDouble(tcpip::Storage& content, double value) {
109  content.writeDouble(value);
110  }
111 
112  static void writeTypedString(tcpip::Storage& content, const std::string& value) {
114  content.writeString(value);
115  }
116 
117  static void writeTypedStringList(tcpip::Storage& content, const std::vector<std::string>& value) {
119  content.writeStringList(value);
120  }
121 
122  static void writeCompound(tcpip::Storage& content, int size) {
124  content.writeInt(size);
125  }
126 
127  static void writePolygon(tcpip::Storage& content, const libsumo::TraCIPositionVector& shape) {
129  if (shape.value.size() <= 255) {
130  content.writeUnsignedByte((int)shape.value.size());
131  } else {
132  content.writeUnsignedByte(0);
133  content.writeInt((int)shape.value.size());
134  }
135  for (const libsumo::TraCIPosition& pos : shape.value) {
136  content.writeDouble(pos.x);
137  content.writeDouble(pos.y);
138  }
139  }
140 
141 
142 };
143 
144 
145 }
146 
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 std::shared_ptr< tcpip::Storage > toStorage(const TraCIResult &v)
Definition: StorageHelper.h:33
static void writePolygon(tcpip::Storage &content, const libsumo::TraCIPositionVector &shape)
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 std::vector< std::string > readTypedStringList(tcpip::Storage &ret, const std::string &error="")
Definition: StorageHelper.h:78
static void writeTypedStringList(tcpip::Storage &content, const std::vector< std::string > &value)
static void writeTypedByte(tcpip::Storage &content, int value)
Definition: StorageHelper.h:97
static void writeTypedString(tcpip::Storage &content, const std::string &value)
static double readTypedDouble(tcpip::Storage &ret, const std::string &error="")
Definition: StorageHelper.h:64
An error which allows to continue.
Definition: TraCIDefs.h:130
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:164
A list of positions.
Definition: TraCIDefs.h:207
std::vector< TraCIPosition > value
Definition: TraCIDefs.h:217
virtual std::string getString() const
Definition: TraCIDefs.h:153
virtual int getType() const
Definition: TraCIDefs.h:156