IWORKContainerContext.h Source File documentation

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
23namespace libetonyek
24{
25
26template<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
31public:
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
50protected:
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
77private:
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 &);
94private:
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: */
IWORKRefContext.h
IWORKToken.h
IWORKTypes_fwd.h
IWORKXMLContextBase.h
libetonyek::IWORKContainerContext
Definition: IWORKContainerContext.h:28
libetonyek::IWORKContainerContext::IWORKContainerContext
IWORKContainerContext(IWORKContainerContext &)
libetonyek::IWORKContainerContext::m_collector
Collector< Type, std::deque< Type > > m_collector
Definition: IWORKContainerContext.h:98
libetonyek::IWORKContainerContext::Dict_t
std::unordered_map< ID_t, Type > Dict_t
Definition: IWORKContainerContext.h:29
libetonyek::IWORKContainerContext::element
IWORKXMLContextPtr_t element(const int name) override
Definition: IWORKContainerContext.h:51
libetonyek::IWORKContainerContext::endOfElement
void endOfElement() override
Definition: IWORKContainerContext.h:69
libetonyek::IWORKContainerContext::m_ref
boost::optional< ID_t > m_ref
Definition: IWORKContainerContext.h:96
libetonyek::IWORKContainerContext::operator=
IWORKContainerContext & operator=(IWORKContainerContext &)
libetonyek::IWORKContainerContext::handleRef
void handleRef()
Definition: IWORKContainerContext.h:78
libetonyek::IWORKContainerContext::IWORKContainerContext
IWORKContainerContext(IWORKXMLParserState &state, std::deque< Type > &elements)
Definition: IWORKContainerContext.h:32
libetonyek::IWORKContainerContext::m_dict
Dict_t *const m_dict
Definition: IWORKContainerContext.h:95
libetonyek::IWORKContainerContext::m_elements
std::deque< Type > & m_elements
Definition: IWORKContainerContext.h:97
libetonyek::IWORKContainerContext::IWORKContainerContext
IWORKContainerContext(IWORKXMLParserState &state, Dict_t &dict, std::deque< Type > &elements)
Definition: IWORKContainerContext.h:41
libetonyek::IWORKXMLContextBase
Definition: IWORKXMLContextBase.h:28
libetonyek::IWORKXMLContextBase::getState
State & getState()
Definition: IWORKXMLContextBase.h:43
libetonyek::IWORKXMLParserState
Definition: IWORKXMLParserState.h:32
ETONYEK_DEBUG_MSG
#define ETONYEK_DEBUG_MSG(M)
Definition: libetonyek_utils.h:54
libetonyek::IWORKToken::null
@ null
Definition: IWORKToken.h:335
libetonyek::IWORKToken::NS_URI_SF
@ NS_URI_SF
Definition: IWORKToken.h:696
libetonyek
Definition: IWORKBezierElement.cpp:21
libetonyek::IWORKXMLContextPtr_t
std::shared_ptr< IWORKXMLContext > IWORKXMLContextPtr_t
Definition: IWORKXMLContext.h:20
libetonyek::get
const ValueT & get(const detail::IWAFieldImpl< TagV, ValueT, Reader > &field)
Definition: IWAField.h:190