xrootd
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 
30 namespace XrdCl
31 {
32  //----------------------------------------------------------------------------
34  //----------------------------------------------------------------------------
35  static struct None{ } none;
36 
37  //----------------------------------------------------------------------------
41  //----------------------------------------------------------------------------
42  template<typename T>
43  class Optional
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  }
61 
62  //------------------------------------------------------------------------
64  //------------------------------------------------------------------------
65  Optional( const Optional& opt ) : optional( opt.optional )
66  {
67  if( !optional ) new( &memory.value ) T( opt.memory.value );
68  }
69 
70  //------------------------------------------------------------------------
72  //------------------------------------------------------------------------
73  Optional( Optional && opt ) : optional( opt.optional )
74  {
75  if( !optional ) new( &memory.value ) T( std::move( opt.memory.value ) );
76  }
77 
78  //------------------------------------------------------------------------
79  // Destructor
80  //------------------------------------------------------------------------
82  {
83  if( optional ) memory.value.~T();
84  }
85 
86  //------------------------------------------------------------------------
88  //------------------------------------------------------------------------
89  Optional& operator=( const Optional& opt )
90  {
91  if( this != &opt )
92  {
93  optional = opt.optional;
94  if( !optional ) memory.value = opt.memory.value;
95  }
96  return *this;
97  }
98 
99  //------------------------------------------------------------------------
101  //------------------------------------------------------------------------
103  {
104  if( this != &opt )
105  {
106  optional = opt.optional;
107  if( !optional ) memory.value = std::move( opt.memory.value );
108  }
109  return *this;
110  }
111 
112  //------------------------------------------------------------------------
114  //------------------------------------------------------------------------
115  operator bool() const
116  {
117  return optional;
118  }
119 
120  //------------------------------------------------------------------------
122  //------------------------------------------------------------------------
124  {
125  return memory.value;
126  }
127 
128  //------------------------------------------------------------------------
130  //------------------------------------------------------------------------
131  const T& operator*() const
132  {
133  return memory.value;
134  }
135 
136  private:
137 
138  //------------------------------------------------------------------------
140  //------------------------------------------------------------------------
141  bool optional;
142 
143  //------------------------------------------------------------------------
146  //------------------------------------------------------------------------
147  union Storage
148  {
149  //----------------------------------------------------------------------
152  //----------------------------------------------------------------------
153  T value;
154  //----------------------------------------------------------------------
156  //----------------------------------------------------------------------
157  inline Storage(){ }
158  //----------------------------------------------------------------------
159  // Destructor
160  //----------------------------------------------------------------------
161  inline ~Storage(){ };
162  } memory; //> memory storage for the optional variable
163  };
164 }
165 
166 #endif // __XRD_CL_OPTIONAL_HH__
bool optional
true if the value is optional, false otherwise
Definition: XrdClOptional.hh:141
Definition: XrdClOptional.hh:43
Optional & operator=(Optional &&opt)
Move assignment operator.
Definition: XrdClOptional.hh:102
Optional(const Optional &opt)
Copy constructor.
Definition: XrdClOptional.hh:65
~Storage()
Definition: XrdClOptional.hh:161
~Optional()
Definition: XrdClOptional.hh:81
T & operator*()
Dereference operator.
Definition: XrdClOptional.hh:123
static struct XrdCl::None none
Definition: XrdClOptional.hh:147
Optional(const T &t)
Constructor for value.
Definition: XrdClOptional.hh:50
none object for initializing empty Optional
Definition: XrdClOptional.hh:35
Definition: XrdClAnyObject.hh:25
Optional(Optional &&opt)
Move constructor.
Definition: XrdClOptional.hh:73
const T & operator*() const
Dereference operator.
Definition: XrdClOptional.hh:131
Storage()
Default constructor.
Definition: XrdClOptional.hh:157
T value
Definition: XrdClOptional.hh:153
union XrdCl::Optional::Storage memory
Optional(const None &n=none)
Default constructor.
Definition: XrdClOptional.hh:58
Optional & operator=(const Optional &opt)
Copy assignment operator.
Definition: XrdClOptional.hh:89