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 <memory>
15#include <stdexcept>
16
17#include <boost/container/deque.hpp>
18#include <boost/optional.hpp>
19
20#include "IWAReader.h"
21#include "libetonyek_utils.h"
22
23namespace libetonyek
24{
25
27{
28public:
49
50public:
51 virtual ~IWAField() = 0;
52
53 virtual Tag tag() const = 0;
54
55 // repeated
56 virtual bool empty() const = 0;
57 virtual std::size_t size() const = 0;
58
59 // optional
60 virtual bool is() const = 0;
61 operator bool() const;
62 bool operator!() const;
63
64 virtual void parse(const RVNGInputStreamPtr_t &input, unsigned long length, bool allowEmpty) = 0;
65};
66
67typedef std::shared_ptr<IWAField> IWAFieldPtr_t;
68
69namespace detail
70{
71
72template<IWAField::Tag TagV, typename ValueT, typename Reader>
73class IWAFieldImpl : public IWAField
74{
75 typedef boost::container::deque<ValueT> container_type;
76
77public:
78 typedef ValueT value_type;
79 typedef ValueT &reference_type;
80 typedef const ValueT &const_reference_type;
81 typedef typename container_type::const_iterator const_iterator;
82 typedef typename container_type::const_reverse_iterator const_reverse_iterator;
83
84public:
86 : IWAField()
87 , m_values()
88 {
89 }
90 // classification
91
92 IWAField::Tag tag() const override
93 {
94 return TagV;
95 }
96
97 // optional interface
98
99 bool is() const override
100 {
101 return !m_values.empty();
102 }
103
105 {
106 if (m_values.empty())
107 throw std::logic_error("the field is unset");
108 return m_values[0];
109 }
110
111 // container interface
112
113 bool empty() const override
114 {
115 return m_values.empty();
116 }
117
118 std::size_t size() const override
119 {
120 return m_values.size();
121 }
122
123 const_reference_type operator[](const std::size_t index) const
124 {
125 if (index >= m_values.size())
126 throw std::out_of_range("index is out of range");
127 return m_values[index];
128 }
129
131 {
132 return m_values.begin();
133 }
134
136 {
137 return m_values.end();
138 }
139
141 {
142 return m_values.rbegin();
143 }
144
146 {
147 return m_values.rend();
148 }
149
150 // conversions
151
152 // TODO: remove this or replace direct use of std::deque by a typedef
153 const std::deque<value_type> repeated() const
154 {
155 const std::deque<value_type> values(m_values.begin(), m_values.end());
156 return values;
157 }
158
159 const boost::optional<value_type> optional() const
160 {
161 return m_values.empty() ? boost::none : boost::make_optional(m_values.front());
162 }
163
164 // initialization
165
166 void parse(const RVNGInputStreamPtr_t &input, const unsigned long length, const bool allowEmpty) override
167 {
168 if (length != 0)
169 {
170 const long start = input->tell();
171 while (!input->isEnd() && (length > static_cast<unsigned long>(input->tell() - start)))
172 {
173 const value_type value(Reader::read(input, length));
174 m_values.push_back(value);
175 }
176 }
177 else if (allowEmpty)
178 {
179 m_values.push_back(value_type());
180 }
181 }
182
183private:
185};
186
187}
188
189template<IWAField::Tag TagV, typename ValueT, typename Reader>
191{
192 return field.get();
193}
194
195template<IWAField::Tag TagV, typename ValueT, typename Reader>
197{
198 return bool(field) ? field.get() : value;
199}
200
201template<IWAField::Tag TagV, typename ValueT, typename Reader, typename DefaultValueT>
202const ValueT get_optional_value_or(const detail::IWAFieldImpl<TagV, ValueT, Reader> &field, const DefaultValueT &value)
203{
204 return bool(field) ? field.get() : ValueT(value);
205}
206
212
215
218
221
222class IWAMessageField : public detail::IWAFieldImpl<IWAField::TAG_MESSAGE, IWAMessage, IWAReader::Message>
223{
224public:
225 const IWAUInt32Field &uint32(std::size_t field) const;
226 const IWAUInt64Field &uint64(std::size_t field) const;
227 const IWASInt32Field &sint32(std::size_t field) const;
228 const IWASInt64Field &sint64(std::size_t field) const;
229 const IWABoolField &bool_(std::size_t field) const;
230
231 const IWAFixed64Field &fixed64(std::size_t field) const;
232 const IWADoubleField &double_(std::size_t field) const;
233
234 const IWAStringField &string(std::size_t field) const;
235 const IWABytesField &bytes(std::size_t field) const;
236 const IWAMessageField &message(std::size_t field) const;
237
238 const IWAFixed32Field &fixed32(std::size_t field) const;
239 const IWAFloatField &float_(std::size_t field) const;
240};
241
242}
243
244#endif
245
246/* vim:set shiftwidth=2 softtabstop=2 expandtab: */
Definition IWAField.h:27
virtual Tag tag() const =0
Tag
Definition IWAField.h:30
@ TAG_INT32
Definition IWAField.h:31
@ TAG_SFIXED64
Definition IWAField.h:40
@ TAG_BYTES
Definition IWAField.h:43
@ TAG_SINT64
Definition IWAField.h:36
@ TAG_UINT64
Definition IWAField.h:34
@ TAG_BOOL
Definition IWAField.h:37
@ TAG_MESSAGE
Definition IWAField.h:44
@ TAG_SFIXED32
Definition IWAField.h:46
@ TAG_DOUBLE
Definition IWAField.h:41
@ TAG_INT64
Definition IWAField.h:32
@ TAG_FIXED32
Definition IWAField.h:45
@ TAG_SINT32
Definition IWAField.h:35
@ TAG_UINT32
Definition IWAField.h:33
@ TAG_STRING
Definition IWAField.h:42
@ TAG_FLOAT
Definition IWAField.h:47
@ TAG_FIXED64
Definition IWAField.h:39
@ TAG_ENUM
Definition IWAField.h:38
virtual bool is() const =0
virtual std::size_t size() const =0
virtual ~IWAField()=0
Definition IWAField.cpp:16
bool operator!() const
Definition IWAField.cpp:25
virtual void parse(const RVNGInputStreamPtr_t &input, unsigned long length, bool allowEmpty)=0
virtual bool empty() const =0
Definition IWAField.h:223
const IWAFloatField & float_(std::size_t field) const
Definition IWAField.cpp:80
const IWASInt64Field & sint64(std::size_t field) const
Definition IWAField.cpp:45
const IWAFixed32Field & fixed32(std::size_t field) const
Definition IWAField.cpp:75
const IWAFixed64Field & fixed64(std::size_t field) const
Definition IWAField.cpp:55
const IWAUInt64Field & uint64(std::size_t field) const
Definition IWAField.cpp:35
const IWABytesField & bytes(std::size_t field) const
const IWAUInt32Field & uint32(std::size_t field) const
Definition IWAField.cpp:30
const IWABoolField & bool_(std::size_t field) const
Definition IWAField.cpp:50
const IWASInt32Field & sint32(std::size_t field) const
Definition IWAField.cpp:40
const IWAMessageField & message(std::size_t field) const
Definition IWAField.cpp:70
Definition IWAField.h:74
const std::deque< value_type > repeated() const
Definition IWAField.h:153
boost::container::deque< ValueT > container_type
Definition IWAField.h:75
ValueT & reference_type
Definition IWAField.h:79
container_type m_values
Definition IWAField.h:184
const ValueT & const_reference_type
Definition IWAField.h:80
const_reverse_iterator rend() const
Definition IWAField.h:145
bool empty() const override
Definition IWAField.h:113
const_iterator begin() const
Definition IWAField.h:130
IWAField::Tag tag() const override
Definition IWAField.h:92
const_reverse_iterator rbegin() const
Definition IWAField.h:140
bool is() const override
Definition IWAField.h:99
container_type::const_reverse_iterator const_reverse_iterator
Definition IWAField.h:82
const_reference_type operator[](const std::size_t index) const
Definition IWAField.h:123
IWAFieldImpl()
Definition IWAField.h:85
void parse(const RVNGInputStreamPtr_t &input, const unsigned long length, const bool allowEmpty) override
Definition IWAField.h:166
const_iterator end() const
Definition IWAField.h:135
std::size_t size() const override
Definition IWAField.h:118
container_type::const_iterator const_iterator
Definition IWAField.h:81
ValueT value_type
Definition IWAField.h:78
const_reference_type get() const
Definition IWAField.h:104
const boost::optional< value_type > optional() const
Definition IWAField.h:159
@ string
Definition IWORKToken.h:418
@ value
Definition IWORKToken.h:631
@ double_
Definition IWORKToken.h:663
@ index
Definition IWORKToken.h:571
@ start
Definition IWORKToken.h:412
Definition IWORKBezierElement.cpp:21
std::shared_ptr< IWAField > IWAFieldPtr_t
Definition IWAField.h:67
detail::IWAFieldImpl< IWAField::TAG_SINT32, int32_t, IWAReader::SInt32 > IWASInt32Field
Definition IWAField.h:209
detail::IWAFieldImpl< IWAField::TAG_UINT32, uint32_t, IWAReader::UInt32 > IWAUInt32Field
Definition IWAField.h:207
std::shared_ptr< librevenge::RVNGInputStream > RVNGInputStreamPtr_t
Definition libetonyek_utils.h:82
detail::IWAFieldImpl< IWAField::TAG_SINT64, int64_t, IWAReader::SInt64 > IWASInt64Field
Definition IWAField.h:210
detail::IWAFieldImpl< IWAField::TAG_UINT64, uint64_t, IWAReader::UInt64 > IWAUInt64Field
Definition IWAField.h:208
detail::IWAFieldImpl< IWAField::TAG_DOUBLE, double, IWAReader::Double > IWADoubleField
Definition IWAField.h:214
detail::IWAFieldImpl< IWAField::TAG_BOOL, bool, IWAReader::Bool > IWABoolField
Definition IWAField.h:211
const ValueT & get_optional_value_or(const detail::IWAFieldImpl< TagV, ValueT, Reader > &field, const ValueT &value)
Definition IWAField.h:196
detail::IWAFieldImpl< IWAField::TAG_FIXED64, uint64_t, IWAReader::Fixed64 > IWAFixed64Field
Definition IWAField.h:213
detail::IWAFieldImpl< IWAField::TAG_STRING, std::string, IWAReader::String > IWAStringField
Definition IWAField.h:216
detail::IWAFieldImpl< IWAField::TAG_FIXED32, uint32_t, IWAReader::Fixed32 > IWAFixed32Field
Definition IWAField.h:219
detail::IWAFieldImpl< IWAField::TAG_FLOAT, float, IWAReader::Float > IWAFloatField
Definition IWAField.h:220
const ValueT & get(const detail::IWAFieldImpl< TagV, ValueT, Reader > &field)
Definition IWAField.h:190
detail::IWAFieldImpl< IWAField::TAG_BYTES, RVNGInputStreamPtr_t, IWAReader::Bytes > IWABytesField
Definition IWAField.h:217

Generated for libetonyek by doxygen 1.12.0