1 /*
2 * Copyright (c) 2011-2015, 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 #include <memory>
38 #include <utility>
39
40 /** Factory that creates an element given an xml element. If no matching builder is found, it uses
41 * the default builder.
42 *
43 * @tparam CDefaultElementBuilder is the class of the element builder to use if no corresponding
44 * builder is found for a given xml element.
45 */
46 template <class CDefaultElementBuilder>
47 class CDefaultElementLibrary : public CElementLibrary
48 {
49 public:
50 virtual ~CDefaultElementLibrary() = default;
51
52 /** Set the default builder used in fallback mechanism.
53 * @see createElement() for more detail on this mechanism.
54 *
55 * @param[in] defaultBuilder if NULL default builder mechanism, else provided builder is used.
56 */
setDefaultBuilder(std::unique_ptr<CDefaultElementBuilder> defaultBuilder)57 void setDefaultBuilder(std::unique_ptr<CDefaultElementBuilder> defaultBuilder)
58 {
59 _defaultBuilder = std::move(defaultBuilder);
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 std::unique_ptr<CDefaultElementBuilder> _defaultBuilder;
76 };
77
78 template <class CDefaultElementBuilder>
createElement(const CXmlElement & xmlElement)79 CElement *CDefaultElementLibrary<CDefaultElementBuilder>::createElement(
80 const CXmlElement &xmlElement) const
81 {
82 CElement *builtElement = CElementLibrary::createElement(xmlElement);
83
84 if (builtElement != NULL) {
85 // The element was created, return it
86 return builtElement;
87 }
88
89 if (_defaultBuilder == nullptr) {
90 // The default builder mechanism is not enabled
91 return NULL;
92 }
93
94 // Use the default builder
95 return _defaultBuilder->createElement(xmlElement);
96 }
97