casa  5.7.0-16
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
State.h
Go to the documentation of this file.
1 //# State.h: application state for CASAtools python module
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 #ifndef CASATOOLS_CONFIG_STATE_H
27 #define CASATOOLS_CONFIG_STATE_H
28 #include <sys/stat.h>
29 #include <list>
30 #include <mutex>
31 #include <string>
32 #include <vector>
33 #include <memory>
34 #include <algorithm>
35 #include <limits.h>
36 #include <stdlib.h>
39 
40 namespace casatools {
42  class State: public casacore::AppState {
43  public:
44 
45  State( ) { }
46 
47  virtual bool initialized( ) const { return true; }
48 
49  virtual std::list<std::string> dataPath( ) const {
50  return data_path;
51  }
52 
53  void clearDataPath( ) {
54  // protect critical section...
55  std::lock_guard<std::mutex> guard(data_path_mutex);
56  data_path.clear( );
57  }
58 
59  void setDataPath(const std::vector<std::string> &new_list) {
60  std::list<std::string> existing_paths;
61  struct stat s;
62 
63  // accept only strings that are the path to a directory
64  std::copy_if( new_list.begin( ), new_list.end( ), std::back_inserter(existing_paths),
65  [&s]( std::string d ) {
66  return (stat(d.c_str( ),&s) == 0) && (s.st_mode & S_IFDIR);
67  } );
68 
69  // protect critical section...
70  std::lock_guard<std::mutex> guard(data_path_mutex);
71 
72  // always clear the existing path
73  data_path.clear( );
74 
75  // convert the paths to fully qualified paths
76  std::transform( existing_paths.begin( ), existing_paths.end( ),
77  std::back_inserter( data_path ),
78  [ ]( const std::string &f ) {
79  char *expanded = realpath(f.c_str( ), NULL);
80  std::string result( expanded ? expanded : "" );
81  free(expanded);
82  return result;
83  } );
84  }
85 
86  // get map of registrations
87  std::list<ServiceId> services( ) { return registrar.services( ); }
88  // returns true if a registration for 'id' was found
89  bool removeService( std::string id ) { return registrar.remove(id); }
90  // returns assigned identifier (likely based upon the proposed_id)
91  ServiceId addService( std::string proposed_id, std::string uri, const std::list<std::string> &types ) {
92  return registrar.add(ServiceId(proposed_id, uri, types));
93  }
94  ServiceId addService( const ServiceId &new_service ) {
95  return registrar.add(new_service);
96  }
97 
98 
99  std::string registryURI( ) {
100  return registrar.uri( );
101  }
102 
103  // do any necessary shutdown functions,
104  void shutdown( );
105 
106  private:
107  std::mutex data_path_mutex;
108  std::list<std::string> data_path;
110  };
111 
112  extern State &get_state( );
113 
114 }
115 
116 
117 #endif
ServiceId addService(std::string proposed_id, std::string uri, const std::list< std::string > &types)
returns assigned identifier (likely based upon the proposed_id)
Definition: State.h:91
ServiceId add(const ServiceId &proposed)
returns assigned identifier (likely based upon the proposed_id)
std::list< ServiceId > services()
get map of registrations
Definition: State.h:87
State & get_state()
virtual std::list< std::string > dataPath() const
get the list of directories in the data path...
Definition: State.h:49
namespace for CASAtools classes within &quot;CASA code&quot;
Definition: Registrar.h:36
bool removeService(std::string id)
returns true if a registration for &#39;id&#39; was found
Definition: State.h:89
void setDataPath(const std::vector< std::string > &new_list)
Definition: State.h:59
std::string uri()
Definition: Registrar.h:101
ServiceId addService(const ServiceId &new_service)
Definition: State.h:94
free(pool)
std::mutex data_path_mutex
Definition: State.h:107
Registrar registrar
Definition: State.h:109
std::string registryURI()
Definition: State.h:99
std::list< ServiceId > services()
get map of registrations
Definition: Registrar.h:79
void clearDataPath()
Definition: State.h:53
virtual bool initialized() const
Definition: State.h:47
Base class for application state.
Definition: AppState.h:56
void shutdown()
do any necessary shutdown functions,
namespace for CASAtools classes within &quot;CASA code&quot;
Definition: State.h:42
transform(a.begin(), a.end(), std::ostream_iterator< int >(cout,"\n"), compose(unary(h), compose(unary(f), unary(f))))
Global Functions.
std::list< std::string > data_path
Definition: State.h:108
bool remove(std::string id)
returns true if a registration for &#39;id&#39; was found