casacore
Loading...
Searching...
No Matches
AppState.h
Go to the documentation of this file.
1// # AppState.h: casacore library configuration without environment variabes
2// # Copyright (C) 2017
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_APPSTATE_H
27#define CASA_APPSTATE_H
28
29#include <casacore/casa/aips.h>
30
31#include <string>
32#include <list>
33#include <mutex>
34
35namespace casacore {
36
37// <summary>
38// Base class for application state
39// </summary>
40
41// <use visibility=export>
42
43// <reviewed reviewer="" date="yyyy/mm/dd" tests="" demos="">
44// </reviewed>
45
46// <synopsis>
47// This class is the base class for casacore state. Its purpose is to
48// allow applications initialize casacore's state without resorting to
49// environment variables. This is done by creating an object whose
50// class is derived from this base class, and then initializing the
51// AppStateSource with the newly created object. After initialization,
52// the AppStateSource takes ownership of the object. Please see the
53// documentation for AppStateSource for more information.
54// </synopsis>
55
56class AppState {
57 public:
58 // use the data path to find the filename...
59 virtual std::string resolve(const std::string &filename) const;
60
61 // get the list of directories in the data path...
62 virtual std::list<std::string> dataPath() const {
63 static std::list<std::string> result;
64 return result;
65 }
66
67 // Get AppState specified directory for (IERS) measures data.
68 //
69 // If data is not found in the specified directory and the
70 // specified directiory name is nonempty (size > 0), an
71 // exception will be thrown in findTab.
72 virtual std::string measuresDir() const {
73 static std::string result;
74 return result;
75 }
76
77 virtual bool initialized() const { return false; }
78
79 virtual ~AppState() {}
80};
81
82// <summary>
83// Allow configuration of casacore without environment variables
84// </summary>
85
86// <use visibility=export>
87
88// <reviewed reviewer="" date="yyyy/mm/dd" tests="" demos="">
89// </reviewed>
90
91// <synopsis>
92// This class allows packages which use casacore to configure casacore
93// behavior without reverting to environment variables. It is composed
94// primarly of static functions. An external application configures
95// casacore by calling the initialize(...) member function passing in
96// a pointer to an object which is derived from the AppState base class.
97// AppStateSource takes ownership of the provided pointer.
98//
99// When casacore no longer depends on compilers whose standard is older
100// than C++11, the raw pointers here should be changed to
101// unique_ptrs. The std::unique_ptr constructor is a constexpr, and it
102// does not throw exceptions.
103// </synopsis>
104
105// <example>
106// class MyState: public casacore::AppState {
107// public:
108// MyState( ) { }
109//
110// const std::list<std::string> &dataPath( ) const {
111// static std::list<std::string> my_path;
112// return my_path;
113// }
114//
115// bool initialized( ) const { return true; }
116// };
117//
118// MyState &get_my_state( ) {
119// if ( AppStateSource::fetch( ).initialized( ) == false )
120// casacore::AppStateSource::initialize( new MyState );
121// return dynamic_cast<MyState&>(AppStateSource::fetch( ));
122// }
123//
124// int main( int argc, char *argv[] ) {
125// MyState &state = get_my_state( );
126// ...
127// return 0;
128// }
129// </example>
131 public:
132 static void initialize(AppState *init) {
133 static std::mutex mutex_p;
134 std::lock_guard<std::mutex> lock(mutex_p);
135 if (user_state) delete user_state;
137 }
138 static AppState &fetch() {
139 static AppState default_result;
140 return user_state ? *user_state : default_result;
141 }
142
143 private:
146 AppStateSource(AppStateSource const &) {} // prevent copying
147 void operator=(AppStateSource const &) {} // prevent assignment
148};
149
150} // namespace casacore
151
152#endif
static AppState & fetch()
Definition AppState.h:138
static AppState * user_state
Definition AppState.h:144
void operator=(AppStateSource const &)
Definition AppState.h:147
static void initialize(AppState *init)
Definition AppState.h:132
AppStateSource(AppStateSource const &)
Definition AppState.h:146
virtual bool initialized() const
Definition AppState.h:77
virtual std::string resolve(const std::string &filename) const
use the data path to find the filename...
virtual std::list< std::string > dataPath() const
get the list of directories in the data path...
Definition AppState.h:62
virtual std::string measuresDir() const
Get AppState specified directory for (IERS) measures data.
Definition AppState.h:72
virtual ~AppState()
Definition AppState.h:79
For temporary backward namespace compatibility, use casa as alias for casacore.
Definition mainpage.dox:28
void init(ArrayInitPolicy initPolicy)
Definition Block.h:622