#line 1 "/user/cvsspst/ees1cg/RAVL/RAVL-0.7/Core/IO/FuncP2Proc.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_DPFUNCP2PROCESS_HEADER #define RAVL_DPFUNCP2PROCESS_HEADER 1 /////////////////////////////////////////////// //! docentry="Ravl.Core.Data Processing.Processes" //! lib=RavlIO //! rcsid="$Id: FuncP2Proc.hh,v 1.7 2002/07/29 14:26:02 craftit Exp $" //! file="Ravl/Core/IO/FuncP2Proc.hh" //! author="Charles Galambos" //! date="09/07/1998" //! userlevel=Normal //! example=exDataProc.cc #include "Ravl/DP/Process.hh" #include "Ravl/DP/ProcCompose.hh" #include "Ravl/FunctionRegister.hh" #include "Ravl/Exception.hh" namespace RavlN { //! userlevel=Develop //: Function ptr to process body. template class DPFuncP2ProcBodyC : public DPProcessBodyC { public: typedef OutT (*FuncT)(InT); FuncT func; DPFuncP2ProcBodyC() {} //: Default constructor. DPFuncP2ProcBodyC(FuncT afunc) : func(afunc) {} //: Constructor. DPFuncP2ProcBodyC(istream &in) : DPProcessBodyC(in) { LoadFunctionPointer(in,func); } //: Stream constructor. DPFuncP2ProcBodyC(BinIStreamC &in) : DPProcessBodyC(in) { LoadFunctionPointer(in,func); } //: Binary stream constructor. virtual bool Save(ostream &out) const { if(!DPProcessBodyC::Save(out)) return false; return SaveFunctionPointer(out,func); } //: Save to ostream. virtual bool Save(BinOStreamC &out) const { if(!DPProcessBodyC::Save(out)) return false; return SaveFunctionPointer(out,func); } //: Save to binary stream. virtual OutT Apply(const InT &dat) { return func(dat); } //: Apply operation. IntT ApplyArray(const SArray1dC &in,SArray1dC &out) { RavlAssert(in.Size() <= out.Size()); for(SArray1dIter2C it(in,out);it;it++) it.Data2() = func(it.Data1()); return in.Size(); } //: Apply function to an array. virtual bool IsStateless() const { return true; } //: Is operation stateless ? }; //! userlevel=Develop //: Function ptr to process, where function has a constant reg as argument. template class DPCPFuncP2ProcBodyC : public DPProcessBodyC { public: typedef OutT (*FuncT)(const InT &); DPCPFuncP2ProcBodyC() {} //: Default constructor. DPCPFuncP2ProcBodyC(FuncT afunc) : func(afunc) {} //: Constructor. virtual OutT Apply(const InT &dat) { return func(dat); } //: Apply operation. virtual bool IsStateless() const { return true; } //: Is operation stateless ? protected: FuncT func; }; //! userlevel=Normal //: Function ptr to process handle. template class DPFuncP2ProcC : public DPProcessC { public: typedef OutT (*FuncT)(InT); typedef OutT (*CPFuncT)(const InT &); DPFuncP2ProcC(FuncT func) : DPProcessC(*new DPFuncP2ProcBodyC(func)) {} //: Constructor. DPFuncP2ProcC(CPFuncT func) : DPProcessC(*new DPCPFuncP2ProcBodyC(func)) {} //: Constructor. }; template DPFuncP2ProcC Process(OutT (*func)(const InT &)) { return DPFuncP2ProcC(func); } //: Turn a function into a process. template DPProcessC operator>>(const DPProcessC &np1,OutT (*func)(InterT)) { return DPComposeProcessC(np1,DPFunc2Proc(func)); } template DPProcessC operator>>(InterT (*func)(InT),const DPProcessC &np1) { return DPComposeProcessC(DPFunc2Proc(func),np1); } // Constant reference versions. template DPProcessC DPFunc2Proc(OutT (*func)(const InT &)) { return DPFuncP2ProcC(func); } template DPProcessC operator>>(const DPProcessC &np1,OutT (*func)(const InterT &)) { return DPComposeProcessC(np1,DPFuncP2ProcC(func)); } template DPProcessC operator>>(InterT (*func)(const InT &),const DPProcessC &np1) { return DPComposeProcessC(DPFuncP2ProcC(func),np1); } #if 0 // Problem: No paramiter is a class type! // Solution: Use the DPFunc2Proc() on one of the func ptrs. template DPProcessC operator>>(InterT (*func1)(InT),OutT (*func2)(InterT)) { return DPComposeProcessC(DPFuncP2ProcC(func1),DPFuncP2ProcC(func2)); } #endif } #endif