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