#line 1 "/user/cvsspst/ees1cg/RAVL/RAVL-0.7/Core/IO/Func2Stream.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_DPFUNC2STREAM_HEADER #define RAVL_DPFUNC2STREAM_HEADER 1 ///////////////////////////////////////////////////// //! rcsid="$Id: Func2Stream.hh,v 1.6 2002/07/31 07:44:54 craftit Exp $" //! docentry="Ravl.Core.Data Processing.Internal" //! file="Ravl/Core/IO/Func2Stream.hh" //! lib=RavlIO //! author="Charles Galambos" //! date="09/07/98" #include "Ravl/DP/StreamOp.hh" #include "Ravl/DP/IOJoin.hh" #include "Ravl/SArr1Iter2.hh" namespace RavlN { ////////////////////////////// //! userlevel=Develop //: Wrapped function input stream body. template class DPFuncIStreamBodyC : public DPIStreamOpBodyC { public: typedef OutT (*FuncT)(ParamT); FuncT func; DPFuncIStreamBodyC(const DPIPortC &nin,FuncT nfunc) : DPIStreamOpBodyC(nin), func(nfunc) {} //: Constructor. virtual OutT Get() { RavlAssert(input.IsValid()); return func(input.Get()); } //: Process next piece of data. virtual bool Get(OutT &outbuff) { InT buff; RavlAssert(input.IsValid()); if(!input.Get(buff)) return false; outbuff = func(buff); return true; } //: Process some data. virtual IntT GetArray(SArray1dC &dest) { RavlAssert(input.IsValid()); SArray1dC src(dest.Size()); int rem = input.GetArray(src); if(rem > 0) src = SArray1dC(src,rem); for(SArray1dIter2C it(src,dest);it;it++) it.Data2() = func(it.Data1()); return rem; } //: Get an array of data from stream. }; ////////////////////////////// //! userlevel=Develop //: Wrapped function input stream body. template class DPCPFuncIStreamBodyC : public DPIStreamOpBodyC { public: typedef OutT (*FuncT)(const ParamT &); FuncT func; DPCPFuncIStreamBodyC(const DPIPortC &nin,FuncT nfunc) : DPIStreamOpBodyC(nin), func(nfunc) {} //: Constructor. virtual OutT Get() { RavlAssert(input.IsValid()); return func(input.Get()); } //: Process next piece of data. virtual bool Get(OutT &outbuff) { InT buff; RavlAssert(input.IsValid()); if(!input.Get(buff)) return false; outbuff = func(buff); return true; } //: Process some data. virtual IntT GetArray(SArray1dC &dest) { RavlAssert(input.IsValid()); SArray1dC src(dest.Size()); int rem = input.GetArray(src); if(rem > 0) src = SArray1dC(src,dest.Size() - rem); for(SArray1dIter2C it(src,dest);it;it++) it.Data2() = func(it.Data1()); return rem; } //: Get an array of data from stream. }; ////////////////////////////// //! userlevel=Develop //: Wrapped function output stream body. template class DPFuncOStreamBodyC : public DPOStreamOpBodyC { public: typedef OutT (*FuncT)(ParamT); FuncT func; DPFuncOStreamBodyC(const DPOPortC &nin,FuncT nfunc) : DPOStreamOpBodyC(nin), func(nfunc) {} //: Constructor. virtual bool Put(const InT &buff) { RavlAssert(output.IsValid()); return output.Put(func(buff)); } //: Process some data. virtual IntT PutArray(const SArray1dC &src) { SArray1dC dest(src.Size()); for(SArray1dIter2C it(src,dest);it;it++) it.Data2() = func(it.Data1()); return output.PutArray(dest); } //: Put an array of data to stream. // returns the number of elements processed. }; ////////////////////////////// //! userlevel=Develop //: Wrapped function output stream body. template class DPCPFuncOStreamBodyC : public DPOStreamOpBodyC { public: typedef OutT (*FuncT)(const ParamT &); FuncT func; DPCPFuncOStreamBodyC(const DPOPortC &nin,FuncT nfunc) : DPOStreamOpBodyC(nin), func(nfunc) {} //: Constructor. virtual bool Put(const InT &buff) { RavlAssert(output.IsValid()); return output.Put(func(buff)); } //: Process some data. virtual IntT PutArray(const SArray1dC &src) { SArray1dC dest(src.Size()); for(SArray1dIter2C it(src,dest);it;it++) it.Data2() = func(it.Data1()); return output.PutArray(dest); } //: Put an array of data to stream. // returns the number of elements processed }; /////////////////////////////////// //! userlevel=Normal //: Wrapped function input stream. template class DPFuncIStreamC : public DPIStreamOpC { public: typedef OutT (*FuncT)(ParamT); typedef OutT (*CPFuncT)(const ParamT &); DPFuncIStreamC() {} //: Default Constructor. DPFuncIStreamC(const DPIPortC &nin,FuncT func) : DPIStreamOpC((DPIPortBodyC &) *new DPFuncIStreamBodyC(nin,func)) {} //: Constructor. DPFuncIStreamC(const DPIPortC &nin,CPFuncT func) : DPIStreamOpC((DPIPortBodyC &) *new DPCPFuncIStreamBodyC(nin,func)) {} //: Constructor. DPFuncIStreamC(const DPFuncIStreamC &oth) : DPIStreamOpC(oth) {} //: Copy Constructor. }; /////////////////////////////////// //! userlevel=Normal //: Wrapped function output stream. template class DPFuncOStreamC : public DPOStreamOpC { public: typedef OutT (*FuncT)(ParamT); typedef OutT (*CPFuncT)(const ParamT &); DPFuncOStreamC() {} //: Default Constructor. DPFuncOStreamC(const DPOPortC &nin,FuncT func) : DPOStreamOpC(*new DPFuncOStreamBodyC(nin,func)) {} //: Constructor. DPFuncOStreamC(const DPOPortC &nin,CPFuncT func) : DPOStreamOpC(*new DPCPFuncOStreamBodyC(nin,func)) {} //: Constructor. DPFuncOStreamC(const DPFuncOStreamC &oth) : DPOStreamOpC(oth) {} //: Copy Constructor. }; /////////////////////////////// //: Composition operators. template DPIPortC operator>>(const DPIPortC &in,OutT (*func)(InT)) { return DPFuncIStreamC (in,func); } template DPIOPortC operator>>(const DPIOPortC &in,OutT (*func)(InT)) { return DPIOPortJoinC(DPFuncIStreamC (in,func),in); } template DPOPortC operator>>(OutT (*func)(InT),const DPOPortC &in) { return DPFuncOStreamC (in,func); } template DPIOPortC operator>>(OutT (*func)(InT),const DPIOPortC &in) { return DPIOPortJoinC(in,DPFuncOStreamC (in,func)); } // Version with constant paramiter. template DPIPortC operator>>(const DPIPortC &in,OutT (*func)(const InT &)) { return DPFuncIStreamC (in,func); } template DPIOPortC operator>>(const DPIOPortC &in,OutT (*func)(const InT &)) { return DPIOPortJoinC(DPFuncIStreamC (in,func),in); } template DPOPortC operator>>(OutT (*func)(const InT &),const DPOPortC &in) { return DPFuncOStreamC (in,func); } template DPIOPortC operator>>(OutT (*func)(const InT &),const DPIOPortC &in) { return DPIOPortJoinC(in,DPFuncOStreamC (in,func)); } } #endif