OpenTREP Logo  0.07.18
C++ Open Travel Request Parsing Library
Loading...
Searching...
No Matches
OutputFormat.cpp
Go to the documentation of this file.
1// //////////////////////////////////////////////////////////////////////
2// Import section
3// //////////////////////////////////////////////////////////////////////
4// STL
5#include <cassert>
6#include <sstream>
7// OpenTREP
9
10namespace OPENTREP {
11
12 // //////////////////////////////////////////////////////////////////////
13 const std::string OutputFormat::_labels[LAST_VALUE] =
14 { "Short", "Full", "JSON", "PROTOBUF" };
15
16 // //////////////////////////////////////////////////////////////////////
17 const char OutputFormat::_formatLabels[LAST_VALUE] = { 'S', 'F', 'J', 'P' };
18
19
20 // //////////////////////////////////////////////////////////////////////
22 assert (false);
23 }
24
25 // //////////////////////////////////////////////////////////////////////
26 OutputFormat::
27 OutputFormat (const OutputFormat& iOutputFormat)
28 : _format (iOutputFormat._format) {
29 }
30
31 // //////////////////////////////////////////////////////////////////////
32 OutputFormat::
33 OutputFormat (const EN_OutputFormat& iOutputFormat)
34 : _format (iOutputFormat) {
35 }
36
37 // //////////////////////////////////////////////////////////////////////
39 OutputFormat::getFormat (const char iFormatChar) {
40 EN_OutputFormat oFormat;
41 switch (iFormatChar) {
42 case 'S': oFormat = SHORT; break;
43 case 'F': oFormat = FULL; break;
44 case 'J': oFormat = JSON; break;
45 case 'P': oFormat = PROTOBUF; break;
46 default: oFormat = LAST_VALUE; break;
47 }
48
49 if (oFormat == LAST_VALUE) {
50 const std::string& lLabels = describeLabels();
51 std::ostringstream oMessage;
52 oMessage << "The output format '" << iFormatChar
53 << "' is not known. Known output formats: " << lLabels;
54 throw CodeConversionException (oMessage.str());
55 }
56
57 return oFormat;
58 }
59
60 // //////////////////////////////////////////////////////////////////////
61 OutputFormat::OutputFormat (const char iFormatChar)
62 : _format (getFormat (iFormatChar)) {
63 }
64
65 // //////////////////////////////////////////////////////////////////////
66 OutputFormat::
67 OutputFormat (const std::string& iFormatStr) {
68 //
69#ifndef NDEBUG
70 const size_t lSize = iFormatStr.size();
71 assert (lSize == 1);
72#endif
73 const char lFormatChar = iFormatStr[0];
74 _format = getFormat (lFormatChar);
75 }
76
77 // //////////////////////////////////////////////////////////////////////
78 const std::string& OutputFormat::
79 getLabel (const EN_OutputFormat& iFormat) {
80 return _labels[iFormat];
81 }
82
83 // //////////////////////////////////////////////////////////////////////
85 getFormatLabel (const EN_OutputFormat& iFormat) {
86 return _formatLabels[iFormat];
87 }
88
89 // //////////////////////////////////////////////////////////////////////
90 std::string OutputFormat::
92 std::ostringstream oStr;
93 oStr << _formatLabels[iFormat];
94 return oStr.str();
95 }
96
97 // //////////////////////////////////////////////////////////////////////
99 std::ostringstream ostr;
100 for (unsigned short idx = 0; idx != LAST_VALUE; ++idx) {
101 if (idx != 0) {
102 ostr << ", ";
103 }
104 ostr << _labels[idx];
105 }
106 return ostr.str();
107 }
108
109 // //////////////////////////////////////////////////////////////////////
112 return _format;
113 }
114
115 // //////////////////////////////////////////////////////////////////////
117 const char oFormatChar = _formatLabels[_format];
118 return oFormatChar;
119 }
120
121 // //////////////////////////////////////////////////////////////////////
123 std::ostringstream oStr;
124 oStr << _formatLabels[_format];
125 return oStr.str();
126 }
127
128 // //////////////////////////////////////////////////////////////////////
129 const std::string OutputFormat::describe() const {
130 std::ostringstream ostr;
131 ostr << _labels[_format];
132 return ostr.str();
133 }
134
135 // //////////////////////////////////////////////////////////////////////
137 operator== (const EN_OutputFormat& iFormat) const {
138 return (_format == iFormat);
139 }
140
141}
static std::string describeLabels()
bool operator==(const EN_OutputFormat &) const
static const std::string & getLabel(const EN_OutputFormat &)
OutputFormat(const EN_OutputFormat &)
std::string getFormatAsString() const
static EN_OutputFormat getFormat(const char)
EN_OutputFormat getFormat() const
static char getFormatLabel(const EN_OutputFormat &)
static std::string getFormatLabelAsString(const EN_OutputFormat &)
const std::string describe() const