|
||||
File indexing completed on 2025-01-30 10:27:53
0001 //------------------------------------------------------------------------------ 0002 // Copyright (c) 2011-2017 by European Organization for Nuclear Research (CERN) 0003 // Author: Michal Simon <michal.simon@cern.ch> 0004 //------------------------------------------------------------------------------ 0005 // This file is part of the XRootD software suite. 0006 // 0007 // XRootD is free software: you can redistribute it and/or modify 0008 // it under the terms of the GNU Lesser General Public License as published by 0009 // the Free Software Foundation, either version 3 of the License, or 0010 // (at your option) any later version. 0011 // 0012 // XRootD is distributed in the hope that it will be useful, 0013 // but WITHOUT ANY WARRANTY; without even the implied warranty of 0014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0015 // GNU General Public License for more details. 0016 // 0017 // You should have received a copy of the GNU Lesser General Public License 0018 // along with XRootD. If not, see <http://www.gnu.org/licenses/>. 0019 // 0020 // In applying this licence, CERN does not waive the privileges and immunities 0021 // granted to it by virtue of its status as an Intergovernmental Organization 0022 // or submit itself to any jurisdiction. 0023 //------------------------------------------------------------------------------ 0024 0025 #ifndef __XRD_CL_OPTIONAL_HH__ 0026 #define __XRD_CL_OPTIONAL_HH__ 0027 0028 #include <utility> 0029 0030 namespace XrdCl 0031 { 0032 //---------------------------------------------------------------------------- 0033 //! none object for initializing empty Optional 0034 //---------------------------------------------------------------------------- 0035 static struct None{ } none; 0036 0037 //---------------------------------------------------------------------------- 0038 //! The Optional class 0039 //! 0040 //! @arg T : type of the optional parameter 0041 //---------------------------------------------------------------------------- 0042 template<typename T> 0043 class Optional 0044 { 0045 public: 0046 0047 //------------------------------------------------------------------------ 0048 //! Constructor for value 0049 //------------------------------------------------------------------------ 0050 Optional( const T& t ) : optional( false ) 0051 { 0052 new( &memory.value ) T( t ); 0053 } 0054 0055 //------------------------------------------------------------------------ 0056 //! Default constructor 0057 //------------------------------------------------------------------------ 0058 Optional( const None& n = none ) : optional( true ) 0059 { 0060 (void)n; 0061 } 0062 0063 //------------------------------------------------------------------------ 0064 //! Copy constructor 0065 //------------------------------------------------------------------------ 0066 Optional( const Optional& opt ) : optional( opt.optional ) 0067 { 0068 if( !optional ) new( &memory.value ) T( opt.memory.value ); 0069 } 0070 0071 //------------------------------------------------------------------------ 0072 //! Move constructor 0073 //------------------------------------------------------------------------ 0074 Optional( Optional && opt ) : optional( opt.optional ) 0075 { 0076 if( !optional ) new( &memory.value ) T( std::move( opt.memory.value ) ); 0077 } 0078 0079 //------------------------------------------------------------------------ 0080 // Destructor 0081 //------------------------------------------------------------------------ 0082 ~Optional() 0083 { 0084 if( optional ) memory.value.~T(); 0085 } 0086 0087 //------------------------------------------------------------------------ 0088 //! Copy assignment operator 0089 //------------------------------------------------------------------------ 0090 Optional& operator=( const Optional& opt ) 0091 { 0092 if( this != &opt ) 0093 { 0094 optional = opt.optional; 0095 if( !optional ) memory.value = opt.memory.value; 0096 } 0097 return *this; 0098 } 0099 0100 //------------------------------------------------------------------------ 0101 //! Move assignment operator 0102 //------------------------------------------------------------------------ 0103 Optional& operator=( Optional&& opt ) 0104 { 0105 if( this != &opt ) 0106 { 0107 optional = opt.optional; 0108 if( !optional ) memory.value = std::move( opt.memory.value ); 0109 } 0110 return *this; 0111 } 0112 0113 //------------------------------------------------------------------------ 0114 //! Conversion to boolean 0115 //------------------------------------------------------------------------ 0116 operator bool() const 0117 { 0118 return optional; 0119 } 0120 0121 //------------------------------------------------------------------------ 0122 //! Dereference operator 0123 //------------------------------------------------------------------------ 0124 T& operator*() 0125 { 0126 return memory.value; 0127 } 0128 0129 //------------------------------------------------------------------------ 0130 //! Dereference operator 0131 //------------------------------------------------------------------------ 0132 const T& operator*() const 0133 { 0134 return memory.value; 0135 } 0136 0137 private: 0138 0139 //------------------------------------------------------------------------ 0140 //! true if the value is optional, false otherwise 0141 //------------------------------------------------------------------------ 0142 bool optional; 0143 0144 //------------------------------------------------------------------------ 0145 //! we use union as this is the only way to obtain memory with correct 0146 //! alignment and don't actually construct the object 0147 //------------------------------------------------------------------------ 0148 union Storage 0149 { 0150 //---------------------------------------------------------------------- 0151 //! value of the optional variable, if the variable is optional is 0152 //! remains uninitialized 0153 //---------------------------------------------------------------------- 0154 T value; 0155 //---------------------------------------------------------------------- 0156 //! Default constructor 0157 //---------------------------------------------------------------------- 0158 inline Storage(){ } 0159 //---------------------------------------------------------------------- 0160 // Destructor 0161 //---------------------------------------------------------------------- 0162 inline ~Storage(){ }; 0163 } memory; //> memory storage for the optional variable 0164 }; 0165 } 0166 0167 #endif // __XRD_CL_OPTIONAL_HH__
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |