From 3bd8d6a6081a83d468b23425c7659893c82aeafb Mon Sep 17 00:00:00 2001 From: Michael Wallner Date: Sat, 21 May 2011 15:26:28 +0000 Subject: [PATCH] * add a resource factory --- php_http_resource_factory.c | 79 +++++++++++++++++++++++++++++++++++++ php_http_resource_factory.h | 53 +++++++++++++++++++++++++ 2 files changed, 132 insertions(+) create mode 100644 php_http_resource_factory.c create mode 100644 php_http_resource_factory.h diff --git a/php_http_resource_factory.c b/php_http_resource_factory.c new file mode 100644 index 0000000..c3ff6f5 --- /dev/null +++ b/php_http_resource_factory.c @@ -0,0 +1,79 @@ +/* + +--------------------------------------------------------------------+ + | PECL :: http | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted provided that the conditions mentioned | + | in the accompanying LICENSE file are met. | + +--------------------------------------------------------------------+ + | Copyright (c) 2004-2010, Michael Wallner | + +--------------------------------------------------------------------+ +*/ + +/* $Id:$ */ + +#include "php_http.h" +#include "php_http_resource_factory.h" + +PHP_HTTP_API php_http_resource_factory_t *php_http_resource_factory_init(php_http_resource_factory_t *f, php_http_resource_factory_ops_t *fops, void *data, void (*dtor)(void *data)) +{ + if (!f) { + f = emalloc(sizeof(*f)); + } + memset(f, 0, sizeof(*f)); + + memcpy(&f->fops, fops, sizeof(*fops)); + + f->data = data; + f->dtor = dtor; + + return f; +} + +PHP_HTTP_API void php_http_resource_factory_dtor(php_http_resource_factory_t *f) +{ + if (f->dtor) { + f->dtor(f->data); + } +} +PHP_HTTP_API void php_http_resource_factory_free(php_http_resource_factory_t **f) +{ + if (*f) { + php_http_resource_factory_dtor(*f); + efree(*f); + *f = NULL; + } +} + +PHP_HTTP_API void *php_http_resource_factory_handle_ctor(php_http_resource_factory_t *f TSRMLS_DC) +{ + if (f->fops.ctor) { + return f->fops.ctor(f->data TSRMLS_CC); + } + return NULL; +} + +PHP_HTTP_API void *php_http_resource_factory_handle_copy(php_http_resource_factory_t *f, void *handle TSRMLS_DC) +{ + if (f->fops.copy) { + return f->fops.copy(f->data, handle TSRMLS_CC); + } + return NULL; +} + +PHP_HTTP_API void php_http_resource_factory_handle_dtor(php_http_resource_factory_t *f, void *handle TSRMLS_DC) +{ + if (f->fops.dtor) { + f->fops.dtor(f->data, handle TSRMLS_CC); + } +} + +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * End: + * vim600: noet sw=4 ts=4 fdm=marker + * vim<600: noet sw=4 ts=4 + */ + diff --git a/php_http_resource_factory.h b/php_http_resource_factory.h new file mode 100644 index 0000000..1a0a8ec --- /dev/null +++ b/php_http_resource_factory.h @@ -0,0 +1,53 @@ +/* + +--------------------------------------------------------------------+ + | PECL :: http | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted provided that the conditions mentioned | + | in the accompanying LICENSE file are met. | + +--------------------------------------------------------------------+ + | Copyright (c) 2004-2010, Michael Wallner | + +--------------------------------------------------------------------+ +*/ + +/* $Id$ */ + +#ifndef PHP_HTTP_RESOURCE_FACTORY_H +#define PHP_HTTP_RESOURCE_FACTORY_H + +typedef void *(*php_http_resource_factory_handle_ctor_t)(void *opaque TSRMLS_DC); +typedef void *(*php_http_resource_factory_handle_copy_t)(void *opaque, void *handle TSRMLS_DC); +typedef void (*php_http_resource_factory_handle_dtor_t)(void *opaque, void *handle TSRMLS_DC); + +typedef struct php_http_resource_factory_ops { + php_http_resource_factory_handle_ctor_t ctor; + php_http_resource_factory_handle_copy_t copy; + php_http_resource_factory_handle_dtor_t dtor; +} php_http_resource_factory_ops_t; + +typedef struct php_http_resource_factory { + php_http_resource_factory_ops_t fops; + + void *data; + void (*dtor)(void *data); + +} php_http_resource_factory_t; + +PHP_HTTP_API php_http_resource_factory_t *php_http_resource_factory_init(php_http_resource_factory_t *f, php_http_resource_factory_ops_t *fops, void *data, void (*dtor)(void *data)); +PHP_HTTP_API void php_http_resource_factory_dtor(php_http_resource_factory_t *f); +PHP_HTTP_API void php_http_resource_factory_free(php_http_resource_factory_t **f); + +PHP_HTTP_API void *php_http_resource_factory_handle_ctor(php_http_resource_factory_t *f TSRMLS_DC); +PHP_HTTP_API void *php_http_resource_factory_handle_copy(php_http_resource_factory_t *f, void *handle TSRMLS_DC); +PHP_HTTP_API void php_http_resource_factory_handle_dtor(php_http_resource_factory_t *f, void *handle TSRMLS_DC); + +#endif /* PHP_HTTP_RESOURCE_FACTORY_H */ + +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * End: + * vim600: noet sw=4 ts=4 fdm=marker + * vim<600: noet sw=4 ts=4 + */ -- 2.30.2