Boost.UI
User Interface Boost library
application.hpp
Go to the documentation of this file.
1 // Copyright (c) 2017 Kolya Kosenko
2 
3 // Distributed under the Boost Software License, Version 1.0.
4 // See http://www.boost.org/LICENSE_1_0.txt
5 
7 
8 #ifndef BOOST_UI_APPLICATION_HPP
9 #define BOOST_UI_APPLICATION_HPP
10 
11 #include <boost/ui/config.hpp>
12 
13 #ifdef BOOST_HAS_PRAGMA_ONCE
14 #pragma once
15 #endif
16 
17 #ifndef BOOST_NO_CXX11_HDR_CHRONO
18 #include <chrono>
19 #endif
20 
21 #include <boost/function.hpp>
22 
23 #ifdef BOOST_UI_USE_CHRONO
24 #include <boost/chrono.hpp>
25 #endif
26 
27 #ifdef BOOST_UI_USE_DATE_TIME
28 #include <boost/date_time/time_duration.hpp>
29 #endif
30 
31 #ifndef BOOST_NO_CXX11_VARIADIC_TEMPLATES
32 #include <boost/move/utility.hpp>
33 #else
34 #include <boost/bind.hpp>
35 #endif
36 
37 namespace boost {
38 namespace ui {
39 
43 
48 BOOST_UI_DECL int entry(int (*ui_main)(int, char*[]), int argc, char* argv[]);
49 BOOST_UI_DECL int entry(int (*ui_main)(), int argc, char* argv[]);
51 
52 #ifndef DOXYGEN
53 
54 namespace detail {
55 BOOST_UI_DECL void on_timeout(int milliseconds, const boost::function<void()>& fn);
56 
57 #ifndef BOOST_NO_CXX11_HDR_CHRONO
58 template <class Rep, class Period>
59 void on_timeout(const std::chrono::duration<Rep, Period>& d,
60  const boost::function<void()>& fn)
61 {
62  on_timeout(static_cast<int>(std::chrono::duration_cast<
63  std::chrono::milliseconds>(d).count()),
64  fn);
65 }
66 #endif
67 
68 #ifdef BOOST_UI_USE_CHRONO
69 template <class Rep, class Period>
70 void on_timeout(const boost::chrono::duration<Rep, Period>& d,
71  const boost::function<void()>& fn)
72 {
73  on_timeout(static_cast<int>(boost::chrono::duration_cast<
74  boost::chrono::milliseconds>(d).count()),
75  fn);
76 }
77 #endif
78 
79 #ifdef BOOST_UI_USE_DATE_TIME
80 template <class T, typename rep_type>
81 void on_timeout(const boost::date_time::time_duration<T, rep_type>& td,
82  const boost::function<void()>& fn)
83 {
84  on_timeout(static_cast<int>( td.total_milliseconds() ), fn);
85 }
86 #endif
87 
88 } // namespace detail
89 
90 #endif
91 
101 
102 #ifndef BOOST_NO_CXX11_HDR_CHRONO
103 #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
104 template <class Rep, class Period, class F, class ...Args>
105 void on_timeout(const std::chrono::duration<Rep, Period>& d,
106  F&& f, Args&&... args)
107 {
108  detail::on_timeout(d, std::bind(boost::forward<F>(f), boost::forward<Args>(args)...));
109 }
110 #else
111 template <class Rep, class Period>
112 void on_timeout(const std::chrono::duration<Rep, Period>& d,
113  const boost::function<void()>& fn)
114 {
115  detail::on_timeout(d, fn);
116 }
117 template <class Rep, class Period, class F, class Arg1>
118 void on_timeout(const std::chrono::duration<Rep, Period>& d,
119  F f, Arg1 a1)
120 {
121  detail::on_timeout(d, boost::bind(f, a1));
122 }
123 template <class Rep, class Period, class F, class Arg1, class Arg2>
124 void on_timeout(const std::chrono::duration<Rep, Period>& d,
125  F f, Arg1 a1, Arg2 a2)
126 {
127  detail::on_timeout(d, boost::bind(f, a1, a2));
128 }
129 #endif
130 #endif
131 
132 #ifdef BOOST_UI_USE_CHRONO
133 #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
134 template <class Rep, class Period, class F, class ...Args>
135 void on_timeout(const boost::chrono::duration<Rep, Period>& d,
136  F&& f, Args&&... args)
137 {
138  detail::on_timeout(d, std::bind(boost::forward<F>(f), boost::forward<Args>(args)...));
139 }
140 #else
141 template <class Rep, class Period>
142 void on_timeout(const boost::chrono::duration<Rep, Period>& d,
143  const boost::function<void()>& fn)
144 {
145  detail::on_timeout(d, fn);
146 }
147 template <class Rep, class Period, class F, class Arg1>
148 void on_timeout(const boost::chrono::duration<Rep, Period>& d,
149  F f, Arg1 a1)
150 {
151  detail::on_timeout(d, boost::bind(f, a1));
152 }
153 template <class Rep, class Period, class F, class Arg1, class Arg2>
154 void on_timeout(const boost::chrono::duration<Rep, Period>& d,
155  F f, Arg1 a1, Arg2 a2)
156 {
157  detail::on_timeout(d, boost::bind(f, a1, a2));
158 }
159 #endif
160 #endif
161 
162 #ifdef BOOST_UI_USE_DATE_TIME
163 #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
164 template <class T, typename rep_type, class F, class ...Args>
165 void on_timeout(const boost::date_time::time_duration<T, rep_type>& td,
166  F&& f, Args&&... args)
167 {
168  detail::on_timeout(td, std::bind(boost::forward<F>(f), boost::forward<Args>(args)...));
169 }
170 #else
171 template <class T, typename rep_type>
172 void on_timeout(const boost::date_time::time_duration<T, rep_type>& td,
173  const boost::function<void()>& fn)
174 {
175  detail::on_timeout(td, fn);
176 }
177 template <class T, typename rep_type, class F, class Arg1>
178 void on_timeout(const boost::date_time::time_duration<T, rep_type>& td,
179  F f, Arg1 a1)
180 {
181  detail::on_timeout(td, boost::bind(f, a1));
182 }
183 template <class T, typename rep_type, class F, class Arg1, class Arg2>
184 void on_timeout(const boost::date_time::time_duration<T, rep_type>& td,
185  F f, Arg1 a1, Arg2 a2)
186 {
187  detail::on_timeout(td, boost::bind(f, a1, a2));
188 }
189 #endif
190 #endif
191 
193 
194 #ifndef DOXYGEN
195 
196 namespace detail {
197 BOOST_UI_DECL void sleep_for_milliseconds(unsigned long milliseconds);
198 BOOST_UI_DECL void sleep_for_microseconds(unsigned long microseconds);
199 } // namespace detail
200 
205 
206 #ifndef BOOST_NO_CXX11_HDR_CHRONO
207 template <class Rep, class Period>
208 void sleep_for(const std::chrono::duration<Rep, Period>& d)
209 {
210  const unsigned long milliseconds = static_cast<unsigned long>(
211  std::chrono::duration_cast<std::chrono::milliseconds>(d).count());
212  if ( milliseconds > 1000 )
213  detail::sleep_for_milliseconds(milliseconds);
214  else
215  detail::sleep_for_microseconds(static_cast<unsigned long>(
216  std::chrono::duration_cast<std::chrono::microseconds>(d).count() ));
217 }
218 #endif
219 
220 #ifdef BOOST_UI_USE_CHRONO
221 template <class Rep, class Period>
222 void sleep_for(const boost::chrono::duration<Rep, Period>& d)
223 {
224  const unsigned long milliseconds = static_cast<unsigned long>(
225  boost::chrono::duration_cast<boost::chrono::milliseconds>(d).count());
226  if ( milliseconds > 1000 )
227  detail::sleep_for_milliseconds(milliseconds);
228  else
229  detail::sleep_for_microseconds(static_cast<unsigned long>(
230  boost::chrono::duration_cast<boost::chrono::microseconds>(d).count() ));
231 }
232 #endif
233 
234 #ifdef BOOST_UI_USE_DATE_TIME
235 template <class T, typename rep_type>
236 void sleep_for(const boost::date_time::time_duration<T, rep_type>& td)
237 {
238  const unsigned long milliseconds = static_cast<unsigned long>(td.total_milliseconds());
239  if ( milliseconds > 1000 )
240  detail::sleep_for_milliseconds(milliseconds);
241  else
242  detail::sleep_for_microseconds(static_cast<unsigned long>( td.total_microseconds() ));
243 }
244 #endif
245 
247 
248 #endif // !DOXYGEN
249 
250 #ifndef DOXYGEN
251 namespace detail {
252 BOOST_UI_DECL void assertion_failed_msg(char const* expr, char const* msg,
253  char const* function,
254  char const* file, long line);
255 } // namespace detail
256 #endif
257 
258 } // namespace ui
259 
260 #ifndef DOXYGEN
261 inline void assertion_failed_msg(char const* expr, char const* msg,
262  char const* function,
263  char const* file, long line)
264 {
265  ui::detail::assertion_failed_msg(expr, msg, function, file, line);
266 }
267 
268 inline void assertion_failed(char const* expr,
269  char const* function,
270  char const* file, long line)
271 {
272  assertion_failed_msg(expr, NULL, function, file, line);
273 }
274 #endif
275 
276 } // namespace boost
277 
278 #endif // BOOST_UI_APPLICATION_HPP
boost::ui::on_timeout
void on_timeout(const std::chrono::duration< Rep, Period > &d, F &&f, Args &&... args)
Calls function one time after the specified time duration.
Definition: application.hpp:105
config.hpp
Configuration options.
boost
Boost C++ libraries namespace.
Definition: window.hpp:19
boost::ui::entry
int entry(int(*ui_main)(int, char *[]), int argc, char *argv[])
UI application entry.