00001 //------------------------------------------------------------------------------ 00002 // Copyright (c) 2011-2017 by European Organization for Nuclear Research (CERN) 00003 // Author: Michal Simon <michal.simon@cern.ch> 00004 //------------------------------------------------------------------------------ 00005 // This file is part of the XRootD software suite. 00006 // 00007 // XRootD is free software: you can redistribute it and/or modify 00008 // it under the terms of the GNU Lesser General Public License as published by 00009 // the Free Software Foundation, either version 3 of the License, or 00010 // (at your option) any later version. 00011 // 00012 // XRootD is distributed in the hope that it will be useful, 00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 // GNU General Public License for more details. 00016 // 00017 // You should have received a copy of the GNU Lesser General Public License 00018 // along with XRootD. If not, see <http://www.gnu.org/licenses/>. 00019 // 00020 // In applying this licence, CERN does not waive the privileges and immunities 00021 // granted to it by virtue of its status as an Intergovernmental Organization 00022 // or submit itself to any jurisdiction. 00023 //------------------------------------------------------------------------------ 00024 00025 #ifndef __XRD_CL_OPTIONAL_HH__ 00026 #define __XRD_CL_OPTIONAL_HH__ 00027 00028 #include <utility> 00029 00030 namespace XrdCl 00031 { 00032 //---------------------------------------------------------------------------- 00034 //---------------------------------------------------------------------------- 00035 static struct None{ } none; 00036 00037 //---------------------------------------------------------------------------- 00041 //---------------------------------------------------------------------------- 00042 template<typename T> 00043 class Optional 00044 { 00045 public: 00046 00047 //------------------------------------------------------------------------ 00049 //------------------------------------------------------------------------ 00050 Optional( const T& t ) : optional( false ) 00051 { 00052 new( &memory.value ) T( t ); 00053 } 00054 00055 //------------------------------------------------------------------------ 00057 //------------------------------------------------------------------------ 00058 Optional( const None& n = none ) : optional( true ) 00059 { 00060 } 00061 00062 //------------------------------------------------------------------------ 00064 //------------------------------------------------------------------------ 00065 Optional( const Optional& opt ) : optional( opt.optional ) 00066 { 00067 if( !optional ) new( &memory.value ) T( opt.memory.value ); 00068 } 00069 00070 //------------------------------------------------------------------------ 00072 //------------------------------------------------------------------------ 00073 Optional( Optional && opt ) : optional( opt.optional ) 00074 { 00075 if( !optional ) new( &memory.value ) T( std::move( opt.memory.value ) ); 00076 } 00077 00078 //------------------------------------------------------------------------ 00080 //------------------------------------------------------------------------ 00081 Optional& operator=( const Optional& opt ) 00082 { 00083 if( this != &opt ) 00084 { 00085 optional = opt.optional; 00086 if( !optional ) memory.value = opt.memory.value; 00087 } 00088 return *this; 00089 } 00090 00091 //------------------------------------------------------------------------ 00093 //------------------------------------------------------------------------ 00094 Optional& operator=( Optional&& opt ) 00095 { 00096 if( this != &opt ) 00097 { 00098 optional = opt.optional; 00099 if( !optional ) memory.value = std::move( opt.memory.value ); 00100 } 00101 return *this; 00102 } 00103 00104 //------------------------------------------------------------------------ 00106 //------------------------------------------------------------------------ 00107 operator bool() const 00108 { 00109 return optional; 00110 } 00111 00112 //------------------------------------------------------------------------ 00114 //------------------------------------------------------------------------ 00115 T& operator*() 00116 { 00117 return memory.value; 00118 } 00119 00120 //------------------------------------------------------------------------ 00122 //------------------------------------------------------------------------ 00123 const T& operator*() const 00124 { 00125 return memory.value; 00126 } 00127 00128 private: 00129 00130 //------------------------------------------------------------------------ 00132 //------------------------------------------------------------------------ 00133 bool optional; 00134 00135 //------------------------------------------------------------------------ 00138 //------------------------------------------------------------------------ 00139 union Storage 00140 { 00141 //---------------------------------------------------------------------- 00144 //---------------------------------------------------------------------- 00145 T value; 00146 } memory; //> memory storage for the optional variable 00147 }; 00148 } 00149 00150 #endif // __XRD_CL_OPTIONAL_HH__