BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
Loading...
Searching...
No Matches
options.h
1/*
2Beam Delivery Simulation (BDSIM) Copyright (C) BDSIM Collaboration, 2001 - 2026.
3
4This file is part of BDSIM.
5
6BDSIM is free software: you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published
8by the Free Software Foundation version 3 of the License.
9
10BDSIM is distributed in the hope that it will be useful, but
11WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with BDSIM. If not, see <http://www.gnu.org/licenses/>.
17*/
18#ifndef OPTIONS_H
19#define OPTIONS_H
20
21#include <iomanip>
22#include <iostream>
23#include <string>
24#include <vector>
25
26#include "published.h"
27#include "optionsBase.h"
28
29namespace GMAD
30{
42 class Options: public Published<OptionsBase>, public OptionsBase
43 {
44 public:
45 Options();
46 explicit Options(const GMAD::OptionsBase& options);
47
49 template<typename T>
50 void set_value(std::string name, T value, bool bExit = false);
51
53 double get_value(std::string property_name) const;
55 std::string get_value_string(std::string property_name) const;
56
62 void Amalgamate(const Options& optionsIn, bool override);
63
65 bool HasBeenSet(const std::string& name) const;
66
67 inline const std::vector<std::string>& KeysOfSetValues() const {return setKeys;}
68
69 private:
71 void PublishMembers();
72 };
73
74 template<typename T>
75 void Options::set_value(std::string name, T value, bool bExit)
76 {
77#ifdef BDSDEBUG
78 std::cout << "options> Setting value " << std::setw(25) << std::left << name << value << std::endl;
79#endif
80 // member method can throw runtime_error, catch and exit gracefully
81 try
82 {
83 set(this, name, value);
84 setKeys.push_back(name);
85 }
86 catch (const std::runtime_error&)
87 {
88 std::cerr << "Error: options> unknown option \"" << name << "\" with value \"" << value << "\"" << std::endl;
89 if (bExit)
90 {exit(1);}
91 else
92 {std::rethrow_exception(std::current_exception());} // to be caught by python
93 }
94 }
95}
96
97#endif
Basic options class independent of Geant4.
Definition optionsBase.h:35
std::vector< std::string > setKeys
A list of all the keys that have been set in this instance.
Options class.
Definition options.h:43
void Amalgamate(const Options &optionsIn, bool override)
Definition options.cc:97
double get_value(std::string property_name) const
get method (only for doubles)
Definition options.cc:39
void PublishMembers()
publish members so these can be looked up from parser
Definition options.cc:143
void set_value(std::string name, T value, bool bExit=false)
set methods by property name
Definition options.h:75
bool HasBeenSet(const std::string &name) const
Whether a parameter has been set using the set_value method or not.
Definition options.cc:138
std::string get_value_string(std::string property_name) const
get method (only for strings)
Definition options.cc:63
Class that provides introspection to its members.
Definition published.h:48
void set(OptionsBase *instance, const std::string &name, double value)
Definition published.h:100
Parser namespace for GMAD language. Combination of Geant4 and MAD.