-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathidlist.cpp
137 lines (120 loc) · 2.77 KB
/
idlist.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
126
127
128
129
130
131
132
133
134
135
136
137
#include "idlist.h"
#include "parameter.h"
using namespace pun;
const char* IntList::PHP_NAME = "Pun\\IntList";
IntList* IntList::get_IntList(Php::Value& val)
{
if (val.isObject()) {
if (val.instanceOf(IntList::PHP_NAME)) {
return (IntList*) val.implementation();
}
}
return nullptr;
}
void
IntList::setup_ext(Php::Extension& ext)
{
Php::Class<IntList> cext(IntList::PHP_NAME);
cext.method<&IntList::__construct>("__construct");
cext.method<&IntList::setArray> ("setArray");
cext.method<&IntList::pushBack> ("pushBack");
cext.method<&IntList::popBack> ("popBack");
cext.method<&IntList::getV> ("getV");
cext.method<&IntList::setV> ("setV");
cext.method<&IntList::back> ("back");
cext.method<&IntList::size> ("size");
cext.method<&IntList::clear> ("clear");
cext.method<&IntList::toArray> ("toArray");
ext.add(std::move(cext));
}
void
IntList::__construct(Php::Parameters& param)
{
setArray(param);
}
// set list from PHP array of integers
void
IntList::setArray(Php::Parameters& param)
{
auto isArray = pun::option_Array(param, 0);
if (!isArray) {
throw Php::Exception("Need (Array of integer)");
}
const Php::Value& v = param[0];
auto ct = v.size();
_store.clear();
_store.reserve(ct);
for(int i = 0; i < v.size(); i++)
{
_store.push_back(v[i]);
}
}
void
IntList::fn_copyIdList(Php::Value& v)
{
auto idle = _store.begin();
auto idend = _store.end();
int idx = 0;
while (idle != idend) {
v[idx] = *idle;
idle++;
idx++;
}
}
// return list as PHP array of integers
Php::Value
IntList::toArray()
{
Php::Value result;
this->fn_copyIdList(result);
return result;
}
void IntList::fn_pushBack(Php::Value& val)
{
_store.push_back(val);
}
void IntList::pushBack(Php::Parameters& param)
{
if (param.size() < 1) {
throw Php::Exception("ValueList\\Pushback(value): missing value");
}
fn_pushBack(param[0]);
}
void IntList::popBack()
{
_store.pop_back();
}
static void checkIndex(int index, unsigned int vlen) {
if (index < 0 || index >= (int) vlen) {
throw Php::Exception("IntList index out of range");
}
}
void
IntList::setV(Php::Parameters& param)
{
int index = pun::check_Int(param,0);
checkIndex(index, _store.size());
pun::need_Value(param,1);
_store[index] = param[1];
}
Php::Value
IntList::getV(Php::Parameters& params) const
{
int index = pun::check_Int(params,0);
checkIndex(index, _store.size());
return Php::Value(_store[index]);
}
Php::Value
IntList::back() const
{
if (_store.size() == 0) {
throw Php::Exception("ValueList getLast on empty list");
}
int index = (int) _store.size() - 1;
return Php::Value(_store[index]);
}
Php::Value
IntList::size() const
{
return Php::Value((int)_store.size());
}