Marine Library  1.0
C++ library for Linux Networking Development
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
coroutine.hh
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016 Zhao DAI <daidodo@gmail.com>
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or any
7  * later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see accompanying file LICENSE.txt
16  * or <http://www.gnu.org/licenses/>.
17  */
18 
26 #ifndef DOZERG_COROUTINE_H_20150420
27 #define DOZERG_COROUTINE_H_20150420
28 
29 #ifndef UNIT_TEST
30 # warning "This file is still under development."
31 #endif
32 
33 #include <signal.h> // MINSIGSTKSZ
34 #include "impl/coroutine_impl.hh"
35 
36 NS_SERVER_BEGIN
37 
38 template<class T>
39 class CCoroutine : public NS_IMPL::CCoroutineBase
40 {
41  //typedefs
42  typedef NS_IMPL::CCoroutineBase __MyBase;
43 public:
44  class yield_type : public NS_IMPL::CYieldBase {
45  friend class CCoroutine<T>;
46  //typedefs
47  typedef NS_IMPL::CYieldBase __MyBase;
48  public:
49  typedef T arg_type;
50  typedef CCoroutine<T> coro_type;
51  //functions
52  arg_type get() const{return arg_;}
53  using __MyBase::operator ();
54  template<class U>
55  void operator ()(CCoroutine<U> & coro, typename CCoroutine<U>::arg_type arg){
56  coro.set(arg);
57  __MyBase::operator ()(coro);
58  }
59  private:
60  explicit yield_type(coro_type & coro):__MyBase(coro){}
61  void set(arg_type arg){arg_ = arg;}
62  //fields
63  arg_type arg_;
64  };
65  typedef typename yield_type::arg_type arg_type;
66  typedef void (* func_type)(yield_type &);
67  //functions
68  explicit CCoroutine(func_type func, size_t stackSz = MINSIGSTKSZ)
69  : __MyBase(CoroProc, stackSz), y_(*this), func_(func)
70  {}
71  void operator ()(arg_type arg){
72  set(arg);
73  __MyBase::resume();
74  }
75  void set(arg_type arg){y_.set(arg);}
76 private:
77  static void CoroProc(int high, int low){
78  CCoroutine & coro = __MyBase::cast<CCoroutine>(high, low);
79  coro.run(coro.func_, coro.y_);
80  }
81  //fields
82  yield_type y_;
83  func_type func_;
84 };
85 
86 template<class T>
87 class CCoroutine<T &> : public NS_IMPL::CCoroutineBase
88 {
89  //typedefs
90  typedef NS_IMPL::CCoroutineBase __MyBase;
91 public:
92  class yield_type : public NS_IMPL::CYieldBase {
93  friend class CCoroutine<T &>;
94  //typedefs
95  typedef NS_IMPL::CYieldBase __MyBase;
96  public:
97  typedef T & arg_type;
98  typedef CCoroutine<T &> coro_type;
99  //functions
100  arg_type get() const{return *arg_;}
101  using __MyBase::operator ();
102  template<class U>
103  void operator ()(CCoroutine<U> & coro, typename CCoroutine<U>::arg_type arg){
104  coro.set(arg);
105  __MyBase::operator ()(coro);
106  }
107  private:
108  explicit yield_type(coro_type & coro):__MyBase(coro){}
109  void set(T * arg){arg_ = arg;}
110  //fields
111  T * arg_;
112  };
113  typedef typename yield_type::arg_type arg_type;
114  typedef void (* func_type)(yield_type &);
115 private:
116  //functions
117  static void CoroProc(int high, int low){
118  CCoroutine & coro = __MyBase::cast<CCoroutine>(high, low);
119  coro.run(coro.func_, coro.y_);
120  }
121 public:
122  explicit CCoroutine(func_type func, size_t stackSz = MINSIGSTKSZ)
123  : __MyBase(CoroProc, stackSz)
124  , y_(*this)
125  , func_(func)
126  {}
127  void operator ()(arg_type arg){
128  set(arg);
129  __MyBase::resume();
130  }
131  void set(arg_type arg){y_.set(&arg);}
132 private:
133  //fields
134  yield_type y_;
135  func_type func_;
136 };
137 
138 template<>
139 class CCoroutine<void> : public NS_IMPL::CCoroutineBase
140 {
141  //typedefs
142  typedef NS_IMPL::CCoroutineBase __MyBase;
143 public:
144  class yield_type : public NS_IMPL::CYieldBase {
145  friend class CCoroutine<void>;
146  //typedefs
147  typedef NS_IMPL::CYieldBase __MyBase;
148  public:
149  typedef CCoroutine<void> coro_type;
150  //functions
151  using __MyBase::operator ();
152  template<class U>
153  void operator ()(CCoroutine<U> & coro, typename CCoroutine<U>::arg_type arg){
154  coro.set(arg);
155  __MyBase::operator ()(coro);
156  }
157  private:
158  explicit yield_type(coro_type & coro):__MyBase(coro){}
159  };
160  typedef void (* func_type)(yield_type &);
161 private:
162  //functions
163  static void CoroProc(int high, int low){
164  CCoroutine & coro = __MyBase::cast<CCoroutine>(high, low);
165  coro.run(coro.func_, coro.y_);
166  }
167 public:
168  explicit CCoroutine(func_type func, size_t stackSz = MINSIGSTKSZ)
169  : __MyBase(CoroProc, stackSz)
170  , y_(*this)
171  , func_(func)
172  {}
173  void operator ()(){__MyBase::resume();}
174 private:
175  //fields
176  yield_type y_;
177  func_type func_;
178 };
179 
180 NS_SERVER_END
181 
182 #endif
183 
Definition: coroutine.hh:39
Definition: coroutine.hh:139
Definition: coroutine.hh:44
Definition: coroutine.hh:87