Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:15:39

0001 //------------------------------------------------------------------------------
0002 // Copyright (c) 2011-2012 by European Organization for Nuclear Research (CERN)
0003 // Author: Lukasz Janyst <ljanyst@cern.ch>
0004 //------------------------------------------------------------------------------
0005 // XRootD is free software: you can redistribute it and/or modify
0006 // it under the terms of the GNU Lesser General Public License as published by
0007 // the Free Software Foundation, either version 3 of the License, or
0008 // (at your option) any later version.
0009 //
0010 // XRootD is distributed in the hope that it will be useful,
0011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
0012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013 // GNU General Public License for more details.
0014 //
0015 // You should have received a copy of the GNU Lesser General Public License
0016 // along with XRootD.  If not, see <http://www.gnu.org/licenses/>.
0017 //------------------------------------------------------------------------------
0018 
0019 #ifndef __XRD_CL_ENV_HH__
0020 #define __XRD_CL_ENV_HH__
0021 
0022 #include <map>
0023 #include <string>
0024 #include <utility>
0025 #include <algorithm>
0026 
0027 #include "XrdSys/XrdSysPthread.hh"
0028 
0029 namespace XrdCl
0030 {
0031   //----------------------------------------------------------------------------
0032   //! A simple key value store intended to hold global configuration.
0033   //! It is able to import the settings from the shell environment, the
0034   //! variables imported this way supersede these provided from the C++
0035   //! code.
0036   //----------------------------------------------------------------------------
0037   class Env
0038   {
0039     public:
0040       //------------------------------------------------------------------------
0041       //! Destructor
0042       //------------------------------------------------------------------------
0043       virtual ~Env() {}
0044 
0045       //------------------------------------------------------------------------
0046       //! Get a string associated to the given key
0047       //!
0048       //! @return true if the value was found, false otherwise
0049       //------------------------------------------------------------------------
0050       bool GetString( const std::string &key, std::string &value );
0051 
0052       //------------------------------------------------------------------------
0053       //! Associate a string with the given key
0054       //!
0055       //! @return false if there is already a shell-imported setting for this
0056       //!         key, true otherwise
0057       //------------------------------------------------------------------------
0058       bool PutString( const std::string &key, const std::string &value );
0059 
0060       //------------------------------------------------------------------------
0061       //! Get an int associated to the given key
0062       //!
0063       //! @return true if the value was found, false otherwise
0064       //------------------------------------------------------------------------
0065       bool GetInt( const std::string &key, int &value );
0066 
0067       //------------------------------------------------------------------------
0068       //! Associate an int with the given key
0069       //!
0070       //! @return false if there is already a shell-imported setting for this
0071       //!         key, true otherwise
0072       //------------------------------------------------------------------------
0073       bool PutInt( const std::string &key, int value );
0074 
0075       //------------------------------------------------------------------------
0076       //! Import an int from the shell environment. Any imported setting
0077       //! takes precedence over the one set by other means.
0078       //!
0079       //! @return true if the setting exists in the shell, false otherwise
0080       //------------------------------------------------------------------------
0081       bool ImportInt( const std::string &key, const std::string &shellKey );
0082 
0083       //------------------------------------------------------------------------
0084       //! Import a string from the shell environment. Any imported setting
0085       //! takes precedence over the one set by ther means.
0086       //!
0087       //! @return true if the setting exists in the shell, false otherwise
0088       //------------------------------------------------------------------------
0089       bool ImportString( const std::string &key, const std::string &shellKey );
0090 
0091       //------------------------------------------------------------------------
0092       //! Get default integer value for the given key
0093       //! @param key   : the key
0094       //! @param value : output parameter, default value corresponding to
0095       //!                the key
0096       //! @return      : true if a default integer value for the given key
0097       //!                exists, false otherwise
0098       //------------------------------------------------------------------------
0099       bool GetDefaultIntValue( const std::string &key, int &value );
0100 
0101       //------------------------------------------------------------------------
0102       //! Get default string value for the given key
0103       //! @param key   : the key
0104       //! @param value : output parameter, default value corresponding to
0105       //!                the key
0106       //! @return      : true if a default string value for the given key
0107       //!                exists, false otherwise
0108       //------------------------------------------------------------------------
0109       bool GetDefaultStringValue( const std::string &key, std::string &value );
0110 
0111       //------------------------------------------------------------------------
0112       // Lock the environment for writing
0113       //------------------------------------------------------------------------
0114       void WriteLock()
0115       {
0116         pLock.WriteLock();
0117       }
0118 
0119       //------------------------------------------------------------------------
0120       // Unlock the environment
0121       //------------------------------------------------------------------------
0122       void UnLock()
0123       {
0124         pLock.UnLock();
0125       }
0126 
0127       //------------------------------------------------------------------------
0128       // Re-initialize the lock
0129       //------------------------------------------------------------------------
0130       void ReInitializeLock()
0131       {
0132         // this is really shaky, but seems to work on linux and fork safety
0133         // is probably not required anywhere else
0134         pLock.UnLock();
0135         pLock.ReInitialize();
0136       }
0137 
0138       //------------------------------------------------------------------------
0139       // Re-create the lock in the same memory
0140       //------------------------------------------------------------------------
0141       void RecreateLock()
0142       {
0143         new( &pLock )XrdSysRWLock();
0144       }
0145 
0146     private:
0147 
0148       //------------------------------------------------------------------------
0149       // Unify the key, make sure it is not case sensitive and strip it of
0150       // the XRD_ prefix if necessary
0151       //------------------------------------------------------------------------
0152       inline std::string UnifyKey( std::string key )
0153       {
0154         //----------------------------------------------------------------------
0155         // Make the key lower case
0156         //----------------------------------------------------------------------
0157         std::transform( key.begin(), key.end(), key.begin(), ::tolower );
0158 
0159         //----------------------------------------------------------------------
0160         // Strip the `xrd_` prefix if necessary
0161         //----------------------------------------------------------------------
0162         static const char prefix[] = "xrd_";
0163         if( key.compare( 0, sizeof( prefix ) - 1, prefix ) == 0 )
0164           key = key.substr( sizeof( prefix ) - 1 );
0165 
0166         return key;
0167       }
0168 
0169       std::string GetEnv( const std::string &key );
0170       typedef std::map<std::string, std::pair<std::string, bool> > StringMap;
0171       typedef std::map<std::string, std::pair<int, bool> >         IntMap;
0172 
0173       XrdSysRWLock pLock;
0174       StringMap    pStringMap;
0175       IntMap       pIntMap;
0176   };
0177 }
0178 
0179 #endif // __XRD_CL_ENV_HH__