CppInterOp
C++ Language Interoperability Layer
Loading...
Searching...
No Matches
Sins.h
Go to the documentation of this file.
1#ifndef LIB_CPPINTEROP_SINS_H
2#define LIB_CPPINTEROP_SINS_H
3
4/// Standard-protected facility allowing access into private members in C++.
5/// Use with caution!
6// NOLINTBEGIN(cppcoreguidelines-macro-usage)
7#define CONCATE_(X, Y) X##Y
8#define CONCATE(X, Y) CONCATE_(X, Y)
9#define ALLOW_ACCESS(CLASS, MEMBER, ...) \
10 template <typename Only, __VA_ARGS__ CLASS::*Member> \
11 struct CONCATE(MEMBER, __LINE__) { \
12 friend __VA_ARGS__ CLASS::*Access(Only*) { return Member; } \
13 }; \
14 template <typename> struct Only_##MEMBER; \
15 template <> struct Only_##MEMBER<CLASS> { \
16 friend __VA_ARGS__ CLASS::*Access(Only_##MEMBER<CLASS>*); \
17 }; \
18 template struct CONCATE(MEMBER, \
19 __LINE__)<Only_##MEMBER<CLASS>, &CLASS::MEMBER>
20
21#define ACCESS(OBJECT, MEMBER) \
22 (OBJECT).* \
23 Access( \
24 (Only_##MEMBER<std::remove_reference_t<decltype(OBJECT)>>*)nullptr)
25
26// NOLINTEND(cppcoreguidelines-macro-usage)
27
28#endif // LIB_CPPINTEROP_SINS_H