AlexaClientSDK  3.0.0
A cross-platform, modular SDK for interacting with the Alexa Voice Service
SettingStringConversion.h
Go to the documentation of this file.
1 /*
2  * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License").
5  * You may not use this file except in compliance with the License.
6  * A copy of the License is located at
7  *
8  * http://aws.amazon.com/apache2.0/
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */
15 #ifndef ALEXA_CLIENT_SDK_SETTINGS_INCLUDE_SETTINGS_SETTINGSTRINGCONVERSION_H_
16 #define ALEXA_CLIENT_SDK_SETTINGS_INCLUDE_SETTINGS_SETTINGSTRINGCONVERSION_H_
17 
18 #include <list>
19 #include <set>
20 #include <sstream>
21 #include <string>
22 #include <utility>
23 #include <vector>
24 
26 
27 namespace alexaClientSDK {
28 namespace settings {
29 
31 constexpr char QUOTE = '"';
32 
33 //----------------- Setting conversion functions for int8_t and uint8_t -----------------//
34 
41 template <typename ValueT>
42 static constexpr bool isIntegralByteType() {
43  return std::is_same<ValueT, int8_t>::value || std::is_same<ValueT, uint8_t>::value;
44 }
45 
51 template <typename ValueT>
52 using IntegralByteType = typename std::enable_if<isIntegralByteType<ValueT>(), ValueT>::type;
53 
62 template <typename ValueT, typename = IntegralByteType<ValueT>>
63 std::pair<bool, std::string> toSettingString(const IntegralByteType<ValueT>& value) {
64  std::stringstream stream;
65  int valueInt = value;
66  stream << valueInt;
67  return {!stream.fail(), stream.str()};
68 }
69 
79 template <typename ValueT, typename = IntegralByteType<ValueT>>
80 std::pair<bool, ValueT> fromSettingString(const std::string& str, const IntegralByteType<ValueT>& defaultValue) {
81  int16_t value = defaultValue;
82  std::stringstream stream;
83  stream << str;
84  stream >> value;
85  return {!stream.fail(), stream.fail() ? defaultValue : value};
86 }
87 
88 //----------------- Setting conversion functions for std::string and enums -----------------//
89 
96 template <typename ValueT>
97 static constexpr bool isEnumOrString() {
98  return std::is_enum<ValueT>::value || std::is_same<ValueT, std::string>::value;
99 }
100 
106 template <typename ValueT>
107 using EnumOrString = typename std::enable_if<isEnumOrString<ValueT>(), ValueT>::type;
108 
119 template <typename ValueT, typename = EnumOrString<ValueT>>
120 std::pair<bool, std::string> toSettingString(const ValueT& value) {
121  std::stringstream stream;
122  stream << QUOTE << value << QUOTE;
123  return {!stream.fail(), stream.str()};
124 }
125 
137 template <typename ValueT, typename = EnumOrString<ValueT>>
138 std::pair<bool, ValueT> fromSettingString(const std::string& str, const ValueT& defaultValue) {
139  if (str.length() < 2 || str[0] != QUOTE || str[str.length() - 1] != QUOTE) {
140  return {false, defaultValue};
141  }
142 
143  ValueT value = defaultValue;
144  std::stringstream stream;
145  stream << str.substr(1, str.length() - 2); // Remove quotes from the string.
146  stream >> value;
147  return {!stream.fail(), value};
148 }
149 
150 //----------------- Setting conversion functions for string set -----------------//
151 
158 template <typename ValueT>
159 static constexpr bool isStringCollection() {
160  return std::is_same<ValueT, std::set<std::string>>::value ||
161  std::is_same<ValueT, std::vector<std::string>>::value || std::is_same<ValueT, std::list<std::string>>::value;
162 }
163 
169 template <typename ValueT>
170 using StringCollection = typename std::enable_if<isStringCollection<ValueT>(), ValueT>::type;
171 
180 template <typename ValueT, StringCollection<ValueT>* = nullptr>
181 std::pair<bool, std::string> toSettingString(const StringCollection<ValueT>& value) {
182  std::vector<std::string> values{value.begin(), value.end()};
184  return std::make_pair(!retValue.empty(), retValue);
185 }
186 
187 static const std::string EMPTY_JSON_LIST = "[]";
188 
198 template <typename ValueT, StringCollection<ValueT>* = nullptr>
199 std::pair<bool, ValueT> fromSettingString(const std::string& str, const StringCollection<ValueT>& defaultValue) {
200  auto values = avsCommon::utils::json::jsonUtils::retrieveStringArray<ValueT>(str);
201  if (values.empty() && str != EMPTY_JSON_LIST) {
202  return std::make_pair(false, defaultValue);
203  }
204  ValueT retValue{values.begin(), values.end()};
205  return std::make_pair(true, retValue);
206 }
207 
208 //----------------- Setting conversion functions for arithmetic types and classes -----------------//
209 
215 template <typename ValueT>
216 using OtherTypes = typename std::enable_if<
217  !isEnumOrString<ValueT>() && !isIntegralByteType<ValueT>() && !isStringCollection<ValueT>(),
218  ValueT>::type;
219 
230 template <typename ValueT>
231 std::pair<bool, std::string> toSettingString(const OtherTypes<ValueT>& value) {
232  std::stringstream stream;
233  stream << std::boolalpha << value;
234  return {!stream.fail(), stream.str()};
235 }
236 
248 template <typename ValueT>
249 std::pair<bool, ValueT> fromSettingString(const std::string& str, const OtherTypes<ValueT>& defaultValue) {
250  ValueT value = defaultValue;
251  std::stringstream stream;
252  stream << str;
253  stream >> std::boolalpha >> value;
254  return {!stream.fail(), stream.fail() ? defaultValue : value};
255 }
256 
257 } // namespace settings
258 } // namespace alexaClientSDK
259 
260 #endif // ALEXA_CLIENT_SDK_SETTINGS_INCLUDE_SETTINGS_SETTINGSTRINGCONVERSION_H_
std::pair< bool, ValueT > fromSettingString(const std::string &str, const IntegralByteType< ValueT > &defaultValue)
Definition: SettingStringConversion.h:80
typename std::enable_if< !isEnumOrString< ValueT >() &&!isIntegralByteType< ValueT >() &&!isStringCollection< ValueT >(), ValueT >::type OtherTypes
Definition: SettingStringConversion.h:218
static constexpr bool isStringCollection()
Definition: SettingStringConversion.h:159
std::string convertToJsonString(const CollectionT &elements)
Definition: JSONUtils.h:297
::std::string string
Definition: gtest-port.h:1097
static constexpr bool isIntegralByteType()
Definition: SettingStringConversion.h:42
static constexpr bool isEnumOrString()
Definition: SettingStringConversion.h:97
std::pair< bool, std::string > toSettingString(const IntegralByteType< ValueT > &value)
Definition: SettingStringConversion.h:63
typename std::enable_if< isEnumOrString< ValueT >(), ValueT >::type EnumOrString
Definition: SettingStringConversion.h:107
typename std::enable_if< isIntegralByteType< ValueT >(), ValueT >::type IntegralByteType
Definition: SettingStringConversion.h:52
static const std::string EMPTY_JSON_LIST
Definition: SettingStringConversion.h:187
constexpr char QUOTE
Quote used for json string values.
Definition: SettingStringConversion.h:31
Whether or not curl logs should be emitted.
Definition: AVSConnectionManager.h:36
typename std::enable_if< isStringCollection< ValueT >(), ValueT >::type StringCollection
Definition: SettingStringConversion.h:170
type
Definition: upload.py:443

AlexaClientSDK 3.0.0 - Copyright 2016-2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. Licensed under the Apache License, Version 2.0