home *** CD-ROM | disk | FTP | other *** search
- /* Copyright (c) IBM Corp. 1992 */
- #ifndef _IREF_H
- #define _IREF_H
-
- #include <iglobals.h>
- #include <istdops.h>
-
- template < class Element >
- class IRef {
- protected:
- Element *ivPtr;
- public:
- IRef () : ivPtr (0) {}
- IRef (Element *ptr) : ivPtr (ptr) {}
-
- operator Element& () { return *ivPtr; }
- operator Element const& () const { return *ivPtr; }
-
- Element* operator & () { return ivPtr; }
- Element const* operator & () const { return ivPtr; }
-
- friend inline Element& elementForOps (IRef < Element > & ref)
- { return *ref.ivPtr; }
- friend inline Element const& elementForOps (IRef < Element > const&ref)
- { return *ref.ivPtr; }
- };
-
- template < class Element >
- class IMgRef {
- protected:
- struct PtrRc {
- Element *ivPtr;
- unsigned int ivRc;
- PtrRc (Element *ptr) : ivPtr (ptr), ivRc (1) {}
- ~PtrRc () { delete ivPtr; }
- } *ivPtrRc;
- public:
-
- IMgRef () : ivPtrRc (0) {}
-
- IMgRef (Element *ptr) {
- ivPtrRc = new PtrRc (ptr);
- }
-
- IMgRef (Element const& e) {
- ivPtrRc = new PtrRc (new Element (e));
- }
-
- IMgRef (IMgRef < Element > const& ref) {
- ivPtrRc = ref.ivPtrRc;
- ivPtrRc->ivRc++;
- }
-
- ~IMgRef () {
- if (ivPtrRc && --ivPtrRc->ivRc == 0) delete ivPtrRc;
- }
-
- IMgRef < Element >& operator= (IMgRef < Element > const& ref) {
- if (ivPtrRc == ref.ivPtrRc) return *this;
- if (ivPtrRc && --ivPtrRc->ivRc == 0) delete ivPtrRc;
- ivPtrRc = ref.ivPtrRc;
- ivPtrRc->ivRc++;
- return *this;
- }
-
- operator Element& () { return *ivPtrRc->ivPtr; }
- operator Element const& () const { return *ivPtrRc->ivPtr; }
-
- friend inline Element& elementForOps (IMgRef < Element > & ref)
- { return *ref.ivPtrRc->ivPtr; }
- friend inline Element const& elementForOps (IMgRef < Element > const&ref)
- { return *ref.ivPtrRc->ivPtr; }
- };
-
- #endif