XRootD
XrdClCtx.hh
Go to the documentation of this file.
1 //------------------------------------------------------------------------------
2 // Copyright (c) 2011-2017 by European Organization for Nuclear Research (CERN)
3 // Author: Krzysztof Jamrog <krzysztof.piotr.jamrog@cern.ch>,
4 // Michal Simon <michal.simon@cern.ch>
5 //------------------------------------------------------------------------------
6 // This file is part of the XRootD software suite.
7 //
8 // XRootD is free software: you can redistribute it and/or modify
9 // it under the terms of the GNU Lesser General Public License as published by
10 // the Free Software Foundation, either version 3 of the License, or
11 // (at your option) any later version.
12 //
13 // XRootD is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU Lesser General Public License
19 // along with XRootD. If not, see <http://www.gnu.org/licenses/>.
20 //
21 // In applying this licence, CERN does not waive the privileges and immunities
22 // granted to it by virtue of its status as an Intergovernmental Organization
23 // or submit itself to any jurisdiction.
24 //------------------------------------------------------------------------------
25 
26 #ifndef SRC_XRDCL_XRDCLCTX_HH_
27 #define SRC_XRDCL_XRDCLCTX_HH_
28 
29 #include <memory>
30 #include <stdexcept>
31 
32 namespace XrdCl
33 {
34  //---------------------------------------------------------------------------
36  //---------------------------------------------------------------------------
37  template<typename T>
38  struct Ctx : protected std::shared_ptr<T*>
39  {
40  //-------------------------------------------------------------------------
42  //-------------------------------------------------------------------------
43  Ctx() : std::shared_ptr<T*>( std::make_shared<T*>() )
44  {
45  }
46 
47  //-------------------------------------------------------------------------
49  //-------------------------------------------------------------------------
50  Ctx( T *ctx ) : std::shared_ptr<T*>( std::make_shared<T*>( ctx ) )
51  {
52  }
53 
54  //-------------------------------------------------------------------------
56  //-------------------------------------------------------------------------
57  Ctx( T &ctx ) : std::shared_ptr<T*>( std::make_shared<T*>( &ctx ) )
58  {
59  }
60 
61  //-------------------------------------------------------------------------
63  //-------------------------------------------------------------------------
64  Ctx( const Ctx &ctx ) : std::shared_ptr<T*>( ctx )
65  {
66  }
67 
68  //-------------------------------------------------------------------------
70  //-------------------------------------------------------------------------
71  Ctx( Ctx &&ctx ) : std::shared_ptr<T*>( std::move( ctx ) )
72  {
73  }
74 
75  //-------------------------------------------------------------------------
77  //-------------------------------------------------------------------------
78  Ctx& operator=( T *ctx )
79  {
80  *this->get() = ctx;
81  return *this;
82  }
83 
84  //-------------------------------------------------------------------------
86  //-------------------------------------------------------------------------
87  Ctx& operator=( T &ctx )
88  {
89  *this->get() = &ctx;
90  return *this;
91  }
92 
93  //------------------------------------------------------------------------
99  //------------------------------------------------------------------------
100  T& operator*() const
101  {
102  if( !bool( *this->get() ) ) throw std::logic_error( "XrdCl::Ctx contains no value!" );
103  return **this->get();
104  }
105 
106  //------------------------------------------------------------------------
112  //------------------------------------------------------------------------
113  T* operator->() const
114  {
115  if( !bool( *this->get() ) ) throw std::logic_error( "XrdCl::Ctx contains no value!" );
116  return *this->get();
117  }
118  };
119 }
120 
121 
122 #endif /* SRC_XRDCL_XRDCLCTX_HH_ */
Utility class for storing a pointer to operation context.
Definition: XrdClCtx.hh:39
Ctx(T &ctx)
Constructor (from reference)
Definition: XrdClCtx.hh:57
Ctx(T *ctx)
Constructor (from pointer)
Definition: XrdClCtx.hh:50
T & operator*() const
Definition: XrdClCtx.hh:100
Ctx & operator=(T *ctx)
Assignment operator (from pointer)
Definition: XrdClCtx.hh:78
Ctx(const Ctx &ctx)
Copy constructor.
Definition: XrdClCtx.hh:64
Ctx & operator=(T &ctx)
Assignment operator (from reference)
Definition: XrdClCtx.hh:87
T * operator->() const
Definition: XrdClCtx.hh:113
Ctx()
Default constructor.
Definition: XrdClCtx.hh:43
Ctx(Ctx &&ctx)
Move constructor.
Definition: XrdClCtx.hh:71