doc
[m6w6/ext-propro] / php_propro.h
1 /*
2 +--------------------------------------------------------------------+
3 | PECL :: propro |
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) 2013 Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 #ifndef PHP_PROPRO_H
14 #define PHP_PROPRO_H
15
16 #ifndef DOXYGEN
17
18 extern zend_module_entry propro_module_entry;
19 #define phpext_propro_ptr &propro_module_entry
20
21 #define PHP_PROPRO_VERSION "1.0.1"
22
23 #ifdef PHP_WIN32
24 # define PHP_PROPRO_API __declspec(dllexport)
25 #elif defined(__GNUC__) && __GNUC__ >= 4
26 # define PHP_PROPRO_API extern __attribute__ ((visibility("default")))
27 #else
28 # define PHP_PROPRO_API extern
29 #endif
30
31 #ifdef ZTS
32 # include <TSRM/TSRM.h>
33 #endif
34
35 #endif
36
37 /**
38 * The internal property proxy.
39 *
40 * Container for the object/array holding the proxied property.
41 */
42 struct php_property_proxy {
43 /** The container holding the property */
44 zval *container;
45 /** The name of the proxied property */
46 char *member_str;
47 /** The length of the name */
48 size_t member_len;
49 };
50 typedef struct php_property_proxy php_property_proxy_t;
51
52 /**
53 * The userland object.
54 *
55 * Return an object instance of php\\PropertyProxy to make your C-struct
56 * member accessible by reference from PHP userland.
57 *
58 * Example:
59 * ~~~~~~~~~~{.c}
60 * static zval *my_read_prop(zval *object, zval *member, int type, zend_literal *key TSRMLS_DC)
61 * {
62 * my_object_t *obj = zend_object_store_get_object(object TSRMLS_CC);
63 * my_prophandler_t *handler;
64 * zval *return_value, *copy = my_cast(IS_STRING, member);
65 *
66 * if (SUCCESS == my_get_prophandler(Z_STRVAL_P(copy), Z_STRLEN_P(copy), &handler)) {
67 * ALLOC_ZVAL(return_value);
68 * Z_SET_REFCOUNT_P(return_value, 0);
69 * Z_UNSET_ISREF_P(return_value);
70 *
71 * if (type == BP_VAR_R) {
72 * handler->read(obj, return_value TSRMLS_CC);
73 * } else {
74 * //
75 * // This is the interesting part
76 * //
77 * php_property_proxy_t *proxy;
78 * zend_object_value proxy_ov;
79 * zend_class_entry *proxy_ce;
80 *
81 * proxy = php_property_proxy_init(object, Z_STRVAL_P(copy), Z_STRLEN_P(copy) TSRMLS_CC);
82 * proxy_ce = php_property_proxy_get_class_entry();
83 * proxy_ov = php_property_proxy_object_new_ex(proxy_ce, proxy, NULL TSRMLS_CC);
84 * RETVAL_OBJVAL(proxy_ov, 0);
85 * }
86 * } else {
87 * zend_object_handlers *oh = zend_get_std_object_handlers();
88 * return_value = oh->read_property(object, member, type, key TSRMLS_CC);
89 * }
90 *
91 * zval_ptr_dtor(&copy);
92 *
93 * return return_value;
94 * }
95 * ~~~~~~~~~~
96 */
97 struct php_property_proxy_object {
98 /** The std zend_object */
99 zend_object zo;
100 /** The object value for easy zval creation */
101 zend_object_value zv;
102 /** The actual property proxy */
103 php_property_proxy_t *proxy;
104 /** A reference to any parent property proxy object */
105 struct php_property_proxy_object *parent;
106 };
107 typedef struct php_property_proxy_object php_property_proxy_object_t;
108
109 /**
110 * Create a property proxy
111 *
112 * The property proxy will forward reads and writes to itself to the
113 * proxied property with name \a member_str of \a container.
114 *
115 * @param container the container holding the property
116 * @param member_str the name of the proxied property
117 * @param member_len the length of the name
118 * @return a new property proxy
119 */
120 PHP_PROPRO_API php_property_proxy_t *php_property_proxy_init(zval *container,
121 const char *member_str, size_t member_len TSRMLS_DC);
122
123 /**
124 * Destroy and free a property proxy.
125 *
126 * The destruction of the property proxy object calls this.
127 *
128 * @param proxy a pointer to the allocated property proxy
129 */
130 PHP_PROPRO_API void php_property_proxy_free(php_property_proxy_t **proxy);
131
132 /**
133 * Get the zend_class_entry of php\\PropertyProxy
134 * @return the class entry pointer
135 */
136 PHP_PROPRO_API zend_class_entry *php_property_proxy_get_class_entry(void);
137
138 /**
139 * Instantiate a new php\\PropertyProxy
140 * @param ce the property proxy or derived class entry
141 * @return the zval object value
142 */
143 PHP_PROPRO_API zend_object_value php_property_proxy_object_new(
144 zend_class_entry *ce TSRMLS_DC);
145
146 /**
147 * Instantiate a new php\\PropertyProxy with \a proxy
148 * @param ce the property proxy or derived class entry
149 * @param proxy the internal property proxy
150 * @param ptr a pointer to store the resulting property proxy object
151 * @return the zval object value
152 */
153 PHP_PROPRO_API zend_object_value php_property_proxy_object_new_ex(
154 zend_class_entry *ce, php_property_proxy_t *proxy,
155 php_property_proxy_object_t **ptr TSRMLS_DC);
156
157 #endif /* PHP_PROPRO_H */
158
159
160 /*
161 * Local variables:
162 * tab-width: 4
163 * c-basic-offset: 4
164 * End:
165 * vim600: noet sw=4 ts=4 fdm=marker
166 * vim<600: noet sw=4 ts=4
167 */