Libosmium  2.20.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
6This file is part of Osmium (https://osmcode.org/libosmium).
7
8Copyright 2013-2023 Jochen Topf <jochen@topf.org> and others (see README).
9
10Boost Software License - Version 1.0 - August 17th, 2003
11
12Permission is hereby granted, free of charge, to any person or organization
13obtaining a copy of the software and accompanying documentation covered by
14this license (the "Software") to use, reproduce, display, distribute,
15execute, and transmit the Software, and to prepare derivative works of the
16Software, and to permit third-parties to whom the Software is furnished to
17do so, all subject to the following:
18
19The copyright notices in the Software and this entire statement, including
20the above license grant, this restriction and the following disclaimer,
21must be included in all copies of the Software, in whole or in part, and
22all derivative works of the Software, unless such copies or derivative
23works are solely in the form of machine-executable object code generated by
24a source language processor.
25
26THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
29SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
30FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
31ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
32DEALINGS IN THE SOFTWARE.
33
34*/
35
36#include <memory>
37#include <utility>
38
39namespace 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>
72
74
75 explicit impl_type(F&& functor) :
76 m_functor(std::move(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, typename X = typename std::enable_if<
91 !std::is_same<TFunction, function_wrapper>::value, void>::type>
92 // cppcheck-suppress noExplicitConstructor
93 function_wrapper(TFunction&& f) : // NOLINT(google-explicit-constructor, hicpp-explicit-conversions)
94 impl(new impl_type<TFunction>(std::forward<TFunction>(f))) {
95 }
96
97 // The integer parameter is only used to signal that we want
98 // the special function wrapper that makes the worker thread
99 // shut down.
100 explicit function_wrapper(int /*dummy*/) :
101 impl(new impl_base()) {
102 }
103
104 bool operator()() {
105 return impl->call();
106 }
107
108 function_wrapper() = default;
109
112
114 impl(std::move(other.impl)) {
115 }
116
118 impl = std::move(other.impl);
119 return *this;
120 }
121
122 ~function_wrapper() = default;
123
124 explicit operator bool() const {
125 return static_cast<bool>(impl);
126 }
127
128 }; // class function_wrapper
129
130 } // namespace thread
131
132} // namespace osmium
133
134#endif // OSMIUM_THREAD_FUNCTION_WRAPPER_HPP
Definition: function_wrapper.hpp:48
std::unique_ptr< impl_base > impl
Definition: function_wrapper.hpp:68
function_wrapper(TFunction &&f)
Definition: function_wrapper.hpp:93
function_wrapper(function_wrapper &&other) noexcept
Definition: function_wrapper.hpp:113
function_wrapper & operator=(const function_wrapper &)=delete
function_wrapper(int)
Definition: function_wrapper.hpp:100
function_wrapper & operator=(function_wrapper &&other) noexcept
Definition: function_wrapper.hpp:117
bool operator()()
Definition: function_wrapper.hpp:104
function_wrapper(const function_wrapper &)=delete
type
Definition: entity_bits.hpp:63
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53
Definition: location.hpp:555
Definition: function_wrapper.hpp:50
virtual bool call()
Definition: function_wrapper.hpp:62
Definition: function_wrapper.hpp:71
impl_type(F &&functor)
Definition: function_wrapper.hpp:75
F m_functor
Definition: function_wrapper.hpp:73
bool call() override
Definition: function_wrapper.hpp:79