summaryrefslogtreecommitdiff
path: root/zend/hiddenpointer.h
blob: 147886e091d38cb5b7ed30a41cf052750e188ea1 (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
/**
 *  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, making it lower case at the same time
        // (php function names are case insensitive, and must even be lowercase
        // because all the lookups are done in lowercase)
        for (int i = 0; i < size + 1; i++)
        {
            // convert char the lower case
            buffer[sizeof(Type *) + i] = tolower(text[i]);
        }
        
        // 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;
        }
    }
    
    /**
     *  Move constructor
     *  @param  that
     */
    HiddenPointer(HiddenPointer<Type> &&that) _NOEXCEPT : _allocated(that._allocated), _buffer(that._buffer)
    {
        // the other object is no longer allocated
        that._allocated = false;
    }
    
    /**
     *  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 *);
    }
    
    /**
     *  Derefence the pointer
     *  @return Type*
     */
    Type *operator->() const
    {
        // type is stored in front of the buffer
        return *((Type **)_buffer);
    }
    
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
 */
}