OpenTREP Logo  0.07.18
C++ Open Travel Request Parsing Library
Loading...
Searching...
No Matches
IATAType.cpp
Go to the documentation of this file.
1// //////////////////////////////////////////////////////////////////////
2// Import section
3// //////////////////////////////////////////////////////////////////////
4// STL
5#include <cassert>
6#include <sstream>
7// OpenTrep Protobuf
8#include <opentrep/Travel.pb.h>
9// OpenTREP
10#include <opentrep/IATAType.hpp>
11
12namespace OPENTREP {
13
14 // //////////////////////////////////////////////////////////////////////
15 const std::string IATAType::_labels[LAST_VALUE] =
16 { "CTY_AIRP", "CTY_HPT", "CTY_RSTN", "CTY_BSTN", "CTY_FERRY",
17 "CITY", "AIRP", "HPT", "RSTN", "BSTN", "FERRY", "OFF" };
18
19 // //////////////////////////////////////////////////////////////////////
20 const char IATAType::_typeLabels[LAST_VALUE] =
21 { 'a', 'h', 'r', 'b', 'p', 'C', 'A', 'H', 'R', 'B', 'P', 'O' };
22
23 // //////////////////////////////////////////////////////////////////////
24 IATAType::IATAType() : _type (LAST_VALUE) {
25 assert (false);
26 }
27
28 // //////////////////////////////////////////////////////////////////////
29 IATAType::IATAType (const IATAType& iIATAType)
30 : _type (iIATAType._type) {
31 }
32
33 // //////////////////////////////////////////////////////////////////////
35 : _type (iIATAType) {
36 }
37
38 // //////////////////////////////////////////////////////////////////////
40 EN_IATAType oType;
41 switch (iTypeChar) {
42 case 'a': oType = CTY_AIRP; break;
43 case 'h': oType = CTY_HPT; break;
44 case 'r': oType = CTY_RSTN; break;
45 case 'b': oType = CTY_BSTN; break;
46 case 'p': oType = CTY_FERRY; break;
47 case 'C': oType = CITY; break;
48 case 'A': oType = AIRP; break;
49 case 'H': oType = HPT; break;
50 case 'R': oType = RSTN; break;
51 case 'B': oType = BSTN; break;
52 case 'P': oType = FERRY; break;
53 case 'O': oType = OFF; break;
54 default: oType = LAST_VALUE; break;
55 }
56
57 if (oType == LAST_VALUE) {
58 const std::string& lLabels = describeLabels();
59 std::ostringstream oMessage;
60 oMessage << "The place type '" << iTypeChar
61 << "' is not known. Known place types: " << lLabels;
62 throw CodeConversionException (oMessage.str());
63 }
64
65 return oType;
66 }
67
68 // //////////////////////////////////////////////////////////////////////
69 IATAType::IATAType (const char iTypeChar)
70 : _type (getType (iTypeChar)) {
71 }
72
73 // //////////////////////////////////////////////////////////////////////
74 IATAType::IATAType (const std::string& iTypeStr) : _type (LAST_VALUE) {
75 if (iTypeStr == "CA") {
76 _type = CTY_AIRP;
77 } else if (iTypeStr == "CH") {
78 _type = CTY_HPT;
79 } else if (iTypeStr == "CR") {
80 _type = CTY_RSTN;
81 } else if (iTypeStr == "CB") {
82 _type = CTY_BSTN;
83 } else if (iTypeStr == "CP") {
84 _type = CTY_FERRY;
85 } else if (iTypeStr == "C") {
86 _type = CITY;
87 } else if (iTypeStr == "A") {
88 _type = AIRP;
89 } else if (iTypeStr == "H") {
90 _type = HPT;
91 } else if (iTypeStr == "R") {
92 _type = RSTN;
93 } else if (iTypeStr == "B") {
94 _type = BSTN;
95 } else if (iTypeStr == "P") {
96 _type = FERRY;
97 } else if (iTypeStr == "O") {
98 _type = OFF;
99 } else {
100 _type = LAST_VALUE;
101 }
102
103 if (_type == LAST_VALUE) {
104 const std::string& lLabels = describeLabels();
105 std::ostringstream oMessage;
106 oMessage << "The place type '" << iTypeStr
107 << "' is not known. Known place types: " << lLabels;
108 throw CodeConversionException (oMessage.str());
109 }
110 }
111
112 // //////////////////////////////////////////////////////////////////////
113 const std::string& IATAType::getLabel (const EN_IATAType& iType) {
114 return _labels[iType];
115 }
116
117 // //////////////////////////////////////////////////////////////////////
119 return _typeLabels[iType];
120 }
121
122 // //////////////////////////////////////////////////////////////////////
123 std::string IATAType::getTypeLabelAsString (const EN_IATAType& iType) {
124 std::ostringstream oStr;
125 oStr << _typeLabels[iType];
126 return oStr.str();
127 }
128
129 // //////////////////////////////////////////////////////////////////////
130 treppb::PlaceType IATAType::getTypeLabelAsPB (const EN_IATAType& iType) {
131 treppb::PlaceType oLocationType;
132 switch (iType) {
133 case CTY_AIRP: oLocationType.set_type (treppb::PlaceType::CTY_AIRP); break;
134 case CTY_HPT: oLocationType.set_type (treppb::PlaceType::CTY_HPT); break;
135 case CTY_RSTN: oLocationType.set_type (treppb::PlaceType::CTY_RSTN); break;
136 case CTY_BSTN: oLocationType.set_type (treppb::PlaceType::CTY_BSTN); break;
137 case CTY_FERRY: oLocationType.set_type (treppb::PlaceType::CTY_FERRY); break;
138 case CITY: oLocationType.set_type (treppb::PlaceType::CITY); break;
139 case AIRP: oLocationType.set_type (treppb::PlaceType::AIRP); break;
140 case HPT: oLocationType.set_type (treppb::PlaceType::HPT); break;
141 case RSTN: oLocationType.set_type (treppb::PlaceType::RSTN); break;
142 case BSTN: oLocationType.set_type (treppb::PlaceType::BSTN); break;
143 case FERRY: oLocationType.set_type (treppb::PlaceType::FERRY); break;
144 case OFF: oLocationType.set_type (treppb::PlaceType::OFF); break;
145 default: oLocationType.set_type (treppb::PlaceType::UNKNOWN); break;
146 }
147 return oLocationType;
148 }
149
150 // //////////////////////////////////////////////////////////////////////
152 std::ostringstream ostr;
153 for (unsigned short idx = 0; idx != LAST_VALUE; ++idx) {
154 if (idx != 0) {
155 ostr << ", ";
156 }
157 ostr << _labels[idx];
158 }
159 return ostr.str();
160 }
161
162 // //////////////////////////////////////////////////////////////////////
164 return _type;
165 }
166
167 // //////////////////////////////////////////////////////////////////////
169 const char oTypeChar = _typeLabels[_type];
170 return oTypeChar;
171 }
172
173 // //////////////////////////////////////////////////////////////////////
174 std::string IATAType::getTypeAsString() const {
175 std::ostringstream oStr;
176 oStr << _typeLabels[_type];
177 return oStr.str();
178 }
179
180 // //////////////////////////////////////////////////////////////////////
181 treppb::PlaceType IATAType::getTypeAsPB() const {
182 return getTypeLabelAsPB (_type);
183 }
184
185 // //////////////////////////////////////////////////////////////////////
186 const std::string IATAType::describe() const {
187 std::ostringstream ostr;
188 ostr << _labels[_type];
189 return ostr.str();
190 }
191
192 // //////////////////////////////////////////////////////////////////////
193 bool IATAType::operator== (const EN_IATAType& iType) const {
194 return (_type == iType);
195 }
196
197 // //////////////////////////////////////////////////////////////////////
198 bool IATAType::operator== (const IATAType& iIATAType) const {
199 return (_type == iIATAType._type);
200 }
201
202}
bool operator==(const EN_IATAType &) const
Definition IATAType.cpp:193
static const std::string & getLabel(const EN_IATAType &)
Definition IATAType.cpp:113
treppb::PlaceType getTypeAsPB() const
Definition IATAType.cpp:181
char getTypeAsChar() const
Definition IATAType.cpp:168
const std::string describe() const
Definition IATAType.cpp:186
EN_IATAType getType() const
Definition IATAType.cpp:163
IATAType(const EN_IATAType &)
Definition IATAType.cpp:34
static treppb::PlaceType getTypeLabelAsPB(const EN_IATAType &)
Definition IATAType.cpp:130
std::string getTypeAsString() const
Definition IATAType.cpp:174
static char getTypeLabel(const EN_IATAType &)
Definition IATAType.cpp:118
static std::string getTypeLabelAsString(const EN_IATAType &)
Definition IATAType.cpp:123
static EN_IATAType getType(const char)
Definition IATAType.cpp:39
static std::string describeLabels()
Definition IATAType.cpp:151