Config.h
1//-*-C++-*-
2/***************************************************************************
3 *
4 * Copyright (C) 2006 by Willem van Straten
5 * Licensed under the Academic Free License version 2.1
6 *
7 ***************************************************************************/
8
9// psrchive/Base/Classes/Pulsar/Config.h
10
11#ifndef __Pulsar_Config_h
12#define __Pulsar_Config_h
13
14#include "Configuration.h"
15#include "TextInterfaceInterpreter.h"
16#include "CommandParser.h"
17
18namespace Pulsar
19{
21 class Config : public Configuration
22 {
23 public:
24
26 Config ();
27
29 void set_filename (const std::string&);
30
32 static std::string get_home ();
33
35 static std::string get_runtime ();
36
38 static Config* get_configuration ();
39
40 class Interface;
41
43 static Interface* get_interface ();
44
46
48 static void ensure_linkage ();
49
50 protected:
51
53 static Config* config;
54
56 static Interface* interface;
57
59 void load ();
60
61 std::string filename;
62 bool databases_loaded;
63 };
64
65
67 template<typename T>
69 {
70
71 public:
72
74 Option (const std::string& name, const T& default_value,
75 const std::string& description = "none",
76 const std::string& detailed_description = "none" );
77
80 const std::string& name,
81 const std::string& default_value,
82 const std::string& description = "none",
83 const std::string& detailed_description = "none" );
84
87 const std::string& name, const T& default_value,
88 const std::string& description = "none",
89 const std::string& detailed_description = "none" );
90
93 const std::string& name,
94 const std::string& description = "none",
95 const std::string& detailed_description = "none" );
96
99 CommandParser* parser,
100 const std::string& name,
101 const std::string& default_value,
102 const std::string& description = "none",
103 const std::string& detailed_description = "none" );
104
106 T& operator = (const T& t)
107 { Configuration::Parameter<T>::set_value(t); return *this; }
108
109 std::string description;
110 std::string detailed_description;
111 };
112
113 template<typename T>
114 std::ostream& operator << (std::ostream& ostr, const Option<T>& o)
115 { return ostr << o.get_value(); }
116
117 template<typename T>
118 std::istream& operator >> (std::istream& istr, Option<T>& option)
119 { T val; istr >> val; option.set_value (val); return istr; }
120
121 class Config::Interface : public TextInterface::Parser
122 {
123 public:
124
125 Interface () { alphabetical = true; }
126
127 template<typename T>
128 class Element
129 {
130 public:
131 typedef TextInterface::Attribute< Configuration::Parameter<T> > Type;
132 };
133
134 template<typename T>
135 typename Element<T>::Type* add (Option<T>* option)
136 {
137 TextInterface::Allocator< Configuration::Parameter<T>,T > gen;
138
139 typename Element<T>::Type* getset;
140
141 getset = gen (option->get_key(),
144
145 getset->instance = option;
146 getset->set_description (option->description);
147 getset->set_detailed_description (option->detailed_description);
148
149 add_value( getset );
150 return getset;
151 }
152
153 template<typename T, typename Parser>
154 void add (Option<T>* option, Parser* parser)
155 {
156 add_value( TextInterface::new_Interpreter( option->get_key(),
157 option->description,
158 option->detailed_description,
159 parser,
160 &CommandParser::empty,
161 &CommandParser::parse ) );
162 }
163 };
164}
165
166
167template<typename T>
168Pulsar::Option<T>::Option (const std::string& _name,
169 const T& _default,
170 const std::string& _description,
171 const std::string& _detailed_description)
172 : Configuration::Parameter<T> (_name, Config::get_configuration(), _default)
173{
174 DEBUG("Pulsar::Option<T> name=" << _name \
175 << " description=" << _description << " default=" << _default);
176
177 description = _description;
178 detailed_description = _detailed_description;
179
180 DEBUG("Pulsar::Option<T> add to interface");
181
182 Config::get_interface()->add (this);
183
184 DEBUG("Pulsar::Option<T> return");
185}
186
187template<typename T>
189 const std::string& _name,
190 const std::string& _default,
191 const std::string& _description,
192 const std::string& _detailed_description )
193 : Configuration::Parameter<T> (_name, Config::get_configuration(),
194 parser, _default)
195{
196 DEBUG("Pulsar::Option<T> parser around Configuration::parameter");
197
198 description = _description;
199 detailed_description = _detailed_description;
200
201 DEBUG("Pulsar::Option<T> parser add to interface");
202
203 Config::get_interface()->add (this, parser);
204
205 DEBUG("Pulsar::Option<T> parser return");
206}
207
208template<typename T>
210 const std::string& _name, const T& _default,
211 const std::string& _description,
212 const std::string& _detailed_description )
213 : Configuration::Parameter<T> (_name, Config::get_configuration(), _default)
214{
215 DEBUG("Pulsar::Option<T> wrap Configuration::parameter");
216
217 description = _description;
218 detailed_description = _detailed_description;
219 parameter.set_key (_name);
220
221 parameter.set_loader( this->loader );
222 this->loader = 0;
223
224 DEBUG("Pulsar::Option<T> wrap add to interface");
225
226 Config::get_interface()->add(this)->instance = &parameter;
227
228 DEBUG("Pulsar::Option<T> wrap return");
229}
230
231template<typename T>
233 const std::string& _name,
234 const std::string& _description,
235 const std::string& _detailed_description )
236 : Configuration::Parameter<T> (_name, Config::get_configuration())
237{
238 DEBUG("Pulsar::Option<T> wrap Configuration::parameter");
239
240 description = _description;
241 detailed_description = _detailed_description;
242 parameter.set_key (_name);
243
244 parameter.set_loader( this->loader );
245 this->loader = 0;
246
247 DEBUG("Pulsar::Option<T> wrap add to interface");
248
249 Config::get_interface()->add(this)->instance = &parameter;
250
251 DEBUG("Pulsar::Option<T> wrap return");
252}
253
254template<typename T>
256 CommandParser* parser,
257 const std::string& _name,
258 const std::string& _default,
259 const std::string& _description,
260 const std::string& _detailed_description )
261 : Configuration::Parameter<T> (_name, Config::get_configuration(),
262 parser, _default)
263{
264 DEBUG("Pulsar::Option<T> wrap parser around Configuration::parameter");
265
266 description = _description;
267 detailed_description = _detailed_description;
268 parameter.set_key (_name);
269
270 DEBUG("Pulsar::Option<T> wrap parser that=" << &parameter << " loader=" << this->loader);
271
272 parameter.set_loader( this->loader );
273 this->loader = 0;
274
275 DEBUG("Pulsar::Option<T> wrap parser add to interface");
276
277 Config::get_interface()->add (this, parser);
278
279 DEBUG("Pulsar::Option<T> wrap parser return");
280}
281
282#endif
283
void set_value(const T &val)
Configuration(const char *filename=0)
Configuration option.
Definition Config.h:69
Option(Configuration::Parameter< T > &parameter, CommandParser *parser, const std::string &name, const std::string &default_value, const std::string &description="none", const std::string &detailed_description="none")
Wrap an existing Configuration::Parameter with an associated parser.
Definition Config.h:255
Option(Configuration::Parameter< T > &parameter, const std::string &name, const std::string &description="none", const std::string &detailed_description="none")
Wrap an existing Configuration::Parameter with own default value.
Definition Config.h:232
Option(const std::string &name, const T &default_value, const std::string &description="none", const std::string &detailed_description="none")
Construct a new Configuration::Parameter.
Definition Config.h:168
Option(CommandParser *parser, const std::string &name, const std::string &default_value, const std::string &description="none", const std::string &detailed_description="none")
Construct a new Configuration::Parameter with an associated parser.
Definition Config.h:188
Option(Configuration::Parameter< T > &parameter, const std::string &name, const T &default_value, const std::string &description="none", const std::string &detailed_description="none")
Wrap an existing Configuration::Parameter.
Definition Config.h:209
Stores PSRCHIVE configuration parameters.
Definition Config.h:22
static Config * get_configuration()
Return the configuration key/value pairs.
Definition Config.C:135
Config()
Default constructor.
Definition Config.C:27
void set_filename(const std::string &)
Set the configuration filename.
Definition Config.C:33
static std::string get_runtime()
Return the name of the runtime directory.
Definition Config.C:120
static Interface * get_interface()
Return the text interface to the configuration parameters.
Definition Config.C:144
static void ensure_linkage()
Ensure that configuration options are linked.
static Interface * interface
The global configuration interpreter.
Definition Config.h:56
void load()
Load the global configuration files.
Definition Config.C:43
static Config * config
The global configuration file.
Definition Config.h:53
static std::string get_home()
Return the name of the installation directory.
Definition Config.C:125
Configuration option.
Definition Config.h:69
Option(const std::string &name, const T &default_value, const std::string &description="none", const std::string &detailed_description="none")
Construct a new Configuration::Parameter.
Definition Config.h:168
T & operator=(const T &t)
Set equal to T operator.
Definition Config.h:106
void add_value(Value *value)
Defines the PSRCHIVE library.
Definition CalSource.h:17

Generated using doxygen 1.14.0