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  //------------------------------------------------------------------------
64  //------------------------------------------------------------------------
65  bool GetInt( const std::string &key, int &value );
66 
67  //------------------------------------------------------------------------
72  //------------------------------------------------------------------------
73  bool PutInt( const std::string &key, int value );
74 
75  //------------------------------------------------------------------------
79  //------------------------------------------------------------------------
80  bool GetPtr( const std::string &key, void* &value );
81 
82  //------------------------------------------------------------------------
86  //------------------------------------------------------------------------
87  bool PutPtr( const std::string &key, void* value );
88 
89  //------------------------------------------------------------------------
94  //------------------------------------------------------------------------
95  bool ImportInt( const std::string &key, const std::string &shellKey );
96 
97  //------------------------------------------------------------------------
102  //------------------------------------------------------------------------
103  bool ImportString( const std::string &key, const std::string &shellKey );
104 
105  //------------------------------------------------------------------------
112  //------------------------------------------------------------------------
113  bool GetDefaultIntValue( const std::string &key, int &value );
114 
115  //------------------------------------------------------------------------
122  //------------------------------------------------------------------------
123  bool GetDefaultStringValue( const std::string &key, std::string &value );
124 
125  //------------------------------------------------------------------------
126  // Lock the environment for writing
127  //------------------------------------------------------------------------
128  void WriteLock()
129  {
130  pLock.WriteLock();
131  }
132 
133  //------------------------------------------------------------------------
134  // Unlock the environment
135  //------------------------------------------------------------------------
136  void UnLock()
137  {
138  pLock.UnLock();
139  }
140 
141  //------------------------------------------------------------------------
142  // Re-initialize the lock
143  //------------------------------------------------------------------------
145  {
146  // this is really shaky, but seems to work on linux and fork safety
147  // is probably not required anywhere else
148  pLock.UnLock();
149  pLock.ReInitialize();
150  }
151 
152  //------------------------------------------------------------------------
153  // Re-create the lock in the same memory
154  //------------------------------------------------------------------------
156  {
157  new( &pLock )XrdSysRWLock();
158  }
159 
160  private:
161 
162  //------------------------------------------------------------------------
163  // Unify the key, make sure it is not case sensitive and strip it of
164  // the XRD_ prefix if necessary
165  //------------------------------------------------------------------------
166  inline std::string UnifyKey( std::string key )
167  {
168  //----------------------------------------------------------------------
169  // Make the key lower case
170  //----------------------------------------------------------------------
171  std::transform( key.begin(), key.end(), key.begin(), ::tolower );
172 
173  //----------------------------------------------------------------------
174  // Strip the `xrd_` prefix if necessary
175  //----------------------------------------------------------------------
176  static const char prefix[] = "xrd_";
177  if( key.compare( 0, sizeof( prefix ) - 1, prefix ) == 0 )
178  key = key.substr( sizeof( prefix ) - 1 );
179 
180  return key;
181  }
182 
183  std::string GetEnv( const std::string &key );
184  typedef std::map<std::string, std::pair<std::string, bool> > StringMap;
185  typedef std::map<std::string, std::pair<int, bool> > IntMap;
186  typedef std::map<std::string, void* > PtrMap;
187 
188  XrdSysRWLock pLock;
189  StringMap pStringMap;
190  IntMap pIntMap;
191  PtrMap pPtrMap;
192  };
193 }
194 
195 #endif // __XRD_CL_ENV_HH__
void RecreateLock()
Definition: XrdClEnv.hh:155
bool PutInt(const std::string &key, int value)
Definition: XrdClEnv.cc:110
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:232
bool ImportString(const std::string &key, const std::string &shellKey)
Definition: XrdClEnv.cc:214
void ReInitializeLock()
Definition: XrdClEnv.hh:144
void WriteLock()
Definition: XrdClEnv.hh:128
bool PutPtr(const std::string &key, void *value)
Definition: XrdClEnv.cc:169
bool ImportInt(const std::string &key, const std::string &shellKey)
Definition: XrdClEnv.cc:185
bool GetPtr(const std::string &key, void *&value)
Definition: XrdClEnv.cc:148
bool GetString(const std::string &key, std::string &value)
Definition: XrdClEnv.cc:31
void UnLock()
Definition: XrdClEnv.hh:136
virtual ~Env()
Destructor.
Definition: XrdClEnv.hh:43
bool GetInt(const std::string &key, int &value)
Definition: XrdClEnv.cc:89
bool GetDefaultStringValue(const std::string &key, std::string &value)
Definition: XrdClEnv.cc:244
void ReInitialize(PrefType)