summaryrefslogtreecommitdiff
path: root/src/hiddenpointer.h
blob: 616dfeee51a34dce9c89df731cb54a5b4690e28b (plain)
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
/**
 *	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 PhpCpp {

/**
 *	Class definition
 */
template <typename Type>
class HiddenPointer
{
public:
	/**
	 *	Constructor to hide the pointer in a buffer
	 *	@param	pointer		The hidden pointer
	 *	@param	text		The visible text
	 *	@param	size		Optional text size
	 */
	HiddenPointer(Type *pointer, const char *text, int size=-1)
	{
		// calculate size
		if (size < 0) size = strlen(text);

		// reserve enough room for the text and the pointer
		_data.reserve(size + sizeof(Type *));
		
		// store the pointer
		_data.assign(std::string((const char *)&pointer, sizeof(Type *)));
		
		// append the text
		_data.append(text, size);
		
		// store pointers
		_pointer = pointer;
		_text = _data.c_str() + sizeof(Type *);
	}
	
	/**
	 *  Constructor to retrieve the object given a buffer
	 *  @param	text		The visible text
	 * 	@param	size		Size of the text
	 */
	HiddenPointer(const char *text, int size=-1)
	{
		// calculate size
		if (size < 0) size = strlen(text);
		
		// the pointer is stored right in front of the name
		_pointer = *((Type **)(text - sizeof(Type *)));
		_text = text;
	}
	
	/**
	 *  Copy constructor
	 * 	@param	that
	 */
	HiddenPointer(const HiddenPointer<Type> &that) : _pointer(that._pointer), _text(that._text), _data(that._data)
	{
		// if data is filled, the text is located inside the data
		if (_data.size() > 0) _text = _data.c_str() + sizeof(Type *);
	}
	
	/**
	 *  Destructor
	 */
	virtual ~HiddenPointer() {}
	
	/**
	 *  Assignment operator
	 *  @param	that
	 * 	@return	HiddenPointer
	 */
	HiddenPointer<Type> operator=(const HiddenPointer &that)
	{
		// skip self assignment
		if (&that == this) return *this;
		
		// copy members
		_pointer = that._pointer;
		_text = that._text;
		_data = that._data;

		// if data is filled, the text is located inside the data
		if (_data.size() > 0) _text = _data.c_str() + sizeof(Type *);
	}
	
	/**
	 *  Retrieve the pointer
	 *  @return Type*
	 */
	Type *pointer()
	{
		return _pointer;
	}
	
	/**
	 *  Retrieve the text
	 *  @return const char *
	 */
	const char *text()
	{
		return _text;
	}
	
	/**
	 *  Cast to the pointer
	 *  @return Type*
	 */
	operator Type* ()
	{
		return _pointer;
	}
	
	/**
	 *  Cast to text
	 *  @return const char *
	 */
	operator const char * ()
	{
		return _text;
	}

private:
	/**
	 *  The actual pointer
	 *  @var Type*
	 */
	Type *_pointer;
	
	/**
	 *  The original text
	 *  @var text
	 */
	const char *_text;
	
	/**
	 *  Optional data buffer
	 *  @var string
	 */
	std::string _data;

};

/**
 *	End of namespace
 */
}