Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/xrootd/private/XrdCl/XrdClCtx.hh was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

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 SRC_XRDCL_XRDCLCTX_HH_
0027 #define SRC_XRDCL_XRDCLCTX_HH_
0028 
0029 #include <memory>
0030 #include <stdexcept>
0031 
0032 namespace XrdCl
0033 {
0034   //---------------------------------------------------------------------------
0035   //! Utility class for storing a pointer to operation context
0036   //---------------------------------------------------------------------------
0037   template<typename T>
0038   struct Ctx : protected std::shared_ptr<T*>
0039   {
0040     //-------------------------------------------------------------------------
0041     //! Default constructor
0042     //-------------------------------------------------------------------------
0043     Ctx() : std::shared_ptr<T*>( std::make_shared<T*>() )
0044     {
0045     }
0046 
0047     //-------------------------------------------------------------------------
0048     //! Constructor (from pointer)
0049     //-------------------------------------------------------------------------
0050     Ctx( T *ctx ) : std::shared_ptr<T*>( std::make_shared<T*>( ctx ) )
0051     {
0052     }
0053 
0054     //-------------------------------------------------------------------------
0055     //! Constructor (from reference)
0056     //-------------------------------------------------------------------------
0057     Ctx( T &ctx ) : std::shared_ptr<T*>( std::make_shared<T*>( &ctx ) )
0058     {
0059     }
0060 
0061     //-------------------------------------------------------------------------
0062     //! Copy constructor
0063     //-------------------------------------------------------------------------
0064     Ctx( const Ctx &ctx ) : std::shared_ptr<T*>( ctx )
0065     {
0066     }
0067 
0068     //-------------------------------------------------------------------------
0069     //! Move constructor
0070     //-------------------------------------------------------------------------
0071     Ctx( Ctx &&ctx ) : std::shared_ptr<T*>( std::move( ctx ) )
0072     {
0073     }
0074 
0075     //-------------------------------------------------------------------------
0076     //! Assignment operator (from pointer)
0077     //-------------------------------------------------------------------------
0078     Ctx& operator=( T *ctx )
0079     {
0080       *this->get() = ctx;
0081       return *this;
0082     }
0083 
0084     //-------------------------------------------------------------------------
0085     //! Assignment operator (from reference)
0086     //-------------------------------------------------------------------------
0087     Ctx& operator=( T &ctx )
0088     {
0089       *this->get() = &ctx;
0090       return *this;
0091     }
0092 
0093     //------------------------------------------------------------------------
0094     //! Dereferencing operator. Note if Ctx is a null-reference this will
0095     //! trigger an exception
0096     //!
0097     //! @return : reference to the underlying value
0098     //! @throws : std::logic_error
0099     //------------------------------------------------------------------------
0100     T& operator*() const
0101     {
0102       if( !bool( *this->get() ) ) throw std::logic_error( "XrdCl::Ctx contains no value!" );
0103       return **this->get();
0104     }
0105 
0106     //------------------------------------------------------------------------
0107     //! Dereferencing member operator. Note if Ctx is a null-reference
0108     //! this will trigger an exception
0109     //!
0110     //! @return : pointer to the underlying value
0111     //! @throws : std::logic_error
0112     //------------------------------------------------------------------------
0113     T* operator->() const
0114     {
0115       if( !bool( *this->get() ) ) throw std::logic_error( "XrdCl::Ctx contains no value!" );
0116       return *this->get();
0117     }
0118   };
0119 }
0120 
0121 
0122 #endif /* SRC_XRDCL_XRDCLCTX_HH_ */