summaryrefslogtreecommitdiff
path: root/include/global.h
blob: 605aeec0686898b0b8bccc504b7c08cb4631e0e5 (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
/**
 *  Global variable
 *
 *  A global variable is a value that - once updated - also updates
 *  the global scope
 *
 *  @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com>
 *  @copyright 2013 Copernica BV
 */

/**
 *  Forward declarations
 */
struct _zval_struct;
struct _zend_string;

/**
 *  Namespace
 */
namespace Php {

/**
 *  Class definition
 */
class PHPCPP_EXPORT Global : public Value
{
public:
    /**
     *  No copy constructor
     *  @param  global
     */
    Global(const Global &global) = delete;

    /**
     *  Move constructor
     *  @param  global
     */
    Global(Global &&global) _NOEXCEPT;

    /**
     *  Destructor
     */
    virtual ~Global();

    /**
     *  Assignment operator
     *  @param  global
     *  @return Global
     */
    /*
    Global &operator=(const Global &global)
    {
        // skip self assignment
        if (&global == this) return *this;

        // call base
        Value::operator=(global);

        // copy name and exists setting
        _name = global._name;
        _exists = global._exists;

        // done
        return *this;
    }
    */

    /**
     *  Move operator
     *  @param  global
     *  @return Global
     */
    /*
    Global &operator=(Global &&global) _NOEXCEPT
    {
        // skip self assignment
        if (&global == this) return *this;

        // call base
        Value::operator=(std::move(global));

        // copy name and exists setting
        _name = std::move(global._name);
        _exists = global._exists;

        // done
        return *this;
    }
    */

    /**
     *  Assignment operator
     *  @param  value
     *  @return Global
     */
    template <typename T>
    Global &operator=(const T &value)
    {
        Value::operator=(value);
        return update();
    }

    /**
     *  Set a certain property
     *  Calling this method will turn the value into an array
     *  @param  index       Index of the property to set
     *  @param  value       Value to set
     *  @return Value       The value that was set
     */
    virtual void set(int index, const Value &value) override
    {
        // update current object
        update();

        // call base
        Value::set(index, value);
    }

    /**
     *  Set a certain property
     *  Calling this method will turn the value into an array
     *  @param  key         Key of the property to set
     *  @param  size        Size of the key
     *  @param  value       Value to set
     *  @return Value       The value that was set
     */
    virtual void set(const char *key, int size, const Value &value) override
    {
        // update current object
        update();

        // call base
        Value::set(key, size, value);
    }


protected:
    /**
     *  Function that is called when the value is updated
     *  @return Value
     */
    Global &update();

private:
    /**
     *  Constructor for non-existing var
     *
     *  @param  name    Name for the variable that does not exist
     */
    Global(const char *name);

    /**
     *  Alternative constructor for non-existing var
     *  @param  name
     */
    Global(const std::string &name);

    /**
     *  Constructor to wrap zval for existing global bar
     *  @param  name
     *  @param  val
     */
    Global(const char *name, struct _zval_struct *val);

    /**
     *  Alternative constructor to wrap zval
     *  @param  name
     *  @param  val
     */
    Global(const std::string &name, struct _zval_struct *val);

    /**
     *  Name of the variable
     *  @var struct _zend_string*
     */
    struct _zend_string *_name;

    /**
     *  Does it already exist?
     *  @var bool
     */
    bool _exists;

    /**
     *  The globals can access the private method from this class
     */
    friend class Globals;
};

/**
 *  End of namespace
 */
}