MySQL 9.0.0
Source Code Documentation
config_reader.h
Go to the documentation of this file.
1/* Copyright (c) 2021, 2024, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24#ifndef CONFIG_READER_INCLUDED
25#define CONFIG_READER_INCLUDED
26
27#include <string>
28
29#define RAPIDJSON_HAS_STDSTRING 1
30
31#include "my_rapidjson_size_t.h"
32
33#include <rapidjson/document.h>
34
35namespace keyring_common::config {
36
38 public:
39 /**
40 Constructor
41
42 Reads JSON from config file and stores it in memory.
43
44 @param [in] config_file_path Full path to configuration file
45 */
46 explicit Config_reader(std::string config_file_path);
47
48 /**
49 Get an element value from JSON document.
50 Assumption: Type is compatible with Get() function and
51 type of element is matching with template argument.
52
53 @param [in] element_name Name of the element being searched
54 @param [out] element_value Value of the element
55
56 @returns status of search operation
57 @retval false Element found. Refer to element_value
58 @retval true Element missing.
59 */
60 template <typename T>
61 bool get_element(const std::string &element_name, T &element_value) {
62 if (!valid_ || !data_.HasMember(element_name)) return true;
63 element_value = data_[element_name].Get<T>();
64 return false;
65 }
66
67 private:
68 /** Configuration file path */
69 std::string config_file_path_;
70 /** Configuration data in JSON */
71 rapidjson::Document data_;
72 /** Validity of configuration data */
73 bool valid_;
74};
75
76} // namespace keyring_common::config
77
78#endif // !CONFIG_READER_INCLUDED
Definition: config_reader.h:37
rapidjson::Document data_
Configuration data in JSON.
Definition: config_reader.h:71
Config_reader(std::string config_file_path)
Constructor.
Definition: config_reader.cc:33
bool valid_
Validity of configuration data.
Definition: config_reader.h:73
bool get_element(const std::string &element_name, T &element_value)
Get an element value from JSON document.
Definition: config_reader.h:61
std::string config_file_path_
Configuration file path.
Definition: config_reader.h:69
Define rapidjson::SizeType to be std::size_t.
Definition: config_reader.cc:31