Libosmium: include/osmium/thread/function_wrapper.hpp Source File

Libosmium Fast and flexible C++ library for working with OpenStreetMap data

Loading...
Searching...
No Matches
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-2026 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
48 class function_wrapper {
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
73 F m_functor;
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 = std::enable_if_t<
91 !std::is_same<TFunction, function_wrapper>::value, void>>
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
110 function_wrapper(const function_wrapper&) = delete;
111 function_wrapper& operator=(const function_wrapper&) = delete;
112
113 function_wrapper(function_wrapper&& other) noexcept :
114 impl(std::move(other.impl)) {
115 }
116
117 function_wrapper& operator=(function_wrapper&& other) noexcept {
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
osmium::thread::function_wrapper::impl
std::unique_ptr< impl_base > impl
Definition function_wrapper.hpp:68
osmium::thread::function_wrapper::function_wrapper
function_wrapper(TFunction &&f)
Definition function_wrapper.hpp:93
osmium::thread::function_wrapper::function_wrapper
function_wrapper(function_wrapper &&other) noexcept
Definition function_wrapper.hpp:113
osmium::thread::function_wrapper::operator=
function_wrapper & operator=(const function_wrapper &)=delete
osmium::thread::function_wrapper::function_wrapper
function_wrapper(int)
Definition function_wrapper.hpp:100
osmium::thread::function_wrapper::~function_wrapper
~function_wrapper()=default
osmium::thread::function_wrapper::operator=
function_wrapper & operator=(function_wrapper &&other) noexcept
Definition function_wrapper.hpp:117
osmium::thread::function_wrapper::function_wrapper
function_wrapper()=default
osmium::thread::function_wrapper::operator()
bool operator()()
Definition function_wrapper.hpp:104
osmium::thread::function_wrapper::function_wrapper
function_wrapper(const function_wrapper &)=delete
osmium::thread
Threading-related low-level code.
Definition function_wrapper.hpp:41
osmium
Namespace for everything in the Osmium library.
Definition assembler.hpp:53
std
Definition location.hpp:654
osmium::thread::function_wrapper::impl_base
Definition function_wrapper.hpp:50
osmium::thread::function_wrapper::impl_base::call
virtual bool call()
Definition function_wrapper.hpp:62
osmium::thread::function_wrapper::impl_base::impl_base
impl_base() noexcept=default
osmium::thread::function_wrapper::impl_type
Definition function_wrapper.hpp:71
osmium::thread::function_wrapper::impl_type::impl_type
impl_type(F &&functor)
Definition function_wrapper.hpp:75
osmium::thread::function_wrapper::impl_type::m_functor
F m_functor
Definition function_wrapper.hpp:73
osmium::thread::function_wrapper::impl_type::call
bool call() override
Definition function_wrapper.hpp:79