1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 #ifndef INCLUDED_SLIDESHOW_DELAYEVENT_HXX
28 #define INCLUDED_SLIDESHOW_DELAYEVENT_HXX
29 
30 #include <boost/function.hpp>
31 
32 #include "event.hxx"
33 #include "debug.hxx"
34 #include <boost/noncopyable.hpp>
35 
36 namespace slideshow {
37 namespace internal {
38 
39 /** Event, which delays the functor call the given amount of time
40  */
41 class Delay : public Event, private ::boost::noncopyable
42 {
43 public:
44     typedef ::boost::function0<void> FunctorT;
45 
46     template <typename FuncT>
47         Delay( FuncT const& func,
48                double nTimeout
49 #if OSL_DEBUG_LEVEL > 1
50             ,  const ::rtl::OUString& rsDescription
51             ) : Event(rsDescription),
52 #else
53             ) :
54 #endif
55             mnTimeout(nTimeout), maFunc(func), mbWasFired(false) {}
56 
57     Delay( const boost::function0<void>& func,
58            double nTimeout
59 #if OSL_DEBUG_LEVEL > 1
60         , const ::rtl::OUString& rsDescription
61         ) : Event(rsDescription),
62 #else
63         ) :
64 #endif
65         mnTimeout(nTimeout),
66         maFunc(func),
67         mbWasFired(false) {}
68 
69     // Event:
70     virtual bool fire();
71     virtual bool isCharged() const;
72     virtual double getActivationTime( double nCurrentTime ) const;
73     // Disposable:
74     virtual void dispose();
75 
76 private:
77     double const mnTimeout;
78     FunctorT maFunc;
79     bool mbWasFired;
80 };
81 
82 #if OSL_DEBUG_LEVEL <= 1
83 
84 /** Generate delay event
85 
86     @param func
87     Functor to call when the event fires.
88 
89     @param nTimeout
90     Timeout in seconds, to wait until functor is called.
91 
92     @return generated delay event
93 */
94 template <typename FuncT>
95 inline EventSharedPtr makeDelay_( FuncT const& func, double nTimeout )
96 {
97     return EventSharedPtr( new Delay( func, nTimeout ) );
98 }
99 
100 /** Generate immediate event
101 
102     @param func
103     Functor to call when the event fires.
104 
105     @return generated immediate event.
106 */
107 template <typename FuncT>
108 inline EventSharedPtr makeEvent_( FuncT const& func )
109 {
110     return EventSharedPtr( new Delay( func, 0.0 ) );
111 }
112 
113 
114 // Strip away description.
115 #define makeDelay(f, t, d) makeDelay_(f, t)
116 #define makeEvent(f, d) makeEvent_(f)
117 
118 #else // OSL_DEBUG_LEVEL > 1
119 
120 class Delay_ : public Delay {
121 public:
122     template <typename FuncT>
123     Delay_( FuncT const& func, double nTimeout,
124         char const* from_function, char const* from_file, int from_line,
125         const ::rtl::OUString& rsDescription)
126         : Delay(func, nTimeout, rsDescription),
127           FROM_FUNCTION(from_function),
128           FROM_FILE(from_file), FROM_LINE(from_line) {}
129 
130     char const* const FROM_FUNCTION;
131     char const* const FROM_FILE;
132     int const FROM_LINE;
133 };
134 
135 template <typename FuncT>
136 inline EventSharedPtr makeDelay_(
137     FuncT const& func, double nTimeout,
138     char const* from_function, char const* from_file, int from_line,
139     const ::rtl::OUString& rsDescription)
140 {
141     return EventSharedPtr( new Delay_( func, nTimeout,
142             from_function, from_file, from_line, rsDescription) );
143 }
144 
145 #define makeDelay(f, t, d) makeDelay_(f, t,                   \
146         BOOST_CURRENT_FUNCTION, __FILE__, __LINE__,           \
147         ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(d)))
148 #define makeEvent(f, d) makeDelay_(f, 0.0,                  \
149         BOOST_CURRENT_FUNCTION, __FILE__, __LINE__,         \
150         ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(d)))
151 
152 #endif // OSL_DEBUG_LEVEL <= 1
153 
154 } // namespace internal
155 } // namespace presentation
156 
157 #endif /* INCLUDED_SLIDESHOW_DELAYEVENT_HXX */
158