casacore
Casarc.h
Go to the documentation of this file.
1 //# Casarc.h: Class to read the casa general resource files
2 //# Copyright (C) 2009
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_CASARC_H
29 #define CASA_CASARC_H
30 
31 #include <casacore/casa/aips.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <string>
35 #include <list>
36 #include <map>
37 
38 namespace casacore { //# NAMESPACE CASACORE - BEGIN
39 
40  class Casarc {
41 
42  friend class CasarcCleanup;
43 
44  public:
45 
46  typedef std::map<std::string,std::string>::const_iterator iterator;
47 
48  // return default casarc file, e.g. ~/.casarc
49  static Casarc &instance( );
50 
51  // set and clear the default casarc path (to something other than ~/.casarc)
52  static void setDefaultPath( const std::string &path );
53  static void clearDefaultPath( );
54 
55  // return a casarc object attached to a particular file
56  static Casarc &instance( const std::string &path );
57 
58  // return the list of rcfiles that have been loaded
59  static const std::list<Casarc*> & list( );
60 
61  // adds the keyword->value mapping
62  void put( const std::string &keyword, const std::string &value );
63 
64  // retrieves the mapping for keyword (or "" if the mapping is not defined)
65  std::string get( const std::string &keyword );
66 
67  // retrieves the mapping for keyword in value (and returns true if the
68  // mapping is defined or false otherwise)
69  bool get( const std::string &keyword, std::string &value );
70 
71  size_t size( ) const;
72 
73  // path to the file that this Casarc mirrors...
74  const std::string &path( ) const { return filename; }
75 
78 
79  private:
80 
81  class meta_entry_ {
82  public:
83  meta_entry_( off_t keys_, int keyl_, off_t vals_, int vall_, off_t times_, int timel_ ) :
84  key_offset_(keys_), key_len_(keyl_),
85  val_offset_(vals_), val_len_(vall_),
86  time_offset_(times_), time_len_(timel_) { }
87 
88  off_t key_offset( ) const { return key_offset_; }
89  int key_length( ) const { return key_len_; }
90 
91  off_t value_offset( ) const { return val_offset_; }
92  int value_length( ) const { return val_len_; }
93 
94  off_t time_offset( ) const { return time_offset_; }
95  int time_length( ) const { return time_len_; }
96 
97  private:
98  off_t key_offset_;
99  int key_len_;
100  off_t val_offset_;
101  int val_len_;
104  };
105 
106  char *mapped_file;
108 
109  std::list<int> stale_fds;
110  void close( int );
111 
112  std::list<pid_t> have_lock;
113 
115  // returns a file descriptor or -1 on error...
116  int lock( lock_mode mode );
117  void unlock( int fd );
118 
119  void sync( );
120  double current_modification_time( struct stat &buf );
121 
122  std::string filename;
123  double timestamp;
124 
125  std::map<std::string,std::string> rcmap;
126  // this is broken off separate from the rcmap to allow the
127  // rcmap to be used for iteration by users of this class...
128  std::map<std::string,meta_entry_> rcmetamap;
129 
130  void read_file( );
131 
132  ino_t inode;
133 
134  // singleton Casarcs, (inode->casarc)
135  static std::map<ino_t,Casarc*> *rcfiles;
136  static std::map<std::string,Casarc*> *filenames;
137  static std::list<Casarc*> *rclist;
138  static std::string *default_path;
139  // path is the path to the .casarc (or .aipsrc file)
140  Casarc( const std::string &path );
141  static bool initialized;
142  static void startup( );
143  static void shutdown( );
144  ~Casarc( ) { }
145  };
146 
147  static class CasarcCleanup {
148  public:
150  ~CasarcCleanup( ) { if ( --creation_count == 0 ) { Casarc::shutdown( ); } }
151  private:
152  static unsigned int creation_count;
154 
155 } //# NAMESPACE CASACORE - END
156 
157 #endif
static unsigned int creation_count
Definition: Casarc.h:152
Definition: Casarc.h:81
int time_length() const
Definition: Casarc.h:95
meta_entry_(off_t keys_, int keyl_, off_t vals_, int vall_, off_t times_, int timel_)
Definition: Casarc.h:83
int key_len_
Definition: Casarc.h:99
int val_len_
Definition: Casarc.h:101
off_t key_offset_
Definition: Casarc.h:98
off_t time_offset() const
Definition: Casarc.h:94
off_t value_offset() const
Definition: Casarc.h:91
off_t time_offset_
Definition: Casarc.h:102
off_t val_offset_
Definition: Casarc.h:100
int value_length() const
Definition: Casarc.h:92
off_t key_offset() const
Definition: Casarc.h:88
int time_len_
Definition: Casarc.h:103
int key_length() const
Definition: Casarc.h:89
Casarc(const std::string &path)
path is the path to the.casarc (or.aipsrc file)
bool get(const std::string &keyword, std::string &value)
retrieves the mapping for keyword in value (and returns true if the mapping is defined or false other...
static Casarc & instance()
return default casarc file, e.g.
off_t mapped_file_size
Definition: Casarc.h:107
iterator begin()
std::map< std::string, meta_entry_ > rcmetamap
this is broken off separate from the rcmap to allow the rcmap to be used for iteration by users of th...
Definition: Casarc.h:128
void put(const std::string &keyword, const std::string &value)
adds the keyword->value mapping
static void startup()
const std::string & path() const
path to the file that this Casarc mirrors...
Definition: Casarc.h:74
size_t size() const
std::map< std::string, std::string >::const_iterator iterator
Definition: Casarc.h:46
static std::string * default_path
Definition: Casarc.h:138
static std::map< ino_t, Casarc * > * rcfiles
singleton Casarcs, (inode->casarc)
Definition: Casarc.h:135
static void shutdown()
static void setDefaultPath(const std::string &path)
set and clear the default casarc path (to something other than ~/.casarc)
int lock(lock_mode mode)
returns a file descriptor or -1 on error...
std::list< int > stale_fds
Definition: Casarc.h:109
std::string get(const std::string &keyword)
retrieves the mapping for keyword (or "" if the mapping is not defined)
static void clearDefaultPath()
static Casarc & instance(const std::string &path)
return a casarc object attached to a particular file
std::map< std::string, std::string > rcmap
Definition: Casarc.h:125
iterator end()
static std::list< Casarc * > * rclist
Definition: Casarc.h:137
void unlock(int fd)
static std::map< std::string, Casarc * > * filenames
Definition: Casarc.h:136
static const std::list< Casarc * > & list()
return the list of rcfiles that have been loaded
std::string filename
Definition: Casarc.h:122
static bool initialized
Definition: Casarc.h:141
char * mapped_file
Definition: Casarc.h:106
double current_modification_time(struct stat &buf)
std::list< pid_t > have_lock
Definition: Casarc.h:112
double timestamp
Definition: Casarc.h:123
this file contains all the compiler specific defines
Definition: mainpage.dox:28
static class casacore::CasarcCleanup local_cleanup_object
LatticeExprNode value(const LatticeExprNode &expr)
This function returns the value of the expression without a mask.