LibreOffice Module vcl (master)  1
threadex.hxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 #ifndef INCLUDED_VCL_THREADEX_HXX
21 #define INCLUDED_VCL_THREADEX_HXX
22 
23 #include <osl/conditn.hxx>
24 #include <tools/link.hxx>
25 #include <vcl/dllapi.h>
26 
27 #include <boost/optional.hpp>
28 #include <memory>
29 
30 namespace vcl
31 {
33  {
34  osl::Condition m_aStart;
35  osl::Condition m_aFinish;
36  bool m_bTimeout;
37 
38  DECL_DLLPRIVATE_LINK( worker, void*, void );
39 
40  public:
42  virtual ~SolarThreadExecutor();
43 
44  virtual void doIt() = 0;
45  void execute();
46  };
47 
48 namespace solarthread {
49 
51 namespace detail {
52 
53 template <typename FuncT, typename ResultT>
55 {
56 public:
57  static ResultT exec( FuncT const& func )
58  {
60  ::std::unique_ptr<ExecutorT> const pExecutor( new ExecutorT(func) );
61  pExecutor->execute();
62  if (pExecutor->m_exc)
63  std::rethrow_exception(pExecutor->m_exc);
64  return *pExecutor->m_result;
65  }
66 
67 private:
68  explicit GenericSolarThreadExecutor( FuncT const& func )
69  : m_func(func), m_result() {}
70 
71  virtual void doIt() override
72  {
73  try {
74  m_result.reset( m_func() );
75  }
76  catch (...) {
77  m_exc = std::current_exception();
78  }
79  }
80 
81  std::exception_ptr m_exc;
82 #ifdef _MSC_VER
83  FuncT m_func; // "const" and std::bind() results in Error C3848 expression would lose const-volatile qualifiers
84 #else
85  FuncT const m_func;
86 #endif
87  // using boost::optional here omits the need that ResultT is default
88  // constructable:
90 };
91 
92 template <typename FuncT>
94 {
95 public:
96  static void exec( FuncT const& func )
97  {
99  ::std::unique_ptr<ExecutorT> const pExecutor( new ExecutorT(func) );
100  pExecutor->execute();
101  if (pExecutor->m_exc)
102  std::rethrow_exception(pExecutor->m_exc);
103  }
104 
105 private:
106  explicit GenericSolarThreadExecutor( FuncT const& func )
107  : m_func(func) {}
108 
109  virtual void doIt() override
110  {
111  try {
112  m_func();
113  }
114  catch (...) {
115  m_exc = std::current_exception();
116  }
117  }
118 
119  std::exception_ptr m_exc;
120  FuncT const m_func;
121 };
122 
123 } // namespace detail
124 
125 
162 template <typename FuncT>
163 inline auto syncExecute(FuncT const& func) -> decltype(func())
164 {
166  FuncT, decltype(func())>::exec(func);
167 }
168 
169 } // namespace solarthread
170 } // namespace vcl
171 
172 #endif // INCLUDED_VCL_THREADEX_HXX
173 
174 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static ResultT exec(FuncT const &func)
Definition: threadex.hxx:57
#define VCL_DLLPUBLIC
Definition: dllapi.h:29
osl::Condition m_aFinish
Definition: threadex.hxx:35
osl::Condition m_aStart
Definition: threadex.hxx:34
auto syncExecute(FuncT const &func) -> decltype(func())
This function will execute the passed functor synchronously in the solar thread, thus the calling thr...
Definition: threadex.hxx:163
typedef void(CALLTYPE *GetFuncDataPtr)(sal_uInt16 &nNo