xrootd
Loading...
Searching...
No Matches
XrdClOptional.hh
Go to the documentation of this file.
1//------------------------------------------------------------------------------
2// Copyright (c) 2011-2017 by European Organization for Nuclear Research (CERN)
3// Author: Michal Simon <michal.simon@cern.ch>
4//------------------------------------------------------------------------------
5// This file is part of the XRootD software suite.
6//
7// XRootD is free software: you can redistribute it and/or modify
8// it under the terms of the GNU Lesser General Public License as published by
9// the Free Software Foundation, either version 3 of the License, or
10// (at your option) any later version.
11//
12// XRootD is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15// GNU General Public License for more details.
16//
17// You should have received a copy of the GNU Lesser General Public License
18// along with XRootD. If not, see <http://www.gnu.org/licenses/>.
19//
20// In applying this licence, CERN does not waive the privileges and immunities
21// granted to it by virtue of its status as an Intergovernmental Organization
22// or submit itself to any jurisdiction.
23//------------------------------------------------------------------------------
24
25#ifndef __XRD_CL_OPTIONAL_HH__
26#define __XRD_CL_OPTIONAL_HH__
27
28#include <utility>
29
30namespace XrdCl
31{
32 //----------------------------------------------------------------------------
34 //----------------------------------------------------------------------------
35 static struct None{ } none;
36
37 //----------------------------------------------------------------------------
41 //----------------------------------------------------------------------------
42 template<typename T>
44 {
45 public:
46
47 //------------------------------------------------------------------------
49 //------------------------------------------------------------------------
50 Optional( const T& t ) : optional( false )
51 {
52 new( &memory.value ) T( t );
53 }
54
55 //------------------------------------------------------------------------
57 //------------------------------------------------------------------------
58 Optional( const None& n = none ) : optional( true )
59 {
60 (void)n;
61 }
62
63 //------------------------------------------------------------------------
65 //------------------------------------------------------------------------
66 Optional( const Optional& opt ) : optional( opt.optional )
67 {
68 if( !optional ) new( &memory.value ) T( opt.memory.value );
69 }
70
71 //------------------------------------------------------------------------
73 //------------------------------------------------------------------------
75 {
76 if( !optional ) new( &memory.value ) T( std::move( opt.memory.value ) );
77 }
78
79 //------------------------------------------------------------------------
80 // Destructor
81 //------------------------------------------------------------------------
83 {
84 if( optional ) memory.value.~T();
85 }
86
87 //------------------------------------------------------------------------
89 //------------------------------------------------------------------------
91 {
92 if( this != &opt )
93 {
94 optional = opt.optional;
95 if( !optional ) memory.value = opt.memory.value;
96 }
97 return *this;
98 }
99
100 //------------------------------------------------------------------------
102 //------------------------------------------------------------------------
104 {
105 if( this != &opt )
106 {
107 optional = opt.optional;
108 if( !optional ) memory.value = std::move( opt.memory.value );
109 }
110 return *this;
111 }
112
113 //------------------------------------------------------------------------
115 //------------------------------------------------------------------------
116 operator bool() const
117 {
118 return optional;
119 }
120
121 //------------------------------------------------------------------------
123 //------------------------------------------------------------------------
125 {
126 return memory.value;
127 }
128
129 //------------------------------------------------------------------------
131 //------------------------------------------------------------------------
132 const T& operator*() const
133 {
134 return memory.value;
135 }
136
137 private:
138
139 //------------------------------------------------------------------------
141 //------------------------------------------------------------------------
143
144 //------------------------------------------------------------------------
147 //------------------------------------------------------------------------
149 {
150 //----------------------------------------------------------------------
153 //----------------------------------------------------------------------
155 //----------------------------------------------------------------------
157 //----------------------------------------------------------------------
158 inline Storage(){ }
159 //----------------------------------------------------------------------
160 // Destructor
161 //----------------------------------------------------------------------
162 inline ~Storage(){ };
163 } memory; //> memory storage for the optional variable
164 };
165}
166
167#endif // __XRD_CL_OPTIONAL_HH__
Definition XrdClOptional.hh:44
union XrdCl::Optional::Storage memory
Optional(const T &t)
Constructor for value.
Definition XrdClOptional.hh:50
Optional(const None &n=none)
Default constructor.
Definition XrdClOptional.hh:58
Optional & operator=(const Optional &opt)
Copy assignment operator.
Definition XrdClOptional.hh:90
Optional & operator=(Optional &&opt)
Move assignment operator.
Definition XrdClOptional.hh:103
T & operator*()
Dereference operator.
Definition XrdClOptional.hh:124
Optional(Optional &&opt)
Move constructor.
Definition XrdClOptional.hh:74
bool optional
true if the value is optional, false otherwise
Definition XrdClOptional.hh:142
Optional(const Optional &opt)
Copy constructor.
Definition XrdClOptional.hh:66
const T & operator*() const
Dereference operator.
Definition XrdClOptional.hh:132
~Optional()
Definition XrdClOptional.hh:82
Definition XrdClAction.hh:34
static struct XrdCl::None none
none object for initializing empty Optional
Definition XrdClOptional.hh:35
Definition XrdClOptional.hh:149
Storage()
Default constructor.
Definition XrdClOptional.hh:158
T value
Definition XrdClOptional.hh:154
~Storage()
Definition XrdClOptional.hh:162