1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 #ifndef INCLUDED_SLIDESHOW_DELAYEVENT_HXX
24 #define INCLUDED_SLIDESHOW_DELAYEVENT_HXX
25 
26 #include <boost/function.hpp>
27 
28 #include "event.hxx"
29 #include "debug.hxx"
30 #include <boost/noncopyable.hpp>
31 
32 namespace slideshow {
33 namespace internal {
34 
35 /** Event, which delays the functor call the given amount of time
36  */
37 class Delay : public Event, private ::boost::noncopyable
38 {
39 public:
40     typedef ::boost::function0<void> FunctorT;
41 
42     template <typename FuncT>
Delay(FuncT const & func,double nTimeout,const::rtl::OUString & rsDescription)43         Delay( FuncT const& func,
44                double nTimeout
45 #if OSL_DEBUG_LEVEL > 1
46             ,  const ::rtl::OUString& rsDescription
47             ) : Event(rsDescription),
48 #else
49             ) :
50 #endif
51             mnTimeout(nTimeout), maFunc(func), mbWasFired(false) {}
52 
Delay(const boost::function0<void> & func,double nTimeout,const::rtl::OUString & rsDescription)53     Delay( const boost::function0<void>& func,
54            double nTimeout
55 #if OSL_DEBUG_LEVEL > 1
56         , const ::rtl::OUString& rsDescription
57         ) : Event(rsDescription),
58 #else
59         ) :
60 #endif
61         mnTimeout(nTimeout),
62         maFunc(func),
63         mbWasFired(false) {}
64 
65     // Event:
66     virtual bool fire();
67     virtual bool isCharged() const;
68     virtual double getActivationTime( double nCurrentTime ) const;
69     // Disposable:
70     virtual void dispose();
71 
72 private:
73     double const mnTimeout;
74     FunctorT maFunc;
75     bool mbWasFired;
76 };
77 
78 #if OSL_DEBUG_LEVEL <= 1
79 
80 /** Generate delay event
81 
82     @param func
83     Functor to call when the event fires.
84 
85     @param nTimeout
86     Timeout in seconds, to wait until functor is called.
87 
88     @return generated delay event
89 */
90 template <typename FuncT>
makeDelay_(FuncT const & func,double nTimeout)91 inline EventSharedPtr makeDelay_( FuncT const& func, double nTimeout )
92 {
93     return EventSharedPtr( new Delay( func, nTimeout ) );
94 }
95 
96 /** Generate immediate event
97 
98     @param func
99     Functor to call when the event fires.
100 
101     @return generated immediate event.
102 */
103 template <typename FuncT>
makeEvent_(FuncT const & func)104 inline EventSharedPtr makeEvent_( FuncT const& func )
105 {
106     return EventSharedPtr( new Delay( func, 0.0 ) );
107 }
108 
109 
110 // Strip away description.
111 #define makeDelay(f, t, d) makeDelay_(f, t)
112 #define makeEvent(f, d) makeEvent_(f)
113 
114 #else // OSL_DEBUG_LEVEL > 1
115 
116 class Delay_ : public Delay {
117 public:
118     template <typename FuncT>
Delay_(FuncT const & func,double nTimeout,char const * from_function,char const * from_file,int from_line,const::rtl::OUString & rsDescription)119     Delay_( FuncT const& func, double nTimeout,
120         char const* from_function, char const* from_file, int from_line,
121         const ::rtl::OUString& rsDescription)
122         : Delay(func, nTimeout, rsDescription),
123           FROM_FUNCTION(from_function),
124           FROM_FILE(from_file), FROM_LINE(from_line) {}
125 
126     char const* const FROM_FUNCTION;
127     char const* const FROM_FILE;
128     int const FROM_LINE;
129 };
130 
131 template <typename FuncT>
makeDelay_(FuncT const & func,double nTimeout,char const * from_function,char const * from_file,int from_line,const::rtl::OUString & rsDescription)132 inline EventSharedPtr makeDelay_(
133     FuncT const& func, double nTimeout,
134     char const* from_function, char const* from_file, int from_line,
135     const ::rtl::OUString& rsDescription)
136 {
137     return EventSharedPtr( new Delay_( func, nTimeout,
138             from_function, from_file, from_line, rsDescription) );
139 }
140 
141 #define makeDelay(f, t, d) makeDelay_(f, t,                   \
142         BOOST_CURRENT_FUNCTION, __FILE__, __LINE__,           \
143         ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(d)))
144 #define makeEvent(f, d) makeDelay_(f, 0.0,                  \
145         BOOST_CURRENT_FUNCTION, __FILE__, __LINE__,         \
146         ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(d)))
147 
148 #endif // OSL_DEBUG_LEVEL <= 1
149 
150 } // namespace internal
151 } // namespace presentation
152 
153 #endif /* INCLUDED_SLIDESHOW_DELAYEVENT_HXX */
154