WvStreams
wvcont.h
1 /* -*- Mode: C++ -*-
2  * Worldvisions Weaver Software:
3  * Copyright (C) 1997-2002 Net Integration Technologies, Inc.
4  *
5  * FIXME: I was too lazy to templatize this properly, so we only support
6  * WvCallback<void*,void*>. It should be possible to work with any kind
7  * of return value and parameter, although it makes sense to limit things
8  * to just one parameter (since it currently has to be returned by yield()
9  * somehow).
10  */
11 
12 #ifndef __WVCONT_H
13 #define __WVCONT_H
14 
15 #include "wvlinklist.h"
16 #include "wvstreamsdebugger.h"
17 #include "wvtr1.h"
18 
19 typedef wv::function<void*(void*)> WvContCallback;
20 
29 class WvCont
30 {
31  struct Data;
32  friend struct Data;
33  typedef WvList<Data> DataList;
34 
35 private:
41  Data *data;
42  static DataList *data_list;
43 
44  static Data *curdata;
45  static int taskdepth;
46 
47  static void bouncer(void *userdata);
48 
53  void *call()
54  { return _call(data); }
55 
60  static void *_call(Data *data);
61 
66  WvCont(Data *data);
67 
68 public:
74  WvCont(const WvContCallback &cb, unsigned long stacksize = 64*1024);
75 
77  WvCont(const WvCont &cb);
78 
80  ~WvCont();
81 
87  void *operator() (void *p1 = 0);
88 
89  // the following are static because a function doesn't really know
90  // which WvCont it belongs to, and only one WvCont can be the "current"
91  // one globally in an application anyway.
92  //
93  // Unfortunately this prevents us from assert()ing that you're in the
94  // context you think you are.
95 
99  static WvCont current();
100 
107  static void *yield(void *ret = 0);
108 
114  static bool isok();
115 
116 
130  template <typename R, typename T>
131  static R c_bouncer(T t, void *_cont)
132  {
133  WvCont &cont = *(WvCont *)_cont;
134  return (R)cont((T)t);
135  }
136 
137 
151  template <typename R>
152  static R c_bouncer(void *_cont)
153  {
154  WvCont &cont = *(WvCont *)_cont;
155  return (R)cont(0);
156  }
157 
158 private:
159  static WvString debugger_conts_run_cb(WvStringParm cmd, WvStringList &args,
160  WvStreamsDebugger::ResultCallback result_cb, void *);
161 };
162 
163 #endif // __WVCONT_H
164 
WvCont::c_bouncer
static R c_bouncer(void *_cont)
A templated function that allows you to pass a WvCont wherever a C-style function pointer of the form...
Definition: wvcont.h:152
WvCont::~WvCont
~WvCont()
Destructor.
Definition: wvcont.cc:83
WvString
WvString is an implementation of a simple and efficient printable-string class.
Definition: wvstring.h:329
WvCont
WvCont provides "continuations", which are apparently also known as semi-coroutines.
Definition: wvcont.h:29
WvCont::current
static WvCont current()
Get a copy of the current WvCont.
Definition: wvcont.cc:213
WvCont::isok
static bool isok()
Tell us if the current context is "okay", that is, not trying to die.
Definition: wvcont.cc:238
WvStringList
This is a WvList of WvStrings, and is a really handy way to parse strings.
Definition: wvstringlist.h:27
WvList
A linked list container class.
Definition: wvlinklist.h:197
WvCont::operator()
void * operator()(void *p1=0)
call the callback, making p1 the return value of yield() or the parameter to the function,...
Definition: wvcont.cc:196
WvCont::Data
Definition: wvcont.cc:14
WvCont::c_bouncer
static R c_bouncer(T t, void *_cont)
A templated function that allows you to pass a WvCont wherever a C-style function pointer of the form...
Definition: wvcont.h:131
WvCont::yield
static void * yield(void *ret=0)
"return" from the current callback, giving value 'ret' to the person who called us.
Definition: wvcont.cc:222