#line 1 "/user/cvsspst/ees1cg/RAVL/RAVL-0.7/Core/IO/IOTap.hh" // This file is part of RAVL, Recognition And Vision Library // Copyright (C) 2001, 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_DPIOTAP_HEADER #define RAVL_DPIOTAP_HEADER 1 /////////////////////////////////////////////////// //! docentry="Ravl.Core.Data Processing.Taps" //! rcsid="$Id: IOTap.hh,v 1.5 2002/07/31 07:44:54 craftit Exp $" //! file="Ravl/Core/IO/IOTap.hh" //! lib=RavlIO //! author="Charles Galambos" //! date="20/07/1998" #include "Ravl/DP/Port.hh" #include "Ravl/DP/IOPort.hh" #include "Ravl/Tuple2.hh" #include "Ravl/Assert.hh" namespace RavlN { //////////////////////////////////// //! userlevel=Develop //: Tap body template class DPIOTapBodyC : public DPIOPortBodyC { public: DPIOTapBodyC(const DPIOPortC &ntarget,const DPOPortC > &ntap) : tap(ntap), target(ntarget) { RavlAssert(target.IsValid()); } //: Constructor. DPIOTapBodyC() {} //: Default Constructor. DPIOTapBodyC(istream &in) : tap(in), target(in) {} //: Stream constructor. virtual void PutEOS(); //: Put End Of Stream marker. virtual bool Put(const OutT &); //: Put data. virtual bool IsPutReady() const; //: Is port ready for data ? virtual InT Get(); //: Get next piece of data. // May block if not ready, or it will return a constructed // with the default constructor. virtual bool Get(InT &buff); //: Try and get next piece of data. // This will NOT block, if no data is ready // it will return false, and not set buff. virtual bool IsGetReady() const { return target.IsGetReady(); } //: Is some data ready ? // true = yes. virtual bool Save(ostream &out) const; //: Save to ostream. inline DPOPortC > &Tap() { return tap; } //: Access to tap. inline DPIOPortC &Target() { return target; } //: Access the object being tapped. protected: DPOPortC > tap; DPIOPortC target; Tuple2C tapout; bool donePut; }; /////////////////////////////////////// //! userlevel=Normal //: Tap handle. template class DPIOTapC : public DPIOPortC { public: DPIOTapC(const DPIOPortC &ntarget, const DPOPortC > &ntap) : DPEntityC(*new DPIOTapBodyC (ntarget,ntap)) {} //: Constructor. //= DPOPortC > () DPIOTapC() : DPEntityC(*new DPIOTapBodyC ()) {} //: Default Constructor. DPIOTapC(istream &in) : DPEntityC(in) {} //: Stream constructor. protected: DPIOTapBodyC &Body() { return dynamic_cast &>(DPEntityC::Body()); } const DPIOTapBodyC &Body() const { return dynamic_cast &>(DPEntityC::Body()); } protected: inline DPOPortC > &Tap() { return Body().Tap(); } //: Access to tap. inline DPIOPortC &Target() { return Body().Target(); } //: Access the object being tapped. inline DPIOPortC &IOPort() { return *this; } //: Simlified cast. }; template void DPIOTapBodyC::PutEOS() { target.PutEOS(); tap.PutEOS(); } template bool DPIOTapBodyC::Put(const OutT &dat) { RavlAssert(target.IsValid()); if(donePut == true) { tapout.Data2() = InT(); tap.Put(tapout); } tapout.Data1() = dat; donePut = true; return target.Put(dat); } template bool DPIOTapBodyC::IsPutReady() const { RavlAssert(target.IsValid()); return target.IsPutReady(); } template InT DPIOTapBodyC::Get() { RavlAssert(target.IsValid()); tapout.Data2() = target.Get(); if(!donePut) tapout.Data1() = OutT(); tap.Put(tapout); donePut=false; return tapout.Data2(); } template bool DPIOTapBodyC::Get(InT &buff) { RavlAssert(target.IsValid()); bool ret = target.Get(buff); if(ret) { tapout.Data2() = buff; if(!donePut) tapout.Data1() = OutT(); tap.Put(tapout); donePut=false; } return ret; } template bool DPIOTapBodyC::Save(ostream &out) const { if(!DPIOPortBodyC::Save(out)) return false; if(!tap.Save(out)) return false; if(!target.Save(out)) return false; return true; } } #endif