Loading...
Searching...
No Matches
configuration_config.tpp
1/*---------------------------------------------------------------------------*\
2*
3* bitpit
4*
5* Copyright (C) 2015-2021 OPTIMAD engineering Srl
6*
7* -------------------------------------------------------------------------
8* License
9* This file is part of bitpit.
10*
11* bitpit is free software: you can redistribute it and/or modify it
12* under the terms of the GNU Lesser General Public License v3 (LGPL)
13* as published by the Free Software Foundation.
14*
15* bitpit is distributed in the hope that it will be useful, but WITHOUT
16* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18* License for more details.
19*
20* You should have received a copy of the GNU Lesser General Public License
21* along with bitpit. If not, see <http://www.gnu.org/licenses/>.
22*
23\*---------------------------------------------------------------------------*/
24#ifndef __BITPIT_CONFIGURATION_TPP__
25#define __BITPIT_CONFIGURATION_TPP__
26
27#include <sstream>
28#include <stdexcept>
29
30namespace bitpit {
31
40template<typename T>
41T Config::get(const std::string &key) const
42{
43 T value;
44 if (std::istringstream(get(key)) >> value) {
45 return value;
46 } else {
47 throw std::runtime_error("Unable to convert the option \"" + key + "\"");
48 }
49}
50
62template<typename T>
63T Config::get(const std::string &key, const T &fallback) const
64{
65 if (hasOption(key)) {
66 return get<T>(key);
67 } else {
68 return fallback;
69 }
70}
71
78template<typename T>
79void Config::set(const std::string &key, const T &value)
80{
81 std::ostringstream valueStream;
82 if (valueStream << value) {
83 set(key, valueStream.str());
84 } else {
85 throw std::runtime_error("Unable to convert the option \"" + key + "\"");
86 }
87}
88
89}
90
91#endif
const std::string & get(const std::string &key) const
bool hasOption(const std::string &key) const
void set(const std::string &key, const std::string &value)
--- layout: doxygen_footer ---