summaryrefslogtreecommitdiff
path: root/include/hiddenpointer.h
blob: f432f65dfe29c2675ce59cf64522f42ce75751da (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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/**
 *  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
        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 *);
    }
    
    /**
     *  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
     *  @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() const
    {
        return _pointer;
    }
    
    /**
     *  Change the pointer
     *  @param  Type*
     */
    void setPointer(Type *pointer)
    {
        // store pointer
        _pointer = pointer;
    
        // overwrite in data
        _data.replace(0, sizeof(Type *), (const char *)&_pointer, sizeof(Type *));
        
        // for safety reasons, we recalculate text pointer
        _text = _data.c_str() + sizeof(Type *);
    }
    
    /**
     *  Retrieve the text
     *  @return const char *
     */
    const char *text() const
    {
        return _text;
    }
    
    /**
     *  Change the text
     *  @param  text
     *  @param  size
     */
    void setText(const char *text, int size=-1)
    {
        // check if size was set
        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 new text
        _text = _data.c_str() + sizeof(Type *);
    }
    
    /**
     *  Cast to the pointer
     *  @return Type*
     */
    operator Type* () const
    {
        return _pointer;
    }
    
    /**
     *  Cast to text
     *  @return const char *
     */
    operator const char * () const
    {
        return _text;
    }
    
    /**
     *  Length of the text
     *  @return int
     */
    int length() const
    {
        return _data.size() - sizeof(Type *);
    }

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
 */
}