Skip to content

Commit e0e48bf

Browse files
committed
update cpp class
1 parent 20423c9 commit e0e48bf

File tree

3 files changed

+79
-19
lines changed

3 files changed

+79
-19
lines changed

CListNode.cc

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,19 @@ CListNode::CListNode()
1818

1919
CListNode::~CListNode()
2020
{
21-
cout << "CListNode Destory" << endl;
2221

2322
if (this->head != nullptr){
2423
list_node *current = this->head;
2524
list_node *pNext = head->next;
2625

2726
while (current != nullptr)
2827
{
29-
30-
cout << "CListNode Destory -- node->" << Z_STRVAL_P(current->value) << endl;
31-
//zval_dtor(current->value);
3228
efree(current);
3329
current = nullptr;
3430
if (pNext){
3531
current = pNext;
3632
pNext = current->next;
3733
}
38-
39-
cout << "CListNode Destory -- while" << endl;
4034
}
4135

4236
}
@@ -64,8 +58,8 @@ int CListNode::add_value(zval* val) {
6458
zval *new_val;
6559
MAKE_STD_ZVAL(new_val);
6660
*new_val = *val;
67-
zval_copy_ctor(new_val);
68-
convert_to_string(new_val);//转成string
61+
zval_copy_ctor(new_val);//如果是非标量 添加引用计数
62+
//convert_to_string(new_val);//转成string
6963

7064
node->value = new_val;
7165
node->next = nullptr;
@@ -82,7 +76,7 @@ int CListNode::add_value(zval* val) {
8276
}
8377

8478
this->size = this->size + 1;
85-
cout << "CListNode Add" << endl;
79+
8680
return 0;
8781
}
8882

@@ -107,3 +101,53 @@ zval* CListNode::fetch_index(int index) {
107101
}
108102

109103
}
104+
105+
int CListNode::del_value(int index)
106+
{
107+
if (index <= 0 || index > this->size)
108+
{
109+
return -1;
110+
}
111+
112+
list_node* pCurrent = head;
113+
list_node* tmp = nullptr;
114+
115+
if (pCurrent != nullptr){
116+
int i = 1;
117+
if (i == index){
118+
119+
head = head->next;
120+
head->prev = nullptr;
121+
efree(pCurrent);
122+
--this->size;
123+
return 0;
124+
}
125+
else{
126+
while (pCurrent != nullptr){
127+
++i;
128+
tmp = pCurrent;
129+
pCurrent = pCurrent->next;
130+
if (i == index && pCurrent != nullptr){
131+
tmp->next = pCurrent->next;
132+
if (pCurrent->next != nullptr){
133+
pCurrent->next->prev = tmp;
134+
}
135+
else{
136+
tail = tmp;
137+
}
138+
zval_dtor(pCurrent->value);
139+
efree(pCurrent);
140+
--this->size;
141+
return 0;
142+
}
143+
144+
}
145+
146+
return -1;
147+
}
148+
149+
}
150+
else{
151+
return -1;
152+
}
153+
}

CListNode.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class CListNode {
1515
static CListNode* create();
1616

1717
int add_value(zval *val);
18-
18+
int del_value(int index);
1919
zval* fetch_index(int index);
2020

2121
int get_length() const;

ListNode.cc

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@ PHP_METHOD(ListNode,__construct)
104104
zend_update_property_string(listnode_ce,thisptr,"p1",strlen("p1"),"bbb" TSRMLS_CC);
105105
zend_declare_class_constant_string(listnode_ce,"AAA",strlen("AAA"),"bbb" TSRMLS_CC);
106106

107-
php_printf("---CListNode -- %d\n", ln);
108107
}
109108

110109

@@ -116,7 +115,7 @@ PHP_METHOD(ListNode, add_value)
116115
zval* thisptr = getThis();
117116
ListNode_object* ln_obj = (ListNode_object*)zend_object_store_get_object(thisptr TSRMLS_CC);
118117
CListNode *cListNode = ln_obj->listnode;
119-
php_printf("---CListNode -- %d\n", cListNode);
118+
120119
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &value) == FAILURE)
121120
{
122121
WRONG_PARAM_COUNT;
@@ -128,16 +127,11 @@ PHP_METHOD(ListNode, add_value)
128127
return;
129128
}
130129

131-
if (IS_LONG == Z_TYPE(*value)){
132-
php_printf("pre - %d\n", Z_LVAL_P(value));
133-
}
134-
135-
php_printf("ret %d\n", ret);
136130
RETURN_LONG(ret);
137131

138132
}
139133
else{
140-
php_printf("nullptr %d\n", value);
134+
php_error(E_ERROR,"cpp obj is nullptr\n");
141135
}
142136

143137
}
@@ -160,11 +154,33 @@ PHP_METHOD(ListNode, fetch_index)
160154
}
161155

162156
}
157+
158+
PHP_METHOD(ListNode, del_value)
159+
{
160+
long index;
161+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &index) == FAILURE)
162+
{
163+
WRONG_PARAM_COUNT;
164+
}
165+
zval* thisptr = getThis();
166+
ListNode_object* ln_obj = (ListNode_object*)zend_object_store_get_object(thisptr TSRMLS_CC);
167+
CListNode *cListNode = ln_obj->listnode;
168+
int ret = cListNode->del_value(index);
169+
170+
if (ret == 0){
171+
RETURN_BOOL(1);
172+
}
173+
else{
174+
RETURN_BOOL(0);
175+
}
176+
}
177+
163178
static zend_function_entry listnode_methods[]=
164179
{
165180
ZEND_ME(ListNode,__construct,NULL,ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
166181
ZEND_ME(ListNode, add_value, NULL, ZEND_ACC_PUBLIC)
167182
ZEND_ME(ListNode, fetch_index, NULL, ZEND_ACC_PUBLIC)
183+
ZEND_ME(ListNode, del_value, NULL, ZEND_ACC_PUBLIC)
168184
{NULL,NULL,NULL}
169185
};
170186

@@ -173,7 +189,7 @@ static zend_function_entry listnode_methods[]=
173189

174190
void free_listnode_object(void* obj TSRMLS_DC)
175191
{
176-
php_printf("free -- CListNode Destory\n");
192+
//php_printf("free -- CListNode Destory\n");
177193

178194
ListNode_object* listnode_obj = (ListNode_object*)obj;
179195
delete listnode_obj->listnode;

0 commit comments

Comments
 (0)