1 /*
2 * Copyright (c) 2011-2014, Intel Corporation
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation and/or
13 * other materials provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its contributors
16 * may be used to endorse or promote products derived from this software without
17 * specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30 #pragma once
31
32 #include "ElementLibrary.h"
33 #include "ElementBuilder.h"
34
35 #include <map>
36 #include <string>
37
38 /** Factory that creates an element given an xml element. If no matching builder is found, it uses
39 * the default builder.
40 *
41 * @tparam CDefaultElementBuilder is the class of the element builder to use if no corresponding
42 * builder is found for a given xml element.
43 */
44 template<class CDefaultElementBuilder>
45 class CDefaultElementLibrary: public CElementLibrary
46 {
47 public:
48
49 explicit CDefaultElementLibrary(bool bEnableDefaultMechanism = true);
~CDefaultElementLibrary()50 virtual ~CDefaultElementLibrary() {}
51
52 /** Enable the default builder fallback mechanism.
53 * @see createElement() for more detail on this mechanism.
54 *
55 * @param[in] bEnable if true/false, activate/deactivate the default builder mechanism.
56 */
enableDefaultMechanism(bool bEnable)57 void enableDefaultMechanism(bool bEnable) {
58 _bEnableDefaultMechanism = bEnable;
59 }
60
61
62 /** Create and return an element instanciated depending on an xmlElement.
63 *
64 * @param[in] xmlElement: The xml element used to find a matching builder
65 *
66 * @return If a matching builder is found, return an element created from the builder,
67 * otherwise:
68 * If the default mechanism is enable (@see enableDefaultMechanism),
69 * create the elemen with the default element builder.
70 * otherwise, return NULL.
71 */
72 CElement* createElement(const CXmlElement& xmlElement) const;
73
74 private:
75 bool _bEnableDefaultMechanism;
76 CDefaultElementBuilder _DefaultElementBuilder;
77 };
78
79 template<class CDefaultElementBuilder>
CDefaultElementLibrary(bool bEnableDefaultMechanism)80 CDefaultElementLibrary<CDefaultElementBuilder>::CDefaultElementLibrary(bool bEnableDefaultMechanism) :
81 _bEnableDefaultMechanism(bEnableDefaultMechanism),
82 _DefaultElementBuilder() {}
83
84 template<class CDefaultElementBuilder>
createElement(const CXmlElement & xmlElement)85 CElement* CDefaultElementLibrary<CDefaultElementBuilder>::createElement(const CXmlElement& xmlElement) const
86 {
87 CElement* builtElement = CElementLibrary::createElement(xmlElement);
88
89 if (builtElement != NULL) {
90 // The element was created, return it
91 return builtElement;
92 }
93
94 if (!_bEnableDefaultMechanism) {
95 // The default builder mechanism is not enabled
96 return NULL;
97 }
98
99 // Use the default builder
100 return _DefaultElementBuilder.createElement(xmlElement);
101 }
102
103