David Wagner | b76c9d6 | 2014-02-05 18:30:24 +0100 | [diff] [blame] | 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. |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 29 | */ |
| 30 | #pragma once |
| 31 | |
| 32 | #include <string> |
| 33 | #include <vector> |
| 34 | #include <stdint.h> |
Kevin Rocard | 3629936 | 2013-02-04 14:57:47 +0100 | [diff] [blame] | 35 | #include <list> |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 36 | #include "XmlSink.h" |
| 37 | #include "XmlSource.h" |
| 38 | |
| 39 | #include "PathNavigator.h" |
| 40 | |
| 41 | using namespace std; |
| 42 | |
| 43 | class CXmlElementSerializingContext; |
| 44 | class CErrorContext; |
| 45 | |
| 46 | class CElement : public IXmlSink, public IXmlSource |
| 47 | { |
| 48 | friend class CAutoLog; |
| 49 | public: |
| 50 | CElement(const string& strName = ""); |
| 51 | virtual ~CElement(); |
| 52 | |
| 53 | // Logging |
Kevin Rocard | ace81f8 | 2012-12-11 16:19:17 +0100 | [diff] [blame] | 54 | void log_info(const string& strMessage, ...) const; |
| 55 | void log_warning(const string& strMessage, ...) const; |
Kevin Rocard | 3629936 | 2013-02-04 14:57:47 +0100 | [diff] [blame] | 56 | void log_table(bool bIsWarning, const list<string> lstrMessage) const; |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 57 | |
| 58 | // Description |
| 59 | void setDescription(const string& strDescription); |
| 60 | const string& getDescription() const; |
| 61 | |
| 62 | // Name / Path |
| 63 | const string& getName() const; |
Patrick Benavoli | 95ac034 | 2011-11-07 20:32:51 +0100 | [diff] [blame] | 64 | void setName(const string& strName); |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 65 | 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 Benavoli | 2ecf900 | 2011-08-31 11:23:24 +0200 | [diff] [blame] | 102 | // Element properties |
| 103 | virtual void showProperties(string& strResult) const; |
| 104 | |
| 105 | // Conversion utilities |
| 106 | static string toString(uint32_t uiValue); |
Guillaume Denneulin | 9533156 | 2012-09-27 15:13:10 +0200 | [diff] [blame] | 107 | static string toString(uint64_t uiValue); |
Patrick Benavoli | 2ecf900 | 2011-08-31 11:23:24 +0200 | [diff] [blame] | 108 | static string toString(int32_t iValue); |
Patrick Benavoli | ee65e6d | 2011-11-20 18:52:24 +0100 | [diff] [blame] | 109 | static string toString(double dValue); |
Patrick Benavoli | 2ecf900 | 2011-08-31 11:23:24 +0200 | [diff] [blame] | 110 | |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 111 | // Checksum for integrity checks |
| 112 | uint8_t computeStructureChecksum() const; |
| 113 | |
| 114 | // Class kind |
| 115 | virtual string getKind() const = 0; |
| 116 | protected: |
| 117 | // Content dumping |
| 118 | virtual void logValue(string& strValue, CErrorContext& errorContext) const; |
Patrick Benavoli | 6ba361d | 2011-08-31 11:23:24 +0200 | [diff] [blame] | 119 | // Utility to underline |
| 120 | static void appendTitle(string& strTo, const string& strTitle); |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 121 | |
| 122 | // Hierarchy |
| 123 | CElement* getLastChild(); |
| 124 | CElement* getParent(); |
| 125 | CElement* findAscendantOfKind(const string& strKind); |
| 126 | CElement* getRoot(); |
| 127 | const CElement* getRoot() const; |
Guillaume Denneulin | 3ba083e | 2014-01-31 15:09:42 +0100 | [diff] [blame] | 128 | |
| 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 Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 140 | private: |
| 141 | // Logging (done by root) |
Kevin Rocard | ace81f8 | 2012-12-11 16:19:17 +0100 | [diff] [blame] | 142 | virtual void doLog(bool bIsWarning, const string& strLog) const; |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 143 | 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 | }; |