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