• Skip to content
  • Skip to link menu
KDE 3.5 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

kviewshell

GSmartPointer.cpp

Go to the documentation of this file.
00001 //C-  -*- C++ -*-
00002 //C- -------------------------------------------------------------------
00003 //C- DjVuLibre-3.5
00004 //C- Copyright (c) 2002  Leon Bottou and Yann Le Cun.
00005 //C- Copyright (c) 2001  AT&T
00006 //C-
00007 //C- This software is subject to, and may be distributed under, the
00008 //C- GNU General Public License, Version 2. The license should have
00009 //C- accompanied the software or you may obtain a copy of the license
00010 //C- from the Free Software Foundation at http://www.fsf.org .
00011 //C-
00012 //C- This program is distributed in the hope that it will be useful,
00013 //C- but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 //C- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 //C- GNU General Public License for more details.
00016 //C- 
00017 //C- DjVuLibre-3.5 is derived from the DjVu(r) Reference Library
00018 //C- distributed by Lizardtech Software.  On July 19th 2002, Lizardtech 
00019 //C- Software authorized us to replace the original DjVu(r) Reference 
00020 //C- Library notice by the following text (see doc/lizard2002.djvu):
00021 //C-
00022 //C-  ------------------------------------------------------------------
00023 //C- | DjVu (r) Reference Library (v. 3.5)
00024 //C- | Copyright (c) 1999-2001 LizardTech, Inc. All Rights Reserved.
00025 //C- | The DjVu Reference Library is protected by U.S. Pat. No.
00026 //C- | 6,058,214 and patents pending.
00027 //C- |
00028 //C- | This software is subject to, and may be distributed under, the
00029 //C- | GNU General Public License, Version 2. The license should have
00030 //C- | accompanied the software or you may obtain a copy of the license
00031 //C- | from the Free Software Foundation at http://www.fsf.org .
00032 //C- |
00033 //C- | The computer code originally released by LizardTech under this
00034 //C- | license and unmodified by other parties is deemed "the LIZARDTECH
00035 //C- | ORIGINAL CODE."  Subject to any third party intellectual property
00036 //C- | claims, LizardTech grants recipient a worldwide, royalty-free, 
00037 //C- | non-exclusive license to make, use, sell, or otherwise dispose of 
00038 //C- | the LIZARDTECH ORIGINAL CODE or of programs derived from the 
00039 //C- | LIZARDTECH ORIGINAL CODE in compliance with the terms of the GNU 
00040 //C- | General Public License.   This grant only confers the right to 
00041 //C- | infringe patent claims underlying the LIZARDTECH ORIGINAL CODE to 
00042 //C- | the extent such infringement is reasonably necessary to enable 
00043 //C- | recipient to make, have made, practice, sell, or otherwise dispose 
00044 //C- | of the LIZARDTECH ORIGINAL CODE (or portions thereof) and not to 
00045 //C- | any greater extent that may be necessary to utilize further 
00046 //C- | modifications or combinations.
00047 //C- |
00048 //C- | The LIZARDTECH ORIGINAL CODE is provided "AS IS" WITHOUT WARRANTY
00049 //C- | OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
00050 //C- | TO ANY WARRANTY OF NON-INFRINGEMENT, OR ANY IMPLIED WARRANTY OF
00051 //C- | MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
00052 //C- +------------------------------------------------------------------
00053 // 
00054 // $Id: GSmartPointer.cpp,v 1.11 2003/11/07 22:08:21 leonb Exp $
00055 // $Name: release_3_5_15 $
00056 
00057 #ifdef HAVE_CONFIG_H
00058 # include "config.h"
00059 #endif
00060 #if NEED_GNUG_PRAGMAS
00061 # pragma implementation
00062 #endif
00063 
00064 // - Author: Leon Bottou, 05/1997
00065 
00066 // From: Leon Bottou, 1/31/2002
00067 // Class GPBuffer has been added (but not documented) by Lizardtech.
00068 // Our original implementation consisted of multiple classes.
00069 // <http://prdownloads.sourceforge.net/djvu/DjVu2_2b-src.tgz>.
00070 
00071 #include <string.h>
00072 #include "GThreads.h"
00073 #include "GSmartPointer.h"
00074 #include "GException.h"
00075 
00076 
00077 #ifdef HAVE_NAMESPACES
00078 namespace DJVU {
00079 # ifdef NOT_DEFINED // Just to fool emacs c++ mode
00080 }
00081 #endif
00082 #endif
00083 
00084 
00085 // ------ STATIC CRITICAL SECTION
00086 
00087 static GCriticalSection gcsCounter;
00088 
00089 
00090 // ------ GPENABLED
00091 
00092 
00093 GPEnabled::~GPEnabled()
00094 {
00095   if (count > 0)
00096     G_THROW( ERR_MSG("GSmartPointer.suspicious") );
00097 }
00098 
00099 void
00100 GPEnabled::destroy()
00101 {
00102   if (count >= 0)
00103     G_THROW( ERR_MSG("GSmartPointer.suspicious") );
00104   delete this;
00105 }
00106 
00107 void 
00108 GPEnabled::ref()
00109 {
00110   gcsCounter.lock();
00111   count++;
00112   gcsCounter.unlock();
00113 }
00114 
00115 void 
00116 GPEnabled::unref()
00117 {
00118   gcsCounter.lock();
00119   if (! --count) 
00120     count = -1;
00121   gcsCounter.unlock();
00122   if (count < 0)
00123     destroy();
00124 }
00125 
00126 
00127 // ------ GPBASE
00128 
00129 
00130 GPBase&
00131 GPBase::assign (GPEnabled *nptr)
00132 {
00133   gcsCounter.lock();
00134   if (nptr)
00135     {
00136       if (nptr->count >= 0)  
00137         nptr->count++;
00138       else
00139         nptr = 0;
00140     }
00141   if (ptr)
00142     {
00143       GPEnabled *old = ptr;
00144       ptr = nptr;
00145       if (! --old->count) 
00146         old->count = -1;
00147       gcsCounter.unlock();      
00148       if (old->count < 0)
00149         old->destroy();
00150     }
00151   else
00152     {
00153       ptr = nptr;
00154       gcsCounter.unlock();
00155     }
00156   return *this;
00157 }
00158 
00159 GPBase&
00160 GPBase::assign (const GPBase &sptr)
00161 {
00162   gcsCounter.lock();
00163   if (sptr.ptr) 
00164     {
00165       sptr.ptr->count++;
00166     }
00167   if (ptr)
00168     {
00169       GPEnabled *old = ptr;
00170       ptr = sptr.ptr;
00171       if (! --old->count) 
00172         old->count = -1;
00173       gcsCounter.unlock();      
00174       if (old->count < 0)
00175         old->destroy();
00176     }
00177   else
00178     {
00179       ptr = sptr.ptr;
00180       gcsCounter.unlock();
00181     }
00182   return *this;
00183 }
00184 
00185 
00186 // ------ GPBUFFERBASE
00187 
00188 
00189 void
00190 GPBufferBase::replace(void *nptr,const size_t n)
00191 {
00192   resize(0,0);
00193   ptr=nptr;
00194   num=n;
00195 }
00196 
00197 GPBufferBase::GPBufferBase(void *&xptr,const size_t n,const size_t t) 
00198   : ptr(xptr), num(n)
00199 {
00200   if (n)
00201     xptr = ::operator new(n*t);
00202   else
00203     xptr = 0;
00204 }
00205 
00206 GPBufferBase::~GPBufferBase()
00207 {
00208   ::operator delete(ptr);
00209 }
00210 
00211 void 
00212 GPBufferBase::swap(GPBufferBase &other)
00213 {
00214   void * const temp_ptr=ptr;
00215   ptr=other.ptr;
00216   other.ptr=temp_ptr;
00217   const size_t temp_num=num;
00218   num=other.num;
00219   other.num=temp_num;
00220 }
00221 
00222 void
00223 GPBufferBase::resize(const size_t n, const size_t t)
00224 {
00225   if(!n && !ptr)
00226     {
00227       num=0;
00228     }
00229   else
00230     {
00231       const size_t s=ptr?(((num<n)?num:n)*t):0;
00232       void *nptr;
00233       GPBufferBase gnptr(nptr, n, t);
00234       if(s)
00235         {
00236           memcpy(nptr, ptr, s);
00237         }
00238       swap(gnptr);
00239     }
00240 }
00241 
00242 void
00243 GPBufferBase::set(const size_t t,const char c)
00244 {
00245   if(num)
00246     memset(ptr,c,num*t);
00247 }
00248 
00249 
00250 #ifdef HAVE_NAMESPACES
00251 }
00252 # ifndef NOT_USING_DJVU_NAMESPACE
00253 using namespace DJVU;
00254 # endif
00255 #endif
00256 

kviewshell

Skip menu "kviewshell"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members

API Reference

Skip menu "API Reference"
  • kviewshell
Generated for API Reference by doxygen 1.5.9
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal