Skip to content

Commit

Permalink
refdb-backend: Add owner refdb to object
Browse files Browse the repository at this point in the history
  • Loading branch information
RogerGee committed Oct 27, 2018
1 parent a08458f commit 3d9092b
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 15 deletions.
5 changes: 3 additions & 2 deletions php-object.h
Original file line number Diff line number Diff line change
Expand Up @@ -361,9 +361,10 @@ namespace php_git2
~php_refdb_backend_object();

git_refdb_backend* backend;
php_git_refdb* owner;
php_zts_member zts;

void create_custom_backend(zval* zobj);
void create_custom_backend(zval* zobj,php_git_refdb* owner = nullptr);

static zend_object_handlers handlers;
static void init(zend_class_entry* ce);
Expand Down Expand Up @@ -466,7 +467,7 @@ namespace php_git2
php_callback_sync* cb,zval* zbackend,php_git_odb* owner TSRMLS_DC);
void php_git2_make_odb_stream(zval* zp,git_odb_stream* stream,php_git_odb* owner TSRMLS_DC);
void php_git2_make_writestream(zval* zp,git_writestream* ws TSRMLS_DC);
void php_git2_make_refdb_backend(zval* zp,git_refdb_backend* backend TSRMLS_DC);
void php_git2_make_refdb_backend(zval* zp,git_refdb_backend* backend,php_git_refdb* owner TSRMLS_DC);

// Useful helpers

Expand Down
5 changes: 3 additions & 2 deletions php-refdb-backend-internal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ zend_function_entry php_git2::refdb_backend_internal_methods[] = {

// Make function implementation

void php_git2::php_git2_make_refdb_backend(zval* zp,git_refdb_backend* backend TSRMLS_DC)
void php_git2::php_git2_make_refdb_backend(zval* zp,git_refdb_backend* backend,php_git_refdb* ownerRefdb TSRMLS_DC)
{
php_refdb_backend_object* obj;
zend_class_entry* ce = php_git2::class_entry[php_git2_refdb_backend_internal_obj];
Expand All @@ -62,8 +62,9 @@ void php_git2::php_git2_make_refdb_backend(zval* zp,git_refdb_backend* backend T
object_init_ex(zp,ce);
obj = reinterpret_cast<php_refdb_backend_object*>(zend_objects_get_address(zp TSRMLS_CC));

// Assign the backend.
// Assign the backend and (optionally) the owner.
obj->backend = backend;
obj->owner = ownerRefdb;
}

// Implementation of php_refdb_backend_internal_object
Expand Down
12 changes: 10 additions & 2 deletions php-refdb-backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,18 +266,23 @@ int refdb_backend_has_property(zval* obj,zval* prop,int chk_type,const zend_lite
// Implementation of php_refdb_backend_object

php_refdb_backend_object::php_refdb_backend_object(zend_class_entry* ce TSRMLS_DC):
backend(nullptr), zts(TSRMLS_C)
backend(nullptr), owner(nullptr), zts(TSRMLS_C)
{
zend_object_std_init(this,ce TSRMLS_CC);
object_properties_init(this,ce);
}

php_refdb_backend_object::~php_refdb_backend_object()
{
// Free the object if we do not have an owner.
if (owner == nullptr && backend != nullptr) {
backend->free(backend);
}

zend_object_std_dtor(this ZTS_MEMBER_CC(this->zts));
}

void php_refdb_backend_object::create_custom_backend(zval* zobj)
void php_refdb_backend_object::create_custom_backend(zval* zobj,php_git_refdb* ownerRefdb)
{
// NOTE: 'zobj' is the zval to the outer object that wraps this custom
// zend_object derivation.
Expand All @@ -291,6 +296,9 @@ void php_refdb_backend_object::create_custom_backend(zval* zobj)
// Create custom backend. Custom backends are always passed off to git2, so
// we are not responsible for calling its free function.
backend = new (emalloc(sizeof(git_refdb_backend_php))) git_refdb_backend_php(zobj);

// Set the backend owner (may be null).
owner = ownerRefdb;
}

/*static*/ zend_object_handlers php_refdb_backend_object::handlers;
Expand Down
42 changes: 33 additions & 9 deletions refdb.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
/**
/*
* refdb.h
*
* This file is a part of php-git2.
*/

#ifndef PHPGIT2_REFDB_H
#define PHPGIT2_REFDB_H

namespace php_git2
{
// Explicitly specialize git2_resource destructor for git_repository.
Expand All @@ -20,7 +23,15 @@ namespace php_git2
private php_zts_base
{
public:
ZTS_CONSTRUCTOR_WITH_BASE(php_git_refdb_backend,php_zts_base)
// Make this object a connector to a php_resource that looks up a
// php_git_refdb resource wrapper object.
using connect_t = php_resource<php_git_refdb>;
typedef git_refdb_backend* target_t;

php_git_refdb_backend(connect_t& conn TSRMLS_DC):
php_zts_base(TSRMLS_C), ownerWrapper(conn)
{
}

git_refdb_backend* byval_git2(unsigned argno = std::numeric_limits<unsigned>::max())
{
Expand All @@ -40,11 +51,14 @@ namespace php_git2
// If the object doesn't have a backing, then we create a custom
// one.
if (object->backend == nullptr) {
object->create_custom_backend(value);
object->create_custom_backend(value,ownerWrapper.get_object());
}

return object->backend;
}

private:
connect_t& ownerWrapper;
};

class php_git_refdb_backend_ref:
Expand All @@ -63,13 +77,15 @@ namespace php_git2

void ret(zval* return_value)
{
php_git2_make_refdb_backend(return_value,backend TSRMLS_CC);
php_git2_make_refdb_backend(return_value,backend,nullptr TSRMLS_CC);
}

private:
git_refdb_backend* backend;
};

using php_git_refdb_backend_connector = connector_wrapper<php_git_refdb_backend>;

} // namespace php_git2

// Function templates:
Expand Down Expand Up @@ -104,7 +120,7 @@ static constexpr auto ZIF_GIT_REFDB_FREE = zif_php_git2_function_free<
>
>;

static constexpr auto ZIF_GIT_REFDB_NEW = zif_php_git2_function<
static constexpr auto ZIF_GIT_REFDB_NEW = zif_php_git2_function_setdeps<
php_git2::func_wrapper<
int,
git_refdb**,
Expand All @@ -113,13 +129,14 @@ static constexpr auto ZIF_GIT_REFDB_NEW = zif_php_git2_function<
php_git2::php_resource_ref<php_git2::php_git_refdb>,
php_git2::php_resource<php_git2::php_git_repository>
>,
php_git2::sequence<0,1>, // Make the git_refdb dependent on the repository.
1,
php_git2::sequence<1>,
php_git2::sequence<0,1>,
php_git2::sequence<0,0>
>;

static constexpr auto ZIF_GIT_REFDB_OPEN = zif_php_git2_function<
static constexpr auto ZIF_GIT_REFDB_OPEN = zif_php_git2_function_setdeps<
php_git2::func_wrapper<
int,
git_refdb**,
Expand All @@ -128,6 +145,7 @@ static constexpr auto ZIF_GIT_REFDB_OPEN = zif_php_git2_function<
php_git2::php_resource_ref<php_git2::php_git_refdb>,
php_git2::php_resource<php_git2::php_git_repository>
>,
php_git2::sequence<0,1>, // Make the git_refdb dependent on the repository.
1,
php_git2::sequence<1>,
php_git2::sequence<0,1>,
Expand All @@ -140,9 +158,13 @@ static constexpr auto ZIF_GIT_REFDB_SET_BACKEND = zif_php_git2_function<
git_refdb*,
git_refdb_backend*>::func<git_refdb_set_backend>,
php_git2::local_pack<
php_git2::php_resource<php_git2::php_git_refdb>,
php_git2::php_git_refdb_backend
>
php_git2::php_git_refdb_backend_connector,
php_git2::php_resource<php_git2::php_git_refdb>
>,
-1,
php_git2::sequence<1,0>,
php_git2::sequence<1,0>,
php_git2::sequence<1,0>
>;

// Function entries:
Expand All @@ -155,6 +177,8 @@ static constexpr auto ZIF_GIT_REFDB_SET_BACKEND = zif_php_git2_function<
PHP_GIT2_FE(git_refdb_open,ZIF_GIT_REFDB_OPEN,NULL) \
PHP_GIT2_FE(git_refdb_set_backend,ZIF_GIT_REFDB_SET_BACKEND,NULL)

#endif

/*
* Local Variables:
* mode:c++
Expand Down

0 comments on commit 3d9092b

Please sign in to comment.