blob: b45c76e72500114ea4b120ecf4dc7e45325caa30 [file] [log] [blame] [view]
Elly Fong-Jones4e3d25c72019-08-12 18:17:151# The Passkey Pattern
2
3The Passkey pattern is used when you need to expose a subset of a class's
4methods to another class in a more granular way than simply friending the other
5class. In essence, it involves creating a "passkey" class that can only be
6constructed by specific other classes, and requiring an instance of that passkey
7class to be passed in when calling methods you wish to restrict the use of. It
8is used like this:
9
10```
11class Foo {
12 public:
13 Foo();
14 ~Foo();
15
16 void NormalPublicMethod();
17 bool AnotherNormalPublicMethod(int a, int b);
18
19 class BarPasskey {
20 private:
21 friend class Bar;
22 BarPasskey() = default;
23 ~BarPasskey() = default;
24 };
25
26 void HelpBarOut(BarPasskey, ...);
27};
28
29...
30
31void Bar::DoStuff() {
32 foo->HelpBarOut(Foo::BarPasskey(), ...);
33}
34```
35
36The private constructor on `Foo::BarPasskey` prevents any class other than `Bar`
37from constructing a `Foo::BarPasskey`, which means that:
38
39* Only `Bar` can call those methods
40* `Bar` can delegate the ability to call those methods to other
41 classes/functions by passing them a `Foo::BarPasskey` instance
42
43This method is effectively free at runtime - a few extra bytes of argument space
44are used to pass in the Passkey object.
45
46It is encouraged to leave the `BarPasskey` parameter unnamed to reinforce that it
47carries no semantic information and is not actually used for anything.