| 1 | // (C) Copyright Mac Murrett 2001. |
|---|
| 2 | // Use, modification and distribution are subject to the |
|---|
| 3 | // Boost Software License, Version 1.0. (See accompanying file |
|---|
| 4 | // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
|---|
| 5 | |
|---|
| 6 | // See http://www.boost.org for most recent version. |
|---|
| 7 | |
|---|
| 8 | #ifndef BOOST_PERIODICAL_MJM012402_HPP |
|---|
| 9 | #define BOOST_PERIODICAL_MJM012402_HPP |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | #include <boost/function.hpp> |
|---|
| 13 | #include <boost/utility.hpp> |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | namespace boost { |
|---|
| 17 | |
|---|
| 18 | namespace threads { |
|---|
| 19 | |
|---|
| 20 | namespace mac { |
|---|
| 21 | |
|---|
| 22 | namespace detail { |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | // class periodical inherits from its template parameter, which should follow the |
|---|
| 26 | // pattern set by classes dt_scheduler and st_scheduler. periodical knows how to |
|---|
| 27 | // call a boost::function, where the xx_scheduler classes only know to to call a |
|---|
| 28 | // member periodically. |
|---|
| 29 | |
|---|
| 30 | template<class Scheduler> |
|---|
| 31 | class periodical: private noncopyable, private Scheduler |
|---|
| 32 | { |
|---|
| 33 | public: |
|---|
| 34 | periodical(function<void> &rFunction); |
|---|
| 35 | ~periodical(); |
|---|
| 36 | |
|---|
| 37 | public: |
|---|
| 38 | void start(); |
|---|
| 39 | void stop(); |
|---|
| 40 | |
|---|
| 41 | protected: |
|---|
| 42 | virtual void periodic_function(); |
|---|
| 43 | |
|---|
| 44 | private: |
|---|
| 45 | function<void> m_oFunction; |
|---|
| 46 | }; |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | template<class Scheduler> |
|---|
| 50 | periodical<Scheduler>::periodical(function<void> &rFunction): |
|---|
| 51 | m_oFunction(rFunction) |
|---|
| 52 | { |
|---|
| 53 | // no-op |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | template<class Scheduler> |
|---|
| 57 | periodical<Scheduler>::~periodical() |
|---|
| 58 | { |
|---|
| 59 | stop(); |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | template<class Scheduler> |
|---|
| 63 | void periodical<Scheduler>::start() |
|---|
| 64 | { |
|---|
| 65 | start_polling(); |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | template<class Scheduler> |
|---|
| 69 | void periodical<Scheduler>::stop() |
|---|
| 70 | { |
|---|
| 71 | stop_polling(); |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | template<class Scheduler> |
|---|
| 76 | inline void periodical<Scheduler>::periodic_function() |
|---|
| 77 | { |
|---|
| 78 | try |
|---|
| 79 | { |
|---|
| 80 | m_oFunction(); |
|---|
| 81 | } |
|---|
| 82 | catch(...) |
|---|
| 83 | { |
|---|
| 84 | } |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | |
|---|
| 88 | } // namespace detail |
|---|
| 89 | |
|---|
| 90 | } // namespace mac |
|---|
| 91 | |
|---|
| 92 | } // namespace threads |
|---|
| 93 | |
|---|
| 94 | } // namespace boost |
|---|
| 95 | |
|---|
| 96 | |
|---|
| 97 | #endif // BOOST_PERIODICAL_MJM012402_HPP |
|---|