User Manual, Developers Guide and API Documentation

CommandQueue.hpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002  * This file is part of openWNS (open Wireless Network Simulator)
00003  * _____________________________________________________________________________
00004  *
00005  * Copyright (C) 2004-2007
00006  * Chair of Communication Networks (ComNets)
00007  * Kopernikusstr. 16, D-52074 Aachen, Germany
00008  * phone: ++49-241-80-27910,
00009  * fax: ++49-241-80-22242
00010  * email: info@openwns.org
00011  * www: http://www.openwns.org
00012  * _____________________________________________________________________________
00013  *
00014  * openWNS is free software; you can redistribute it and/or modify it under the
00015  * terms of the GNU Lesser General Public License version 2 as published by the
00016  * Free Software Foundation;
00017  *
00018  * openWNS is distributed in the hope that it will be useful, but WITHOUT ANY
00019  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
00020  * A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
00021  * details.
00022  *
00023  * You should have received a copy of the GNU Lesser General Public License
00024  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00025  *
00026  ******************************************************************************/
00027 
00028 #ifndef WNS_EVENTS_SCHEDULER_COMMANDQUEUE_HPP
00029 #define WNS_EVENTS_SCHEDULER_COMMANDQUEUE_HPP
00030 
00031 #include <WNS/events/scheduler/ICommand.hpp>
00032 #include <WNS/events/scheduler/Callable.hpp>
00033 #include <WNS/container/FastListEnabler.hpp>
00034 #include <WNS/container/FastList.hpp>
00035 #include <WNS/Assure.hpp>
00036 
00037 #include <pthread.h>
00038 
00039 namespace wns { namespace events { namespace scheduler {
00050     class CommandQueue
00051     {
00052     public:
00056         CommandQueue();
00057 
00061         virtual
00062         ~CommandQueue();
00063 
00067         void
00068         reset();
00069 
00073         ICommandPtr
00074         queueCommand(const Callable& callee);
00075 
00079         void
00080         dequeueCommand(const ICommandPtr& command);
00081 
00085         void
00086         runCommands();
00087 
00088     private:
00089 
00096         class Command :
00097             public virtual ICommand,
00098             public wns::container::SingleFastListEnabler< wns::SmartPtr<Command> >
00099         {
00100         public:
00101             Command(const wns::events::scheduler::Callable& callable) :
00102                 callable_(callable),
00103                 state_(NotSubmitted)
00104             {
00105             }
00106 
00110             void
00111             operator()()
00112             {
00113                 callable_();
00114             }
00115 
00116             enum State {
00117                 NotSubmitted,
00118                 Queued,
00119                 Running,
00120                 Finished,
00121                 Canceled
00122             };
00123 
00124             virtual bool
00125             isNotSubmitted() const
00126             {
00127                 return NotSubmitted == state_;
00128             }
00129 
00130             virtual bool
00131             isQueued() const
00132             {
00133                 return Queued == state_;
00134             }
00135 
00136             virtual bool
00137             isRunning() const
00138             {
00139                 return Running == state_;
00140             }
00141 
00142             virtual bool
00143             isFinished() const
00144             {
00145                 return Finished == state_;
00146             }
00147 
00148             virtual bool
00149             isCanceled() const
00150             {
00151                 return Canceled == state_;
00152             }
00153 
00154             wns::events::scheduler::Callable callable_;
00155 
00156             State state_;
00157         };
00158 
00159         typedef wns::SmartPtr<Command> CommandPtr;
00160 
00161         typedef wns::container::FastList<CommandPtr> CommandContainer;
00162 
00163         CommandContainer queue_;
00164 
00165         pthread_mutex_t mutex_;
00166 
00167     }; // CommandQueue
00168 } // scheduler
00169 } // events
00170 } // wns
00171 
00172 #endif // NOT defined WNS_EVENTS_SCHEDULER_COMMANDQUEUE_HPP

Generated on Tue May 22 03:31:35 2012 for openWNS by  doxygen 1.5.5