kviewshell
GSmartPointer.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057 #ifndef _GSMARTPOINTER_H_
00058 #define _GSMARTPOINTER_H_
00059 #ifdef HAVE_CONFIG_H
00060 #include "config.h"
00061 #endif
00062 #if NEED_GNUG_PRAGMAS
00063 # pragma interface
00064 #endif
00065
00093
00094 #if defined(_MSC_VER)
00095
00096
00097 #pragma warning( disable : 4243 )
00098 #endif
00099
00100 #include "DjVuGlobal.h"
00101
00102 #ifdef HAVE_NAMESPACES
00103 namespace DJVU {
00104 # ifdef NOT_DEFINED // Just to fool emacs c++ mode
00105 }
00106 #endif
00107 #endif
00108
00109
00110
00111
00112
00113
00114 class GPBufferBase
00115 {
00116 public:
00117 GPBufferBase(void *&,const size_t n,const size_t t);
00118 void swap(GPBufferBase &p);
00119 void resize(const size_t n,const size_t t);
00120 void replace(void *nptr,const size_t n);
00121 void set(const size_t t,const char c);
00122 ~GPBufferBase();
00123 operator int(void) const { return ptr ? num : 0; }
00124 private:
00125 void *&ptr;
00126 size_t num;
00127 };
00128
00129 template<class TYPE>
00130 class GPBuffer : public GPBufferBase
00131 {
00132 public:
00133 GPBuffer(TYPE *&xptr,const size_t n=0) : GPBufferBase((void *&)xptr,n,sizeof(TYPE)) {}
00134 inline void resize(const size_t n) {GPBufferBase::resize(n,sizeof(TYPE));}
00135 inline void clear(void) {GPBufferBase::set(sizeof(TYPE),0);}
00136 inline void set(const char c) {GPBufferBase::set(sizeof(TYPE),c);}
00137 inline operator int(void) const {return GPBufferBase::operator int();}
00138 };
00139
00145 class GPEnabled
00146 {
00147 public:
00149 GPEnabled();
00151 GPEnabled(const GPEnabled & obj);
00153 virtual ~GPEnabled();
00155 GPEnabled & operator=(const GPEnabled & obj);
00158 int get_count(void) const;
00159 protected:
00161 volatile int count;
00162 private:
00163 friend class GPBase;
00164 void unref();
00165 void ref();
00166 void destroy();
00167 };
00168
00169
00170
00178 class GPBase
00179 {
00180 public:
00182 GPBase();
00186 GPBase(const GPBase &sptr);
00190 GPBase(GPEnabled *nptr);
00192 ~GPBase();
00194 GPEnabled* get() const;
00198 GPBase& assign(const GPBase &sptr);
00203 GPBase& assign(GPEnabled *nptr);
00205 GPBase & operator=(const GPBase & obj);
00207 int operator==(const GPBase & g2) const;
00208 protected:
00210 GPEnabled *ptr;
00211 };
00212
00213
00269 template <class TYPE>
00270 class GP : protected GPBase
00271 {
00272 public:
00274 GP();
00277 GP(const GP<TYPE> &sptr);
00283 GP(TYPE *nptr);
00288 operator TYPE* () const;
00294 GP<TYPE>& operator= (TYPE *nptr);
00297 GP<TYPE>& operator= (const GP<TYPE> &sptr);
00302 TYPE* operator->() const;
00306 TYPE& operator*() const;
00312 int operator== (TYPE *nptr) const;
00318 int operator!= (TYPE *nptr) const;
00327 int operator! () const;
00328 };
00329
00331
00332
00333
00334 inline
00335 GPEnabled::GPEnabled()
00336 : count(0)
00337 {
00338 }
00339
00340 inline
00341 GPEnabled::GPEnabled(const GPEnabled & obj)
00342 : count(0)
00343 {
00344
00345 }
00346
00347 inline int
00348 GPEnabled::get_count(void) const
00349 {
00350 return count;
00351 }
00352
00353 inline GPEnabled &
00354 GPEnabled::operator=(const GPEnabled & obj)
00355 {
00356
00357
00358
00359 return *this;
00360 }
00361
00362
00363
00364 inline
00365 GPBase::GPBase()
00366 : ptr(0)
00367 {
00368 }
00369
00370 inline
00371 GPBase::GPBase(GPEnabled *nptr)
00372 : ptr(0)
00373 {
00374 assign(nptr);
00375 }
00376
00377 inline
00378 GPBase::GPBase(const GPBase &sptr)
00379 {
00380 if (sptr.ptr)
00381 sptr.ptr->ref();
00382 ptr = sptr.ptr;
00383 }
00384
00385 inline
00386 GPBase::~GPBase()
00387 {
00388 GPEnabled *old = ptr;
00389 ptr = 0;
00390 if (old)
00391 old->unref();
00392 }
00393
00394 inline GPEnabled*
00395 GPBase::get() const
00396 {
00397 return ptr;
00398 }
00399
00400 inline GPBase &
00401 GPBase::operator=(const GPBase & obj)
00402 {
00403 return assign(obj);
00404 }
00405
00406 inline int
00407 GPBase::operator==(const GPBase & g2) const
00408 {
00409 return ptr == g2.ptr;
00410 }
00411
00412
00413
00414
00415
00416
00417 template <class TYPE> inline
00418 GP<TYPE>::GP()
00419 {
00420 }
00421
00422 template <class TYPE> inline
00423 GP<TYPE>::GP(TYPE *nptr)
00424 : GPBase((GPEnabled*)nptr)
00425 {
00426 }
00427
00428 template <class TYPE> inline
00429 GP<TYPE>::GP(const GP<TYPE> &sptr)
00430 : GPBase((const GPBase&) sptr)
00431 {
00432 }
00433
00434 template <class TYPE> inline
00435 GP<TYPE>::operator TYPE* () const
00436 {
00437 return (TYPE*) ptr;
00438 }
00439
00440 template <class TYPE> inline TYPE*
00441 GP<TYPE>::operator->() const
00442 {
00443 return (TYPE*) ptr;
00444 }
00445
00446 template <class TYPE> inline TYPE&
00447 GP<TYPE>::operator*() const
00448 {
00449 return *(TYPE*) ptr;
00450 }
00451
00452 template <class TYPE> inline GP<TYPE>&
00453 GP<TYPE>::operator= (TYPE *nptr)
00454 {
00455 return (GP<TYPE>&)( assign(nptr) );
00456 }
00457
00458 template <class TYPE> inline GP<TYPE>&
00459 GP<TYPE>::operator= (const GP<TYPE> &sptr)
00460 {
00461 return (GP<TYPE>&)( assign((const GPBase&)sptr) );
00462 }
00463
00464 template <class TYPE> inline int
00465 GP<TYPE>::operator== (TYPE *nptr) const
00466 {
00467 return ( (TYPE*)ptr == nptr );
00468 }
00469
00470 template <class TYPE> inline int
00471 GP<TYPE>::operator!= (TYPE *nptr) const
00472 {
00473 return ( (TYPE*)ptr != nptr );
00474 }
00475
00476 template <class TYPE> inline int
00477 GP<TYPE>::operator! () const
00478 {
00479 return !ptr;
00480 }
00481
00482
00483 #ifdef HAVE_NAMESPACES
00484 }
00485 # ifndef NOT_USING_DJVU_NAMESPACE
00486 using namespace DJVU;
00487 # endif
00488 #endif
00489 #endif