00001 // 00002 // Copyright (C) 2007 Free Software Foundation, Inc. 00003 // 00004 // This file is part of GNU Cygnal. 00005 // 00006 // This program is free software; you can redistribute it and/or modify 00007 // it under the terms of the GNU General Public License as published by 00008 // the Free Software Foundation; either version 3 of the License, or 00009 // (at your option) any later version. 00010 // 00011 // This program is distributed in the hope that it will be useful, 00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 // GNU General Public License for more details. 00015 // 00016 // You should have received a copy of the GNU General Public License 00017 // along with this program; if not, write to the Free Software 00018 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 00019 // 00020 00025 #pragma once 00026 #ifndef __ACT_hpp___ 00027 #define __ACT_hpp___ 00028 00029 #include <boost/functional.hpp> 00030 #include <boost/shared_ptr.hpp> 00031 using boost::shared_ptr ; 00032 00033 namespace ACT { 00034 //------------------------- 00065 class ACT_State 00066 { 00067 public: 00069 enum state { 00071 Ready, 00073 Would_Block, 00075 Completed, 00077 Bad 00078 } ; 00079 00080 private: 00082 state x ; 00083 00084 public: 00086 ACT_State( state x ) : x( x ) {} ; 00087 00089 ACT_State & operator=( state y ) { x = y ; return * this ; } 00090 00092 inline bool ready() const { return x == Ready ; } 00093 00095 inline bool would_block() const { return x == Would_Block ; } 00096 00098 inline bool completed() const { return x == Completed ; } 00099 00101 inline bool bad() const { return x == Bad ; } 00102 00105 inline bool working() const { return x <= Would_Block ; } 00106 } ; 00107 00108 //------------------------- 00109 // Forward 00110 class wakeup_listener ; 00111 //------------------------- 00166 class basic_act { 00168 ACT_State the_state ; 00169 00170 protected: 00172 basic_act() 00173 : the_state( ACT_State::Ready ) 00174 {} 00175 00177 inline ACT_State set_ready() { return the_state = ACT_State::Ready ; } 00178 00180 inline ACT_State set_would_block() { return the_state = ACT_State::Would_Block ; } 00181 00183 inline ACT_State set_completed() { return the_state = ACT_State::Completed ; } 00184 00186 inline ACT_State set_bad() { return the_state = ACT_State::Bad ; } 00187 00189 inline ACT_State set_state( ACT_State x ) { return the_state = x ; } 00190 00191 public: 00193 inline ACT_State internal_state() { return the_state ; } 00194 00196 inline bool ready() const { return the_state.ready() ; } 00197 00199 inline bool would_block() const { return the_state.would_block() ; } 00200 00202 inline bool working() const { return the_state.working() ; } 00203 00205 inline bool completed() const { return the_state.completed() ; } 00206 00208 inline bool bad() const { return the_state.bad() ; } 00209 00211 virtual ACT_State operator()( void ) =0 ; 00212 00214 virtual ACT_State operator()( wakeup_listener * ) =0 ; 00215 00216 } ; 00217 00218 //------------------------- 00223 class simple_act 00224 : public basic_act 00225 { 00226 protected: 00237 virtual ACT_State run( void ) =0 ; 00238 00239 public: 00246 inline ACT_State operator()( void ) 00247 { 00248 if ( ! working() ) return internal_state() ; 00249 return run() ; 00250 } 00251 00252 inline ACT_State operator()( wakeup_listener * ) 00253 { 00254 return operator()() ; 00255 } 00256 } ; 00257 00258 //------------------------- 00263 class autonomous_act 00264 : public basic_act 00265 { 00267 friend class act ; 00268 00269 protected: 00310 virtual ACT_State run( wakeup_listener * ) =0 ; 00311 00312 public: 00314 inline ACT_State operator()( wakeup_listener * w ) 00315 { 00316 if ( ! working() ) return internal_state() ; 00317 return run( w ) ; 00318 } 00319 00321 inline ACT_State operator()() { return operator()( 0 ) ; } 00322 } ; 00323 00324 //------------------------- 00334 class act 00335 { 00337 shared_ptr< basic_act > the_body ; 00338 00339 public: 00345 act( basic_act * the_body ) ; 00346 00348 act( shared_ptr< basic_act > ) ; 00349 00351 ~act() ; 00352 00354 shared_ptr< basic_act > the_underlying_act() 00355 { 00356 return the_body ; 00357 } 00358 00361 ACT_State operator()( wakeup_listener * x ) { 00362 if ( working() ) { 00363 return the_body -> operator()( x ) ; 00364 } 00365 return the_body -> internal_state() ; 00366 } 00367 00369 inline bool working() const { return the_body -> working() ; } 00370 00372 inline bool completed() const { return the_body -> completed() ; } 00373 00375 inline bool bad() const { return the_body -> bad() ; } 00376 } ; 00377 00378 } // end of namespace ACT 00379 00380 #endif // end of inclusion protection
1.5.4