IWORKValueContext.h Source File documentation

IWORKValueContext.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 IWORKVALUECONTEXT_H_INCLUDED
11 #define IWORKVALUECONTEXT_H_INCLUDED
12
13 #include <boost/optional.hpp>
14
15 #include "IWORKXMLContextBase.h"
16
17 namespace libetonyek
18 {
19
20 template<typename Type, class NestedParser, unsigned Id, unsigned Id2=0>
21 class IWORKValueContext : public IWORKXMLElementContextBase
22 {
23 public:
24 IWORKValueContext(IWORKXMLParserState &state, boost::optional<Type> &value)
25 : IWORKXMLElementContextBase(state)
26 , m_value()
27 , m_optional(value)
28 , m_direct(nullptr)
29 , m_isSet(nullptr)
30 {
31 }
32
33 IWORKValueContext(IWORKXMLParserState &state, Type &value, bool *isSet = nullptr)
34 : IWORKXMLElementContextBase(state)
35 , m_value()
36 , m_optional(m_value)
37 , m_direct(&value)
38 , m_isSet(isSet)
39 {
40 }
41
42 protected:
43 void set(const Type &value)
44 {
45 m_value = value;
46 }
47
48 IWORKXMLContextPtr_t element(const int name) override
49 {
50 if (name == Id || (Id2 && name == Id2))
51 return std::make_shared<NestedParser>(getState(), m_value);
52 ETONYEK_DEBUG_MSG(("IWORKXMLContextPtr_t::element: found unexpected element %d\n", name));
53 return IWORKXMLContextPtr_t();
54 }
55
56 void endOfElement() override
57 {
58 if (m_value)
59 {
60 if (m_direct)
61 {
62 *m_direct = get(m_value);
63 if (m_isSet)
64 *m_isSet |= true;
65 }
66 else
67 {
68 m_optional = m_value;
69 }
70 }
71 }
72
73 private:
74 IWORKValueContext(const IWORKValueContext &);
75 IWORKValueContext &operator=(const IWORKValueContext &);
76
77 boost::optional<Type> m_value;
78 boost::optional<Type> &m_optional;
79 Type *const m_direct;
80 bool *const m_isSet;
81 };
82
83 }
84
85 #endif // IWORKVALUECONTEXT_H_INCLUDED
86
87 /* vim:set shiftwidth=2 softtabstop=2 expandtab: */
libetonyek
Definition: IWORKBezierElement.cpp:20
libetonyek::IWORKXMLContextPtr_t
std::shared_ptr< IWORKXMLContext > IWORKXMLContextPtr_t
Definition: IWORKXMLContext.h:18
ETONYEK_DEBUG_MSG
#define ETONYEK_DEBUG_MSG(M)
Definition: libetonyek_utils.h:54
libetonyek::IWORKValueContext
Definition: IWORKValueContext.h:21
libetonyek::IWORKValueContext::m_isSet
bool *const m_isSet
Definition: IWORKValueContext.h:80
libetonyek::IWORKXMLParserState
Definition: IWORKXMLParserState.h:31
libetonyek::IWORKValueContext::element
IWORKXMLContextPtr_t element(const int name) override
Definition: IWORKValueContext.h:48
libetonyek::IWORKValueContext::IWORKValueContext
IWORKValueContext(IWORKXMLParserState &state, boost::optional< Type > &value)
Definition: IWORKValueContext.h:24
libetonyek::IWORKValueContext::endOfElement
void endOfElement() override
Definition: IWORKValueContext.h:56
libetonyek::IWORKValueContext::IWORKValueContext
IWORKValueContext(IWORKXMLParserState &state, Type &value, bool *isSet=nullptr)
Definition: IWORKValueContext.h:33
libetonyek::IWORKValueContext::set
void set(const Type &value)
Definition: IWORKValueContext.h:43
libetonyek::IWORKXMLContextBase
Definition: IWORKXMLContextBase.h:27
IWORKXMLContextBase.h
libetonyek::IWORKValueContext::m_value
boost::optional< Type > m_value
Definition: IWORKValueContext.h:77
libetonyek::IWORKValueContext::m_direct
Type *const m_direct
Definition: IWORKValueContext.h:79
libetonyek::IWORKValueContext::m_optional
boost::optional< Type > & m_optional
Definition: IWORKValueContext.h:78
libetonyek::get
const ValueT & get(const detail::IWAFieldImpl< TagV, ValueT, Reader > &field)
Definition: IWAField.h:190
libetonyek::IWORKValueContext::operator=
IWORKValueContext & operator=(const IWORKValueContext &)
libetonyek::IWORKToken::name
@ name
Definition: IWORKToken.h:578
libetonyek::IWORKToken::value
@ value
Definition: IWORKToken.h:624
libetonyek::IWORKXMLContextBase::getState
State & getState()
Definition: IWORKXMLContextBase.h:43