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 "ParameterType.h"
33 
34 #include <string>
35 
36 class CFixedPointParameterType : public CParameterType
37 {
38 public:
39     CFixedPointParameterType(const std::string &strName);
40 
41     // From IXmlSink
42     virtual bool fromXml(const CXmlElement &xmlElement, CXmlSerializingContext &serializingContext);
43 
44     // From IXmlSource
45     virtual void toXml(CXmlElement &xmlElement, CXmlSerializingContext &serializingContext) const;
46 
47     // XML Serialization value space handling
48     // Value space handling for configuration import
49     virtual void handleValueSpaceAttribute(
50         CXmlElement &xmlConfigurableElementSettingsElement,
51         CConfigurationAccessContext &configurationAccessContext) const;
52 
53     /// Conversion
54     // String
55     virtual bool toBlackboard(const std::string &strValue, uint32_t &uiValue,
56                               CParameterAccessContext &parameterAccessContext) const;
57     virtual bool fromBlackboard(std::string &strValue, const uint32_t &uiValue,
58                                 CParameterAccessContext &parameterAccessContext) const;
59     // Double
60     virtual bool toBlackboard(double dUserValue, uint32_t &uiValue,
61                               CParameterAccessContext &parameterAccessContext) const;
62     virtual bool fromBlackboard(double &dUserValue, uint32_t uiValue,
63                                 CParameterAccessContext &parameterAccessContext) const;
64 
65     // Element properties
66     virtual void showProperties(std::string &strResult) const;
67 
68     // CElement
69     virtual std::string getKind() const;
70 
71 private:
72     // Util size
73     size_t getUtilSizeInBits() const;
74     // Range computation
75     void getRange(double &dMin, double &dMax) const;
76 
77     /**
78      * Convert a decimal raw represented string into an unsigned long integer.
79      * In case of a failing conversion or encodability, this function set the error to
80      * illegal value provided and gives the range allowed for the parameter.
81      *
82      * @param[in] strValue Parameter read from the XML file representated as a string in decimal
83      *                     raw format
84      * @param[out] uiValue Parameter representated as a long unsigned integer.
85      * @param[in,out] parameterAccessContext Parameter access context.
86      *
87      * @return true if the string was successfully converted, false otherwise.
88      */
89     bool convertFromDecimal(const std::string &strValue, uint32_t &uiValue,
90                             CParameterAccessContext &parameterAccessContext) const;
91 
92     /**
93      * Convert an hexadecimal raw represented string into an unsigned long integer.
94      * In case of a failing conversion or encodability, this function set the error to
95      * illegal value provided and gives the range allowed for the parameter.
96      *
97      * @param[in] strValue Parameter read from the XML file representated as a string in hexadecimal
98      *                     raw format
99      * @param[out] uiValue Parameter representated as a long unsigned integer.
100      * @param[in,out] parameterAccessContext Parameter access context.
101      *
102      * @return true if the string was successfully converted, false otherwise.
103      */
104     bool convertFromHexadecimal(const std::string &strValue, uint32_t &uiValue,
105                                 CParameterAccessContext &parameterAccessContext) const;
106 
107     /**
108      * Convert a Qn.m represented string into an unsigned long integer.
109      * In case of a failing conversion or encodability, this function set the error to
110      * illegal value provided and gives the range allowed for the parameter.
111      *
112      * @param[in] strValue Parameter read from the XML file representated as a string in Qn.m
113      *                     representation.
114      * @param[out] uiValue Parameter representated as a long unsigned integer.
115      * @param[in,out] parameterAccessContext Parameter access context.
116      *
117      * @return true if the string was successfully converted, false otherwise.
118      */
119     bool convertFromQnm(const std::string &strValue, uint32_t &uiValue,
120                         CParameterAccessContext &parameterAccessContext) const;
121 
122     /**
123      * Set the out of range error.
124      * In case of a failing conversion or encodability, this function set the error to
125      * illegal value provided and gives the range allowed for the parameter.
126      *
127      * @param[in] strValue Parameter read from the XML file representated as a string
128      * @param[in,out] parameterAccessContext Parameter Access Context
129      */
130     void setOutOfRangeError(const std::string &strValue,
131                             CParameterAccessContext &parameterAccessContext) const;
132 
133     // Check if data is encodable
134     bool checkValueAgainstRange(double dValue) const;
135 
136     /**
137      * Convert a double towards a Qn.m representation which is stored in binary format.
138      * This value is rounded if the double is not encodable in the corresponding Qn.m format.
139      *
140      * @param[in] dValue the double which should be converted.
141      *
142      * @return the integer which contains the converted Qn.m number.
143      */
144     int32_t doubleToBinaryQnm(double dValue) const;
145 
146     /**
147      * Convert a Qn.m binary number towards its double representation.
148      *
149      * @param[in] iValue the integer which contains the Qn.m number which should be converted.
150      *
151      * @return the double which contains the double representation of iValue.
152      */
153     double binaryQnmToDouble(int32_t iValue) const;
154 
155     // Integral part in Q notation
156     uint32_t _uiIntegral{0};
157     // Fractional part in Q notation
158     uint32_t _uiFractional{0};
159 };
160