IWORKContainerContext.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 IWORKCONTAINERCONTEXT_H_INCLUDED
11 #define IWORKCONTAINERCONTEXT_H_INCLUDED
12
13 #include <cassert>
14 #include <deque>
15
16 #include <boost/optional.hpp>
17
18 #include "IWORKToken.h"
19 #include "IWORKTypes_fwd.h"
20 #include "IWORKRefContext.h"
21 #include "IWORKXMLContextBase.h"
22
23 namespace libetonyek
24 {
25
26 template<typename Type, class NestedParser, template<typename T, class C> class Collector, unsigned Id, unsigned RefId = 0>
27 class IWORKContainerContext : public IWORKXMLElementContextBase
28 {
29 typedef std::unordered_map<ID_t, Type> Dict_t;
30
31 public:
32 IWORKContainerContext(IWORKXMLParserState &state, std::deque<Type> &elements)
33 : IWORKXMLElementContextBase(state)
34 , m_dict(nullptr)
35 , m_ref()
36 , m_elements(elements)
37 , m_collector(elements)
38 {
39 }
40
41 IWORKContainerContext(IWORKXMLParserState &state, Dict_t &dict, std::deque<Type> &elements)
42 : IWORKXMLElementContextBase(state)
43 , m_dict(&dict)
44 , m_ref()
45 , m_elements(elements)
46 , m_collector(elements)
47 {
48 }
49
50 protected:
51 IWORKXMLContextPtr_t element(const int name) override
52 {
53 if (m_ref && m_dict)
54 handleRef();
55 else if (m_collector.pending())
56 m_collector.push();
57
58 if (name == Id)
59 return m_collector.template makeContext<NestedParser>(getState());
60 else if ((RefId != 0) && (name == RefId))
61 return std::make_shared<IWORKRefContext>(getState(), m_ref);
62 else if (name!=(IWORKToken::NS_URI_SF | IWORKToken::null))
63 {
64 ETONYEK_DEBUG_MSG(("IWORKContainerContext::handleRef: find unknown element %d\n", int(name)));
65 }
66 return IWORKXMLContextPtr_t();
67 }
68
69 void endOfElement() override
70 {
71 if (m_ref && m_dict)
72 handleRef();
73 else if (m_collector.pending())
74 m_collector.push();
75 }
76
77 private:
78 void handleRef()
79 {
80 assert(m_dict);
81
82 const typename Dict_t::const_iterator it = m_dict->find(get(m_ref));
83 if (it == m_dict->end())
84 {
85 ETONYEK_DEBUG_MSG(("IWORKContainerContext::handleRef: unknown ref \"%s\"\n", get(m_ref).c_str()));
86 m_elements.push_back(Type());
87 }
88 else
89 m_elements.push_back(it->second);
90 m_ref.reset();
91 }
92 IWORKContainerContext(IWORKContainerContext &);
93 IWORKContainerContext &operator=(IWORKContainerContext &);
94 private:
95 Dict_t *const m_dict;
96 boost::optional<ID_t> m_ref;
97 std::deque<Type> &m_elements;
98 Collector<Type, std::deque<Type> > m_collector;
99 };
100
101 }
102
103 #endif // IWORKCONTAINERCONTEXT_H_INCLUDED
104
105 /* vim:set shiftwidth=2 softtabstop=2 expandtab: */
ETONYEK_DEBUG_MSG
#define ETONYEK_DEBUG_MSG(M)
Definition: libetonyek_utils.h:54
libetonyek
Definition: IWORKBezierElement.cpp:20
libetonyek::IWORKContainerContext::m_collector
Collector< Type, std::deque< Type > > m_collector
Definition: IWORKContainerContext.h:98
libetonyek::IWORKXMLContextPtr_t
std::shared_ptr< IWORKXMLContext > IWORKXMLContextPtr_t
Definition: IWORKXMLContext.h:18
libetonyek::IWORKContainerContext::m_elements
std::deque< Type > & m_elements
Definition: IWORKContainerContext.h:97
libetonyek::KEY1Token::dict
Definition: KEY1Token.h:51
IWORKXMLContextBase.h
libetonyek::IWORKContainerContext::Dict_t
std::unordered_map< ID_t, Type > Dict_t
Definition: IWORKContainerContext.h:29
libetonyek::IWORKToken::null
Definition: IWORKToken.h:330
libetonyek::IWORKContainerContext::element
IWORKXMLContextPtr_t element(const int name) override
Definition: IWORKContainerContext.h:51
IWORKTypes_fwd.h
libetonyek::IWORKContainerContext::m_dict
Dict_t *const m_dict
Definition: IWORKContainerContext.h:95
libetonyek::IWORKContainerContext::IWORKContainerContext
IWORKContainerContext(IWORKXMLParserState &state, std::deque< Type > &elements)
Definition: IWORKContainerContext.h:32
libetonyek::IWORKXMLContextBase::getState
State & getState()
Definition: IWORKXMLContextBase.h:43
libetonyek::IWORKToken::NS_URI_SF
Definition: IWORKToken.h:688
name
const char * name
Definition: IWORKToken.cpp:43
libetonyek::IWORKContainerContext::operator=
IWORKContainerContext & operator=(IWORKContainerContext &)
IWORKRefContext.h
libetonyek::IWORKContainerContext::handleRef
void handleRef()
Definition: IWORKContainerContext.h:78
libetonyek::IWORKContainerContext
Definition: IWORKContainerContext.h:27
libetonyek::IWORKXMLContextBase
Definition: IWORKXMLContextBase.h:27
libetonyek::IWORKXMLParserState
Definition: IWORKXMLParserState.h:31
libetonyek::IWORKContainerContext::m_ref
boost::optional< ID_t > m_ref
Definition: IWORKContainerContext.h:96
IWORKToken.h
libetonyek::IWORKContainerContext::IWORKContainerContext
IWORKContainerContext(IWORKXMLParserState &state, Dict_t &dict, std::deque< Type > &elements)
Definition: IWORKContainerContext.h:41
libetonyek::IWORKContainerContext::endOfElement
void endOfElement() override
Definition: IWORKContainerContext.h:69