WvStreams
wvbellpull.h
00001 /* -*- Mode: C++ -*-
00002  * Worldvisions Weaver Software:
00003  *   Copyright (C) 1997-2002 Net Integration Technologies, Inc.
00004  */
00005 #ifndef __WVBATCHSIGNAL_H
00006 #define __WVBATCHSIGNAL_H
00007 
00008 #include <uniconf.h>
00009 #include <wvistreamlist.h>
00010 #include <wvtr1.h>
00011 
00012 
00013 class WvInvertedStream: public WvStream
00014 {
00015 public:
00016     WvInvertedStream(char *_id):
00017         WvStream()
00018     {
00019         WvIStreamList::globallist.append(this, false, _id);
00020     }
00021     ~WvInvertedStream()
00022     {
00023         WvIStreamList::globallist.unlink(this);
00024     }
00025 };
00026 
00027 
00028 /*
00029  * This class is a functor compatible with UniConfCallback,
00030  * IWvStreamCallback and WvStreamCallback, as well as supporting being
00031  * called with no parameters.
00032  *
00033  * It will turn any number of calls to any of these callbacks into a
00034  * single call to the callback you give to it, which will be sent on
00035  * the next run through the main loop.
00036  *
00037  * Think of it as an elevator button: pressing it a bunch of times has
00038  * no more effect than pressing it once, and while it doesn't do what
00039  * you tell it right away, it will do it soon enough.
00040  */
00041 class WvBellPull
00042 {
00043 public:
00044     WvBellPull(WvCallback<> _cb):
00045         cb(_cb),
00046         bellpull(new WvInvertedStream("bellpull"))
00047     {
00048         bellpull->setcallback(
00049             WvStreamCallback(this, &WvBellPull::bellpull_cb), NULL);
00050     }
00051 
00052     WvBellPull(const WvBellPull& _other):
00053         cb(_other.cb),
00054         bellpull(_other.bellpull)
00055     {
00056         bellpull->addRef();
00057     }
00058 
00059     ~WvBellPull()
00060     {
00061         bellpull->release();
00062     }
00063     void delay(time_t msec_timeout)
00064     {
00065         bellpull->alarm(msec_timeout);
00066     }
00067     void cancel()
00068     {
00069         bellpull->alarm(-1);
00070     }
00071     void operator()()
00072     {
00073         bellpull->alarm(0);
00074     }
00075     void operator()(IWvStream&)
00076     {
00077         bellpull->alarm(0);
00078     }
00079     void operator()(WvStream&, void*)
00080     {
00081         bellpull->alarm(0);
00082     }
00083     void operator()(const UniConf &, const UniConfKey &)
00084     {
00085         bellpull->alarm(0);
00086     }
00087 
00088 private:
00089     WvCallback<> cb;
00090     WvInvertedStream *bellpull;
00091 
00092     void bellpull_cb(WvStream&, void*)
00093     {
00094         cb();
00095     }
00096 };
00097 
00098 #endif /* __WVBATCHSIGNAL_H */