IWORKStyleContainer.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 IWORKSTYLECONTAINER_H_INCLUDED
11 #define IWORKSTYLECONTAINER_H_INCLUDED
12
13 #include <memory>
14 #include <string>
15
16 #include <boost/optional.hpp>
17
18 #include "IWORKStyle_fwd.h"
19 #include "IWORKStyleContext.h"
20 #include "IWORKToken.h"
21 #include "IWORKXMLParserState.h"
22
23 #include "IWORKXMLContextBase.h"
24
25 namespace libetonyek
26 {
27
28 template<int TokenId, int RefTokenId, int TokenId2=0, int RefTokenId2=0>
29 class IWORKStyleContainer : public IWORKXMLElementContextBase
30 {
31 public:
32 IWORKStyleContainer(IWORKXMLParserState &state, IWORKStylePtr_t &style, IWORKStyleMap_t &styleMap);
33 IWORKStyleContainer(IWORKXMLParserState &state, IWORKStylePtr_t &style, IWORKStyleMap_t &styleMap, IWORKStyleMap_t &styleMap2);
34
35 protected:
36 IWORKXMLContextPtr_t element(int name) override;
37 void endOfElement() override;
38
39 private:
40 IWORKStyleContainer(IWORKStyleContainer const &);
41 IWORKStyleContainer &operator=(IWORKStyleContainer const &);
42
43 IWORKStylePtr_t &m_style;
44 IWORKStyleMap_t &m_styleMap;
45 std::shared_ptr<IWORKStyleContext> m_context;
46 boost::optional<ID_t> m_ref;
47 IWORKStyleMap_t *m_styleMap2;
48 boost::optional<ID_t> m_ref2;
49 };
50
51 template<int TokenId, int RefTokenId, int TokenId2, int RefTokenId2>
52 IWORKStyleContainer<TokenId, RefTokenId, TokenId2, RefTokenId2>::IWORKStyleContainer(IWORKXMLParserState &state, IWORKStylePtr_t &style, IWORKStyleMap_t &styleMap)
53 : IWORKXMLElementContextBase(state)
54 , m_style(style)
55 , m_styleMap(styleMap)
56 , m_context()
57 , m_ref()
58 , m_styleMap2(nullptr)
59 , m_ref2()
60 {
61 }
62
63 template<int TokenId, int RefTokenId, int TokenId2, int RefTokenId2>
64 IWORKStyleContainer<TokenId, RefTokenId, TokenId2, RefTokenId2>::IWORKStyleContainer(IWORKXMLParserState &state, IWORKStylePtr_t &style, IWORKStyleMap_t &styleMap, IWORKStyleMap_t &styleMap2)
65 : IWORKXMLElementContextBase(state)
66 , m_style(style)
67 , m_styleMap(styleMap)
68 , m_context()
69 , m_ref()
70 , m_styleMap2(&styleMap2)
71 , m_ref2()
72 {
73 }
74
75 template<int TokenId, int RefTokenId, int TokenId2, int RefTokenId2>
76 IWORKXMLContextPtr_t IWORKStyleContainer<TokenId, RefTokenId, TokenId2, RefTokenId2>::element(const int name)
77 {
78 switch (name)
79 {
80 case TokenId :
81 m_context = std::make_shared<IWORKStyleContext>(getState(), &m_styleMap);
82 return m_context;
83 case RefTokenId :
84 return std::make_shared<IWORKRefContext>(getState(), m_ref);
85 case IWORKToken::NS_URI_SF | IWORKToken::null:
86 return IWORKXMLContextPtr_t();
87 case IWORKToken::INVALID_TOKEN: // TokenId2 and RefTokenId2 are optional, so avoid unintentional match
88 break;
89 default:
90 if (name==TokenId2)
91 {
92 m_context = std::make_shared<IWORKStyleContext>(getState(), m_styleMap2);
93 return m_context;
94 }
95 if (name==RefTokenId2)
96 return std::make_shared<IWORKRefContext>(getState(), m_ref2);
97 break;
98 }
99 ETONYEK_DEBUG_MSG(("IWORKStyleContainer<...>::element: unknown element %d\n", name));
100 return IWORKXMLContextPtr_t();
101 }
102
103 template<int TokenId, int RefTokenId, int TokenId2, int RefTokenId2>
104 void IWORKStyleContainer<TokenId, RefTokenId, TokenId2, RefTokenId2>::endOfElement()
105 {
106 if (m_ref)
107 m_style = getState().getStyleByName(get(m_ref).c_str(), m_styleMap);
108 else if (m_ref2 && m_styleMap2)
109 m_style = getState().getStyleByName(get(m_ref2).c_str(), *m_styleMap2);
110 else if (m_context)
111 m_style=m_context->getStyle();
112 }
113
114 }
115
116 #endif // IWORKSTYLECONTAINER_H_INCLUDED
117
118 /* vim:set shiftwidth=2 softtabstop=2 expandtab: */
IWORKStyle_fwd.h
ETONYEK_DEBUG_MSG
#define ETONYEK_DEBUG_MSG(M)
Definition: libetonyek_utils.h:54
libetonyek
Definition: IWORKBezierElement.cpp:20
libetonyek::IWORKXMLContextPtr_t
std::shared_ptr< IWORKXMLContext > IWORKXMLContextPtr_t
Definition: IWORKXMLContext.h:18
IWORKXMLContextBase.h
libetonyek::IWORKStyleContainer::IWORKStyleContainer
IWORKStyleContainer(IWORKXMLParserState &state, IWORKStylePtr_t &style, IWORKStyleMap_t &styleMap)
Definition: IWORKStyleContainer.h:52
libetonyek::IWORKStyleContainer::endOfElement
void endOfElement() override
Definition: IWORKStyleContainer.h:104
libetonyek::IWORKToken::null
Definition: IWORKToken.h:330
IWORKStyleContext.h
libetonyek::IWORKStylePtr_t
std::shared_ptr< IWORKStyle > IWORKStylePtr_t
Definition: IWORKStyle_fwd.h:21
m_ref
boost::optional< ID_t > m_ref
Definition: IWORKPathElement.cpp:285
libetonyek::IWORKStyleContainer::m_style
IWORKStylePtr_t & m_style
Definition: IWORKStyleContainer.h:43
libetonyek::IWORKStyleContainer::m_ref
boost::optional< ID_t > m_ref
Definition: IWORKStyleContainer.h:46
libetonyek::IWORKStyleContainer::m_styleMap2
IWORKStyleMap_t * m_styleMap2
Definition: IWORKStyleContainer.h:47
IWORKXMLParserState.h
libetonyek::IWORKToken::style
Definition: IWORKToken.h:414
m_context
std::shared_ptr< IWORKXMLContext > m_context
Definition: IWORKPropertyMapElement.cpp:90
libetonyek::IWORKToken::NS_URI_SF
Definition: IWORKToken.h:688
name
const char * name
Definition: IWORKToken.cpp:43
libetonyek::IWORKToken::INVALID_TOKEN
Definition: IWORKToken.h:27
libetonyek::IWORKXMLContextBase
Definition: IWORKXMLContextBase.h:27
libetonyek::IWORKXMLParserState
Definition: IWORKXMLParserState.h:31
libetonyek::IWORKStyleContainer
Definition: IWORKStyleContainer.h:29
IWORKToken.h
libetonyek::IWORKStyleContainer::operator=
IWORKStyleContainer & operator=(IWORKStyleContainer const &)
libetonyek::IWORKStyleContainer::m_context
std::shared_ptr< IWORKStyleContext > m_context
Definition: IWORKStyleContainer.h:45
libetonyek::IWORKStyleContainer::element
IWORKXMLContextPtr_t element(int name) override
Definition: IWORKStyleContainer.h:76
libetonyek::IWORKStyleContainer::m_ref2
boost::optional< ID_t > m_ref2
Definition: IWORKStyleContainer.h:48
libetonyek::IWORKStyleContainer::m_styleMap
IWORKStyleMap_t & m_styleMap
Definition: IWORKStyleContainer.h:44
libetonyek::IWORKStyleMap_t
std::unordered_map< ID_t, IWORKStylePtr_t > IWORKStyleMap_t
Definition: IWORKStyle_fwd.h:24
m_style
IWORKStylePtr_t m_style
Definition: IWORKPropertyMapElement.cpp:172