Libosmium  2.11.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-2017 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 <algorithm>
37 #include <memory>
38 
39 namespace osmium {
40 
41  namespace thread {
42 
49 
50  struct impl_base {
51 
52  virtual ~impl_base() = default;
53  virtual bool call() {
54  return true;
55  }
56 
57  }; // struct impl_base
58 
59  std::unique_ptr<impl_base> impl;
60 
61  template <typename F>
62  struct impl_type : impl_base {
63 
65 
66  explicit impl_type(F&& functor) :
67  m_functor(std::forward<F>(functor)) {
68  }
69 
70  bool call() override {
71  m_functor();
72  return false;
73  }
74 
75  }; // struct impl_type
76 
77  public:
78 
79  // Constructor must not be "explicit" for wrapper
80  // to work seemlessly.
81  template <typename TFunction>
82  function_wrapper(TFunction&& f) :
83  impl(new impl_type<TFunction>(std::forward<TFunction>(f))) {
84  }
85 
86  // The integer parameter is only used to signal that we want
87  // the special function wrapper that makes the worker thread
88  // shut down.
90  impl(new impl_base()) {
91  }
92 
93  bool operator()() {
94  return impl->call();
95  }
96 
97  function_wrapper() = default;
98 
100  impl(std::move(other.impl)) {
101  }
102 
104  impl = std::move(other.impl);
105  return *this;
106  }
107 
108  function_wrapper(const function_wrapper&) = delete;
109  function_wrapper& operator=(const function_wrapper&) = delete;
110 
111  ~function_wrapper() = default;
112 
113  explicit operator bool() const {
114  return static_cast<bool>(impl);
115  }
116 
117  }; // class function_wrapper
118 
119  } // namespace thread
120 
121 } // namespace osmium
122 
123 #endif // OSMIUM_THREAD_FUNCTION_WRAPPER_HPP
F m_functor
Definition: function_wrapper.hpp:64
Definition: function_wrapper.hpp:50
Definition: reader_iterator.hpp:39
impl_type(F &&functor)
Definition: function_wrapper.hpp:66
function_wrapper & operator=(function_wrapper &&other)
Definition: function_wrapper.hpp:103
function_wrapper(function_wrapper &&other)
Definition: function_wrapper.hpp:99
std::unique_ptr< impl_base > impl
Definition: function_wrapper.hpp:59
Namespace for everything in the Osmium library.
Definition: assembler.hpp:73
Definition: function_wrapper.hpp:62
Definition: function_wrapper.hpp:48
function_wrapper(int)
Definition: function_wrapper.hpp:89
function_wrapper(TFunction &&f)
Definition: function_wrapper.hpp:82
bool operator()()
Definition: function_wrapper.hpp:93
virtual bool call()
Definition: function_wrapper.hpp:53
bool call() override
Definition: function_wrapper.hpp:70