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