home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 10 Tools
/
10-Tools.zip
/
cset21v1.zip
/
IBMCPP
/
IBMCLASS
/
IPTR.H
< prev
next >
Wrap
Text File
|
1993-09-22
|
3KB
|
83 lines
/*******************************************************************************
* *
* COPYRIGHT: *
* IBM C/C++ Tools Version 2.01 - Collection Class Library *
* Licensed Materials - Property of IBM *
* (C) Copyright IBM Corporation 1992, 1993 *
* All Rights Reserved *
* US Government Users Restricted Rights - Use, duplication, or disclosure *
* restricted by GSA ADP Schedule Contract with IBM Corp. *
* *
*******************************************************************************/
#ifndef _IPTR_H
#define _IPTR_H
#include <iglobals.h>
#include <istdops.h>
template < class Element >
class IPtr {
protected:
Element *ivPtr;
public:
IPtr () : ivPtr (0) {}
IPtr (Element *ptr) : ivPtr (ptr) {}
Element& operator * () const { return *ivPtr; }
Element* operator-> () const { return ivPtr; }
friend inline Element& elementForOps (IPtr < Element >& ptr)
{ return *ptr.ivPtr; }
friend inline Element const& elementForOps (IPtr < Element > const& ptr)
{ return *ptr.ivPtr; }
};
template < class Element >
class IMgPtr {
protected:
struct PtrRc
{ Element *ivPtr;
unsigned int ivRc;
PtrRc (Element *ptr) : ivPtr (ptr), ivRc (1) {}
~PtrRc () { delete ivPtr; }
} *ivPtrRc;
public:
IMgPtr () : ivPtrRc (0) {}
IMgPtr (Element *ptr)
{ ivPtrRc = new PtrRc (ptr);
}
IMgPtr (Element const& e)
{ ivPtrRc = new PtrRc (new Element (e));
}
IMgPtr (IMgPtr < Element > const& ptr)
{ ivPtrRc = ptr.ivPtrRc;
ivPtrRc->ivRc++;
}
~IMgPtr ()
{ if (ivPtrRc && --ivPtrRc->ivRc == 0) delete ivPtrRc;
}
IMgPtr < Element >& operator= (IMgPtr < Element > const& ptr)
{ if (ivPtrRc == ptr.ivPtrRc) return *this;
if (ivPtrRc && --ivPtrRc->ivRc == 0) delete ivPtrRc;
ivPtrRc = ptr.ivPtrRc;
ivPtrRc->ivRc++;
return *this;
}
Element& operator * () const { return *ivPtrRc->ivPtr; }
Element* operator-> () const { return ivPtrRc->ivPtr; }
friend inline Element& elementForOps (IMgPtr < Element >& ptr)
{ return *ptr.ivPtrRc->ivPtr; }
friend inline Element const& elementForOps (IMgPtr < Element > const& ptr)
{ return *ptr.ivPtrRc->ivPtr; }
};
#endif