Libosmium 2.22.0 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
6 This file is part of Osmium (https://osmcode.org/libosmium).
7
8 Copyright 2013-2025 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
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
Definition function_wrapper.hpp:48
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
Namespace for everything in the Osmium library.
Definition assembler.hpp:53
std
Definition location.hpp:555
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