Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / include / vcl / threadex.hxx
blob6330a71bcc2892b93a0fc987fdea0e914950583e
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #ifndef INCLUDED_VCL_THREADEX_HXX
21 #define INCLUDED_VCL_THREADEX_HXX
23 #include <osl/conditn.hxx>
24 #include <osl/thread.h>
25 #include <tools/link.hxx>
26 #include <vcl/dllapi.h>
28 #include <cppuhelper/exc_hlp.hxx>
29 #include <boost/optional.hpp>
30 #include <memory>
32 namespace vcl
34 class VCL_DLLPUBLIC SolarThreadExecutor
36 osl::Condition m_aStart;
37 osl::Condition m_aFinish;
38 long m_nReturn;
39 bool m_bTimeout;
41 DECL_DLLPRIVATE_LINK( worker, void*, void );
43 public:
44 SolarThreadExecutor();
45 virtual ~SolarThreadExecutor();
47 virtual long doIt() = 0;
48 void execute();
51 namespace solarthread {
53 /// @internal
54 namespace detail {
56 template <typename FuncT, typename ResultT>
57 class GenericSolarThreadExecutor : public SolarThreadExecutor
59 public:
60 static ResultT exec( FuncT const& func )
62 typedef GenericSolarThreadExecutor<FuncT, ResultT> ExecutorT;
63 ::std::unique_ptr<ExecutorT> const pExecutor( new ExecutorT(func) );
64 pExecutor->execute();
65 if (pExecutor->m_exc.hasValue())
66 ::cppu::throwException( pExecutor->m_exc );
67 return *pExecutor->m_result;
70 private:
71 explicit GenericSolarThreadExecutor( FuncT const& func )
72 : m_exc(), m_func(func), m_result() {}
74 virtual long doIt() override
76 try {
77 m_result.reset( m_func() );
79 catch (css::uno::Exception &) {
80 // only UNO exceptions can be dispatched:
81 m_exc = ::cppu::getCaughtException();
83 return 0;
86 css::uno::Any m_exc;
87 #ifdef _MSC_VER
88 FuncT m_func; // "const" and std::bind() results in Error C3848 expression would lose const-volatile qualifiers
89 #else
90 FuncT const m_func;
91 #endif
92 // using boost::optional here omits the need that ResultT is default
93 // constructable:
94 ::boost::optional<ResultT> m_result;
97 template <typename FuncT>
98 class GenericSolarThreadExecutor<FuncT, void> : public SolarThreadExecutor
100 private:
101 explicit GenericSolarThreadExecutor( FuncT const& func )
102 : m_exc(), m_func(func) {}
104 virtual long doIt() override
106 try {
107 m_func();
109 catch (css::uno::Exception &) {
110 // only UNO exceptions can be dispatched:
111 m_exc = ::cppu::getCaughtException();
113 return 0;
116 css::uno::Any m_exc;
117 FuncT const m_func;
120 } // namespace detail
123 /** This function will execute the passed functor synchronously in the
124 solar thread, thus the calling thread will (eventually) be blocked until
125 the functor has been called.
126 Any UNO exception that came up calling the functor in the solar thread
127 will be caught and rethrown in the calling thread. Any non-UNO
128 exception needs to be handled by the called functor.
129 The result type of this function needs to be default constructable.
130 Please keep in mind not to pass addresses to stack variables
131 (e.g. for out parameters) to foreign threads, use inout_by_ref()
132 for this purpose. For in parameters, this may not affect you, because
133 the functor object is copy constructed into free store. This way
134 you must not use \verbatim std::cref()/std::ref() \endverbatim or similar
135 for objects on your thread's stack.
136 Use inout_by_ref() or inout_by_ptr() for this purpose, e.g.
138 \code{.cpp}
139 using namespace vcl::solarthread;
141 long n = 3;
142 // calling foo( long & r ):
143 syncExecute( std::bind( &foo, inout_by_ref(n) ) );
144 // calling foo( long * p ):
145 syncExecute( std::bind( &foo, inout_by_ptr(&n) ) );
147 char const* pc = "default";
148 // calling foo( char const** ppc ):
149 syncExecute( std::bind( &foo, inout_by_ptr(&pc) ) );
150 // calling foo( char const*& rpc ):
151 syncExecute( std::bind( &foo, inout_by_ref(pc) ) );
152 \endcode
154 @tpl ResultT result type, defaults to FuncT::result_type to seamlessly
155 support mem_fn and bind
156 @tpl FuncT functor type, let your compiler deduce this type
157 @param func functor object to be executed in solar thread
158 @return return value of functor
160 template <typename FuncT>
161 inline typename FuncT::result_type syncExecute( FuncT const& func )
163 return detail::GenericSolarThreadExecutor<
164 FuncT, typename FuncT::result_type>::exec(func);
167 } // namespace solarthread
168 } // namespace vcl
170 #endif // INCLUDED_VCL_THREADEX_HXX
172 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */