aboutsummaryrefslogtreecommitdiffstats
path: root/include/distortos/internal/synchronization/BoundQueueFunctor.hpp
blob: 35a9d140999ddca64038e07952498abd8171186e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/**
 * \file
 * \brief BoundQueueFunctor class header
 *
 * \author Copyright (C) 2015 Kamil Szczygiel http://www.distortec.com http://www.freddiechopin.info
 *
 * \par License
 * This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not
 * distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#ifndef INCLUDE_DISTORTOS_INTERNAL_SYNCHRONIZATION_BOUNDQUEUEFUNCTOR_HPP_
#define INCLUDE_DISTORTOS_INTERNAL_SYNCHRONIZATION_BOUNDQUEUEFUNCTOR_HPP_

#include "distortos/internal/synchronization/QueueFunctor.hpp"

#include <utility>

namespace distortos
{

namespace internal
{

/**
 * \brief BoundQueueFunctor is a type-erased QueueFunctor which calls its bound functor to execute actions on queue's
 * storage
 *
 * \tparam F is the type of bound functor, it will be called with <em>void*</em> as only argument
 */

template<typename F>
class BoundQueueFunctor : public QueueFunctor
{
public:

	/**
	 * \brief BoundQueueFunctor's constructor
	 *
	 * \param [in] boundFunctor is a rvalue reference to bound functor which will be used to move-construct internal
	 * bound functor
	 */

	constexpr explicit BoundQueueFunctor(F&& boundFunctor) :
			boundFunctor_{std::move(boundFunctor)}
	{

	}

	/**
	 * \brief Calls the bound functor which will execute some action on queue's storage (like copy-constructing,
	 * swapping, destroying, emplacing, ...)
	 *
	 * \param [in,out] storage is a pointer to storage with/for element
	 */

	void operator()(void* const storage) const override
	{
		boundFunctor_(storage);
	}

private:

	/// bound functor
	F boundFunctor_;
};

/**
 * \brief Helper factory function to make BoundQueueFunctor object with deduced template arguments
 *
 * \tparam F is the type of bound functor, it will be called with <em>void*</em> as only argument
 *
 * \param [in] boundFunctor is a rvalue reference to bound functor which will be used to move-construct internal bound
 * functor
 *
 * \return BoundQueueFunctor object with deduced template arguments
 */

template<typename F>
constexpr BoundQueueFunctor<F> makeBoundQueueFunctor(F&& boundFunctor)
{
	return BoundQueueFunctor<F>{std::move(boundFunctor)};
}

}	// namespace internal

}	// namespace distortos

#endif	// INCLUDE_DISTORTOS_INTERNAL_SYNCHRONIZATION_BOUNDQUEUEFUNCTOR_HPP_