xref: /aosp_15_r20/external/parameter-framework/upstream/parameter/Element.h (revision c33452fb792a5495ec310a9626f2638b053af5dd)
1*c33452fbSAndroid Build Coastguard Worker /*
2*c33452fbSAndroid Build Coastguard Worker  * Copyright (c) 2011-2015, Intel Corporation
3*c33452fbSAndroid Build Coastguard Worker  * All rights reserved.
4*c33452fbSAndroid Build Coastguard Worker  *
5*c33452fbSAndroid Build Coastguard Worker  * Redistribution and use in source and binary forms, with or without modification,
6*c33452fbSAndroid Build Coastguard Worker  * are permitted provided that the following conditions are met:
7*c33452fbSAndroid Build Coastguard Worker  *
8*c33452fbSAndroid Build Coastguard Worker  * 1. Redistributions of source code must retain the above copyright notice, this
9*c33452fbSAndroid Build Coastguard Worker  * list of conditions and the following disclaimer.
10*c33452fbSAndroid Build Coastguard Worker  *
11*c33452fbSAndroid Build Coastguard Worker  * 2. Redistributions in binary form must reproduce the above copyright notice,
12*c33452fbSAndroid Build Coastguard Worker  * this list of conditions and the following disclaimer in the documentation and/or
13*c33452fbSAndroid Build Coastguard Worker  * other materials provided with the distribution.
14*c33452fbSAndroid Build Coastguard Worker  *
15*c33452fbSAndroid Build Coastguard Worker  * 3. Neither the name of the copyright holder nor the names of its contributors
16*c33452fbSAndroid Build Coastguard Worker  * may be used to endorse or promote products derived from this software without
17*c33452fbSAndroid Build Coastguard Worker  * specific prior written permission.
18*c33452fbSAndroid Build Coastguard Worker  *
19*c33452fbSAndroid Build Coastguard Worker  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20*c33452fbSAndroid Build Coastguard Worker  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21*c33452fbSAndroid Build Coastguard Worker  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22*c33452fbSAndroid Build Coastguard Worker  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23*c33452fbSAndroid Build Coastguard Worker  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24*c33452fbSAndroid Build Coastguard Worker  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25*c33452fbSAndroid Build Coastguard Worker  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26*c33452fbSAndroid Build Coastguard Worker  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27*c33452fbSAndroid Build Coastguard Worker  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28*c33452fbSAndroid Build Coastguard Worker  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29*c33452fbSAndroid Build Coastguard Worker  */
30*c33452fbSAndroid Build Coastguard Worker #pragma once
31*c33452fbSAndroid Build Coastguard Worker 
32*c33452fbSAndroid Build Coastguard Worker #include "parameter_export.h"
33*c33452fbSAndroid Build Coastguard Worker 
34*c33452fbSAndroid Build Coastguard Worker #include <string>
35*c33452fbSAndroid Build Coastguard Worker #include <vector>
36*c33452fbSAndroid Build Coastguard Worker #include <stdint.h>
37*c33452fbSAndroid Build Coastguard Worker #include "XmlSink.h"
38*c33452fbSAndroid Build Coastguard Worker #include "XmlSource.h"
39*c33452fbSAndroid Build Coastguard Worker 
40*c33452fbSAndroid Build Coastguard Worker #include "PathNavigator.h"
41*c33452fbSAndroid Build Coastguard Worker 
42*c33452fbSAndroid Build Coastguard Worker class CXmlElementSerializingContext;
43*c33452fbSAndroid Build Coastguard Worker namespace utility
44*c33452fbSAndroid Build Coastguard Worker {
45*c33452fbSAndroid Build Coastguard Worker class ErrorContext;
46*c33452fbSAndroid Build Coastguard Worker } // namespace utility
47*c33452fbSAndroid Build Coastguard Worker 
48*c33452fbSAndroid Build Coastguard Worker class PARAMETER_EXPORT CElement : public IXmlSink, public IXmlSource
49*c33452fbSAndroid Build Coastguard Worker {
50*c33452fbSAndroid Build Coastguard Worker public:
51*c33452fbSAndroid Build Coastguard Worker     CElement(const std::string &strName = "");
52*c33452fbSAndroid Build Coastguard Worker     ~CElement() override;
53*c33452fbSAndroid Build Coastguard Worker 
54*c33452fbSAndroid Build Coastguard Worker     // Description
55*c33452fbSAndroid Build Coastguard Worker     void setDescription(const std::string &strDescription);
56*c33452fbSAndroid Build Coastguard Worker     const std::string &getDescription() const;
57*c33452fbSAndroid Build Coastguard Worker 
58*c33452fbSAndroid Build Coastguard Worker     // Name / Path
59*c33452fbSAndroid Build Coastguard Worker     const std::string &getName() const;
60*c33452fbSAndroid Build Coastguard Worker     void setName(const std::string &strName);
61*c33452fbSAndroid Build Coastguard Worker     bool rename(const std::string &strName, std::string &strError);
62*c33452fbSAndroid Build Coastguard Worker     std::string getPath() const;
63*c33452fbSAndroid Build Coastguard Worker     std::string getQualifiedPath() const;
64*c33452fbSAndroid Build Coastguard Worker 
65*c33452fbSAndroid Build Coastguard Worker     // Creation / build
66*c33452fbSAndroid Build Coastguard Worker     virtual bool init(std::string &strError);
67*c33452fbSAndroid Build Coastguard Worker     virtual void clean();
68*c33452fbSAndroid Build Coastguard Worker 
69*c33452fbSAndroid Build Coastguard Worker     // Children management
70*c33452fbSAndroid Build Coastguard Worker     void addChild(CElement *pChild);
71*c33452fbSAndroid Build Coastguard Worker     bool removeChild(CElement *pChild);
72*c33452fbSAndroid Build Coastguard Worker     void listChildren(std::string &strChildList) const;
73*c33452fbSAndroid Build Coastguard Worker     std::string listQualifiedPaths(bool bDive, size_t level = 0) const;
74*c33452fbSAndroid Build Coastguard Worker     void listChildrenPaths(std::string &strChildPathList) const;
75*c33452fbSAndroid Build Coastguard Worker 
76*c33452fbSAndroid Build Coastguard Worker     // Hierarchy query
77*c33452fbSAndroid Build Coastguard Worker     size_t getNbChildren() const;
78*c33452fbSAndroid Build Coastguard Worker     CElement *findChildOfKind(const std::string &strKind);
79*c33452fbSAndroid Build Coastguard Worker     const CElement *findChildOfKind(const std::string &strKind) const;
80*c33452fbSAndroid Build Coastguard Worker     const CElement *getParent() const;
81*c33452fbSAndroid Build Coastguard Worker 
82*c33452fbSAndroid Build Coastguard Worker     /**
83*c33452fbSAndroid Build Coastguard Worker      * Get a child element (const)
84*c33452fbSAndroid Build Coastguard Worker      *
85*c33452fbSAndroid Build Coastguard Worker      * Note: this method will assert if given a wrong child index (>= number of children)
86*c33452fbSAndroid Build Coastguard Worker      *
87*c33452fbSAndroid Build Coastguard Worker      * @param[in] index the index of the child element from 0 to number of children - 1
88*c33452fbSAndroid Build Coastguard Worker      * @return the child element
89*c33452fbSAndroid Build Coastguard Worker      */
90*c33452fbSAndroid Build Coastguard Worker     const CElement *getChild(size_t index) const;
91*c33452fbSAndroid Build Coastguard Worker 
92*c33452fbSAndroid Build Coastguard Worker     /**
93*c33452fbSAndroid Build Coastguard Worker      * Get a child element
94*c33452fbSAndroid Build Coastguard Worker      *
95*c33452fbSAndroid Build Coastguard Worker      * Note: this method will assert if given a wrong child index (>= number of children)
96*c33452fbSAndroid Build Coastguard Worker      *
97*c33452fbSAndroid Build Coastguard Worker      * @param[in] index the index of the child element from 0 to number of children - 1
98*c33452fbSAndroid Build Coastguard Worker      * @return the child element
99*c33452fbSAndroid Build Coastguard Worker      */
100*c33452fbSAndroid Build Coastguard Worker     CElement *getChild(size_t index);
101*c33452fbSAndroid Build Coastguard Worker 
102*c33452fbSAndroid Build Coastguard Worker     const CElement *findChild(const std::string &strName) const;
103*c33452fbSAndroid Build Coastguard Worker     CElement *findChild(const std::string &strName);
104*c33452fbSAndroid Build Coastguard Worker     const CElement *findDescendant(CPathNavigator &pathNavigator) const;
105*c33452fbSAndroid Build Coastguard Worker     CElement *findDescendant(CPathNavigator &pathNavigator);
106*c33452fbSAndroid Build Coastguard Worker     bool isDescendantOf(const CElement *pCandidateAscendant) const;
107*c33452fbSAndroid Build Coastguard Worker 
108*c33452fbSAndroid Build Coastguard Worker     // From IXmlSink
109*c33452fbSAndroid Build Coastguard Worker     bool fromXml(const CXmlElement &xmlElement,
110*c33452fbSAndroid Build Coastguard Worker                  CXmlSerializingContext &serializingContext) override;
111*c33452fbSAndroid Build Coastguard Worker 
112*c33452fbSAndroid Build Coastguard Worker     // From IXmlSource
113*c33452fbSAndroid Build Coastguard Worker     void toXml(CXmlElement &xmlElement, CXmlSerializingContext &serializingContext) const override;
114*c33452fbSAndroid Build Coastguard Worker 
115*c33452fbSAndroid Build Coastguard Worker     /**
116*c33452fbSAndroid Build Coastguard Worker      * Serialize the children to XML
117*c33452fbSAndroid Build Coastguard Worker      *
118*c33452fbSAndroid Build Coastguard Worker      * This method is virtual, to be derived in case a special treatment is
119*c33452fbSAndroid Build Coastguard Worker      * needed before doing so.
120*c33452fbSAndroid Build Coastguard Worker      *
121*c33452fbSAndroid Build Coastguard Worker      * @param[in,out] xmlElement the XML Element below which the children must
122*c33452fbSAndroid Build Coastguard Worker      *                be serialized (which may or may not be the CElement
123*c33452fbSAndroid Build Coastguard Worker      *                object upon which this method is called)
124*c33452fbSAndroid Build Coastguard Worker      * @param[in,out] serializingContext information about the serialization
125*c33452fbSAndroid Build Coastguard Worker      */
126*c33452fbSAndroid Build Coastguard Worker     virtual void childrenToXml(CXmlElement &xmlElement,
127*c33452fbSAndroid Build Coastguard Worker                                CXmlSerializingContext &serializingContext) const;
128*c33452fbSAndroid Build Coastguard Worker 
129*c33452fbSAndroid Build Coastguard Worker     // Content structure dump
130*c33452fbSAndroid Build Coastguard Worker     std::string dumpContent(utility::ErrorContext &errorContext, const size_t depth = 0) const;
131*c33452fbSAndroid Build Coastguard Worker 
132*c33452fbSAndroid Build Coastguard Worker     // Element properties
133*c33452fbSAndroid Build Coastguard Worker     virtual void showProperties(std::string &strResult) const;
134*c33452fbSAndroid Build Coastguard Worker 
135*c33452fbSAndroid Build Coastguard Worker     // Class kind
136*c33452fbSAndroid Build Coastguard Worker     virtual std::string getKind() const = 0;
137*c33452fbSAndroid Build Coastguard Worker 
138*c33452fbSAndroid Build Coastguard Worker     /**
139*c33452fbSAndroid Build Coastguard Worker      * Fill the Description field of the Xml Element during XML composing.
140*c33452fbSAndroid Build Coastguard Worker      *
141*c33452fbSAndroid Build Coastguard Worker      * @param[in,out] xmlElement to fill with the description
142*c33452fbSAndroid Build Coastguard Worker      */
143*c33452fbSAndroid Build Coastguard Worker     void setXmlDescriptionAttribute(CXmlElement &xmlElement) const;
144*c33452fbSAndroid Build Coastguard Worker 
145*c33452fbSAndroid Build Coastguard Worker     /**
146*c33452fbSAndroid Build Coastguard Worker      * Appends if found human readable description property.
147*c33452fbSAndroid Build Coastguard Worker      *
148*c33452fbSAndroid Build Coastguard Worker      * @param[out] strResult in which the description is wished to be appended.
149*c33452fbSAndroid Build Coastguard Worker      */
150*c33452fbSAndroid Build Coastguard Worker     void showDescriptionProperty(std::string &strResult) const;
151*c33452fbSAndroid Build Coastguard Worker 
152*c33452fbSAndroid Build Coastguard Worker     /**
153*c33452fbSAndroid Build Coastguard Worker      * Returns Xml element name used for element XML importing/exporting functionalities
154*c33452fbSAndroid Build Coastguard Worker      */
155*c33452fbSAndroid Build Coastguard Worker     virtual std::string getXmlElementName() const;
156*c33452fbSAndroid Build Coastguard Worker 
157*c33452fbSAndroid Build Coastguard Worker protected:
158*c33452fbSAndroid Build Coastguard Worker     // Content dumping
159*c33452fbSAndroid Build Coastguard Worker     virtual std::string logValue(utility::ErrorContext &errorContext) const;
160*c33452fbSAndroid Build Coastguard Worker 
161*c33452fbSAndroid Build Coastguard Worker     // Hierarchy
162*c33452fbSAndroid Build Coastguard Worker     CElement *getParent();
163*c33452fbSAndroid Build Coastguard Worker 
164*c33452fbSAndroid Build Coastguard Worker     /**
165*c33452fbSAndroid Build Coastguard Worker      * Creates a child CElement from a child XML Element
166*c33452fbSAndroid Build Coastguard Worker      *
167*c33452fbSAndroid Build Coastguard Worker      * @param[in] childElement the XML element to create CElement from
168*c33452fbSAndroid Build Coastguard Worker      * @param[in] elementSerializingContext the serializing context
169*c33452fbSAndroid Build Coastguard Worker      *
170*c33452fbSAndroid Build Coastguard Worker      * @return child a pointer on the CElement object that has been added to the tree
171*c33452fbSAndroid Build Coastguard Worker      */
172*c33452fbSAndroid Build Coastguard Worker     CElement *createChild(const CXmlElement &childElement,
173*c33452fbSAndroid Build Coastguard Worker                           CXmlSerializingContext &elementSerializingContext);
174*c33452fbSAndroid Build Coastguard Worker 
175*c33452fbSAndroid Build Coastguard Worker     static const std::string gDescriptionPropertyName;
176*c33452fbSAndroid Build Coastguard Worker 
177*c33452fbSAndroid Build Coastguard Worker private:
178*c33452fbSAndroid Build Coastguard Worker     // Returns Name or Kind if no Name
179*c33452fbSAndroid Build Coastguard Worker     std::string getPathName() const;
180*c33452fbSAndroid Build Coastguard Worker     // Returns true if children dynamic creation is to be dealt with
181*c33452fbSAndroid Build Coastguard Worker     virtual bool childrenAreDynamic() const;
182*c33452fbSAndroid Build Coastguard Worker     // House keeping
183*c33452fbSAndroid Build Coastguard Worker     void removeChildren();
184*c33452fbSAndroid Build Coastguard Worker     // Fill XmlElement during XML composing
185*c33452fbSAndroid Build Coastguard Worker     void setXmlNameAttribute(CXmlElement &xmlElement) const;
186*c33452fbSAndroid Build Coastguard Worker 
187*c33452fbSAndroid Build Coastguard Worker     // Name
188*c33452fbSAndroid Build Coastguard Worker     std::string _strName;
189*c33452fbSAndroid Build Coastguard Worker 
190*c33452fbSAndroid Build Coastguard Worker     // Description
191*c33452fbSAndroid Build Coastguard Worker     std::string _strDescription;
192*c33452fbSAndroid Build Coastguard Worker 
193*c33452fbSAndroid Build Coastguard Worker     // Child iterators
194*c33452fbSAndroid Build Coastguard Worker     typedef std::vector<CElement *>::iterator ChildArrayIterator;
195*c33452fbSAndroid Build Coastguard Worker     typedef std::vector<CElement *>::reverse_iterator ChildArrayReverseIterator;
196*c33452fbSAndroid Build Coastguard Worker     // Children
197*c33452fbSAndroid Build Coastguard Worker     std::vector<CElement *> _childArray;
198*c33452fbSAndroid Build Coastguard Worker     // Parent
199*c33452fbSAndroid Build Coastguard Worker     CElement *_pParent{nullptr};
200*c33452fbSAndroid Build Coastguard Worker };
201