AlexaClientSDK  1.26.0
A cross-platform, modular SDK for interacting with the Alexa Voice Service
ConfigurationNode.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 
16 #ifndef ALEXA_CLIENT_SDK_AVSCOMMON_UTILS_INCLUDE_AVSCOMMON_UTILS_CONFIGURATION_CONFIGURATIONNODE_H_
17 #define ALEXA_CLIENT_SDK_AVSCOMMON_UTILS_INCLUDE_AVSCOMMON_UTILS_CONFIGURATION_CONFIGURATIONNODE_H_
18 
19 #include <chrono>
20 #include <cstddef>
21 #include <iostream>
22 #include <memory>
23 #include <mutex>
24 #include <set>
25 #include <string>
26 #include <vector>
27 
28 #include <rapidjson/document.h>
29 
30 namespace alexaClientSDK {
31 namespace avsCommon {
32 namespace utils {
33 namespace configuration {
34 
72 public:
88  static bool initialize(const std::vector<std::shared_ptr<std::istream>>& jsonStreams);
89 
95  static void uninitialize();
96 
102  static std::shared_ptr<ConfigurationNode> createRoot();
103 
109  static ConfigurationNode getRoot();
110 
115 
125  bool getBool(const std::string& key, bool* out = nullptr, bool defaultValue = false) const;
126 
136  bool getInt(const std::string& key, int* out = nullptr, int defaultValue = 0) const;
137 
147  bool getUint32(const std::string& key, uint32_t* out = nullptr, uint32_t defaultValue = 0) const;
148 
158  bool getString(const std::string& key, std::string* out = nullptr, std::string defaultValue = "") const;
159 
167  bool getStringValues(const std::string& key, std::set<std::string>* out = nullptr) const;
168 
182  template <typename InputType, typename OutputType, typename DefaultType>
183  bool getDuration(
184  const std::string& key,
185  OutputType* out = static_cast<std::chrono::seconds>(0),
186  DefaultType defaultValue = std::chrono::seconds(0)) const;
187 
195  ConfigurationNode operator[](const std::string& key) const;
196 
202  operator bool() const;
203 
215  template <typename Type>
216  bool getValue(
217  const std::string& key,
218  Type* out,
219  Type defaultValue,
220  bool (rapidjson::Value::*isType)() const,
221  Type (rapidjson::Value::*getType)() const) const;
222 
228  std::string serialize() const;
229 
237  ConfigurationNode getArray(const std::string& key) const;
238 
244  std::size_t getArraySize() const;
245 
253  ConfigurationNode operator[](const std::size_t index) const;
254 
255 private:
262  ConfigurationNode(const rapidjson::Value* object);
263 
273  bool getString(const std::string& key, const char** out, const char* defaultValue) const;
274 
276  const rapidjson::Value* m_object;
277 
282  static std::mutex m_mutex;
283 
285  static rapidjson::Document m_document;
286 
288  static ConfigurationNode m_root;
289 };
290 
291 template <typename InputType, typename OutputType, typename DefaultType>
292 bool ConfigurationNode::getDuration(const std::string& key, OutputType* out, DefaultType defaultValue) const {
293  int temp;
294  auto result = getInt(key, &temp);
295  if (out) {
296  *out = OutputType(result ? InputType(temp) : defaultValue);
297  }
298  return result;
299 }
300 
301 template <typename Type>
303  const std::string& key,
304  Type* out,
305  Type defaultValue,
306  bool (rapidjson::Value::*isType)() const,
307  Type (rapidjson::Value::*getType)() const) const {
308  if (key.empty() || !m_object) {
309  if (out) {
310  *out = defaultValue;
311  }
312  return false;
313  }
314  auto it = m_object->FindMember(key.c_str());
315  if (m_object->MemberEnd() == it || !(it->value.*isType)()) {
316  if (out) {
317  *out = defaultValue;
318  }
319  return false;
320  }
321  if (out) {
322  *out = (it->value.*getType)();
323  }
324  return true;
325 }
326 
327 } // namespace configuration
328 } // namespace utils
329 } // namespace avsCommon
330 } // namespace alexaClientSDK
331 
332 #endif // ALEXA_CLIENT_SDK_AVSCOMMON_UTILS_INCLUDE_AVSCOMMON_UTILS_CONFIGURATION_CONFIGURATIONNODE_H_
static void uninitialize()
Definition: ConfigurationNode.cpp:129
bool getStringValues(const std::string &key, std::set< std::string > *out=nullptr) const
Definition: ConfigurationNode.cpp:206
bool getBool(const std::string &key, bool *out=nullptr, bool defaultValue=false) const
Definition: ConfigurationNode.cpp:146
static std::shared_ptr< ConfigurationNode > createRoot()
Definition: ConfigurationNode.cpp:135
bool getValue(const std::string &key, Type *out, Type defaultValue, bool(rapidjson::Value::*isType)() const, Type(rapidjson::Value::*getType)() const) const
Definition: ConfigurationNode.h:302
bool getString(const std::string &key, std::string *out=nullptr, std::string defaultValue="") const
Definition: ConfigurationNode.cpp:158
static int index
Mock buffer index.
Definition: AndroidSLESMediaQueueTest.cpp:70
std::string serialize() const
Definition: ConfigurationNode.cpp:189
static ConfigurationNode getRoot()
Definition: ConfigurationNode.cpp:139
Whether or not curl logs should be emitted.
Definition: AVSConnectionManager.h:36
static bool initialize(const std::vector< std::shared_ptr< std::istream >> &jsonStreams)
Definition: ConfigurationNode.cpp:95
std::size_t getArraySize() const
Definition: ConfigurationNode.cpp:240
bool getUint32(const std::string &key, uint32_t *out=nullptr, uint32_t defaultValue=0) const
Definition: ConfigurationNode.cpp:154
ConfigurationNode getArray(const std::string &key) const
Definition: ConfigurationNode.cpp:224
bool getDuration(const std::string &key, OutputType *out=static_cast< std::chrono::seconds >(0), DefaultType defaultValue=std::chrono::seconds(0)) const
Definition: ConfigurationNode.h:292
bool getInt(const std::string &key, int *out=nullptr, int defaultValue=0) const
Definition: ConfigurationNode.cpp:150
ConfigurationNode operator[](const std::string &key) const
Definition: ConfigurationNode.cpp:171
static const std::string key
The database key to be used by the protocol given the METADATA object.
Definition: SharedAVSSettingProtocolTest.cpp:58

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