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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
/**
* HiddenPointer.h
*
* Helper class that we use to hide a pointer in a string. We do this
* by creating a string buffer that is a littlebit bigger, and put
* the hidden pointer in front of the name
*
* @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com>
* @copyright 2013 Copernica BV
*/
/**
* Set up namespace
*/
namespace Php {
/**
* Class definition
*/
template <typename Type>
class HiddenPointer
{
public:
/**
* Constructor to hide the pointer in a buffer
* @param pointer The pointer to hide
* @param text The visible text
* @param size Optional text size
*/
HiddenPointer(Type *pointer, const char *text, int size=-1)
{
// calculate size of the text
if (size < 0) size = strlen(text);
// allocate data + trailing null + size of pointer
char *buffer = new char[size + 1 + sizeof(Type *)];
// copy pointer into the buffer
memcpy(buffer, &pointer, sizeof(Type *));
// copy the name into the buffer
memcpy(buffer + sizeof(Type *), text, size + 1);
// store in member
_buffer = buffer;
}
/**
* Hide pointer in buffer
* @param pointer
* @param text
*/
HiddenPointer(Type *pointer, const std::string &text) :
HiddenPointer(pointer, text.c_str(), text.size()) {}
/**
* Constructor to retrieve the object given a buffer
* @param text The visible text
*/
HiddenPointer(const char *text) :
_buffer(text - sizeof(Type *)), // the buffer starts before the actual text
_allocated(false) {} // no memory was allocated
/**
* Copy constructor
* @param that
*/
HiddenPointer(const HiddenPointer<Type> &that) : _allocated(that._allocated)
{
// is the other object allocated?
if (_allocated)
{
// allocate this object too, call other constructor
HiddenPointer(that, that);
}
else
{
// just copy the data
_buffer = that._buffer;
}
}
/**
* Destructor
*/
virtual ~HiddenPointer()
{
// destruct data
if (_allocated) delete[] _buffer;
}
/**
* Assignment operator
* @param that
* @return HiddenPointer
*/
HiddenPointer<Type> operator=(const HiddenPointer &that)
{
// skip self assignmend
if (this == &that) return *this;
// deallocate current object
if (_allocated) delete[] _buffer;
// copy allocated setting
_allocated = that._allocated;
// is the other object allocated?
if (_allocated)
{
// allocate this object too, call constructor
HiddenPointer(that, that);
}
else
{
// just copy the data
_buffer = that._buffer;
}
// done
return *this;
}
/**
* Retrieve the pointer
* @return Type*
*/
operator Type * () const
{
// type is stored in front of the buffer
return *((Type **)_buffer);
}
/**
* Retrieve the text
* @return const char *
*/
operator const char * () const
{
// name starts a number of bytes further
return _buffer + sizeof(Type *);
}
private:
/**
* Buffer that holds both the pointer and the text - the text starts
* a number of bytes further up
* @var buffer
*/
const char *_buffer = nullptr;
/**
* Was this allocated?
* @var bool
*/
bool _allocated = true;
};
/**
* End of namespace
*/
}
|