-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBadge.h
55 lines (47 loc) · 1.29 KB
/
Badge.h
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
#pragma once
#include "get_Nth_type.h"
#include <cstddef>
namespace utils {
// See https://awesomekling.github.io/Serenity-C++-patterns-The-Badge
template<typename... types>
class Badge
{
static constexpr std::size_t S = sizeof...(types);
static_assert(S <= 4, "The maximum number of arguments to Badge is four.");
friend typename get_Nth_type<0 % S, types...>::type;
friend typename get_Nth_type<1 % S, types...>::type;
friend typename get_Nth_type<2 % S, types...>::type;
friend typename get_Nth_type<3 % S, types...>::type;
Badge() {}
};
// Useful for passing the this pointer of the calling class.
//
// Usage:
//
// void A::f(utils::BadgeCaller<B> b) // Can only be called by a member function of B.
// {
// B* bp = b; // Pointer to the calling object.
// ...
// }
//
// void B::f()
// {
// a.f(this); // Pass this pointer of B from within a member function of B.
// }
//
// If only const access to B is allowed use:
//
// void A::f(utils::BadgeCaller<B> const b);
//
template<typename Caller>
class BadgeCaller
{
private:
Caller* m_caller;
friend Caller;
BadgeCaller(Caller* caller) : m_caller(caller) { }
public:
operator Caller*() { return m_caller; }
operator Caller const*() const { return m_caller; }
};
} // namespace utils