casa  5.7.0-16
 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 #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  virtual bool initialized( ) const { return false; }
69 
70  virtual ~AppState( ) { }
71 };
72 
73 // <summary>
74 // Allow configuration of casacore without environment variables
75 // </summary>
76 
77 // <use visibility=export>
78 
79 // <reviewed reviewer="" date="yyyy/mm/dd" tests="" demos="">
80 // </reviewed>
81 
82 // <synopsis>
83 // This class allows packages which use casacore to configure casacore
84 // behavior without reverting to environment variables. It is composed
85 // primarly of static functions. An external application configures
86 // casacore by calling the initialize(...) member function passing in
87 // a pointer to an object which is derived from the AppState base class.
88 // AppStateSource takes ownership of the provided pointer.
89 //
90 // When casacore no longer depends on compilers whose standard is older
91 // than C++11, the raw pointers here should be changed to
92 // unique_ptrs. The std::unique_ptr constructor is a constexpr, and it
93 // does not throw exceptions.
94 // </synopsis>
95 
96 // <example>
97 // class MyState: public casacore::AppState {
98 // public:
99 // MyState( ) { }
100 //
101 // const std::list<std::string> &dataPath( ) const {
102 // static std::list<std::string> my_path;
103 // return my_path;
104 // }
105 //
106 // bool initialized( ) const { return true; }
107 // };
108 //
109 // MyState &get_my_state( ) {
110 // if ( AppStateSource::fetch( ).initialized( ) == false )
111 // casacore::AppStateSource::initialize( new MyState );
112 // return dynamic_cast<MyState&>(AppStateSource::fetch( ));
113 // }
114 //
115 // int main( int argc, char *argv[] ) {
116 // MyState &state = get_my_state( );
117 // ...
118 // return 0;
119 // }
120 // </example>
122 public:
123 
124  static void initialize(AppState *init) {
125  static Mutex mutex_p;
126  ScopedMutexLock lock(mutex_p);
127  if ( user_state ) delete user_state;
128  user_state = init;
129  }
130  static AppState &fetch( ) {
131  static AppState default_result;
132  return user_state ? *user_state : default_result;
133  }
134 
135 private:
138  AppStateSource( AppStateSource const &) { } // prevent copying
139  void operator=(AppStateSource const &) { } // prevent assignment
140 };
141 
142 } //# NAMESPACE CASACORE - END
143 
144 #endif
virtual std::string resolve(const std::string &filename) const
use the data path to find the filename...
static AppState & fetch()
Definition: AppState.h:130
virtual ~AppState()
Definition: AppState.h:70
virtual std::list< std::string > dataPath() const
get the list of directories in the data path...
Definition: AppState.h:63
void operator=(AppStateSource const &)
Definition: AppState.h:139
static AppState * user_state
Definition: AppState.h:136
Wrapper around a pthreads mutex.
Definition: Mutex.h:58
static void initialize(AppState *init)
Definition: AppState.h:124
virtual bool initialized() const
Definition: AppState.h:68
Exception-safe lock/unlock of a mutex.
Definition: Mutex.h:146
Base class for application state.
Definition: AppState.h:56
AppStateSource(AppStateSource const &)
Definition: AppState.h:138
Allow configuration of casacore without environment variables.
Definition: AppState.h:121
#define casacore
&lt;X11/Intrinsic.h&gt; #defines true, false, casacore::Bool, and String.
Definition: X11Intrinsic.h:42