Chi-Tech
basic_options.cc
Go to the documentation of this file.
1#include "basic_options.h"
2
3/**Returns a constant reference to an option that matches the
4 * requested name. If no name-match is found the method will throw
5 * a std::out_of_range exception.*/
7 operator()(const std::string& option_name) const
8{
9 for (const auto& option : options_)
10 {
11 if (option.Name() == option_name)
12 return option;
13 }
14
15 throw std::out_of_range("Basic option " + option_name +
16 " does not appear to exist.");
17}
18
19/**Returns a constant reference to an option at the given
20 * index. If the index is out of range then a std::out_of_range
21 * exception is thrown. This method can potentially be faster than
22 * the string comparison equivalent.*/
24 operator()(size_t index) const
25{
26 if (index < options_.size())
27 return options_[index];
28
29 throw std::out_of_range("Basic option with index " + std::to_string(index) +
30 " does not appear to exist.");
31}
32
33/**Returns a non-constant reference to an option that matches the
34 * requested name. If no name-match is found the method will throw
35 * a std::out_of_range exception.*/
37 operator[](const std::string& option_name)
38{
39 for (auto& option : options_)
40 {
41 if (option.Name() == option_name)
42 return option;
43 }
44
45 throw std::out_of_range("Basic option \"" + option_name +
46 "\" does not appear to exist.");
47}
48
49/**Returns a non-constant reference to an option at the given
50 * index. If the index is out of range then a std::out_of_range
51 * exception is thrown. This method can potentially be faster than
52 * the string comparison equivalent.*/
54 operator[](size_t index)
55{
56 if (index < options_.size())
57 return options_[index];
58
59 throw std::out_of_range("Basic option with index " + std::to_string(index) +
60 " does not appear to exist.");
61}
62
63/**Adds an option to the options list.*/
64template<>
65void chi_physics::BasicOptions::
66 AddOption<std::string>(const std::string &option_name,
67 const std::string& value)
68{
69 options_.emplace_back(option_name, value);
70}
71
72template<>
73void chi_physics::BasicOptions::
74 AddOption<bool>(const std::string &option_name, const bool& value)
75{
76 options_.emplace_back(option_name, value);
77}
78
79template<>
80void chi_physics::BasicOptions::
81 AddOption<int64_t>(const std::string &option_name, const int64_t& value)
82{
83 options_.emplace_back(option_name, value);
84}
85
86template<>
87void chi_physics::BasicOptions::
88 AddOption<double>(const std::string &option_name, const double& value)
89{
90 options_.emplace_back(option_name, value);
91}
92
93/**Attempts to find an option that matches the requested name.
94 * If one is found then its corresponding index is
95 * returned. If it is not found then a std::out_of_range
96 * exception is thrown.*/
98 GetOptionIndexFromName(const std::string& option_name) const
99{
100 size_t index=0;
101 for (const auto& option : options_)
102 {
103 if (option.Name() == option_name)
104 return index;
105 ++index;
106 }
107
108 throw std::out_of_range("Basic option " + option_name +
109 " does not appear to exist.");
110}
BasicOption & operator[](const std::string &option_name)
std::vector< BasicOption > options_
Definition: basic_options.h:49
size_t GetOptionIndexFromName(const std::string &option_name) const
const BasicOption & operator()(const std::string &option_name) const
Definition: basic_options.cc:7