#line 1 "/user/cvsspst/ees1cg/RAVL/RAVL-0.7/Core/Base/RCHandleV.hh" // This file is part of RAVL, Recognition And Vision Library // Copyright (C) 2002, University of Surrey // This code may be redistributed under the terms of the GNU Lesser // General Public License (LGPL). See the lgpl.licence file for details or // see http://www.gnu.org/copyleft/lesser.html // file-header-ends-here #ifndef RAVL_RCHANDLEV_HEADER #define RAVL_RCHANDLEV_HEADER 1 //! docentry="Ravl.Core.Reference Counting" //! lib=RavlCore //! userlevel=Normal //! rcsid="$Id: RCHandleV.hh,v 1.5 2002/08/08 16:03:10 craftit Exp $" //! author="Charles Galambos" //! example=exRefCounterInherit.cc //! file="Ravl/Core/Base/RCHandleV.hh" #include "Ravl/RefCounter.hh" #include "Ravl/Exception.hh" namespace RavlN { class RCAbstractC; template class RCHandleVC; template istream &operator>>(istream &strm,RCHandleVC &obj); template BinIStreamC &operator>>(BinIStreamC &strm,RCHandleVC &obj); //! userlevel=Normal //: Base class for all reference counted objects, where derivation is expected. // This holds a count of the number of handles that // are available for this object. class RCBodyVC : public RCBodyC { public: virtual ~RCBodyVC(); //: Destructor. virtual bool Save(ostream &out) const; //: Save to stream 'out'. virtual bool Save(BinOStreamC &out) const; //: Save to binary stream 'out'. virtual RCBodyVC &Copy() const; //: Make copy of body. // This should be provided in derived classes. // this funtion will issue an assertion failure if called. virtual RCBodyC &DeepCopy(UIntT levels = ((UIntT) -1)) const; //: Make a deep copy of body. // This should be provided in derived classes. // this funtion will issue an assertion failure if called. protected: RCBodyVC() {} //: Default constructor. // Creates a handle with 0 reference counts. RCBodyVC(istream &strm) {} //: Construct from a stream. RCBodyVC(BinIStreamC &strm) {} //: Construct from a binary stream. friend class RCAbstractC ; }; istream &operator>>(istream &,RCBodyVC &obj); //: Input virtual body. // No-op. ostream &operator<<(ostream &,const RCBodyC &obj); //: Output virtual body. // No-op. //! userlevel=Normal //: Handle from objects derived from RCBodyVC. template class RCHandleVC : public RCHandleC { public: RCHandleVC() {} //: Default constructor. // Creates an invalid handle. RCHandleVC(const RCAbstractC &oth) : RCHandleC(*dynamic_cast(const_cast (&oth.Body()))) {} //: Copy Constructor. // Creates a new reference to 'oth' protected: RCHandleVC(BodyT &bod) : RCHandleC(bod) {} //: Body constructor. RCHandleVC(BodyT *bod) : RCHandleC(bod) {} //: Construct from a body pointer. // The pointer may be 0. const BodyT &Body() const { return RCHandleC::Body(); } //: Constant access to body of object. BodyT &Body() { return RCHandleC::Body(); } //: Constant access to body of object. public: bool Save(ostream &out) const { return Body().Save(out); } //: Save to stream 'out'. bool Save(BinOStreamC &out) const { return Body().Save(out); } //: Save to binary stream 'out'. RCAbstractC Abstract() { return RCAbstractC(Body()); } //: Create an abstract handle. #if RAVL_NEW_ANSI_CXX_DRAFT friend istream &operator>> (istream &strm,RCHandleVC &obj); friend BinIStreamC &operator>> (BinIStreamC &strm,RCHandleVC &obj); #else friend istream &operator>> (istream &strm,RCHandleVC &obj); friend BinIStreamC &operator>> (BinIStreamC &strm,RCHandleVC &obj); #endif }; typedef RCHandleC AbstractC; //: Abstract object handle. // Note: Objects which used abstract handles MUST be derived // from RCBodyVC. RCBodyVC *VCLoad(istream &s); //: Load object from a stream via a virtual constructor RCBodyVC *VCLoad(BinIStreamC &s); //: Load object from a binary stream via a virtual constructor template BodyT *VCLoad(istream &s,BodyT *) { RCBodyVC *bp = VCLoad(s); BodyT *ret = dynamic_cast(bp); if(ret == 0) { delete bp; throw ExceptionErrorCastC("Virtual constructor failed.",typeid(RCBodyVC),typeid(BodyT)); } return ret; } template BodyT *VCLoad(BinIStreamC &s,BodyT *) { RCBodyVC *bp = VCLoad(s); BodyT *ret = dynamic_cast(bp); if(ret == 0) { delete bp; throw ExceptionErrorCastC("Virtual construction failed.",typeid(RCBodyVC),typeid(BodyT)); } return ret; } #define RAVL_VIRTUALCONSTRUCTOR(strm,targType) VCLoad(strm,(targType *) 0) template ostream &operator<<(ostream &strm,const RCHandleVC &obj) { obj.Save(strm); return strm; } //: Write a handle to a stream. template BinOStreamC &operator<<(BinOStreamC &strm,const RCHandleVC &obj) { obj.Save(strm); return strm; } //: Write binary handle to a stream. template istream &operator>>(istream &strm,RCHandleVC &obj) { obj = RCHandleVC(dynamic_cast(VCLoad(strm))); return strm; } //: Read a handle from a stream. template BinIStreamC &operator>>(BinIStreamC &strm,RCHandleVC &obj) { obj = RCHandleVC(dynamic_cast(VCLoad(strm))); return strm; } //: Read a handle from a binary stream. }; #endif