Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

wvdelayedcallback.h

Go to the documentation of this file.
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 "wvcallback.h"
00010 #include "wvistreamlist.h"
00011 
00012 /**
00013  * A WvCallback wrapper that delays until the next tick of the WvIStreamList
00014  * main loop.
00015  *
00016  * There are restrictions on the type of the wrapped callback though:
00017  *   1. The return type must be void
00018  *   2. All parameter types must be copy-constructible value types
00019  */
00020 template<class InnerCallback>
00021 class WvDelayedCallback
00022 {
00023 private:
00024     typedef typename InnerCallback::ReturnType R;
00025     typedef typename InnerCallback::Parm1 P1;
00026     typedef typename InnerCallback::Parm2 P2;
00027     typedef typename InnerCallback::Parm3 P3;
00028     typedef typename InnerCallback::Parm4 P4;
00029     typedef typename InnerCallback::Parm5 P5;
00030     typedef typename InnerCallback::Parm6 P6;
00031     typedef typename InnerCallback::Parm7 P7;
00032     typedef typename InnerCallback::Parm8 P8;
00033     typedef WvCallbackImpl<R, P1, P2, P3, P4, P5, P6, P7, P8> Impl;
00034     typedef typename Impl::FrozenParams FrozenParams;
00035     InnerCallback cb;
00036     FrozenParams *frozen;
00037     WvStream *stream;
00038 
00039     void thaw(WvStream &stream, void *userdata)
00040     {
00041         cb.thaw(*frozen);
00042         stream.close();
00043     }
00044 
00045 public:
00046     template<typename PtrToObject, typename PtrToMember>
00047     WvDelayedCallback(PtrToObject obj, PtrToMember member)
00048         : cb(InnerCallback(obj, member)), frozen(0), stream(new WvStream)
00049     {
00050         stream->setcallback(WvStreamCallback(this, &WvDelayedCallback::thaw), 0);
00051         WvIStreamList::globallist.append(stream, true);
00052     }
00053     template<typename Functor>
00054     WvDelayedCallback(const Functor& func)
00055         : cb(InnerCallback(func)), frozen(0), stream(new WvStream)
00056     {
00057         stream->setcallback(WvStreamCallback(this, &WvDelayedCallback::thaw), 0);
00058         WvIStreamList::globallist.append(stream, true);
00059     }
00060     WvDelayedCallback(const WvDelayedCallback &other)
00061         : cb(other.cb), frozen(0), stream(new WvStream)
00062     {
00063         stream->setcallback(WvStreamCallback(this, &WvDelayedCallback::thaw), 0);
00064         WvIStreamList::globallist.append(stream, true);
00065     }
00066     ~WvDelayedCallback()
00067     {
00068         stream->setcallback(0, 0);
00069         stream->close();
00070         delete frozen;
00071     }
00072     R operator()()
00073     {
00074         frozen = new FrozenParams;
00075         stream->alarm(0);
00076         // you can't delay a callback that has a non-void return type, sorry
00077     }
00078     R operator()(P1 p1)
00079     {
00080         frozen = new FrozenParams(p1);
00081         stream->alarm(0);
00082     }
00083     R operator()(P1 p1, P2 p2)
00084     {
00085         frozen = new FrozenParams(p1, p2);
00086         stream->alarm(0);
00087     }
00088     R operator()(P1 p1, P2 p2, P3 p3)
00089     {
00090         frozen = new FrozenParams(p1, p2, p3);
00091         stream->alarm(0);
00092     }
00093     R operator()(P1 p1, P2 p2, P3 p3, P4 p4)
00094     {
00095         frozen = new FrozenParams(p1, p2, p3, p4);
00096         stream->alarm(0);
00097     }
00098     R operator()(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5)
00099     {
00100         frozen = new FrozenParams(p1, p2, p3, p4, p5);
00101         stream->alarm(0);
00102     }
00103     R operator()(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6)
00104     {
00105         frozen = new FrozenParams(p1, p2, p3, p4, p5, p6);
00106         stream->alarm(0);
00107     }
00108     R operator()(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7)
00109     {
00110         frozen = new FrozenParams(p1, p2, p3, p4, p5, p6, p7);
00111         stream->alarm(0);
00112     }
00113     R operator()(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7, P8 p8)
00114     {
00115         frozen = new FrozenParams(p1, p2, p3, p4, p5, p6, p7, p8);
00116         stream->alarm(0);
00117     }
00118 };
00119 #endif

Generated on Sat Mar 13 14:55:37 2004 for WvStreams by doxygen 1.3.6-20040222