openMSX
outer.hh
Go to the documentation of this file.
1#ifndef OUTER_HH
2#define OUTER_HH
3
4#include <bit>
5#include <cstddef>
6
7// Example usage:
8// class Foo {
9// ...
10// struct Bar : public SomeBaseClass {
11// ...
12// void f() override {
13// // transform the 'this' pointer to the 'bar' inner object
14// // into a reference to the outer object of type Foo.
15// Foo& foo = OUTER(Foo, bar);
16// }
17// } bar;
18// };
19//
20// This avoids having to store a back-pointer from the inner 'bar' object to
21// the outer 'Foo' object.
22//
23// The 'bar' object lives at a fixed offset inside the 'Foo' object, so
24// changing the bar-this-pointer into a Foo-reference is a matter of
25// subtracting this (compile-time-constant) offset.
26//
27// At least the offset is constant in the absence of virtual inheritance (we
28// don't use this c++ feature in openMSX). Also strictly speaking the c++
29// standard doesn't guarantee the offset is constant in the presence of virtual
30// functions, though it is in all practical implementations. See here for more
31// background info:
32// http://stackoverflow.com/questions/1129894/why-cant-you-use-offsetof-on-non-pod-structures-in-c
33
34
35// Adjust the current 'this' pointer (pointer to the inner object) to a
36// reference to the outer object. The first parameter is the type of the outer
37// object, the second parameter is the name of the 'this' member variable in
38// the outer object.
39
40template<typename T> int checkInvalidOuterUsage(T) { return 0; }
41
42#define OUTER(type, member) (checkInvalidOuterUsage<const decltype(std::declval<type&>().member)*>(this), \
43 *std::bit_cast<type*>(std::bit_cast<uintptr_t>(this) - offsetof(type, member)))
44
45#endif
int checkInvalidOuterUsage(T)
Definition outer.hh:40