Fix operator for Visual Studio
[m6w6/ext-http] / src / php_http_object.c
1 /*
2 +--------------------------------------------------------------------+
3 | PECL :: http |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted provided that the conditions mentioned |
7 | in the accompanying LICENSE file are met. |
8 +--------------------------------------------------------------------+
9 | Copyright (c) 2004-2014, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 #include "php_http_api.h"
14
15 static zend_object_handlers php_http_object_handlers;
16
17 zend_object *php_http_object_new(zend_class_entry *ce)
18 {
19 return &php_http_object_new_ex(ce, NULL)->zo;
20 }
21
22 php_http_object_t *php_http_object_new_ex(zend_class_entry *ce, void *intern)
23 {
24 php_http_object_t *o;
25
26 o = ecalloc(1, sizeof(*o) + zend_object_properties_size(ce));
27 zend_object_std_init(&o->zo, ce);
28 object_properties_init(&o->zo, ce);
29
30 o->intern = intern;
31 o->zo.handlers = &php_http_object_handlers;
32
33 return o;
34 }
35
36 void php_http_object_free(zend_object *object)
37 {
38 zend_object_std_dtor(object);
39 }
40
41 ZEND_RESULT_CODE php_http_new(void **obj_ptr, zend_class_entry *ce, php_http_new_t create, zend_class_entry *parent_ce, void *intern_ptr)
42 {
43 void *obj;
44
45 if (!ce) {
46 ce = parent_ce;
47 } else if (parent_ce && !instanceof_function(ce, parent_ce)) {
48 php_http_throw(unexpected_val, "Class %s does not extend %s", ce->name->val, parent_ce->name->val);
49 return FAILURE;
50 }
51
52 obj = create(ce, intern_ptr);
53 if (obj_ptr) {
54 *obj_ptr = obj;
55 }
56 return SUCCESS;
57 }
58
59 php_http_object_method_t *php_http_object_method_init(php_http_object_method_t *cb, zval *zobject, const char *method_str, size_t method_len)
60 {
61 if (!cb) {
62 cb = ecalloc(1, sizeof(*cb));
63 } else {
64 memset(cb, 0, sizeof(*cb));
65 }
66
67 cb->fci.size = sizeof(cb->fci);
68 ZVAL_STRINGL(&cb->fci.function_name, method_str, method_len);
69 #if PHP_VERSION_ID < 70300
70 cb->fcc.initialized = 1;
71 #endif
72 cb->fcc.calling_scope = cb->fcc.called_scope = Z_OBJCE_P(zobject);
73 cb->fcc.function_handler = Z_OBJ_HT_P(zobject)->get_method(&Z_OBJ_P(zobject), Z_STR(cb->fci.function_name), NULL);
74
75 return cb;
76 }
77
78 void php_http_object_method_dtor(php_http_object_method_t *cb)
79 {
80 zval_ptr_dtor(&cb->fci.function_name);
81 }
82
83 void php_http_object_method_free(php_http_object_method_t **cb)
84 {
85 if (*cb) {
86 php_http_object_method_dtor(*cb);
87 efree(*cb);
88 *cb = NULL;
89 }
90 }
91
92 ZEND_RESULT_CODE php_http_object_method_call(php_http_object_method_t *cb, zval *zobject, zval *retval_ptr, int argc, zval *args)
93 {
94 ZEND_RESULT_CODE rv;
95 zval retval;
96
97 ZVAL_UNDEF(&retval);
98 Z_ADDREF_P(zobject);
99 cb->fci.object = Z_OBJ_P(zobject);
100 cb->fcc.object = Z_OBJ_P(zobject);
101
102 cb->fci.retval = retval_ptr ? retval_ptr : &retval;
103
104 cb->fci.param_count = argc;
105 cb->fci.params = args;
106
107 if (cb->fcc.called_scope != Z_OBJCE_P(zobject)) {
108 cb->fcc.called_scope = Z_OBJCE_P(zobject);
109 cb->fcc.function_handler = Z_OBJ_HT_P(zobject)->get_method(&Z_OBJ_P(zobject), Z_STR(cb->fci.function_name), NULL);
110 }
111
112 rv = zend_call_function(&cb->fci, &cb->fcc);
113
114 zval_ptr_dtor(zobject);
115 if (!retval_ptr && !Z_ISUNDEF(retval)) {
116 zval_ptr_dtor(&retval);
117 }
118
119 return rv;
120 }
121
122 PHP_MINIT_FUNCTION(http_object)
123 {
124 memcpy(&php_http_object_handlers, zend_get_std_object_handlers(), sizeof(php_http_object_handlers));
125 php_http_object_handlers.offset = XtOffsetOf(php_http_object_t, zo);
126
127 return SUCCESS;
128 }
129
130 /*
131 * Local variables:
132 * tab-width: 4
133 * c-basic-offset: 4
134 * End:
135 * vim600: noet sw=4 ts=4 fdm=marker
136 * vim<600: noet sw=4 ts=4
137 */
138