diff --git a/parameter/ElementHandle.cpp b/parameter/ElementHandle.cpp index 8f597fd66..ecf1bcffb 100644 --- a/parameter/ElementHandle.cpp +++ b/parameter/ElementHandle.cpp @@ -41,6 +41,20 @@ using std::string; using std::mutex; using std::lock_guard; +/** @return 0 by default, ie for non overloaded types. */ +template +static size_t getUserInputSize(const T & /*scalar*/) +{ + return 0; +} + +/** @return the vector's size. */ +template +static size_t getUserInputSize(const std::vector &vector) +{ + return vector.size(); +} + ElementHandle::ElementHandle(CConfigurableElement &element, CParameterMgr ¶meterMgr) : mElement(element), mParameterMgr(parameterMgr) { @@ -146,18 +160,6 @@ struct isVector> : std::true_type { }; -template -size_t ElementHandle::getSize(T /*value*/) -{ - return 0; -} - -template -size_t ElementHandle::getSize(std::vector &values) -{ - return values.size(); -} - bool ElementHandle::getAsXML(std::string &xmlValue, std::string &error) const { std::string result; @@ -194,7 +196,7 @@ bool ElementHandle::setAsBytes(const std::vector &bytesValue, std::stri template bool ElementHandle::setAs(const T value, string &error) const { - if (not checkSetValidity(getSize(value), error)) { + if (not checkSetValidity(getUserInputSize(value), error)) { return false; } // Safe downcast thanks to isParameter check in checkSetValidity diff --git a/parameter/include/ElementHandle.h b/parameter/include/ElementHandle.h index 1a6023423..7d0888a81 100644 --- a/parameter/include/ElementHandle.h +++ b/parameter/include/ElementHandle.h @@ -234,11 +234,6 @@ class PARAMETER_EXPORT ElementHandle template bool getAs(T &value, std::string &error) const; - template - static size_t getSize(T value); - template - static size_t getSize(std::vector &values); - CBaseParameter &getParameter(); const CBaseParameter &getParameter() const; diff --git a/test/functional-tests/Handle.cpp b/test/functional-tests/Handle.cpp index 1c594fdd3..c309fe41e 100644 --- a/test/functional-tests/Handle.cpp +++ b/test/functional-tests/Handle.cpp @@ -602,4 +602,46 @@ SCENARIO("Mapping handle access", "[handler][mapping]") } } +SCENARIO_METHOD(SettingsTestPF, "Handle Get/Set as various kinds", "[handler][dynamic]") +{ + ElementHandle intScalar(*this, "/test/test/parameter_block/integer"); + WHEN ("Setting a scalar integer") { + WHEN ("As an array") { + THEN ("It should fail") { + CHECK_THROWS(intScalar.setAsIntegerArray({0, 0})); + } + } + WHEN ("As a scalalar") { + THEN ("It should succeed") { + uint32_t expected = 111; + CHECK_NOTHROW(intScalar.setAsInteger(expected)); + AND_THEN ("Getting it back should give the same value") { + uint32_t back = 42; + CHECK_NOTHROW(intScalar.getAsInteger(back)); + CHECK(back == expected); + } + } + } + } + + ElementHandle intArray(*this, "/test/test/parameter_block/integer_array"); + WHEN ("Setting a array integer") { + WHEN ("As a scalar") { + THEN ("It should fail") { + CHECK_THROWS(intArray.setAsSignedInteger(0)); + } + } + WHEN ("As a integer") { + THEN ("It should succeed") { + const std::vector expected = {-9, 8, -7, 6}; + CHECK_NOTHROW(intArray.setAsSignedIntegerArray(expected)); + AND_THEN ("Getting it back should give the same value") { + std::vector back = {-42, 42, 43, -43}; + CHECK_NOTHROW(intArray.getAsSignedIntegerArray(back)); + CHECK(back == expected); + } + } + } + } +} } // namespace parameterFramework diff --git a/test/functional-tests/include/ElementHandle.hpp b/test/functional-tests/include/ElementHandle.hpp index dc48ce908..fe42b71ea 100644 --- a/test/functional-tests/include/ElementHandle.hpp +++ b/test/functional-tests/include/ElementHandle.hpp @@ -76,6 +76,28 @@ class ElementHandle : private FailureWrapper<::ElementHandle> /** Wrap EH::getAsDouble to throw an exception on failure. */ void getAsDouble(double &value) const { mayFailCall(&EH::getAsDouble, value); } + void setAsInteger(uint32_t value) { mayFailCall(&EH::setAsInteger, value); } + void getAsInteger(uint32_t &value) const { mayFailCall(&EH::getAsInteger, value); } + void setAsIntegerArray(const std::vector &value) + { + mayFailCall(&EH::setAsIntegerArray, value); + } + void getAsIntegerArray(std::vector &value) const + { + mayFailCall(&EH::getAsIntegerArray, value); + } + + void setAsSignedInteger(int32_t value) { mayFailCall(&EH::setAsSignedInteger, value); } + void getAsSignedInteger(int32_t &value) const { mayFailCall(&EH::getAsSignedInteger, value); } + void setAsSignedIntegerArray(const std::vector &value) + { + mayFailCall(&EH::setAsSignedIntegerArray, value); + } + void getAsSignedIntegerArray(std::vector &value) const + { + mayFailCall(&EH::getAsSignedIntegerArray, value); + } + std::string getStructureAsXML() const { return mayFailGet(&EH::getStructureAsXML); } std::string getAsXML() const { return mayFailGet(&EH::getAsXML); }