casacore
Loading...
Searching...
No Matches
AipsrcValue.h
Go to the documentation of this file.
1// # AipsrcValue.h: Class to read values from the Aipsrc general resource files
2// # Copyright (C) 1995,1996,1997,1999,2002,2003
3// # Associated Universities, Inc. Washington DC, USA.
4// #
5// # This library is free software; you can redistribute it and/or modify it
6// # under the terms of the GNU Library General Public License as published by
7// # the Free Software Foundation; either version 2 of the License, or (at your
8// # option) any later version.
9// #
10// # This library is distributed in the hope that it will be useful, but WITHOUT
11// # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12// # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13// # License for more details.
14// #
15// # You should have received a copy of the GNU Library General Public License
16// # along with this library; if not, write to the Free Software Foundation,
17// # Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18// #
19// # Correspondence concerning AIPS++ should be addressed as follows:
20// # Internet email: casa-feedback@nrao.edu.
21// # Postal address: AIPS++ Project Office
22// # National Radio Astronomy Observatory
23// # 520 Edgemont Road
24// # Charlottesville, VA 22903-2475 USA
25
26#ifndef CASA_AIPSRCVALUE_H
27#define CASA_AIPSRCVALUE_H
28
29#include <casacore/casa/aips.h>
30#include <casacore/casa/BasicSL/String.h>
31#include <casacore/casa/System/Aipsrc.h>
32
33#include <mutex>
34#include <vector>
35
36namespace casacore { // # NAMESPACE CASACORE - BEGIN
37
38// # Forward declarations
39class Unit;
40
41// <summary> Class to read values from the Aipsrc general resource files
42// </summary>
43
44// <use visibility=export>
45
46// <reviewed reviewer="mhaller" date="1997/10/08" tests="tAipsrcValue" demos="">
47// </reviewed>
48
49// <prerequisite>
50// <li> <linkto class=Aipsrc>Aipsrc</linkto>
51// </prerequisite>
52//
53// <etymology>
54// A class for getting values from the Aipsrc files
55// </etymology>
56//
57// <synopsis>
58// The static AipsrcValue class can get typed values from the Aipsrc
59// resource files.<br>
60// The basic interaction with the class is with the static keyword match
61// functions:
62// <srcblock>
63// Bool AipsrcValue<Type>::find(Type &result, const String &keyword)
64// Bool AipsrcValue<Type>::find(Type &result, const String &keyword,
65// const Type &deflt)
66// </srcblock>
67// comparable to the standard (String) <linkto class=Aipsrc>Aipsrc</linkto>
68// find.<br>
69// If the resource file contains a multi-valued keyword, use the
70// <linkto class=AipsrcVector>AipsrcVector</linkto> class instead.
71//
72// The class is templated. For ease of use typedefs are provided for:
73// <srcblock>
74// AipsrcDouble, AipsrcInt, AipsrcBool, AipsrcString
75// AipsrcVDouble, AipsrcVInt, AipsrcVBool, AipsrcVString
76// </srcblock>
77// In addition to the above finds, special finds:
78// <srcblock>
79// Bool AipsrcValue<Type>::find(Type &result, const String &keyword,
80// const Unit &defun, const Unit &resun)
81// Bool AipsrcValue<Type>::find(Type &result, const String &keyword,
82// const Unit &defun, const Unit &resun,
83// const Type &deflt)
84// </srcblock>
85// are provided. These finds will read the keyword value as a Quantity.
86// If no units are given, the defun are assumed. The result is converted
87// to the resun, before the value is returned. E.g.
88// <srcblock>
89// Double x;
90// find(x, "time.offset", "h", "d");
91// </srcblock>
92// will return:
93// <ul>
94// <li> 2.5/24 for a value specified as 2.5 in resource file
95// <li> 2.5/24 for 2:30:00
96// <li> 0.5/24 for 30min
97// <li> 0.5 for 0.5d
98// </ul>
99//
100// The class has <src>registerRC, get, set</src> functions as described in
101// <linkto class=Aipsrc>Aipsrc</linkto>. Note that registration is on a
102// per Type basis, and hence registration of the same keyword in different
103// types (and possible sets) act on different values, but with the same
104// result if no set has been done.
105//
106// Specialisation exists for <src>Bool</src>, where <src>True</src> is
107// any value string starting with one of 'yYtT123456789', and False in
108// all other cases, and no finds with Units are provided. Strings are
109// supposed to be handled by standard <linkto class=Aipsrc>Aipsrc</linkto>
110// class for single values, and a specialisation exists for the
111// <linkto class=AipsrcVector>AipsrcVector</linkto> case.
112//
113// </synopsis>
114//
115// <example>
116// <srcblock>
117// String tzoff; // result of keyword find
118// if (!AipsrcValue<Double>::find(tzoff, "time.zone.offset")) { // look for key
119// tzoff = -5;
120// };
121// </srcblock>
122// A more convenient way of accomplishing the same result is:
123// <srcblock>
124// AipsrcDouble::find(tzoff, "time.zone.offset", -5);
125// </srcblock>
126// or even:
127// <srcblock>
128// AipsrcDouble::find(tzoff, "time.zone.offset",
129// "h", "h", -5);
130// </srcblock>
131// Here the final argument is the default to use if the keyword is not found
132// at all.
133// </example>
134//
135//
136// <templating>
137// <li> All types with a <src>>></src> defined.
138// <note role=warning>
139// Since interpretation of the keyword value string is done with the standard
140// input right-shift operator, specialisations are necessary for non-standard
141// cases like Bool. They are provided. String is supposed to be handled by
142// standard Aipsrc.
143// </note>
144// </templating>
145//
146// <motivation>
147// Programs need a way to interact with the AipsrcValue files.
148// </motivation>
149//
150// <thrown>
151// <li>AipsError if the environment variables HOME and/or AIPSPATH not set.
152// </thrown>
153//
154// <todo asof="1997/08/07">
155// </todo>
156
157template <class T>
158class AipsrcValue : public Aipsrc {
159 public:
160 // # Member functions
161 // The <src>find()</src> functions will, given a keyword, return the value
162 // of a matched keyword found in the files. If no match found the
163 // function will be False, and the default returned if specified.
164 // <group>
165 static Bool find(T &value, const String &keyword);
166 static Bool find(T &value, const String &keyword, const T &deflt);
167 // </group>
168 // These <src>find()</src> functions will, given a keyword, read the value
169 // of a matched keyword as a Quantity. If no unit has been given in the
170 // keyword value, the default_unit Unit will be assumed. The value returned
171 // will be converted to the result_unit Unit. If no match found, the default
172 // value is returned (see example above).
173 // <group>
174 static Bool find(T &value, const String &keyword, const Unit &default_unit,
175 const Unit &result_unit);
176 static Bool find(T &value, const String &keyword, const Unit &default_unit,
177 const Unit &result_unit, const T &deflt);
178 // </group>
179 // Functions to register keywords for later use in get() and set(). The
180 // returned value is the index for get() and set().
181 // <group>
182 static uInt registerRC(const String &keyword, const T &deflt);
183 static uInt registerRC(const String &keyword, const Unit &default_unit, const Unit &result_unit,
184 const T &deflt);
185 // </group>
186
187 // Gets are like find, but using registered integers rather than names. The
188 // aipsrc file is read only once, and values can be set as well.
189 // This function can't return a reference, because this would give access to
190 // the value without protection by the mutex.
191 // <group>
192 static const T get(uInt keyword);
193 // </group>
194
195 // Sets allow registered values to be set
196 // <group>
197 static void set(uInt keyword, const T &deflt);
198 // </group>
199
200 // Save registered value to <src>$HOME/.aipsrc</src>
201 static void save(uInt keyword);
202
203 private:
204 // # Data
205 // The global AipsrcValue object
206 inline static std::mutex theirMutex;
207 // Register list
208 // <group>
209 inline static std::vector<T> tlst;
210 inline static std::vector<String> ntlst;
211 // </group>
212};
213
214template <>
215Bool AipsrcValue<String>::find(String &value, const String &keyword, const Unit &defun,
216 const Unit &resun);
217
218// <summary> Specialization of AipsrcValue for Bool </summary>
219
220// <synopsis>
221// </synopsis>
222
223template <>
224class AipsrcValue<Bool> : public Aipsrc {
225 public:
226 static Bool find(Bool &value, const String &keyword);
227 static Bool find(Bool &value, const String &keyword, const Bool &deflt);
228 static uInt registerRC(const String &keyword, const Bool &deflt);
229 static Bool get(uInt keyword);
230 static void set(uInt keyword, const Bool &deflt);
231 static void save(uInt keyword);
232
233 private:
234 inline static std::mutex theirMutex;
235 static_assert(sizeof(unsigned char) == sizeof(bool));
236 inline static std::vector<unsigned char> tlst;
237 inline static std::vector<String> ntlst;
238};
239
240// # Declare extern templates for often used types.
241extern template class AipsrcValue<Bool>;
242extern template class AipsrcValue<Int>;
243extern template class AipsrcValue<Double>;
244extern template class AipsrcValue<String>;
245
246} // namespace casacore
247
248#ifndef CASACORE_NO_AUTO_TEMPLATES
249#include <casacore/casa/System/AipsrcValue.tcc>
250#endif // # CASACORE_NO_AUTO_TEMPLATES
251#endif
static std::vector< String > ntlst
static Bool find(Bool &value, const String &keyword)
static std::vector< unsigned char > tlst
static void save(uInt keyword)
static Bool find(Bool &value, const String &keyword, const Bool &deflt)
static Bool get(uInt keyword)
static uInt registerRC(const String &keyword, const Bool &deflt)
static std::mutex theirMutex
static void set(uInt keyword, const Bool &deflt)
static const T get(uInt keyword)
Gets are like find, but using registered integers rather than names.
static Bool find(T &value, const String &keyword)
The find() functions will, given a keyword, return the value of a matched keyword found in the files.
static uInt registerRC(const String &keyword, const Unit &default_unit, const Unit &result_unit, const T &deflt)
static Bool find(T &value, const String &keyword, const Unit &default_unit, const Unit &result_unit)
These find() functions will, given a keyword, read the value of a matched keyword as a Quantity.
static std::vector< Double > tlst
static void save(uInt keyword)
Save registered value to $HOME/.aipsrc.
static Bool find(T &value, const String &keyword, const T &deflt)
static uInt registerRC(const String &keyword, const T &deflt)
Functions to register keywords for later use in get() and set().
static std::vector< String > ntlst
static void set(uInt keyword, const T &deflt)
Sets allow registered values to be set.
static Bool find(T &value, const String &keyword, const Unit &default_unit, const Unit &result_unit, const T &deflt)
String: the storage and methods of handling collections of characters.
Definition String.h:355
For temporary backward namespace compatibility, use casa as alias for casacore.
Definition mainpage.dox:28
unsigned int uInt
Definition aipstype.h:49
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40
NewDelAllocator< T > NewDelAllocator< T >::value
Definition Allocator.h:360