Libosmium  2.14.0
Fast and flexible C++ library for working with OpenStreetMap data
function_wrapper.hpp
Go to the documentation of this file.
1 #ifndef OSMIUM_THREAD_FUNCTION_WRAPPER_HPP
2 #define OSMIUM_THREAD_FUNCTION_WRAPPER_HPP
3 
4 /*
5 
6 This file is part of Osmium (http://osmcode.org/libosmium).
7 
8 Copyright 2013-2018 Jochen Topf <jochen@topf.org> and others (see README).
9 
10 Boost Software License - Version 1.0 - August 17th, 2003
11 
12 Permission is hereby granted, free of charge, to any person or organization
13 obtaining a copy of the software and accompanying documentation covered by
14 this license (the "Software") to use, reproduce, display, distribute,
15 execute, and transmit the Software, and to prepare derivative works of the
16 Software, and to permit third-parties to whom the Software is furnished to
17 do so, all subject to the following:
18 
19 The copyright notices in the Software and this entire statement, including
20 the above license grant, this restriction and the following disclaimer,
21 must be included in all copies of the Software, in whole or in part, and
22 all derivative works of the Software, unless such copies or derivative
23 works are solely in the form of machine-executable object code generated by
24 a source language processor.
25 
26 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
29 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
30 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
31 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
32 DEALINGS IN THE SOFTWARE.
33 
34 */
35 
36 #include <memory>
37 #include <utility>
38 
39 namespace osmium {
40 
41  namespace thread {
42 
49 
50  struct impl_base {
51 
52  impl_base() noexcept = default;
53 
54  impl_base(const impl_base&) noexcept = default;
55  impl_base& operator=(const impl_base&) noexcept = default;
56 
57  impl_base(impl_base&&) noexcept = default;
58  impl_base& operator=(impl_base&&) noexcept = default;
59 
60  virtual ~impl_base() noexcept = default;
61 
62  virtual bool call() {
63  return true;
64  }
65 
66  }; // struct impl_base
67 
68  std::unique_ptr<impl_base> impl;
69 
70  template <typename F>
71  struct impl_type : impl_base {
72 
74 
75  explicit impl_type(F&& functor) :
76  m_functor(std::forward<F>(functor)) {
77  }
78 
79  bool call() override {
80  m_functor();
81  return false;
82  }
83 
84  }; // struct impl_type
85 
86  public:
87 
88  // Constructor must not be "explicit" for wrapper
89  // to work seemlessly.
90  template <typename TFunction>
91  // cppcheck-suppress noExplicitConstructor
92  function_wrapper(TFunction&& f) : // NOLINT(google-explicit-constructor, hicpp-explicit-conversions, misc-forwarding-reference-overload)
93  impl(new impl_type<TFunction>(std::forward<TFunction>(f))) {
94  }
95 
96  // The integer parameter is only used to signal that we want
97  // the special function wrapper that makes the worker thread
98  // shut down.
99  explicit function_wrapper(int /*dummy*/) :
100  impl(new impl_base()) {
101  }
102 
103  bool operator()() {
104  return impl->call();
105  }
106 
107  function_wrapper() = default;
108 
109  function_wrapper(const function_wrapper&) = delete;
110  function_wrapper& operator=(const function_wrapper&) = delete;
111 
112  function_wrapper(function_wrapper&& other) noexcept :
113  impl(std::move(other.impl)) {
114  }
115 
117  impl = std::move(other.impl);
118  return *this;
119  }
120 
121  ~function_wrapper() = default;
122 
123  explicit operator bool() const {
124  return static_cast<bool>(impl);
125  }
126 
127  }; // class function_wrapper
128 
129  } // namespace thread
130 
131 } // namespace osmium
132 
133 #endif // OSMIUM_THREAD_FUNCTION_WRAPPER_HPP
F m_functor
Definition: function_wrapper.hpp:73
impl_base & operator=(const impl_base &) noexcept=default
Definition: function_wrapper.hpp:50
Definition: location.hpp:550
impl_type(F &&functor)
Definition: function_wrapper.hpp:75
virtual ~impl_base() noexcept=default
std::unique_ptr< impl_base > impl
Definition: function_wrapper.hpp:68
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53
Definition: function_wrapper.hpp:71
function_wrapper & operator=(function_wrapper &&other) noexcept
Definition: function_wrapper.hpp:116
Definition: function_wrapper.hpp:48
function_wrapper(int)
Definition: function_wrapper.hpp:99
function_wrapper(TFunction &&f)
Definition: function_wrapper.hpp:92
bool operator()()
Definition: function_wrapper.hpp:103
function_wrapper(function_wrapper &&other) noexcept
Definition: function_wrapper.hpp:112
virtual bool call()
Definition: function_wrapper.hpp:62
bool call() override
Definition: function_wrapper.hpp:79