OpenTREP Logo  0.07.7
C++ Open Travel Request Parsing Library
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 
10 namespace 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  // //////////////////////////////////////////////////////////////////////
21  OutputFormat::OutputFormat() : _format (LAST_VALUE) {
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  // //////////////////////////////////////////////////////////////////////
122  std::string OutputFormat::getFormatAsString() const {
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 }
OPENTREP::OutputFormat::getFormatAsChar
char getFormatAsChar() const
Definition: OutputFormat.cpp:116
OPENTREP::OutputFormat::getFormatLabel
static char getFormatLabel(const EN_OutputFormat &)
Definition: OutputFormat.cpp:85
OPENTREP::OutputFormat::getFormatLabelAsString
static std::string getFormatLabelAsString(const EN_OutputFormat &)
Definition: OutputFormat.cpp:91
OPENTREP::CodeConversionException
Definition: OPENTREP_exceptions.hpp:226
OPENTREP::OutputFormat::getFormatAsString
std::string getFormatAsString() const
Definition: OutputFormat.cpp:122
OPENTREP::LOG::LAST_VALUE
@ LAST_VALUE
Definition: OPENTREP_log.hpp:22
OPENTREP::OutputFormat::FULL
@ FULL
Definition: OutputFormat.hpp:21
OPENTREP::OutputFormat::describeLabels
static std::string describeLabels()
Definition: OutputFormat.cpp:98
OPENTREP
Definition: BasChronometer.cpp:10
OPENTREP::OutputFormat
Enumeration of output formats.
Definition: OutputFormat.hpp:17
OPENTREP::OutputFormat::SHORT
@ SHORT
Definition: OutputFormat.hpp:20
OPENTREP::OutputFormat::PROTOBUF
@ PROTOBUF
Definition: OutputFormat.hpp:23
OPENTREP::OutputFormat::getLabel
static const std::string & getLabel(const EN_OutputFormat &)
Definition: OutputFormat.cpp:79
OPENTREP::OutputFormat::describe
const std::string describe() const
Definition: OutputFormat.cpp:129
OPENTREP::OutputFormat::operator==
bool operator==(const EN_OutputFormat &) const
Definition: OutputFormat.cpp:137
OPENTREP::OutputFormat::getFormat
EN_OutputFormat getFormat() const
Definition: OutputFormat.cpp:111
OPENTREP::OutputFormat::LAST_VALUE
@ LAST_VALUE
Definition: OutputFormat.hpp:24
OutputFormat.hpp
OPENTREP::OutputFormat::EN_OutputFormat
EN_OutputFormat
Definition: OutputFormat.hpp:19
OPENTREP::OutputFormat::JSON
@ JSON
Definition: OutputFormat.hpp:22