AlexaClientSDK  3.0.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 
204  ConfigurationNode getChildNode(const char* key) const;
205 
211  operator bool() const;
212 
224  template <typename Type>
225  bool getValue(
226  const std::string& key,
227  Type* out,
228  Type defaultValue,
229  bool (rapidjson::Value::*isType)() const,
230  Type (rapidjson::Value::*getType)() const) const;
231 
237  std::string serialize() const;
238 
246  ConfigurationNode getArray(const std::string& key) const;
247 
253  std::size_t getArraySize() const;
254 
262  ConfigurationNode operator[](const std::size_t index) const;
263 
264 private:
271  ConfigurationNode(const rapidjson::Value* object);
272 
282  bool getString(const std::string& key, const char** out, const char* defaultValue) const;
283 
285  const rapidjson::Value* m_object;
286 
291  static std::mutex m_mutex;
292 
294  static rapidjson::Document m_document;
295 
297  static ConfigurationNode m_root;
298 };
299 
300 template <typename InputType, typename OutputType, typename DefaultType>
301 bool ConfigurationNode::getDuration(const std::string& key, OutputType* out, DefaultType defaultValue) const {
302  int temp;
303  auto result = getInt(key, &temp);
304  if (out) {
305  *out = OutputType(result ? InputType(temp) : defaultValue);
306  }
307  return result;
308 }
309 
310 template <typename Type>
312  const std::string& key,
313  Type* out,
314  Type defaultValue,
315  bool (rapidjson::Value::*isType)() const,
316  Type (rapidjson::Value::*getType)() const) const {
317  if (key.empty() || !m_object) {
318  if (out) {
319  *out = defaultValue;
320  }
321  return false;
322  }
323  auto it = m_object->FindMember(key.c_str());
324  if (m_object->MemberEnd() == it || !(it->value.*isType)()) {
325  if (out) {
326  *out = defaultValue;
327  }
328  return false;
329  }
330  if (out) {
331  *out = (it->value.*getType)();
332  }
333  return true;
334 }
335 
336 } // namespace configuration
337 } // namespace utils
338 } // namespace avsCommon
339 } // namespace alexaClientSDK
340 
341 #endif // ALEXA_CLIENT_SDK_AVSCOMMON_UTILS_INCLUDE_AVSCOMMON_UTILS_CONFIGURATION_CONFIGURATIONNODE_H_
::std::string string
Definition: gtest-port.h:1097
bool getStringValues(const std::string &key, std::set< std::string > *out=nullptr) const
bool getBool(const std::string &key, bool *out=nullptr, bool defaultValue=false) const
bool getValue(const std::string &key, Type *out, Type defaultValue, bool(rapidjson::Value::*isType)() const, Type(rapidjson::Value::*getType)() const) const
Definition: ConfigurationNode.h:311
bool getString(const std::string &key, std::string *out=nullptr, std::string defaultValue="") const
static int index
Mock buffer index.
Definition: AndroidSLESMediaQueueTest.cpp:70
bool Value(const T &value, M matcher)
Definition: gmock-matchers.h:4347
static std::shared_ptr< ConfigurationNode > createRoot()
Whether or not curl logs should be emitted.
Definition: AVSConnectionManager.h:36
bool getUint32(const std::string &key, uint32_t *out=nullptr, uint32_t defaultValue=0) const
ConfigurationNode getArray(const std::string &key) const
bool getDuration(const std::string &key, OutputType *out=static_cast< std::chrono::seconds >(0), DefaultType defaultValue=std::chrono::seconds(0)) const
Definition: ConfigurationNode.h:301
bool getInt(const std::string &key, int *out=nullptr, int defaultValue=0) const
static bool initialize(const std::vector< std::shared_ptr< std::istream >> &jsonStreams)
ConfigurationNode operator[](const std::string &key) const
static const std::string key
The database key to be used by the protocol given the METADATA object.
Definition: SharedAVSSettingProtocolTest.cpp:58

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