csp::common::EvaluateFunction< RESULT, FUNCTION > Class Template Reference

Inheritance diagram for csp::common::EvaluateFunction< RESULT, FUNCTION >:

csp::CSProcess csp::ThreadCSProcess boost::noncopyable List of all members.

Detailed Description

template<typename RESULT, typename FUNCTION = RESULT (*) ()>
class csp::common::EvaluateFunction< RESULT, FUNCTION >

Evaluates a function and sends out the output.

This process calls a function/calls operator() on a function class once and sends out the output. It can be viewed as a simple way to distribute some processing into another thread. For example, this will search a list in another process:

        template <typename T>
        class ListSearcher
        {
        public:
            std::list<T>* data;
            T const * target;
            typename std::list<T>::iterator operator()()
            {
                return std::find(data->begin(),data->end(),*target);
            }
            
            inline ListSearcher(std::list<T>* _data,T const * _target)
                :   data(_data),target(_target)
            {
            }
        };
        
        // In some process:
        list<int> hugeList;
        // ... fill hugeList ...
        One2OneChannel< list<int>::iterator > c0,c1,c2;
        const int target0 = 36;
        const int target1 = 49;
        const int target2 = 64;
        ScopedForking forking;
        forking.fork(new EvaluateFunction< list<int>::iterator , ListSearcher<int> >(ListSearcher<int>(&hugeList,&target0),c0.writer()));
        forking.fork(new EvaluateFunction< list<int>::iterator , ListSearcher<int> >(ListSearcher<int>(&hugeList,&target1),c1.writer()));
        forking.fork(new EvaluateFunction< list<int>::iterator , ListSearcher<int> >(ListSearcher<int>(&hugeList,&target2),c2.writer()));
        
        list<int>::iterator res0,res1,res2;
        c0.reader() >> res0;
        c1.reader() >> res1;
        c2.reader() >> res2;
        
        if (res0 == hugeList.end() && res1 == hugeList.end() && res2 == hugeList.end())
        {
            //No values found, do something
        }

A simpler example is:

        One2OneChannel<int> c;
        ScopedForking forking;
        forking.fork(new EvaluateFunction<int>(&::rand,c.writer()));
        int n;
        //Read a random value into n:
        c.reader() >> n;

To use this process, you will need to include <cppcsp/common/basic.h>

DATA_TYPE Requirements

RESULT must meet the requirements of the channels it is being used with.

DATA_TYPE Requirements

FUNCTION must have a copy constructor, and must either be:


Public Member Functions

 EvaluateFunction (const FUNCTION &_func, const Chanout< RESULT > &_out)
 Constructs the process.

Protected Member Functions

void run ()
 You must implement this function to provide the code for your process.


Constructor & Destructor Documentation

template<typename RESULT, typename FUNCTION = RESULT (*) ()>
csp::common::EvaluateFunction< RESULT, FUNCTION >::EvaluateFunction ( const FUNCTION &  _func,
const Chanout< RESULT > &  _out 
) [inline]

Constructs the process.

Parameters:
_func The function to call
_out The output channel to send the result out on.


Member Function Documentation

template<typename RESULT, typename FUNCTION = RESULT (*) ()>
void csp::common::EvaluateFunction< RESULT, FUNCTION >::run (  )  [inline, protected, virtual]

You must implement this function to provide the code for your process.

When the run method finishes, the process will terminate.

You should not let an uncaught exception cause the end of this function. If it derives from std::exception, it will be caught (although this behaviour should not be relied upon) but otherwise undefined behaviour will result.

Implements csp::ThreadCSProcess.


Generated on Mon Aug 20 12:24:29 2007 for C++CSP2 by  doxygen 1.4.7