Back to home page

EIC code displayed by LXR

 
 

    


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

0001 //------------------------------------------------------------------------------
0002 // Copyright (c) 2011-2017 by European Organization for Nuclear Research (CERN)
0003 // Author: Krzysztof Jamrog <krzysztof.piotr.jamrog@cern.ch>,
0004 //         Michal Simon <michal.simon@cern.ch>
0005 //------------------------------------------------------------------------------
0006 // This file is part of the XRootD software suite.
0007 //
0008 // XRootD is free software: you can redistribute it and/or modify
0009 // it under the terms of the GNU Lesser General Public License as published by
0010 // the Free Software Foundation, either version 3 of the License, or
0011 // (at your option) any later version.
0012 //
0013 // XRootD is distributed in the hope that it will be useful,
0014 // but WITHOUT ANY WARRANTY; without even the implied warranty of
0015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0016 // GNU General Public License for more details.
0017 //
0018 // You should have received a copy of the GNU Lesser General Public License
0019 // along with XRootD.  If not, see <http://www.gnu.org/licenses/>.
0020 //
0021 // In applying this licence, CERN does not waive the privileges and immunities
0022 // granted to it by virtue of its status as an Intergovernmental Organization
0023 // or submit itself to any jurisdiction.
0024 //------------------------------------------------------------------------------
0025 
0026 #ifndef __XRD_CL_OPERATION_PARAMS_HH__
0027 #define __XRD_CL_OPERATION_PARAMS_HH__
0028 
0029 #include "XrdCl/XrdClFwd.hh"
0030 #include "XrdCl/XrdClOptional.hh"
0031 
0032 #include <future>
0033 #include <string>
0034 #include <sstream>
0035 #include <unordered_map>
0036 
0037 namespace XrdCl
0038 {
0039 
0040   //----------------------------------------------------------------------------
0041   //! Base class for Arg.
0042   //----------------------------------------------------------------------------
0043   template<typename T>
0044   class ArgBase
0045   {
0046     public:
0047 
0048       //------------------------------------------------------------------------
0049       //! Default Constructor.
0050       //------------------------------------------------------------------------
0051       ArgBase()
0052       {
0053       }
0054 
0055       //------------------------------------------------------------------------
0056       //! Destructor.
0057       //------------------------------------------------------------------------
0058       virtual ~ArgBase()
0059       {
0060       }
0061 
0062       //------------------------------------------------------------------------
0063       //! Constructor
0064       //!
0065       //! @param value : the value of the argument
0066       //------------------------------------------------------------------------
0067       ArgBase( T value ) : holder( new PlainValue( std::move( value ) ) )
0068       {
0069       }
0070 
0071       //------------------------------------------------------------------------
0072       //! Constructor.
0073       //!
0074       //! @param ftr : future value of the argument
0075       //------------------------------------------------------------------------
0076       ArgBase( std::future<T> &&ftr ) : holder( new FutureValue( std::move( ftr ) ) )
0077       {
0078       }
0079 
0080       //------------------------------------------------------------------------
0081       //! Constructor.
0082       //!
0083       //! @param fwd : forwarded value of the argument
0084       //------------------------------------------------------------------------
0085       ArgBase( const Fwd<T> &fwd ) : holder( new FwdValue( fwd ) )
0086       {
0087       }
0088 
0089       //------------------------------------------------------------------------
0090       //! Get Constructor.
0091       //------------------------------------------------------------------------
0092       ArgBase( ArgBase &&arg ) : holder( std::move( arg.holder ) )
0093       {
0094       }
0095 
0096       //------------------------------------------------------------------------
0097       //! @return : value of the argument
0098       //------------------------------------------------------------------------
0099       inline T& Get() const
0100       {
0101         if( !holder ) throw std::logic_error( "XrdCl::ArgBase::Get(): value not set." );
0102         return holder->Get();
0103       }
0104 
0105       operator T() const
0106       {
0107         return Get();
0108       }
0109 
0110     protected:
0111 
0112       //------------------------------------------------------------------------
0113       //! Abstract class for holding a value
0114       //------------------------------------------------------------------------
0115       struct ValueHolder
0116       {
0117         //----------------------------------------------------------------------
0118         //! Virtual Destructor (important ;-).
0119         //----------------------------------------------------------------------
0120         virtual ~ValueHolder()
0121         {
0122         }
0123 
0124         //----------------------------------------------------------------------
0125         //! @return : the value
0126         //----------------------------------------------------------------------
0127         virtual T& Get() = 0;
0128       };
0129 
0130       //------------------------------------------------------------------------
0131       //! A helper class for holding plain value
0132       //------------------------------------------------------------------------
0133       struct PlainValue : public ValueHolder
0134       {
0135           //--------------------------------------------------------------------
0136           //! Constructor
0137           //!
0138           //! @param value : the value to be hold by us
0139           //--------------------------------------------------------------------
0140           PlainValue( T &&value ) : value( std::move( value ) )
0141           {
0142           }
0143 
0144           //--------------------------------------------------------------------
0145           //! @return : the value
0146           //--------------------------------------------------------------------
0147           T& Get()
0148           {
0149             return value;
0150           }
0151 
0152         private:
0153           //--------------------------------------------------------------------
0154           //! the value
0155           //--------------------------------------------------------------------
0156           T value;
0157       };
0158 
0159       //------------------------------------------------------------------------
0160       //! A helper class for holding future value
0161       //------------------------------------------------------------------------
0162       struct FutureValue : public ValueHolder
0163       {
0164           //--------------------------------------------------------------------
0165           //! Constructor
0166           //!
0167           //! @param ftr : the future value to be hold by us
0168           //--------------------------------------------------------------------
0169           FutureValue( std::future<T> &&ftr ) : ftr( std::move( ftr ) )
0170           {
0171           }
0172 
0173           //--------------------------------------------------------------------
0174           //! @return : the value
0175           //--------------------------------------------------------------------
0176           T& Get()
0177           {
0178             if( val ) return *val;
0179             val = ftr.get();
0180             return *val;
0181           }
0182 
0183         private:
0184           //--------------------------------------------------------------------
0185           //! the future value
0186           //--------------------------------------------------------------------
0187           std::future<T> ftr;
0188           Optional<T>    val;
0189       };
0190 
0191       //------------------------------------------------------------------------
0192       //! A helper class for holding forwarded value
0193       //------------------------------------------------------------------------
0194       struct FwdValue : public ValueHolder
0195       {
0196           //--------------------------------------------------------------------
0197           //! Constructor
0198           //!
0199           //! @param fwd : the forwarded value to be hold by us
0200           //--------------------------------------------------------------------
0201           FwdValue( const Fwd<T> &fwd ) : fwd( fwd )
0202           {
0203           }
0204 
0205           //--------------------------------------------------------------------
0206           //! @return : the value
0207           //--------------------------------------------------------------------
0208           T& Get()
0209           {
0210             return *fwd;
0211           }
0212 
0213         private:
0214           //--------------------------------------------------------------------
0215           //! the forwarded value
0216           //--------------------------------------------------------------------
0217           Fwd<T> fwd;
0218       };
0219 
0220       //------------------------------------------------------------------------
0221       //! Holds the value of the argument
0222       //------------------------------------------------------------------------
0223       std::unique_ptr<ValueHolder> holder;
0224   };
0225 
0226   //----------------------------------------------------------------------------
0227   //! Operation argument.
0228   //! The argument is optional, user may initialize it with 'notdef'
0229   //!
0230   //! @arg T : real type of the argument
0231   //----------------------------------------------------------------------------
0232   template<typename T>
0233   class Arg : public ArgBase<T>
0234   {
0235     public:
0236 
0237       //------------------------------------------------------------------------
0238       //! Default Constructor.
0239       //------------------------------------------------------------------------
0240       Arg()
0241       {
0242       }
0243 
0244       //------------------------------------------------------------------------
0245       //! Constructor.
0246       //!
0247       //! @param value : value of the argument (will be std::moved)
0248       //------------------------------------------------------------------------
0249       Arg( T value ) : ArgBase<T>( std::move( value ) )
0250       {
0251       }
0252 
0253       //------------------------------------------------------------------------
0254       //! Constructor.
0255       //!
0256       //! @param ftr : future value of the argument (will be std::moved)
0257       //------------------------------------------------------------------------
0258       Arg( std::future<T> &&ftr ) : ArgBase<T>( std::move( ftr ) )
0259       {
0260       }
0261 
0262       //------------------------------------------------------------------------
0263       //! Constructor.
0264       //!
0265       //! @param fwd : forwarded value of the argument (will be std::moved)
0266       //------------------------------------------------------------------------
0267       Arg( const Fwd<T> &fwd ) : ArgBase<T>( fwd )
0268       {
0269       }
0270 
0271       //------------------------------------------------------------------------
0272       //! Get Constructor.
0273       //------------------------------------------------------------------------
0274       Arg( Arg &&arg ) : ArgBase<T>( std::move( arg ) )
0275       {
0276       }
0277 
0278       //------------------------------------------------------------------------
0279       //! Get-Assignment.
0280       //------------------------------------------------------------------------
0281       Arg& operator=( Arg &&arg )
0282       {
0283         if( &arg == this ) return *this;
0284         this->holder = std::move( arg.holder );
0285         return *this;
0286       }
0287   };
0288 
0289   //----------------------------------------------------------------------------
0290   //! Operation argument.
0291   //! Specialized for 'std::string', might be constructed in addition from c-like
0292   //! string (const char*)
0293   //----------------------------------------------------------------------------
0294   template<>
0295   class Arg<std::string> : public ArgBase<std::string>
0296   {
0297     public:
0298 
0299       //------------------------------------------------------------------------
0300       //! Default Constructor.
0301       //------------------------------------------------------------------------
0302       Arg()
0303       {
0304       }
0305 
0306       //------------------------------------------------------------------------
0307       //! Constructor.
0308       //!
0309       //! @param str : value of the argument
0310       //------------------------------------------------------------------------
0311       Arg( std::string str ) : ArgBase<std::string>( str )
0312       {
0313       }
0314 
0315       //------------------------------------------------------------------------
0316       //! Constructor.
0317       //!
0318       //! @param cstr : value of the argument
0319       //------------------------------------------------------------------------
0320       Arg( const char *cstr ) : ArgBase<std::string>( cstr )
0321       {
0322       }
0323 
0324       //------------------------------------------------------------------------
0325       //! Constructor.
0326       //------------------------------------------------------------------------
0327       Arg( std::future<std::string> &&ftr ) : ArgBase<std::string>( std::move( ftr ) )
0328       {
0329       }
0330 
0331       //------------------------------------------------------------------------
0332       //! Constructor.
0333       //------------------------------------------------------------------------
0334       Arg( const Fwd<std::string> &fwd ) : ArgBase<std::string>( fwd )
0335       {
0336       }
0337 
0338 
0339       //------------------------------------------------------------------------
0340       //! Get Constructor.
0341       //-----------------------------------------------------------------------
0342       Arg( Arg &&arg ) : ArgBase<std::string>( std::move( arg ) )
0343       {
0344       }
0345 
0346       //------------------------------------------------------------------------
0347       //! Get-Assignment.
0348       //------------------------------------------------------------------------
0349       Arg& operator=( Arg &&arg )
0350       {
0351         if( &arg == this ) return *this;
0352         this->holder = std::move( arg.holder );
0353         return *this;
0354       }
0355   };
0356 }
0357 
0358 #endif // __XRD_CL_OPERATION_PARAMS_HH__