blob: 6f5e74fe13215b6108efed9480face42e7ca938f [file] [log] [blame]
David Wagnerb76c9d62014-02-05 18:30:24 +01001/*
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.
Patrick Benavoli68a91282011-08-31 11:23:23 +020029 */
30#pragma once
31
32#include <string>
33#include <vector>
34#include <stdint.h>
Kevin Rocard36299362013-02-04 14:57:47 +010035#include <list>
Patrick Benavoli68a91282011-08-31 11:23:23 +020036#include "XmlSink.h"
37#include "XmlSource.h"
38
39#include "PathNavigator.h"
40
41using namespace std;
42
43class CXmlElementSerializingContext;
44class CErrorContext;
45
46class CElement : public IXmlSink, public IXmlSource
47{
48 friend class CAutoLog;
49public:
50 CElement(const string& strName = "");
51 virtual ~CElement();
52
53 // Logging
Kevin Rocardace81f82012-12-11 16:19:17 +010054 void log_info(const string& strMessage, ...) const;
55 void log_warning(const string& strMessage, ...) const;
Kevin Rocard36299362013-02-04 14:57:47 +010056 void log_table(bool bIsWarning, const list<string> lstrMessage) const;
Patrick Benavoli68a91282011-08-31 11:23:23 +020057
58 // Description
59 void setDescription(const string& strDescription);
60 const string& getDescription() const;
61
62 // Name / Path
63 const string& getName() const;
Patrick Benavoli95ac0342011-11-07 20:32:51 +010064 void setName(const string& strName);
Patrick Benavoli68a91282011-08-31 11:23:23 +020065 bool rename(const string& strName, string& strError);
66 string getPath() const;
67 string getQualifiedPath() const;
68
69 // Creation / build
70 virtual bool init(string& strError);
71 virtual void clean();
72
73 // Children management
74 void addChild(CElement* pChild);
75 bool removeChild(CElement* pChild);
76 void listChildren(string& strChildList) const;
77 string listQualifiedPaths(bool bDive, uint32_t uiLevel = 0) const;
78 void listChildrenPaths(string& strChildPathList) const;
79
80 // Hierarchy query
81 uint32_t getNbChildren() const;
82 CElement* findChildOfKind(const string& strKind);
83 const CElement* findChildOfKind(const string& strKind) const;
84 const CElement* getParent() const;
85 const CElement* getChild(uint32_t uiIndex) const;
86 CElement* getChild(uint32_t uiIndex);
87 const CElement* findChild(const string& strName) const;
88 CElement* findChild(const string& strName);
89 const CElement* findDescendant(CPathNavigator& pathNavigator) const;
90 CElement* findDescendant(CPathNavigator& pathNavigator);
91 bool isDescendantOf(const CElement* pCandidateAscendant) const;
92
93 // From IXmlSink
94 virtual bool fromXml(const CXmlElement& xmlElement, CXmlSerializingContext& serializingContext);
95
96 // From IXmlSource
97 virtual void toXml(CXmlElement& xmlElement, CXmlSerializingContext& serializingContext) const;
98
99 // Content structure dump
100 void dumpContent(string& strContent, CErrorContext& errorContext, const uint32_t uiDepth = 0) const;
101
Patrick Benavoli2ecf9002011-08-31 11:23:24 +0200102 // Element properties
103 virtual void showProperties(string& strResult) const;
104
105 // Conversion utilities
106 static string toString(uint32_t uiValue);
Guillaume Denneulin95331562012-09-27 15:13:10 +0200107 static string toString(uint64_t uiValue);
Patrick Benavoli2ecf9002011-08-31 11:23:24 +0200108 static string toString(int32_t iValue);
Patrick Benavoliee65e6d2011-11-20 18:52:24 +0100109 static string toString(double dValue);
Patrick Benavoli2ecf9002011-08-31 11:23:24 +0200110
Patrick Benavoli68a91282011-08-31 11:23:23 +0200111 // Checksum for integrity checks
112 uint8_t computeStructureChecksum() const;
113
114 // Class kind
115 virtual string getKind() const = 0;
116protected:
117 // Content dumping
118 virtual void logValue(string& strValue, CErrorContext& errorContext) const;
Patrick Benavoli6ba361d2011-08-31 11:23:24 +0200119 // Utility to underline
120 static void appendTitle(string& strTo, const string& strTitle);
Patrick Benavoli68a91282011-08-31 11:23:23 +0200121
122 // Hierarchy
123 CElement* getLastChild();
124 CElement* getParent();
125 CElement* findAscendantOfKind(const string& strKind);
126 CElement* getRoot();
127 const CElement* getRoot() const;
Guillaume Denneulin3ba083e2014-01-31 15:09:42 +0100128
129 /**
130 * Creates a child CElement from a child XML Element
131 *
132 * @param[in] childElement the XML element to create CElement from
133 * @param[in] elementSerializingContext the serializing context
134 *
135 * @return child a pointer on the CElement object that has been added to the tree
136 */
137 CElement* createChild(const CXmlElement& childElement,
138 CXmlSerializingContext& elementSerializingContext);
139
Patrick Benavoli68a91282011-08-31 11:23:23 +0200140private:
141 // Logging (done by root)
Kevin Rocardace81f82012-12-11 16:19:17 +0100142 virtual void doLog(bool bIsWarning, const string& strLog) const;
Patrick Benavoli68a91282011-08-31 11:23:23 +0200143 virtual void nestLog() const;
144 virtual void unnestLog() const;
145 // Returns Name or Kind if no Name
146 string getPathName() const;
147 // Returns true if children dynamic creation is to be dealt with
148 virtual bool childrenAreDynamic() const;
149 // House keeping
150 void removeChildren();
151 // For logging
152 uint32_t getDepth() const;
153 // Fill XmlElement during XML composing
154 void setXmlNameAttribute(CXmlElement& xmlElement) const;
155
156 // Name
157 string _strName;
158
159 // Description
160 string _strDescription;
161
162 // Child iterators
163 typedef vector<CElement*>::iterator ChildArrayIterator;
164 typedef vector<CElement*>::reverse_iterator ChildArrayReverseIterator;
165 // Children
166 vector<CElement*> _childArray;
167 // Parent
168 CElement* _pParent;
169};