WvStreams
wvdelayedcallback.h
1 /* -*- Mode: C++ -*-
2  * Worldvisions Weaver Software:
3  * Copyright (C) 2003 Net Integration Technologies, Inc.
4  *
5  */
6 #ifndef __WVDELAYEDCALLBACK_H
7 #define __WVDELAYEDCALLBACK_H
8 
9 #include "wvistreamlist.h"
10 #include "wvtr1.h"
11 
29 template<class Functor>
31 {
32 private:
33  Functor func;
34  WvStream *stream;
35  wv::function<void()> frozen;
36 
37 public:
38  WvDelayedCallback(const Functor& _func):
39  func(_func), stream(new WvStream), frozen(0)
40  {
41  WvIStreamList::globallist.append(stream, true, "WvDelayedCallback");
42  }
44  func(other.func), stream(new WvStream), frozen(0)
45  {
46  WvIStreamList::globallist.append(stream, true, "WvDelayedCallback");
47  }
49  {
50  stream->close();
51  }
52  void operator()()
53  {
54  stream->setcallback(func);
55  stream->alarm(0);
56  }
57  template<typename P1>
58  void operator()(P1 &p1)
59  {
60  stream->setcallback(wv::bind(func, p1));
61  stream->alarm(0);
62  }
63  template<typename P1,
64  typename P2>
65  void operator()(P1 &p1, P2 &p2)
66  {
67  stream->setcallback(wv::bind(func, p1, p2));
68  stream->alarm(0);
69  }
70  template<typename P1,
71  typename P2,
72  typename P3>
73  void operator()(P1 &p1, P2 &p2, P3 &p3)
74  {
75  stream->setcallback(wv::bind(func, p1, p2, p3));
76  stream->alarm(0);
77  }
78  template<typename P1,
79  typename P2,
80  typename P3,
81  typename P4>
82  void operator()(P1 &p1, P2 &p2, P3 &p3, P4 &p4)
83  {
84  stream->setcallback(wv::bind(func, p1, p2, p3, p4));
85  stream->alarm(0);
86  }
87  template<typename P1,
88  typename P2,
89  typename P3,
90  typename P4,
91  typename P5>
92  void operator()(P1 &p1, P2 &p2, P3 &p3, P4 &p4, P5 &p5)
93  {
94  stream->setcallback(wv::bind(func, p1, p2, p3, p4, p5));
95  stream->alarm(0);
96  }
97  template<typename P1,
98  typename P2,
99  typename P3,
100  typename P4,
101  typename P5,
102  typename P6>
103  void operator()(P1 &p1, P2 &p2, P3 &p3, P4 &p4, P5 &p5, P6 &p6)
104  {
105  stream->setcallback(wv::bind(func, p1, p2, p3, p4, p5, p6));
106  stream->alarm(0);
107  }
108  template<typename P1,
109  typename P2,
110  typename P3,
111  typename P4,
112  typename P5,
113  typename P6,
114  typename P7>
115  void operator()(P1 &p1, P2 &p2, P3 &p3, P4 &p4, P5 &p5, P6 &p6, P7 &p7)
116  {
117  stream->setcallback(wv::bind(func, p1, p2, p3, p4, p5, p6, p7));
118  stream->alarm(0);
119  }
120  template<typename P1,
121  typename P2,
122  typename P3,
123  typename P4,
124  typename P5,
125  typename P6,
126  typename P7,
127  typename P8>
128  void operator()(P1 &p1, P2 &p2, P3 &p3, P4 &p4, P5 &p5, P6 &p6, P7 &p7,
129  P8 &p8)
130  {
131  stream->setcallback(wv::bind(func, p1, p2, p3, p4, p5, p6, p7, p8));
132  stream->alarm(0);
133  }
134 };
135 
136 
137 /*
138  * We put the following in the wv:: namespace so that they match wv::bind
139  * and wv::function from wvtr1.h.
140  */
141 namespace wv
142 {
151  template <typename T>
152  inline T delayed(T cb)
153  {
154  return WvDelayedCallback<T>(cb);
155  }
156 
165  template <typename T>
166  inline wv::function<T> delayed(T *cb)
167  {
169  }
170 }
171 
172 #endif
WvStream::close
virtual void close()
Close the stream if it is open; isok() becomes false from now on.
Definition: wvstream.cc:341
WvStream::alarm
void alarm(time_t msec_timeout)
set an alarm, ie.
Definition: wvstream.cc:1048
WvStream
Unified support for streams, that is, sequences of bytes that may or may not be ready for read/write ...
Definition: wvstream.h:24
WvStream::setcallback
void setcallback(IWvStreamCallback _callfunc)
define the callback function for this stream, called whenever the callback() member is run,...
Definition: wvstream.cc:1130
WvDelayedCallback
A WvCallback wrapper that delays until the next tick of the WvIStreamList main loop.
Definition: wvdelayedcallback.h:30