ChimeraTK-DeviceAccess
03.18.00
accessPrivateData.h
Go to the documentation of this file.
1
// SPDX-FileCopyrightText: Deutsches Elektronen-Synchrotron DESY, MSK, ChimeraTK Project <chimeratk-support@desy.de>
2
// SPDX-License-Identifier: LGPL-3.0-or-later
3
#pragma once
4
5
// Helper classes to access private data of another class, which can be useful
6
// for whitebox tests This code was taken from:
7
// https://gist.github.com/dabrahams/1528856
8
9
namespace
accessPrivateData
{
10
11
// This is a rewrite and analysis of the technique in this article:
12
// http://bloglitb.blogspot.com/2010/07/access-to-private-members-thats-easy.html
13
14
// ------- Framework -------
15
// The little library required to work this magic
16
17
// Generate a static data member of type Tag::type in which to store
18
// the address of a private member. It is crucial that Tag does not
19
// depend on the /value/ of the the stored address in any way so that
20
// we can access it from ordinary code without directly touching
21
// private data.
22
template
<
class
Tag>
23
struct
stowed
{
24
static
typename
Tag::type
value
;
25
};
26
template
<
class
Tag>
27
typename
Tag::type
stowed<Tag>::value
;
28
29
// Generate a static data member whose constructor initializes
30
// stowed<Tag>::value. This type will only be named in an explicit
31
// instantiation, where it is legal to pass the address of a private
32
// member.
33
template
<
class
Tag,
typename
Tag::type x>
34
struct
stow_private
{
35
stow_private
() {
stowed<Tag>::value
= x; }
36
static
stow_private
instance
;
37
};
38
template
<
class
Tag,
typename
Tag::type x>
39
stow_private<Tag, x>
stow_private<Tag, x>::instance
;
40
41
/*
42
// ------- Usage -------
43
// A demonstration of how to use the library, with explanation
44
45
struct A
46
{
47
A() : x("proof!") {}
48
private:
49
char const* x;
50
};
51
52
// A tag type for A::x. Each distinct private member you need to
53
// access should have its own tag. Each tag should contain a
54
// nested ::type that is the corresponding pointer-to-member type.
55
struct A_x { typedef char const*(A::*type); };
56
57
// Explicit instantiation; the only place where it is legal to pass
58
// the address of a private member. Generates the static ::instance
59
// that in turn initializes stowed<Tag>::value.
60
template class stow_private<A_x,&A::x>;
61
62
int main()
63
{
64
A a;
65
66
// Use the stowed private member pointer
67
std::cout << a.*stowed<A_x>::value << std::endl;
68
};
69
*/
70
71
}
// namespace accessPrivateData
accessPrivateData::stow_private::stow_private
stow_private()
Definition:
accessPrivateData.h:35
accessPrivateData::stow_private::instance
static stow_private instance
Definition:
accessPrivateData.h:36
accessPrivateData::stowed
Definition:
accessPrivateData.h:23
accessPrivateData::stow_private
Definition:
accessPrivateData.h:34
accessPrivateData::stowed::value
static Tag::type value
Definition:
accessPrivateData.h:24
accessPrivateData
Definition:
accessPrivateData.h:9
sources
ChimeraTK-DeviceAccess
tests
include
accessPrivateData.h
Generated by
1.8.17