summaryrefslogtreecommitdiff
path: root/include/libcamera/bound_method.h
blob: bdeb5469cda3a9e81144adfab5080dcb91dce0ad (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
 * Copyright (C) 2019, Google Inc.
 *
 * bound_method.h - Method bind and invocation
 */
#ifndef __LIBCAMERA_BOUND_METHOD_H__
#define __LIBCAMERA_BOUND_METHOD_H__

#include <tuple>
#include <type_traits>

namespace libcamera {

class Object;

enum ConnectionType {
	ConnectionTypeAuto,
	ConnectionTypeDirect,
	ConnectionTypeQueued,
	ConnectionTypeBlocking,
};

class BoundMethodBase
{
public:
	BoundMethodBase(void *obj, Object *object, ConnectionType type)
		: obj_(obj), object_(object), connectionType_(type)
	{
	}
	virtual ~BoundMethodBase() {}

	template<typename T, typename std::enable_if<!std::is_same<Object, T>::value>::type * = nullptr>
	bool match(T *obj) { return obj == obj_; }
	bool match(Object *object) { return object == object_; }

	Object *object() const { return object_; }
	ConnectionType connectionType() const { return connectionType_; }

	void activatePack(void *pack, bool deleteMethod);
	virtual void invokePack(void *pack) = 0;

protected:
	void *obj_;
	Object *object_;
	ConnectionType connectionType_;
};

template<typename... Args>
class BoundMethodArgs : public BoundMethodBase
{
private:
#ifndef __DOXYGEN__
	/*
	 * This is a cheap partial implementation of std::integer_sequence<>
	 * from C++14.
	 */
	template<int...>
	struct sequence {
	};

	template<int N, int... S>
	struct generator : generator<N-1, N-1, S...> {
	};

	template<int... S>
	struct generator<0, S...> {
		typedef sequence<S...> type;
	};
#endif

	using PackType = std::tuple<typename std::remove_reference<Args>::type...>;

	template<int... S>
	void invokePack(void *pack, sequence<S...>)
	{
		PackType *args = static_cast<PackType *>(pack);
		invoke(std::get<S>(*args)...);
		delete args;
	}

public:
	BoundMethodArgs(void *obj, Object *object, ConnectionType type)
		: BoundMethodBase(obj, object, type) {}

	void invokePack(void *pack) override
	{
		invokePack(pack, typename generator<sizeof...(Args)>::type());
	}

	virtual void activate(Args... args) = 0;
	virtual void invoke(Args... args) = 0;
};

template<typename T, typename... Args>
class BoundMemberMethod : public BoundMethodArgs<Args...>
{
public:
	using PackType = std::tuple<typename std::remove_reference<Args>::type...>;

	BoundMemberMethod(T *obj, Object *object, void (T::*func)(Args...),
			  ConnectionType type = ConnectionTypeAuto)
		: BoundMethodArgs<Args...>(obj, object, type), func_(func)
	{
	}

	bool match(void (T::*func)(Args...)) const { return func == func_; }

	void activate(Args... args)
	{
		if (this->object_)
			BoundMethodBase::activatePack(new PackType{ args... }, false);
		else
			(static_cast<T *>(this->obj_)->*func_)(args...);
	}

	void invoke(Args... args)
	{
		(static_cast<T *>(this->obj_)->*func_)(args...);
	}

private:
	void (T::*func_)(Args...);
};

template<typename... Args>
class BoundStaticMethod : public BoundMethodArgs<Args...>
{
public:
	BoundStaticMethod(void (*func)(Args...))
		: BoundMethodArgs<Args...>(nullptr, nullptr, ConnectionTypeAuto),
		  func_(func)
	{
	}

	bool match(void (*func)(Args...)) const { return func == func_; }

	void activate(Args... args) { (*func_)(args...); }
	void invoke(Args...) {}

private:
	void (*func_)(Args...);
};

}; /* namespace libcamera */

#endif /* __LIBCAMERA_BOUND_METHOD_H__ */