Audio Processing Framework (APF) version 0.5.0
parameter_map.h
Go to the documentation of this file.
1/******************************************************************************
2 Copyright (c) 2012-2016 Institut für Nachrichtentechnik, Universität Rostock
3 Copyright (c) 2006-2012 Quality & Usability Lab
4 Deutsche Telekom Laboratories, TU Berlin
5
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 THE SOFTWARE.
23*******************************************************************************/
24
25// https://AudioProcessingFramework.github.io/
26
29
30#ifndef APF_PARAMETER_MAP_H
31#define APF_PARAMETER_MAP_H
32
33#include <map>
34#include <stdexcept> // for std::out_of_range
35#include <utility> // for std::forward
36
37#include "stringtools.h"
38
39namespace apf
40{
41
67struct parameter_map : std::map<std::string, std::string>
68{
72 template<typename... Args>
73 explicit parameter_map(Args&&... args)
74 : std::map<std::string, std::string>(std::forward<Args>(args)...)
75 {}
76
84 const std::string& operator[](const std::string& k) const
85 {
86 try
87 {
88 return this->at(k);
89 }
90 catch (const std::out_of_range&)
91 {
92 throw std::out_of_range("Parameter \"" + k + "\" does not exist in map!");
93 }
94 }
95
106 std::string& operator[](const std::string& k)
107 {
108 try
109 {
110 return this->at(k);
111 }
112 catch (const std::out_of_range&)
113 {
114 throw std::out_of_range("Parameter \"" + k + "\" does not exist in map!");
115 }
116 }
117
134 template<typename T>
135 T get(const std::string& k, const T& def) const
136 {
137 try
138 {
139 return this->get<T>(k);
140 }
141 catch (const std::out_of_range&)
142 {
143 return def;
144 }
145 catch (const std::invalid_argument&)
146 {
147 return def;
148 }
149 }
150
172 template<typename char_T>
173 std::basic_string<char_T>
174 get(const std::string& k, const char_T* const def) const
175 {
176 return this->get(k, std::basic_string<char_T>(def));
177 }
178
185 template<typename T>
186 T get(const std::string& k) const
187 {
188 T temp;
189 try
190 {
191 temp = str::S2RV<T>(this->operator[](k));
192 }
193 catch (std::invalid_argument& e)
194 {
195 throw std::invalid_argument(
196 "parameter_map key \"" + k + "\": " + e.what());
197 }
198 return temp;
199 }
200
208 template<typename T>
209 const std::string& set(const std::string& k, const T& v)
210 {
211 return std::map<std::string, std::string>::operator[](k) = str::A2S(v);
212 }
213
218 bool has_key(const std::string& k) const
219 {
220 return this->count(k) > 0;
221 }
222};
223
224} // namespace apf
225
226#endif
std::string A2S(const T &input)
Converter "Anything to String".
Definition: stringtools.h:52
Audio Processing Framework.
Definition: iterator.h:61
Helper functions for string conversion.
A "dictionary" for parameters.
Definition: parameter_map.h:68
std::string & operator[](const std::string &k)
"Setter".
T get(const std::string &k, const T &def) const
Get value converted to given type.
std::basic_string< char_T > get(const std::string &k, const char_T *const def) const
Overloaded function for character array (aka C-string).
const std::string & operator[](const std::string &k) const
"Getter".
Definition: parameter_map.h:84
const std::string & set(const std::string &k, const T &v)
Set value.
T get(const std::string &k) const
Throwing getter.
parameter_map(Args &&... args)
Constructor.
Definition: parameter_map.h:73
bool has_key(const std::string &k) const
Check if a given parameter is available.