casacore
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 #include <string>
31 #include <list>
32 #include <casacore/casa/aips.h>
33 #include <casacore/casa/OS/Mutex.h>
34 
35 namespace 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 
56 class AppState {
57 public:
58 
59  // use the data path to find the filename...
60  virtual std::string resolve(const std::string &filename) const;
61 
62  // get the list of directories in the data path...
63  virtual std::list<std::string> dataPath( ) const {
64  static std::list<std::string> result;
65  return result;
66  }
67 
68  // Get AppState specified directory for (IERS) measures data.
69  //
70  // If data is not found in the specified directory and the
71  // specified directiory name is nonempty (size > 0), an
72  // exception will be thrown in findTab.
73  virtual std::string measuresDir( ) const {
74  static std::string result;
75  return result;
76  }
77 
78  virtual bool initialized( ) const { return false; }
79 
80  virtual ~AppState( ) { }
81 };
82 
83 // <summary>
84 // Allow configuration of casacore without environment variables
85 // </summary>
86 
87 // <use visibility=export>
88 
89 // <reviewed reviewer="" date="yyyy/mm/dd" tests="" demos="">
90 // </reviewed>
91 
92 // <synopsis>
93 // This class allows packages which use casacore to configure casacore
94 // behavior without reverting to environment variables. It is composed
95 // primarly of static functions. An external application configures
96 // casacore by calling the initialize(...) member function passing in
97 // a pointer to an object which is derived from the AppState base class.
98 // AppStateSource takes ownership of the provided pointer.
99 //
100 // When casacore no longer depends on compilers whose standard is older
101 // than C++11, the raw pointers here should be changed to
102 // unique_ptrs. The std::unique_ptr constructor is a constexpr, and it
103 // does not throw exceptions.
104 // </synopsis>
105 
106 // <example>
107 // class MyState: public casacore::AppState {
108 // public:
109 // MyState( ) { }
110 //
111 // const std::list<std::string> &dataPath( ) const {
112 // static std::list<std::string> my_path;
113 // return my_path;
114 // }
115 //
116 // bool initialized( ) const { return true; }
117 // };
118 //
119 // MyState &get_my_state( ) {
120 // if ( AppStateSource::fetch( ).initialized( ) == false )
121 // casacore::AppStateSource::initialize( new MyState );
122 // return dynamic_cast<MyState&>(AppStateSource::fetch( ));
123 // }
124 //
125 // int main( int argc, char *argv[] ) {
126 // MyState &state = get_my_state( );
127 // ...
128 // return 0;
129 // }
130 // </example>
132 public:
133 
134  static void initialize(AppState *init) {
135  static Mutex mutex_p;
136  ScopedMutexLock lock(mutex_p);
137  if ( user_state ) delete user_state;
138  user_state = init;
139  }
140  static AppState &fetch( ) {
141  static AppState default_result;
142  return user_state ? *user_state : default_result;
143  }
144 
145 private:
148  AppStateSource( AppStateSource const &) { } // prevent copying
149  void operator=(AppStateSource const &) { } // prevent assignment
150 };
151 
152 } //# NAMESPACE CASACORE - END
153 
154 #endif
Allow configuration of casacore without environment variables.
Definition: AppState.h:131
static AppState & fetch()
Definition: AppState.h:140
static AppState * user_state
Definition: AppState.h:146
void operator=(AppStateSource const &)
Definition: AppState.h:149
static void initialize(AppState *init)
Definition: AppState.h:134
AppStateSource(AppStateSource const &)
Definition: AppState.h:148
virtual std::list< std::string > dataPath() const
get the list of directories in the data path...
Definition: AppState.h:63
virtual bool initialized() const
Definition: AppState.h:78
virtual std::string resolve(const std::string &filename) const
use the data path to find the filename...
virtual std::string measuresDir() const
Get AppState specified directory for (IERS) measures data.
Definition: AppState.h:73
virtual ~AppState()
Definition: AppState.h:80
Exception-safe lock/unlock of a mutex.
Definition: Mutex.h:147
this file contains all the compiler specific defines
Definition: mainpage.dox:28