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>
28 {
29  typedef std::unordered_map<ID_t, Type> Dict_t;
30 
31 public:
32  IWORKContainerContext(IWORKXMLParserState &state, std::deque<Type> &elements)
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)
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  }
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: */
#define ETONYEK_DEBUG_MSG(M)
Definition: libetonyek_utils.h:54
Collector< Type, std::deque< Type > > m_collector
Definition: IWORKContainerContext.h:98
std::shared_ptr< IWORKXMLContext > IWORKXMLContextPtr_t
Definition: IWORKXMLContext.h:18
std::deque< Type > & m_elements
Definition: IWORKContainerContext.h:97
Definition: KEY1Token.h:51
std::unordered_map< ID_t, Type > Dict_t
Definition: IWORKContainerContext.h:29
Definition: IWORKToken.h:335
IWORKXMLContextPtr_t element(const int name) override
Definition: IWORKContainerContext.h:51
Dict_t *const m_dict
Definition: IWORKContainerContext.h:95
IWORKContainerContext(IWORKXMLParserState &state, std::deque< Type > &elements)
Definition: IWORKContainerContext.h:32
State & getState()
Definition: IWORKXMLContextBase.h:43
Definition: IWORKToken.h:696
const char * name
Definition: IWORKToken.cpp:43
IWORKContainerContext & operator=(IWORKContainerContext &)
void handleRef()
Definition: IWORKContainerContext.h:78
Definition: IWORKContainerContext.h:27
Definition: IWORKXMLContextBase.h:27
Definition: IWORKXMLParserState.h:31
boost::optional< ID_t > m_ref
Definition: IWORKContainerContext.h:96
IWORKContainerContext(IWORKXMLParserState &state, Dict_t &dict, std::deque< Type > &elements)
Definition: IWORKContainerContext.h:41
void endOfElement() override
Definition: IWORKContainerContext.h:69

Generated for libetonyek by doxygen 1.8.6