File: SettingRef.cpp

package info (click to toggle)
ausaxs 1.1.8-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 72,592 kB
  • sloc: cpp: 49,853; ansic: 6,901; python: 730; makefile: 18
file content (100 lines) | stat: -rw-r--r-- 6,067 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// SPDX-License-Identifier: LGPL-3.0-or-later
// Author: Kristian Lytje

#include <settings/SettingRef.h>
#include <utility/Exceptions.h>
#include <utility/Limit.h>

#include <algorithm>

using namespace ausaxs;

std::unordered_map<std::string, std::shared_ptr<settings::io::detail::ISettingRef>>& settings::io::detail::ISettingRef::get_stored_settings() {
    static std::unordered_map<std::string, std::shared_ptr<settings::io::detail::ISettingRef>> stored_settings;
    return stored_settings;
}

template<> std::string ausaxs::settings::io::detail::type_as_string<std::string>(const std::string&) {return "string";}
template<> std::string ausaxs::settings::io::detail::type_as_string<double>(const double&) {return "double";}
template<> std::string ausaxs::settings::io::detail::type_as_string<int>(const int&) {return "int";}
template<> std::string ausaxs::settings::io::detail::type_as_string<unsigned int>(const unsigned int&) {return "uint";}
template<> std::string ausaxs::settings::io::detail::type_as_string<bool>(const bool&) {return "bool";}
template<> std::string ausaxs::settings::io::detail::type_as_string<std::vector<std::string>>(const std::vector<std::string>&) {return "vector-string";}
template<> std::string ausaxs::settings::io::detail::type_as_string<std::vector<double>>(const std::vector<double>&) {return "vector-double";}
template<> std::string ausaxs::settings::io::detail::type_as_string<std::vector<int>>(const std::vector<int>&) {return "vector-int";}
template<> std::string ausaxs::settings::io::detail::type_as_string<ausaxs::Limit>(const ausaxs::Limit&) {return "limit";}

template<> std::string settings::io::detail::SettingRef<std::string>::get() const {return settingref;}
template<> std::string settings::io::detail::SettingRef<double>::get() const {return std::to_string(settingref);}
template<> std::string settings::io::detail::SettingRef<int>::get() const {return std::to_string(settingref);}
template<> std::string settings::io::detail::SettingRef<unsigned int>::get() const {return std::to_string(settingref);}
template<> std::string settings::io::detail::SettingRef<bool>::get() const {return std::to_string(settingref);}
template<> std::string settings::io::detail::SettingRef<Limit>::get() const {return std::to_string(settingref.min) + " " + std::to_string(settingref.max);}
template<> std::string settings::io::detail::SettingRef<std::vector<std::string>>::get() const {
    std::string str;
    std::for_each(settingref.begin(), settingref.end(), [&str] (const std::string& s) {str += s + " ";});
    return str;
}
template<> std::string settings::io::detail::SettingRef<std::vector<double>>::get() const {
    std::string str;
    std::for_each(settingref.begin(), settingref.end(), [&str] (double s) {str += std::to_string(s) + " ";});
    return str;
}
template<> std::string settings::io::detail::SettingRef<std::vector<int>>::get() const {
    std::string str;
    std::for_each(settingref.begin(), settingref.end(), [&str] (int s) {str += std::to_string(s) + " ";});
    return str;
}


template<> void settings::io::detail::SettingRef<std::string>::set(const std::vector<std::string>& str) {
    if (str.size() != 1) {throw except::parse_error("Settings::SmartOption::parse: Option \"" + get() + "\" received too many settings.");}
    settingref = str[0];
}
template<> void settings::io::detail::SettingRef<bool>::set(const std::vector<std::string>& str) {
    if (str.size() != 1) {throw except::parse_error("Settings::SmartOption::parse: Option \"" + get() + "\" received too many settings.");}

    if (str[0] == "true" || str[0] == "TRUE" || str[0] == "1") {settingref = true; return;}
    else if (str[0] == "false" || str[0] == "FALSE" || str[0] == "0") {settingref = false; return;}
    throw except::parse_error("Settings::parse_bool: Option \"" + get() + "\" expected boolean string, but got \"" + str[0] + "\".");
}
template<> void settings::io::detail::SettingRef<double>::set(const std::vector<std::string>& str) {
    if (str.size() != 1) {throw except::parse_error("Settings::SmartOption::parse: Option \"" + get() + "\" received too many settings.");}
    settingref = std::stod(str[0]);
}
template<> void settings::io::detail::SettingRef<int>::set(const std::vector<std::string>& str) {
    if (str.size() != 1) {throw except::parse_error("Settings::SmartOption::parse: Option \"" + get() + "\" received too many settings.");}
    settingref = std::stoi(str[0]); 
}
template<> void settings::io::detail::SettingRef<unsigned int>::set(const std::vector<std::string>& str) {
    if (str.size() != 1) {throw except::parse_error("Settings::SmartOption::parse: Option \"" + get() + "\" received too many settings.");}
    settingref = std::stoi(str[0]);
}
template<> void settings::io::detail::SettingRef<std::vector<std::string>>::set(const std::vector<std::string>& str) {
    settingref = str;
}

template<> void settings::io::detail::SettingRef<Limit>::set(const std::vector<std::string>& str) {
    if (str.size() != 2) {throw except::parse_error("Settings::SmartOption::parse: Option \"" + get() + "\" received too many settings.");}
    settingref.min = std::stod(str[0]);
    settingref.max = std::stod(str[1]);
}

template<> void settings::io::detail::SettingRef<std::vector<double>>::set(const std::vector<std::string>& str) {
    std::vector<double> new_val;
    for (auto& s : str) {
        if (s.empty() || s == " ") {continue;}
        new_val.push_back(std::stod(s));
    }
    if (new_val.empty()) {throw except::parse_error("Settings::SmartOption::parse: Option \"" + get() + "\" received no settings.");}
    settingref = std::move(new_val);
}
template<> void settings::io::detail::SettingRef<std::vector<int>>::set(const std::vector<std::string>& str) {
    std::vector<int> new_val;
    for (auto& s : str) {
        if (s.empty() || s == " ") {continue;}
        new_val.push_back(std::stoi(s));
    }
    if (new_val.empty()) {throw except::parse_error("Settings::SmartOption::parse: Option \"" + get() + "\" received no settings.");}
    settingref = std::move(new_val);
}