Fix typo
[LibreOffice.git] / include / vcl / threadex.hxx
blobf75e8efe5893ad5bc9d64ffa2428b57218f9450a
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 <tools/link.hxx>
25 #include <vcl/dllapi.h>
27 #include <exception>
28 #include <optional>
29 #include <memory>
31 namespace vcl
33 class VCL_DLLPUBLIC SolarThreadExecutor
35 osl::Condition m_aStart;
36 osl::Condition m_aFinish;
37 bool m_bTimeout;
39 DECL_DLLPRIVATE_LINK( worker, void*, void );
41 public:
42 SolarThreadExecutor();
43 virtual ~SolarThreadExecutor();
45 virtual void doIt() = 0;
46 void execute();
49 namespace solarthread {
51 /// @internal
52 namespace detail {
54 template <typename FuncT, typename ResultT>
55 class GenericSolarThreadExecutor final : public SolarThreadExecutor
57 public:
58 static ResultT exec( FuncT const& func )
60 typedef GenericSolarThreadExecutor<FuncT, ResultT> ExecutorT;
61 ::std::unique_ptr<ExecutorT> const pExecutor( new ExecutorT(func) );
62 pExecutor->execute();
63 if (pExecutor->m_exc)
64 std::rethrow_exception(pExecutor->m_exc);
65 return *pExecutor->m_result;
68 private:
69 explicit GenericSolarThreadExecutor( FuncT func )
70 : m_func(std::move(func)), m_result() {}
72 virtual void doIt() override
74 try {
75 m_result = m_func();
77 catch (...) {
78 m_exc = std::current_exception();
82 std::exception_ptr m_exc;
83 #ifdef _MSC_VER
84 FuncT m_func; // "const" and std::bind() results in Error C3848 expression would lose const-volatile qualifiers
85 #else
86 FuncT const m_func;
87 #endif
88 // using std::optional here omits the need that ResultT is default
89 // constructable:
90 ::std::optional<ResultT> m_result;
93 template <typename FuncT>
94 class GenericSolarThreadExecutor<FuncT, void> final : public SolarThreadExecutor
96 public:
97 static void exec( FuncT const& func )
99 typedef GenericSolarThreadExecutor<FuncT, void> ExecutorT;
100 ::std::unique_ptr<ExecutorT> const pExecutor( new ExecutorT(func) );
101 pExecutor->execute();
102 if (pExecutor->m_exc)
103 std::rethrow_exception(pExecutor->m_exc);
106 private:
107 explicit GenericSolarThreadExecutor( FuncT func )
108 : m_func(std::move(func)) {}
110 virtual void doIt() override
112 try {
113 m_func();
115 catch (...) {
116 m_exc = std::current_exception();
120 std::exception_ptr m_exc;
121 FuncT const m_func;
124 } // namespace detail
127 /** This function will execute the passed functor synchronously in the
128 solar thread, thus the calling thread will (eventually) be blocked until
129 the functor has been called.
130 Any exception that came up calling the functor in the solar thread
131 will be caught and rethrown in the calling thread.
132 The result type of this function needs to be default constructable.
133 Please keep in mind not to pass addresses to stack variables
134 (e.g. for out parameters) to foreign threads, use inout_by_ref()
135 for this purpose. For in parameters, this may not affect you, because
136 the functor object is copy constructed into free store. This way
137 you must not use \verbatim std::cref()/std::ref() \endverbatim or similar
138 for objects on your thread's stack.
139 Use inout_by_ref() or inout_by_ptr() for this purpose, e.g.
141 \code{.cpp}
142 using namespace vcl::solarthread;
144 long n = 3;
145 // calling foo( long & r ):
146 syncExecute( std::bind( &foo, inout_by_ref(n) ) );
147 // calling foo( long * p ):
148 syncExecute( std::bind( &foo, inout_by_ptr(&n) ) );
150 char const* pc = "default";
151 // calling foo( char const** ppc ):
152 syncExecute( std::bind( &foo, inout_by_ptr(&pc) ) );
153 // calling foo( char const*& rpc ):
154 syncExecute( std::bind( &foo, inout_by_ref(pc) ) );
155 \endcode
157 @tpl ResultT result type, defaults to FuncT::result_type to seamlessly
158 support mem_fn and bind
159 @tpl FuncT functor type, let your compiler deduce this type
160 @param func functor object to be executed in solar thread
161 @return return value of functor
163 template <typename FuncT>
164 inline auto syncExecute(FuncT const& func) -> decltype(func())
166 return detail::GenericSolarThreadExecutor<
167 FuncT, decltype(func())>::exec(func);
170 } // namespace solarthread
171 } // namespace vcl
173 #endif // INCLUDED_VCL_THREADEX_HXX
175 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */