| Home | C-Bits Package Documentation | Project Page |
00001 /* ====================================================================
00002 * The CBITS Software License, Version 1.0
00003 *
00004 * Copyright (c) 2002-2003 Bruce Lowery
00005 * All rights reserved
00006 *
00007 * Redistribution and use of this software, in source and binary forms,
00008 * with or without modification, are permitted provided that the above
00009 * copyright notice, this paragraph, and the following paragraph are
00010 * retained in each source code file.
00011 *
00012 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
00013 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00014 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00015 * DISCLAIMED. IN NO EVENT SHALL BRUCE LOWERY OR OTHER CONTRIBUTORS
00016 * TO THE CBITS LIBRARY BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00017 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00018 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
00019 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00020 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00021 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
00022 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00023 * SUCH DAMAGE.
00024 * ====================================================================
00025 */
00026
00027 #ifndef __SYNC_PTR_H__
00028 #define __SYNC_PTR_H__
00029
00030 namespace cbits
00031 {
00032
00078 template< typename Lockable >
00079 class sync_ptr
00080 {
00081 public:
00082
00090 sync_ptr<Lockable>
00091 (
00092 Lockable* t,
00093 const bool autofree=false
00094 )
00095 : _t(t),_owned(true),_autofree(autofree)
00096 {
00097 _t->lock();
00098 }
00099
00100
00107 sync_ptr<Lockable>( const sync_ptr<Lockable>& r )
00108 {
00109 if( ! r._owned )
00110 {
00111 throw std::exception();
00112 }
00113
00114 _t = r._t;
00115 _owned = true;
00116 _autofree = r._autofree;
00117 r.release();
00118 }
00119
00120
00127 sync_ptr<Lockable>& operator=( const sync_ptr<Lockable>& r )
00128 {
00129 if( ! r._owned )
00130 {
00131 throw std::exception();
00132 }
00133
00134 if( _owned )
00135 {
00136 _t->unlock();
00137 if( _autofree )
00138 {
00139 delete _t;
00140 }
00141 _t = 0;
00142 }
00143
00144 _t = r._t;
00145 _owned = true;
00146 _autofree = r._autofree;
00147 r.release();
00148 return *this;
00149 }
00150
00151
00155 bool operator==( const sync_ptr<Lockable>& r )
00156 {
00157 return _t == r._t;
00158 }
00159
00160
00164 Lockable* operator->()
00165 {
00166 return _t;
00167 }
00168
00169
00173 Lockable& operator*()
00174 {
00175 return *_t;
00176 }
00177
00181 const bool isOwner() const
00182 {
00183 return _owned;
00184 }
00185
00196 ~sync_ptr<Lockable>()
00197 {
00198 if( _owned )
00199 {
00200 _t->unlock();
00201
00202 if( _autofree )
00203 {
00204 delete _t;
00205 _t = 0;
00206 }
00207 }
00208 }
00209
00210
00211 private:
00212
00218 void release() const
00219 {
00220 _owned = false; // don't unlock in destructor
00221 _autofree = false; // don't delete obj in destructor
00222 }
00223
00224 Lockable* _t;
00225 mutable bool _owned;
00226 mutable bool _autofree;
00228 };
00229
00230 };
00231
00232 #endif
00233
00234 /*
00235 * $Id: sync_ptr.h,v 1.2 2002/10/14 01:21:04 brulow Exp $
00236 *
00237 * History: (Add nothing manually below)
00238 * -----------------------------------------------------------------------
00239 *
00240 * $Log: sync_ptr.h,v $
00241 * Revision 1.2 2002/10/14 01:21:04 brulow
00242 * Add unit tests for pointer pkg
00243 *
00244 * Revision 1.1 2002/10/13 17:17:45 brulow
00245 * Add a 'synchronizing' smart pointer
00246 *
00247 *
00248 */
|
|