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