XRootD
XrdClEnv.hh
Go to the documentation of this file.
1 //------------------------------------------------------------------------------
2 // Copyright (c) 2011-2012 by European Organization for Nuclear Research (CERN)
3 // Author: Lukasz Janyst <ljanyst@cern.ch>
4 //------------------------------------------------------------------------------
5 // XRootD is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // XRootD is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with XRootD. If not, see <http://www.gnu.org/licenses/>.
17 //------------------------------------------------------------------------------
18 
19 #ifndef __XRD_CL_ENV_HH__
20 #define __XRD_CL_ENV_HH__
21 
22 #include <map>
23 #include <string>
24 #include <utility>
25 #include <algorithm>
26 
27 #include "XrdSys/XrdSysPthread.hh"
28 
29 namespace XrdCl
30 {
31  //----------------------------------------------------------------------------
36  //----------------------------------------------------------------------------
37  class Env
38  {
39  public:
40  //------------------------------------------------------------------------
42  //------------------------------------------------------------------------
43  virtual ~Env() {}
44 
45  //------------------------------------------------------------------------
49  //------------------------------------------------------------------------
50  bool GetString( const std::string &key, std::string &value );
51 
52  //------------------------------------------------------------------------
57  //------------------------------------------------------------------------
58  bool PutString( const std::string &key, const std::string &value );
59 
60  //------------------------------------------------------------------------
65  //------------------------------------------------------------------------
66  bool DelString( const std::string &key );
67 
68  //------------------------------------------------------------------------
72  //------------------------------------------------------------------------
73  bool GetInt( const std::string &key, int &value );
74 
75  //------------------------------------------------------------------------
80  //------------------------------------------------------------------------
81  bool PutInt( const std::string &key, int value );
82 
83  //------------------------------------------------------------------------
88  //------------------------------------------------------------------------
89  bool DelInt( const std::string &key );
90 
91  //------------------------------------------------------------------------
95  //------------------------------------------------------------------------
96  bool GetPtr( const std::string &key, void* &value );
97 
98  //------------------------------------------------------------------------
102  //------------------------------------------------------------------------
103  bool PutPtr( const std::string &key, void* value );
104 
105  //------------------------------------------------------------------------
110  //------------------------------------------------------------------------
111  bool ImportInt( const std::string &key, const std::string &shellKey );
112 
113  //------------------------------------------------------------------------
118  //------------------------------------------------------------------------
119  bool ImportString( const std::string &key, const std::string &shellKey );
120 
121  //------------------------------------------------------------------------
128  //------------------------------------------------------------------------
129  bool GetDefaultIntValue( const std::string &key, int &value );
130 
131  //------------------------------------------------------------------------
138  //------------------------------------------------------------------------
139  bool GetDefaultStringValue( const std::string &key, std::string &value );
140 
141  //------------------------------------------------------------------------
142  // Lock the environment for writing
143  //------------------------------------------------------------------------
144  void WriteLock()
145  {
146  pLock.WriteLock();
147  }
148 
149  //------------------------------------------------------------------------
150  // Unlock the environment
151  //------------------------------------------------------------------------
152  void UnLock()
153  {
154  pLock.UnLock();
155  }
156 
157  //------------------------------------------------------------------------
158  // Re-initialize the lock
159  //------------------------------------------------------------------------
161  {
162  // this is really shaky, but seems to work on linux and fork safety
163  // is probably not required anywhere else
164  pLock.UnLock();
165  pLock.ReInitialize();
166  }
167 
168  //------------------------------------------------------------------------
169  // Re-create the lock in the same memory
170  //------------------------------------------------------------------------
172  {
173  new( &pLock )XrdSysRWLock();
174  }
175 
176  private:
177 
178  //------------------------------------------------------------------------
179  // Unify the key, make sure it is not case sensitive and strip it of
180  // the XRD_ prefix if necessary
181  //------------------------------------------------------------------------
182  inline std::string UnifyKey( std::string key )
183  {
184  //----------------------------------------------------------------------
185  // Make the key lower case
186  //----------------------------------------------------------------------
187  std::transform( key.begin(), key.end(), key.begin(), ::tolower );
188 
189  //----------------------------------------------------------------------
190  // Strip the `xrd_` prefix if necessary
191  //----------------------------------------------------------------------
192  static const char prefix[] = "xrd_";
193  if( key.compare( 0, sizeof( prefix ) - 1, prefix ) == 0 )
194  key = key.substr( sizeof( prefix ) - 1 );
195 
196  return key;
197  }
198 
199  std::string GetEnv( const std::string &key );
200  typedef std::map<std::string, std::pair<std::string, bool> > StringMap;
201  typedef std::map<std::string, std::pair<int, bool> > IntMap;
202  typedef std::map<std::string, void* > PtrMap;
203 
204  XrdSysRWLock pLock;
205  StringMap pStringMap;
206  IntMap pIntMap;
207  PtrMap pPtrMap;
208  };
209 }
210 
211 #endif // __XRD_CL_ENV_HH__
void RecreateLock()
Definition: XrdClEnv.hh:171
bool DelInt(const std::string &key)
Definition: XrdClEnv.cc:174
bool PutInt(const std::string &key, int value)
Definition: XrdClEnv.cc:136
bool PutString(const std::string &key, const std::string &value)
Definition: XrdClEnv.cc:52
bool GetDefaultIntValue(const std::string &key, int &value)
Definition: XrdClEnv.cc:284
bool ImportString(const std::string &key, const std::string &shellKey)
Definition: XrdClEnv.cc:266
void ReInitializeLock()
Definition: XrdClEnv.hh:160
void WriteLock()
Definition: XrdClEnv.hh:144
bool PutPtr(const std::string &key, void *value)
Definition: XrdClEnv.cc:221
bool ImportInt(const std::string &key, const std::string &shellKey)
Definition: XrdClEnv.cc:237
bool GetPtr(const std::string &key, void *&value)
Definition: XrdClEnv.cc:200
bool DelString(const std::string &key)
Definition: XrdClEnv.cc:89
bool GetString(const std::string &key, std::string &value)
Definition: XrdClEnv.cc:31
void UnLock()
Definition: XrdClEnv.hh:152
virtual ~Env()
Destructor.
Definition: XrdClEnv.hh:43
bool GetInt(const std::string &key, int &value)
Definition: XrdClEnv.cc:115
bool GetDefaultStringValue(const std::string &key, std::string &value)
Definition: XrdClEnv.cc:296
void ReInitialize(PrefType)