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
31namespace XrdCl
32{
33 //---------------------------------------------------------------------------
35 //---------------------------------------------------------------------------
36 template<typename T>
37 struct Ctx : protected std::shared_ptr<T*>
38 {
39 //-------------------------------------------------------------------------
41 //-------------------------------------------------------------------------
42 Ctx() : std::shared_ptr<T*>( std::make_shared<T*>() )
43 {
44 }
45
46 //-------------------------------------------------------------------------
48 //-------------------------------------------------------------------------
49 Ctx( T *ctx ) : std::shared_ptr<T*>( std::make_shared<T*>( ctx ) )
50 {
51 }
52
53 //-------------------------------------------------------------------------
55 //-------------------------------------------------------------------------
56 Ctx( T &ctx ) : std::shared_ptr<T*>( std::make_shared<T*>( &ctx ) )
57 {
58 }
59
60 //-------------------------------------------------------------------------
62 //-------------------------------------------------------------------------
63 Ctx( const Ctx &ctx ) : std::shared_ptr<T*>( ctx )
64 {
65 }
66
67 //-------------------------------------------------------------------------
69 //-------------------------------------------------------------------------
70 Ctx( Ctx &&ctx ) : std::shared_ptr<T*>( std::move( ctx ) )
71 {
72 }
73
74 //-------------------------------------------------------------------------
76 //-------------------------------------------------------------------------
77 Ctx& operator=( T *ctx )
78 {
79 *this->get() = ctx;
80 return *this;
81 }
82
83 //-------------------------------------------------------------------------
85 //-------------------------------------------------------------------------
86 Ctx& operator=( T &ctx )
87 {
88 *this->get() = &ctx;
89 return *this;
90 }
91
92 //------------------------------------------------------------------------
98 //------------------------------------------------------------------------
99 T& operator*() const
100 {
101 if( !bool( *this->get() ) ) throw std::logic_error( "XrdCl::Ctx contains no value!" );
102 return **this->get();
103 }
104
105 //------------------------------------------------------------------------
111 //------------------------------------------------------------------------
112 T* operator->() const
113 {
114 if( !bool( *this->get() ) ) throw std::logic_error( "XrdCl::Ctx contains no value!" );
115 return *this->get();
116 }
117 };
118}
119
120
121#endif /* SRC_XRDCL_XRDCLCTX_HH_ */
Definition: XrdClAction.hh:34
auto get(const nlohmann::detail::iteration_proxy_value< IteratorType > &i) -> decltype(i.key())
Definition: XrdOucJson.hh:4497
Definition: XrdOucJson.hh:4517
Utility class for storing a pointer to operation context.
Definition: XrdClCtx.hh:38
Ctx & operator=(T *ctx)
Assignment operator (from pointer)
Definition: XrdClCtx.hh:77
Ctx(T &ctx)
Constructor (from reference)
Definition: XrdClCtx.hh:56
T * operator->() const
Definition: XrdClCtx.hh:112
Ctx(T *ctx)
Constructor (from pointer)
Definition: XrdClCtx.hh:49
Ctx & operator=(T &ctx)
Assignment operator (from reference)
Definition: XrdClCtx.hh:86
Ctx(const Ctx &ctx)
Copy constructor.
Definition: XrdClCtx.hh:63
Ctx()
Default constructor.
Definition: XrdClCtx.hh:42
T & operator*() const
Definition: XrdClCtx.hh:99
Ctx(Ctx &&ctx)
Move constructor.
Definition: XrdClCtx.hh:70