-
Notifications
You must be signed in to change notification settings - Fork 339
/
Copy pathglobal.cpp
125 lines (106 loc) · 2.52 KB
/
global.cpp
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
/**
* Global.cpp
*
* Implementation for the global variable
*
* @author Emiel Bruijntjes <[email protected]>
* @copyright 2013 Copernica BV
*/
#include "includes.h"
/**
* Namespace
*/
namespace Php {
/**
* Move constructor
* @param global
*/
Global::Global(Global &&global) _NOEXCEPT :
Value(std::move(global)),
_name(global._name),
_exists(global._exists)
{
// remove from other global to avoid double free
global._name = nullptr;
}
/**
* Constructor for non-existing var
*
* @param name Name for the variable that does not exist
*/
Global::Global(const char *name) :
Value(),
_name(zend_string_init(name, ::strlen(name), 1)),
_exists(false) {}
/**
* Alternative constructor for non-existing var
* @param name
*/
Global::Global(const std::string &name) :
Value(),
_name(zend_string_init(name.data(), name.size(), 1)),
_exists(false) {}
/**
* Constructor to wrap zval for existing global bar
* @param name
* @param val
*/
Global::Global(const char *name, struct _zval_struct *val) :
Value(val, true),
_name(zend_string_init(name, ::strlen(name), 1)),
_exists(true) {}
/**
* Alternative constructor to wrap zval
* @param name
* @param val
*/
Global::Global(const std::string &name, struct _zval_struct *val) :
Value(val, true),
_name(zend_string_init(name.data(), name.size(), 1)),
_exists(true) {}
/**
* Destructor
*/
Global::~Global()
{
// release the string
if (_name) zend_string_release(_name);
}
/**
* Function that is called when the value is updated
* @return Value
*/
Global &Global::update()
{
// skip if the variable already exists
if (_exists) return *this;
// add one extra reference because the variable now is a global var too
Z_TRY_ADDREF_P(_val);
// add the variable to the globals
zend_symtable_update_ind(&EG(symbol_table), _name, _val);
// remember that the variable now exists
_exists = true;
// done
return *this;
}
/**
* Update the underlying value forcefully
* @return Value
*/
Global &Global::force_update()
{
// make sure the global already exists
if (! _exists) return update();
// remove the old value
zend_symtable_del_ind(&EG(symbol_table), _name);
// add one extra reference because the variable now is a global var too
Z_TRY_ADDREF_P(_val);
// update the internal symtable
zend_symtable_update_ind(&EG(symbol_table), _name, _val);
_exists = true;
return *this;
}
/**
* End of namespace
*/
}