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 "parameter_export.h" 33 34 #include <stdint.h> 35 #include <limits> 36 37 #include "TypeElement.h" 38 39 #include <string> 40 41 class CParameterAccessContext; 42 class CConfigurationAccessContext; 43 44 class PARAMETER_EXPORT CParameterType : public CTypeElement 45 { 46 public: 47 CParameterType(const std::string &strName); 48 ~CParameterType() override = default; 49 50 // Size 51 size_t getSize() const; 52 53 // Unit 54 std::string getUnit() const; 55 void setUnit(const std::string &strUnit); 56 57 // From IXmlSink 58 bool fromXml(const CXmlElement &xmlElement, 59 CXmlSerializingContext &serializingContext) override; 60 61 // From IXmlSource 62 void toXml(CXmlElement &xmlElement, CXmlSerializingContext &serializingContext) const override; 63 64 /// Conversions 65 // String 66 virtual bool toBlackboard(const std::string &strValue, uint32_t &uiValue, 67 CParameterAccessContext ¶meterAccessContext) const = 0; 68 virtual bool fromBlackboard(std::string &strValue, const uint32_t &uiValue, 69 CParameterAccessContext ¶meterAccessContext) const = 0; 70 // Boolean 71 virtual bool toBlackboard(bool bUserValue, uint32_t &uiValue, 72 CParameterAccessContext ¶meterAccessContext) const; 73 virtual bool fromBlackboard(bool &bUserValue, uint32_t uiValue, 74 CParameterAccessContext ¶meterAccessContext) const; 75 // Integer 76 virtual bool toBlackboard(uint32_t uiUserValue, uint32_t &uiValue, 77 CParameterAccessContext ¶meterAccessContext) const; 78 virtual bool fromBlackboard(uint32_t &uiUserValue, uint32_t uiValue, 79 CParameterAccessContext ¶meterAccessContext) const; 80 // Signed Integer 81 virtual bool toBlackboard(int32_t iUserValue, uint32_t &uiValue, 82 CParameterAccessContext ¶meterAccessContext) const; 83 virtual bool fromBlackboard(int32_t &iUserValue, uint32_t uiValue, 84 CParameterAccessContext ¶meterAccessContext) const; 85 // Double 86 virtual bool toBlackboard(double dUserValue, uint32_t &uiValue, 87 CParameterAccessContext ¶meterAccessContext) const; 88 virtual bool fromBlackboard(double &dUserValue, uint32_t uiValue, 89 CParameterAccessContext ¶meterAccessContext) const; 90 91 /** Value space handling for settings import/export from/to XML 92 * 93 * During export, this method set the "ValueSpace" attribute of the future 94 * XML element according to the export context. 95 * 96 * During import, this method reads the "ValueSpace" attribute of the XML 97 * element being imported and sets the access context's value space 98 * accordingly. 99 * 100 * @param[in,out] xmlConfigurableElementSettingsElement the element being imported or exported 101 * @param[in,out] configurationAccessContext the import or export context 102 */ 103 virtual void handleValueSpaceAttribute( 104 CXmlElement &xmlConfigurableElementSettingsElement, 105 CConfigurationAccessContext &configurationAccessContext) const; 106 107 // Element properties 108 void showProperties(std::string &strResult) const override; 109 110 // Default value handling (simulation only) 111 virtual uint32_t getDefaultValue() const; 112 113 /** 114 * Sign extension (32 bits) 115 * 116 * @param[in,out] iData the data which will be sign extended 117 */ 118 void signExtend(int32_t &iData) const; 119 120 /** 121 * Sign extension (64 bits) 122 * 123 * @param[in,out] iData the data which will be sign extended 124 */ 125 void signExtend(int64_t &iData) const; 126 127 protected: 128 // Object creation 129 void populate(CElement *pElement) const override; 130 // Size 131 void setSize(size_t size); 132 133 // Check data has no bit set outside available range (based on byte size) and 134 // check data is consistent with available range, with respect to its sign 135 bool isEncodable(uint32_t uiData, bool bIsSigned) const; 136 bool isEncodable(uint64_t uiData, bool bIsSigned) const; 137 // Remove all bits set outside available range 138 uint32_t makeEncodable(uint32_t uiData) const; 139 140 /** Compute max value according to the parameter type */ 141 template <typename type> getMaxValue()142 type getMaxValue() const 143 { 144 return getSize() < sizeof(type) 145 ? (static_cast<type>(1) 146 << (getSize() * std::numeric_limits<unsigned char>::digits - 1)) - 147 1 148 : std::numeric_limits<type>::max(); 149 } 150 151 private: 152 void setXmlUnitAttribute(CXmlElement &xmlElement) const; 153 154 // Instantiation 155 CInstanceConfigurableElement *doInstantiate() const override; 156 // Generic Access 157 template <typename type> 158 void doSignExtend(type &data) const; 159 template <typename type> 160 bool doIsEncodable(type data, bool bIsSigned) const; 161 162 // Size in bytes 163 size_t _size{0}; 164 // Unit 165 std::string _strUnit; 166 167 static const std::string gUnitPropertyName; 168 }; 169