xref: /ext-ds/src/php/classes/php_queue_ce.c (revision 300d1065)
1 #include "../../common.h"
2 
3 #include "../parameters.h"
4 #include "../arginfo.h"
5 
6 #include "../iterators/php_queue_iterator.h"
7 #include "../handlers/php_queue_handlers.h"
8 #include "../objects/php_queue.h"
9 
10 #include "php_collection_ce.h"
11 #include "php_queue_ce.h"
12 
13 #define METHOD(name) PHP_METHOD(Queue, name)
14 
15 zend_class_entry *php_ds_queue_ce;
16 
METHOD(__construct)17 METHOD(__construct)
18 {
19     PARSE_OPTIONAL_ZVAL(values);
20 
21     if (values) {
22         ds_queue_push_all(THIS_DS_QUEUE(), values);
23     }
24 }
25 
METHOD(allocate)26 METHOD(allocate)
27 {
28     PARSE_LONG(capacity);
29     ds_queue_allocate(THIS_DS_QUEUE(), capacity);
30 }
31 
METHOD(capacity)32 METHOD(capacity)
33 {
34     PARSE_NONE;
35     RETURN_LONG(ds_queue_capacity(THIS_DS_QUEUE()));
36 }
37 
METHOD(push)38 METHOD(push)
39 {
40     PARSE_VARIADIC_ZVAL();
41     ds_queue_push(THIS_DS_QUEUE(), argc, argv);
42 }
43 
METHOD(pop)44 METHOD(pop)
45 {
46     PARSE_NONE;
47     ds_queue_pop_throw(THIS_DS_QUEUE(), return_value);
48 }
49 
METHOD(peek)50 METHOD(peek)
51 {
52     PARSE_NONE;
53     RETURN_ZVAL_COPY(ds_queue_peek_throw(THIS_DS_QUEUE()));
54 }
55 
METHOD(copy)56 METHOD(copy)
57 {
58     PARSE_NONE;
59     RETURN_OBJ(php_ds_queue_create_clone(THIS_DS_QUEUE()));
60 }
61 
METHOD(count)62 METHOD(count)
63 {
64     PARSE_NONE;
65     RETURN_LONG(QUEUE_SIZE(THIS_DS_QUEUE()));
66 }
67 
METHOD(clear)68 METHOD(clear)
69 {
70     PARSE_NONE;
71     ds_queue_clear(THIS_DS_QUEUE());
72 }
73 
METHOD(toArray)74 METHOD(toArray)
75 {
76     PARSE_NONE;
77     ds_queue_to_array(THIS_DS_QUEUE(), return_value);
78 }
79 
METHOD(isEmpty)80 METHOD(isEmpty)
81 {
82     PARSE_NONE;
83     RETURN_BOOL(QUEUE_SIZE(THIS_DS_QUEUE()) == 0);
84 }
85 
METHOD(jsonSerialize)86 METHOD(jsonSerialize)
87 {
88     PARSE_NONE;
89     ds_queue_to_array(THIS_DS_QUEUE(), return_value);
90 }
91 
METHOD(getIterator)92 METHOD(getIterator) {
93     PARSE_NONE;
94     ZVAL_COPY(return_value, getThis());
95 }
96 
METHOD(offsetExists)97 METHOD(offsetExists)
98 {
99     ARRAY_ACCESS_BY_KEY_NOT_SUPPORTED();
100 }
101 
METHOD(offsetGet)102 METHOD(offsetGet)
103 {
104     ARRAY_ACCESS_BY_KEY_NOT_SUPPORTED();
105 }
106 
METHOD(offsetSet)107 METHOD(offsetSet)
108 {
109     PARSE_ZVAL_ZVAL(offset, value);
110 
111     if (Z_TYPE_P(offset) == IS_NULL) {
112         ds_queue_push(THIS_DS_QUEUE(), 1, value);
113     } else {
114         ARRAY_ACCESS_BY_KEY_NOT_SUPPORTED();
115     }
116 }
117 
METHOD(offsetUnset)118 METHOD(offsetUnset)
119 {
120     ARRAY_ACCESS_BY_KEY_NOT_SUPPORTED();
121 }
122 
php_ds_register_queue()123 void php_ds_register_queue()
124 {
125     zend_class_entry ce;
126 
127     zend_function_entry methods[] = {
128         PHP_DS_ME(Queue, __construct)
129         PHP_DS_ME(Queue, allocate)
130         PHP_DS_ME(Queue, capacity)
131         PHP_DS_ME(Queue, peek)
132         PHP_DS_ME(Queue, pop)
133         PHP_DS_ME(Queue, push)
134         PHP_DS_ME(Queue, getIterator)
135 
136         PHP_DS_ME(Queue, offsetExists)
137         PHP_DS_ME(Queue, offsetGet)
138         PHP_DS_ME(Queue, offsetSet)
139         PHP_DS_ME(Queue, offsetUnset)
140 
141         PHP_DS_COLLECTION_ME_LIST(Queue)
142         PHP_FE_END
143     };
144 
145     INIT_CLASS_ENTRY(ce, PHP_DS_NS(Queue), methods);
146 
147     php_ds_queue_ce = zend_register_internal_class(&ce);
148     php_ds_queue_ce->ce_flags      |= ZEND_ACC_FINAL;
149     php_ds_queue_ce->create_object  = php_ds_queue_create_object;
150     php_ds_queue_ce->get_iterator   = php_ds_queue_get_iterator;
151     php_ds_queue_ce->serialize      = php_ds_queue_serialize;
152     php_ds_queue_ce->unserialize    = php_ds_queue_unserialize;
153 
154     zend_declare_class_constant_long(php_ds_queue_ce, STR_AND_LEN("MIN_CAPACITY"), DS_DEQUE_MIN_CAPACITY);
155 
156     zend_class_implements(php_ds_queue_ce, 2,
157         collection_ce,
158         zend_ce_arrayaccess
159     );
160 
161     php_ds_register_queue_handlers();
162 }
163