blocxx
|
00001 /******************************************************************************* 00002 * Copyright (C) 2005, Quest Software, Inc. All rights reserved. 00003 * Copyright (C) 2006, Novell, Inc. All rights reserved. 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that the following conditions are met: 00007 * 00008 * * Redistributions of source code must retain the above copyright notice, 00009 * this list of conditions and the following disclaimer. 00010 * * Redistributions in binary form must reproduce the above copyright 00011 * notice, this list of conditions and the following disclaimer in the 00012 * documentation and/or other materials provided with the distribution. 00013 * * Neither the name of 00014 * Quest Software, Inc., 00015 * nor Novell, Inc., 00016 * nor Network Associates, 00017 * nor the names of its contributors or employees may be used to 00018 * endorse or promote products derived from this software without 00019 * specific prior written permission. 00020 * 00021 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00022 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00023 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00024 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 00025 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00026 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00027 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00028 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00029 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00030 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00031 * POSSIBILITY OF SUCH DAMAGE. 00032 *******************************************************************************/ 00033 00034 00035 #ifndef BLOCXX_AUTO_RESOURCE_HPP_INCLUDE_GUARD_ 00036 #define BLOCXX_AUTO_RESOURCE_HPP_INCLUDE_GUARD_ 00037 00042 #include "blocxx/BLOCXX_config.h" 00043 #include "blocxx/SafeBool.hpp" 00044 00045 namespace BLOCXX_NAMESPACE 00046 { 00047 00051 template <typename Policy> 00052 struct AutoResourceRef 00053 { 00054 typedef typename Policy::handle_type ref_type; 00055 ref_type hdl; 00056 00057 explicit AutoResourceRef(ref_type h) 00058 : hdl(h) 00059 { 00060 } 00061 }; 00062 00096 template <typename Policy> 00097 class AutoResource 00098 { 00099 typedef typename Policy::handle_type handle_type; 00100 handle_type hdl; 00101 00102 public: 00103 00108 AutoResource() // throw() 00109 : hdl(Policy::null()) 00110 { 00111 } 00112 00116 explicit AutoResource(handle_type h) // throw() 00117 : hdl(h) 00118 { 00119 } 00120 00121 #if defined __HP_aCC 00122 00123 // 00124 AutoResource(const AutoResource & x) // throw() 00125 : hdl(const_cast<AutoResource &>(x).release()) 00126 { 00127 } 00128 #else 00129 00130 // 00131 AutoResource(AutoResource & x) // throw() 00132 : hdl(x.release()) 00133 { 00134 } 00135 #endif 00136 00137 #if defined __HP_aCC 00138 00139 // 00140 AutoResource & operator=(const AutoResource & x) // throw() 00141 { 00142 reset(const_cast<AutoResource &>(x).release()); 00143 return *this; 00144 } 00145 #else 00146 00147 // 00148 AutoResource & operator=(AutoResource & x) // throw() 00149 { 00150 reset(x.release()); 00151 return *this; 00152 } 00153 #endif 00154 00155 // 00156 ~AutoResource() // throw() 00157 { 00158 Policy::free(hdl); 00159 } 00160 00161 BLOCXX_SAFE_BOOL_IMPL(AutoResource<Policy>, handle_type, AutoResource<Policy>::hdl, (hdl != Policy::null())); 00162 00164 // 00165 handle_type get() const // throw() 00166 { 00167 return hdl; 00168 } 00169 00171 // 00172 handle_type release() // throw() 00173 { 00174 handle_type h = hdl; 00175 hdl = Policy::null(); 00176 return h; 00177 } 00178 00185 void reset(handle_type h) // throw() 00186 { 00187 if (!Policy::equal(h, hdl)) { 00188 Policy::free(hdl); 00189 hdl = h; 00190 } 00191 } 00192 00193 void reset() // throw() 00194 { 00195 reset(Policy::null()); 00196 } 00197 00202 AutoResource(AutoResourceRef<Policy> href) // throw() 00203 : hdl(href.hdl) 00204 { 00205 } 00206 00211 operator AutoResourceRef<Policy>() // throw() 00212 { 00213 return AutoResourceRef<Policy>(this->release()); 00214 } 00215 00223 AutoResource & operator=(AutoResourceRef<Policy> href) // throw() 00224 { 00225 if (!Policy::equal(href.hdl, this->get())) 00226 { 00227 Policy::free(hdl); 00228 hdl = href.hdl; 00229 } 00230 return *this; 00231 } 00232 00233 }; 00234 00235 } // namespace BLOCXX_NAMESPACE 00236 00237 #endif