1 /*
2  * Copyright 2003-2021 The Music Player Daemon Project
3  * http://www.musicpd.org
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 #ifndef MPD_CONFIG_DATA_HXX
21 #define MPD_CONFIG_DATA_HXX
22 
23 #include "Option.hxx"
24 #include "Param.hxx"
25 #include "Block.hxx"
26 
27 #include <array>
28 #include <chrono>
29 #include <forward_list>
30 
31 class AllocatedPath;
32 
33 struct ConfigData {
34 	std::array<std::forward_list<ConfigParam>, std::size_t(ConfigOption::MAX)> params;
35 	std::array<std::forward_list<ConfigBlock>, std::size_t(ConfigBlockOption::MAX)> blocks;
36 
37 	void Clear();
38 
GetParamListConfigData39 	auto &GetParamList(ConfigOption option) noexcept {
40 		return params[size_t(option)];
41 	}
42 
GetParamListConfigData43 	const auto &GetParamList(ConfigOption option) const noexcept {
44 		return params[size_t(option)];
45 	}
46 
47 	void AddParam(ConfigOption option, ConfigParam &&param) noexcept;
48 
49 	[[gnu::pure]]
GetParamConfigData50 	const ConfigParam *GetParam(ConfigOption option) const noexcept {
51 		const auto &list = GetParamList(option);
52 		return list.empty() ? nullptr : &list.front();
53 	}
54 
55 	template<typename F>
WithConfigData56 	auto With(ConfigOption option, F &&f) const {
57 		const auto *param = GetParam(option);
58 		return param != nullptr
59 			? param->With(std::forward<F>(f))
60 			: f(nullptr);
61 	}
62 
63 	[[gnu::pure]]
64 	const char *GetString(ConfigOption option,
65 			      const char *default_value=nullptr) const noexcept;
66 
67 	/**
68 	 * Returns an optional configuration variable which contains an
69 	 * absolute path.  If there is a tilde prefix, it is expanded.
70 	 * Returns nullptr if the value is not present.
71 	 *
72 	 * Throws #std::runtime_error on error.
73 	 */
74 	AllocatedPath GetPath(ConfigOption option) const;
75 
76 	unsigned GetUnsigned(ConfigOption option,
77 			     unsigned default_value) const;
78 
79 	std::chrono::steady_clock::duration
80 	GetUnsigned(ConfigOption option,
81 		    std::chrono::steady_clock::duration default_value) const;
82 
83 	unsigned GetPositive(ConfigOption option,
84 			     unsigned default_value) const;
85 
86 	std::chrono::steady_clock::duration
87 	GetPositive(ConfigOption option,
88 		    std::chrono::steady_clock::duration default_value) const;
89 
90 	bool GetBool(ConfigOption option, bool default_value) const;
91 
GetBlockListConfigData92 	auto &GetBlockList(ConfigBlockOption option) noexcept {
93 		return blocks[size_t(option)];
94 	}
95 
GetBlockListConfigData96 	const auto &GetBlockList(ConfigBlockOption option) const noexcept {
97 		return blocks[size_t(option)];
98 	}
99 
100 	ConfigBlock &AddBlock(ConfigBlockOption option,
101 			      ConfigBlock &&block) noexcept;
102 
103 	[[gnu::pure]]
GetBlockConfigData104 	const ConfigBlock *GetBlock(ConfigBlockOption option) const noexcept {
105 		const auto &list = GetBlockList(option);
106 		return list.empty() ? nullptr : &list.front();
107 	}
108 
109 	/**
110 	 * Find a block with a matching attribute.
111 	 *
112 	 * Throws if a block doesn't have the specified (mandatory) key.
113 	 *
114 	 * @param option the blocks to search
115 	 * @param key the attribute name
116 	 * @param value the expected attribute value
117 	 */
118 	[[gnu::pure]]
119 	const ConfigBlock *FindBlock(ConfigBlockOption option,
120 				     const char *key, const char *value) const;
121 
122 	ConfigBlock &MakeBlock(ConfigBlockOption option,
123 				     const char *key, const char *value);
124 };
125 
126 #endif
127