WvStreams
wvdelayedcallback.h
00001 /* -*- Mode: C++ -*-
00002  * Worldvisions Weaver Software:
00003  *   Copyright (C) 2003 Net Integration Technologies, Inc.
00004  *
00005  */
00006 #ifndef __WVDELAYEDCALLBACK_H
00007 #define __WVDELAYEDCALLBACK_H
00008 
00009 #include "wvistreamlist.h"
00010 #include "wvtr1.h"
00011 
00029 template<class Functor>
00030 class WvDelayedCallback
00031 {
00032 private:
00033     Functor func;
00034     WvStream *stream;
00035     wv::function<void()> frozen;
00036 
00037 public:
00038   WvDelayedCallback(const Functor& _func):
00039       func(_func), stream(new WvStream), frozen(0)
00040     {
00041         WvIStreamList::globallist.append(stream, true, "WvDelayedCallback");
00042     }
00043     WvDelayedCallback(const WvDelayedCallback &other):
00044         func(other.func), stream(new WvStream), frozen(0)
00045     {
00046         WvIStreamList::globallist.append(stream, true, "WvDelayedCallback");
00047     }
00048     ~WvDelayedCallback()
00049     {
00050         stream->close();
00051     }
00052     void operator()()
00053     {
00054         stream->setcallback(func);
00055         stream->alarm(0);
00056     }
00057     template<typename P1>
00058     void operator()(P1 &p1)
00059     {
00060         stream->setcallback(wv::bind(func, p1));
00061         stream->alarm(0);
00062     }
00063     template<typename P1,
00064              typename P2>
00065     void operator()(P1 &p1, P2 &p2)
00066     {
00067         stream->setcallback(wv::bind(func, p1, p2));
00068         stream->alarm(0);
00069     }
00070     template<typename P1,
00071              typename P2,
00072              typename P3>
00073     void operator()(P1 &p1, P2 &p2, P3 &p3)
00074     {
00075         stream->setcallback(wv::bind(func, p1, p2, p3));
00076         stream->alarm(0);
00077     }
00078     template<typename P1,
00079              typename P2,
00080              typename P3,
00081              typename P4>
00082     void operator()(P1 &p1, P2 &p2, P3 &p3, P4 &p4)
00083     {
00084         stream->setcallback(wv::bind(func, p1, p2, p3, p4));
00085         stream->alarm(0);
00086     }
00087     template<typename P1,
00088              typename P2,
00089              typename P3,
00090              typename P4,
00091              typename P5>
00092     void operator()(P1 &p1, P2 &p2, P3 &p3, P4 &p4, P5 &p5)
00093     {
00094         stream->setcallback(wv::bind(func, p1, p2, p3, p4, p5));
00095         stream->alarm(0);
00096     }
00097     template<typename P1,
00098              typename P2,
00099              typename P3,
00100              typename P4,
00101              typename P5,
00102              typename P6>
00103     void operator()(P1 &p1, P2 &p2, P3 &p3, P4 &p4, P5 &p5, P6 &p6)
00104     {
00105         stream->setcallback(wv::bind(func, p1, p2, p3, p4, p5, p6));
00106         stream->alarm(0);
00107     }
00108     template<typename P1,
00109              typename P2,
00110              typename P3,
00111              typename P4,
00112              typename P5,
00113              typename P6,
00114              typename P7>
00115     void operator()(P1 &p1, P2 &p2, P3 &p3, P4 &p4, P5 &p5, P6 &p6, P7 &p7)
00116     {
00117         stream->setcallback(wv::bind(func, p1, p2, p3, p4, p5, p6, p7));
00118         stream->alarm(0);
00119     }
00120     template<typename P1,
00121              typename P2,
00122              typename P3,
00123              typename P4,
00124              typename P5,
00125              typename P6,
00126              typename P7,
00127              typename P8>
00128     void operator()(P1 &p1, P2 &p2, P3 &p3, P4 &p4, P5 &p5, P6 &p6, P7 &p7,
00129                     P8 &p8)
00130     {
00131         stream->setcallback(wv::bind(func, p1, p2, p3, p4, p5, p6, p7, p8));
00132         stream->alarm(0);
00133     }
00134 };
00135 
00136 
00137 /*
00138  * We put the following in the wv:: namespace so that they match wv::bind
00139  * and wv::function from wvtr1.h.
00140  */
00141 namespace wv
00142 {
00151     template <typename T>
00152     inline T delayed(T cb)
00153     {
00154         return WvDelayedCallback<T>(cb);
00155     }
00156     
00165     template <typename T>
00166     inline wv::function<T> delayed(T *cb)
00167     {
00168         return WvDelayedCallback< wv::function<T> >(cb);
00169     }
00170 }
00171 
00172 #endif