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
37namespace 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 */
45class ElementHandle : private FailureWrapper<::ElementHandle>
46{
47    ElementHandle(const ElementHandle &other) = delete;
48    ElementHandle &operator=(const ElementHandle &other) = delete;
49
50private:
51    using EH = ::ElementHandle;
52
53public:
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     */
63    size_t getSize() const { return EH::getSize(); }
64
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. */
75    void setAsDouble(double value) { mayFailCall(&EH::setAsDouble, value); }
76    /** Wrap EH::getAsDouble to throw an exception on failure. */
77    void getAsDouble(double &value) const { mayFailCall(&EH::getAsDouble, value); }
78
79    void setAsInteger(uint32_t value) { mayFailCall(&EH::setAsInteger, value); }
80    void getAsInteger(uint32_t &value) const { mayFailCall(&EH::getAsInteger, value); }
81    void setAsIntegerArray(const std::vector<uint32_t> &value)
82    {
83        mayFailCall(&EH::setAsIntegerArray, value);
84    }
85    void getAsIntegerArray(std::vector<uint32_t> &value) const
86    {
87        mayFailCall(&EH::getAsIntegerArray, value);
88    }
89
90    void setAsSignedInteger(int32_t value) { mayFailCall(&EH::setAsSignedInteger, value); }
91    void getAsSignedInteger(int32_t &value) const { mayFailCall(&EH::getAsSignedInteger, value); }
92    void setAsSignedIntegerArray(const std::vector<int32_t> &value)
93    {
94        mayFailCall(&EH::setAsSignedIntegerArray, value);
95    }
96    void getAsSignedIntegerArray(std::vector<int32_t> &value) const
97    {
98        mayFailCall(&EH::getAsSignedIntegerArray, value);
99    }
100
101    std::string getStructureAsXML() const { return mayFailGet(&EH::getStructureAsXML); }
102
103    std::string getAsXML() const { return mayFailGet(&EH::getAsXML); }
104    void setAsXML(const std::string &settings) { mayFailSet(&EH::setAsXML, settings); }
105
106    std::vector<uint8_t> getAsBytes() const
107    {
108        std::vector<uint8_t> settings(getSize());
109        mayFailCall(&EH::getAsBytes, settings);
110        return settings;
111    }
112    void setAsBytes(const std::vector<uint8_t> &settings) { mayFailSet(&EH::setAsBytes, settings); }
113};
114
115} // parameterFramework
116