IWAField.h
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /*
3  * This file is part of the libetonyek project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  */
9 
10 #ifndef IWAFIELD_H_INCLUDED
11 #define IWAFIELD_H_INCLUDED
12 
13 #include <deque>
14 #include <stdexcept>
15 
16 #include <boost/optional.hpp>
17 #include <boost/shared_ptr.hpp>
18 
19 #include "IWAReader.h"
20 #include "libetonyek_utils.h"
21 
22 namespace libetonyek
23 {
24 
25 class IWAField
26 {
27 public:
28  enum Tag
29  {
47  };
48 
49 public:
50  virtual ~IWAField() = 0;
51 
52  virtual Tag tag() const = 0;
53 
54  // repeated
55  virtual bool empty() const = 0;
56  virtual std::size_t size() const = 0;
57 
58  // optional
59  virtual bool is() const = 0;
60  operator bool() const;
61  bool operator!() const;
62 
63  virtual void parse(const RVNGInputStreamPtr_t &input, unsigned long length) = 0;
64 };
65 
66 typedef boost::shared_ptr<IWAField> IWAFieldPtr_t;
67 
68 namespace detail
69 {
70 
71 template<IWAField::Tag TagV, typename ValueT, typename Reader>
72 class IWAFieldImpl : public IWAField
73 {
74  typedef std::deque<ValueT> container_type;
75 
76 public:
77  typedef ValueT value_type;
78  typedef ValueT &reference_type;
79  typedef const ValueT &const_reference_type;
80  typedef typename container_type::const_iterator const_iterator;
81  typedef typename container_type::const_reverse_iterator const_reverse_iterator;
82 
83 public:
84  // classification
85 
86  virtual IWAField::Tag tag() const
87  {
88  return TagV;
89  }
90 
91  // optional interface
92 
93  virtual bool is() const
94  {
95  return !m_values.empty();
96  }
97 
98  const_reference_type get() const
99  {
100  if (m_values.empty())
101  throw std::logic_error("the field is unset");
102  return m_values[0];
103  }
104 
105  // container interface
106 
107  virtual bool empty() const
108  {
109  return m_values.empty();
110  }
111 
112  virtual std::size_t size() const
113  {
114  return m_values.size();
115  }
116 
117  const_reference_type operator[](const std::size_t index) const
118  {
119  if (index >= m_values.size())
120  throw std::out_of_range("index is out of range");
121  return m_values[index];
122  }
123 
124  const_iterator begin() const
125  {
126  return m_values.begin();
127  }
128 
129  const_iterator end() const
130  {
131  return m_values.end();
132  }
133 
134  const_reverse_iterator rbegin() const
135  {
136  return m_values.rbegin();
137  }
138 
139  const_reverse_iterator rend() const
140  {
141  return m_values.rend();
142  }
143 
144  // conversions
145 
146  const std::deque<value_type> &repeated() const
147  {
148  return m_values;
149  }
150 
151  const boost::optional<value_type> optional() const
152  {
153  return m_values.empty() ? boost::none : boost::make_optional(m_values.front());
154  }
155 
156  // initialization
157 
158  virtual void parse(const RVNGInputStreamPtr_t &input, const unsigned long length)
159  {
160  const long start = input->tell();
161  while (!input->isEnd() && (length > static_cast<unsigned long>(input->tell() - start)))
162  {
163  const value_type value(Reader::read(input, length));
164  m_values.push_back(value);
165  }
166  }
167 
168 private:
169  std::deque<ValueT> m_values;
170 };
171 
172 }
173 
174 template<IWAField::Tag TagV, typename ValueT, typename Reader>
175 const ValueT &get(const detail::IWAFieldImpl<TagV, ValueT, Reader> &field)
176 {
177  return field.get();
178 }
179 
180 template<IWAField::Tag TagV, typename ValueT, typename Reader>
181 const ValueT &get_optional_value_or(const detail::IWAFieldImpl<TagV, ValueT, Reader> &field, const ValueT &value)
182 {
183  return bool(field) ? field.get() : value;
184 }
185 
186 template<IWAField::Tag TagV, typename ValueT, typename Reader, typename DefaultValueT>
187 const ValueT get_optional_value_or(const detail::IWAFieldImpl<TagV, ValueT, Reader> &field, const DefaultValueT &value)
188 {
189  return bool(field) ? field.get() : ValueT(value);
190 }
191 
197 
200 
203 
206 
207 class IWAMessageField : public detail::IWAFieldImpl<IWAField::TAG_MESSAGE, IWAMessage, IWAReader::Message>
208 {
209 public:
210  const IWAUInt32Field &uint32(std::size_t field) const;
211  const IWAUInt64Field &uint64(std::size_t field) const;
212  const IWASInt32Field &sint32(std::size_t field) const;
213  const IWASInt64Field &sint64(std::size_t field) const;
214  const IWABoolField &bool_(std::size_t field) const;
215 
216  const IWAFixed64Field &fixed64(std::size_t field) const;
217  const IWADoubleField &double_(std::size_t field) const;
218 
219  const IWAStringField &string(std::size_t field) const;
220  const IWABytesField &bytes(std::size_t field) const;
221  const IWAMessageField &message(std::size_t field) const;
222 
223  const IWAFixed32Field &fixed32(std::size_t field) const;
224  const IWAFloatField &float_(std::size_t field) const;
225 };
226 
227 }
228 
229 #endif
230 
231 /* vim:set shiftwidth=2 softtabstop=2 expandtab: */
virtual std::size_t size() const =0
Definition: IWORKBezierElement.cpp:18
const IWAUInt64Field & uint64(std::size_t field) const
Definition: IWAField.cpp:35
const IWAFixed32Field & fixed32(std::size_t field) const
Definition: IWAField.cpp:75
virtual bool is() const
Definition: IWAField.h:93
Definition: IWAField.h:43
detail::IWAFieldImpl< IWAField::TAG_FIXED64, uint64_t, IWAReader::Fixed64 > IWAFixed64Field
Definition: IWAField.h:198
Definition: IWAField.h:32
const_reference_type operator[](const std::size_t index) const
Definition: IWAField.h:117
detail::IWAFieldImpl< IWAField::TAG_BOOL, bool, IWAReader::Bool > IWABoolField
Definition: IWAField.h:196
detail::IWAFieldImpl< IWAField::TAG_STRING, std::string, IWAReader::String > IWAStringField
Definition: IWAField.h:201
bool operator!() const
Definition: IWAField.cpp:25
detail::IWAFieldImpl< IWAField::TAG_UINT64, uint64_t, IWAReader::UInt64 > IWAUInt64Field
Definition: IWAField.h:193
Definition: IWAField.h:36
detail::IWAFieldImpl< IWAField::TAG_UINT32, uint32_t, IWAReader::UInt32 > IWAUInt32Field
Definition: IWAField.h:192
Definition: IWAField.h:33
virtual Tag tag() const =0
const IWASInt32Field & sint32(std::size_t field) const
Definition: IWAField.cpp:40
virtual ~IWAField()=0
Definition: IWAField.cpp:16
Definition: IWAField.h:31
ValueT value_type
Definition: IWAField.h:77
virtual void parse(const RVNGInputStreamPtr_t &input, unsigned long length)=0
std::deque< ValueT > container_type
Definition: IWAField.h:74
Definition: IWAField.h:45
detail::IWAFieldImpl< IWAField::TAG_FLOAT, float, IWAReader::Float > IWAFloatField
Definition: IWAField.h:205
Definition: IWAField.h:30
detail::IWAFieldImpl< IWAField::TAG_SINT32, int32_t, IWAReader::SInt32 > IWASInt32Field
Definition: IWAField.h:194
std::deque< ValueT > m_values
Definition: IWAField.h:169
Tag
Definition: IWAField.h:28
virtual std::size_t size() const
Definition: IWAField.h:112
Definition: IWAField.h:40
Definition: IWAField.h:44
detail::IWAFieldImpl< IWAField::TAG_BYTES, RVNGInputStreamPtr_t, IWAReader::Bytes > IWABytesField
Definition: IWAField.h:202
detail::IWAFieldImpl< IWAField::TAG_DOUBLE, double, IWAReader::Double > IWADoubleField
Definition: IWAField.h:199
Definition: IWAField.h:46
virtual bool is() const =0
container_type::const_reverse_iterator const_reverse_iterator
Definition: IWAField.h:81
Definition: IWAField.h:42
const std::deque< value_type > & repeated() const
Definition: IWAField.h:146
const ValueT & get_optional_value_or(const detail::IWAFieldImpl< TagV, ValueT, Reader > &field, const ValueT &value)
Definition: IWAField.h:181
const IWASInt64Field & sint64(std::size_t field) const
Definition: IWAField.cpp:45
virtual void parse(const RVNGInputStreamPtr_t &input, const unsigned long length)
Definition: IWAField.h:158
const IWAUInt32Field & uint32(std::size_t field) const
Definition: IWAField.cpp:30
const_reverse_iterator rend() const
Definition: IWAField.h:139
boost::shared_ptr< IWAField > IWAFieldPtr_t
Definition: IWAField.h:66
const IWADoubleField & double_(std::size_t field) const
Definition: IWAField.cpp:60
Definition: IWAField.h:35
virtual IWAField::Tag tag() const
Definition: IWAField.h:86
const IWABoolField & bool_(std::size_t field) const
Definition: IWAField.cpp:50
virtual bool empty() const =0
Definition: IWAField.h:72
boost::shared_ptr< librevenge::RVNGInputStream > RVNGInputStreamPtr_t
Definition: libetonyek_utils.h:111
const ValueT & const_reference_type
Definition: IWAField.h:79
Definition: IWAField.h:207
Definition: IWAField.h:39
const_reverse_iterator rbegin() const
Definition: IWAField.h:134
const IWAMessageField & message(std::size_t field) const
Definition: IWAField.cpp:70
const IWAFixed64Field & fixed64(std::size_t field) const
Definition: IWAField.cpp:55
Definition: IWAField.h:25
ValueT & reference_type
Definition: IWAField.h:78
Definition: IWAField.h:34
const IWABytesField & bytes(std::size_t field) const
detail::IWAFieldImpl< IWAField::TAG_SINT64, int64_t, IWAReader::SInt64 > IWASInt64Field
Definition: IWAField.h:195
Definition: IWAField.h:41
const_reference_type get() const
Definition: IWAField.h:98
const IWAFloatField & float_(std::size_t field) const
Definition: IWAField.cpp:80
const boost::optional< value_type > optional() const
Definition: IWAField.h:151
Definition: IWAField.h:37
virtual bool empty() const
Definition: IWAField.h:107
Definition: IWAField.h:38
container_type::const_iterator const_iterator
Definition: IWAField.h:80
const_iterator begin() const
Definition: IWAField.h:124
detail::IWAFieldImpl< IWAField::TAG_FIXED32, uint32_t, IWAReader::Fixed32 > IWAFixed32Field
Definition: IWAField.h:204
const IWAStringField & string(std::size_t field) const
Definition: IWAField.cpp:65
const_iterator end() const
Definition: IWAField.h:129

Generated for libetonyek by doxygen 1.8.10