• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "ParameterFramework.hpp"
33 #include "FailureWrapper.hpp"
34 
35 #include <ElementHandle.h>
36 
37 namespace parameterFramework
38 {
39 /** Wrapper around ::ElementHandle to throw exceptions on errors and have more
40  * user friendly methods.
41  * Contrary to ::ElementHandle, is constructed through it's constructor
42  * and not a factory method.
43  * @see parameterFramework::ParameterFramework for the main PF interface.
44  */
45 class ElementHandle : private FailureWrapper<::ElementHandle>
46 {
47     ElementHandle(const ElementHandle &other) = delete;
48     ElementHandle &operator=(const ElementHandle &other) = delete;
49 
50 private:
51     using EH = ::ElementHandle;
52 
53 public:
ElementHandle(ParameterFramework & pf,const std::string & path)54     ElementHandle(ParameterFramework &pf, const std::string &path)
55         : FailureWrapper(pf.createElementHandle(path))
56     {
57     }
58 
59     /** Wrap EH::getSize.
60      *
61      * @note: can not use `using EH::getSize` as getSize has private overloads in EH.
62      */
getSize() const63     size_t getSize() const { return EH::getSize(); }
64 
getMappingData(const std::string & key)65     std::string getMappingData(const std::string &key)
66     {
67         std::string value;
68         if (not EH::getMappingData(key, value)) {
69             throw Exception("Could not find mapping key \"" + key + "\" in " + EH::getPath());
70         }
71         return value;
72     }
73 
74     /** Wrap EH::setAsDouble to throw an exception on failure. */
setAsDouble(double value)75     void setAsDouble(double value) { mayFailCall(&EH::setAsDouble, value); }
76     /** Wrap EH::getAsDouble to throw an exception on failure. */
getAsDouble(double & value) const77     void getAsDouble(double &value) const { mayFailCall(&EH::getAsDouble, value); }
78 
setAsBoolean(bool value)79     void setAsBoolean(bool value) { mayFailCall(&EH::setAsBoolean, value); }
getAsBoolean(bool & value) const80     void getAsBoolean(bool &value) const { mayFailCall(&EH::getAsBoolean, value); }
81 
setAsInteger(uint32_t value)82     void setAsInteger(uint32_t value) { mayFailCall(&EH::setAsInteger, value); }
getAsInteger(uint32_t & value) const83     void getAsInteger(uint32_t &value) const { mayFailCall(&EH::getAsInteger, value); }
setAsIntegerArray(const std::vector<uint32_t> & value)84     void setAsIntegerArray(const std::vector<uint32_t> &value)
85     {
86         mayFailCall(&EH::setAsIntegerArray, value);
87     }
getAsIntegerArray(std::vector<uint32_t> & value) const88     void getAsIntegerArray(std::vector<uint32_t> &value) const
89     {
90         mayFailCall(&EH::getAsIntegerArray, value);
91     }
92 
setAsSignedInteger(int32_t value)93     void setAsSignedInteger(int32_t value) { mayFailCall(&EH::setAsSignedInteger, value); }
getAsSignedInteger(int32_t & value) const94     void getAsSignedInteger(int32_t &value) const { mayFailCall(&EH::getAsSignedInteger, value); }
setAsSignedIntegerArray(const std::vector<int32_t> & value)95     void setAsSignedIntegerArray(const std::vector<int32_t> &value)
96     {
97         mayFailCall(&EH::setAsSignedIntegerArray, value);
98     }
getAsSignedIntegerArray(std::vector<int32_t> & value) const99     void getAsSignedIntegerArray(std::vector<int32_t> &value) const
100     {
101         mayFailCall(&EH::getAsSignedIntegerArray, value);
102     }
103 
getStructureAsXML() const104     std::string getStructureAsXML() const { return mayFailGet(&EH::getStructureAsXML); }
105 
getAsXML() const106     std::string getAsXML() const { return mayFailGet(&EH::getAsXML); }
setAsXML(const std::string & settings)107     void setAsXML(const std::string &settings) { mayFailSet(&EH::setAsXML, settings); }
108 
getAsBytes() const109     std::vector<uint8_t> getAsBytes() const
110     {
111         std::vector<uint8_t> settings(getSize());
112         mayFailCall(&EH::getAsBytes, settings);
113         return settings;
114     }
setAsBytes(const std::vector<uint8_t> & settings)115     void setAsBytes(const std::vector<uint8_t> &settings) { mayFailSet(&EH::setAsBytes, settings); }
116 };
117 
118 } // namespace parameterFramework
119