This commit is contained in:
CismonX 2019-03-20 22:55:19 +08:00
parent bc8b5398ef
commit 0ef27a3e75
9 changed files with 123 additions and 137 deletions

24
.travis.yml Normal file
View File

@ -0,0 +1,24 @@
dist: xenial
group: edge
language: php
php:
- 7.1
- 7.2
- 7.3
- nightly
before_install:
- sudo add-apt-repository -y ppa:ubuntu-toolchain-r/test
- sudo apt-get update -qq
install:
- sudo apt-get install -qq g++-7
- sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-7 90
script:
- phpize
- ./configure
- make
- make test

View File

@ -8,5 +8,5 @@ if test "$PHP_ARMA" != "no"; then
src/interfaces.cc \
src/fill.cc \
src/complex.cc"
PHP_NEW_EXTENSION(arma, $ARMA_SRC, $ext_shared, , -std=c++14)
PHP_NEW_EXTENSION(arma, $ARMA_SRC, $ext_shared, , -std=c++17)
fi

View File

@ -9,7 +9,7 @@
namespace php_arma
{
template <typename T>
PHP_METHOD(Complex, __construct)
ZEND_NAMED_FUNCTION(complex<T>::__construct)
{
zval *real, *imag;
ZEND_PARSE_PARAMETERS_START(0, 2)
@ -48,12 +48,8 @@ namespace php_arma
Z_OBJ_HT_P(current) = PHP_ARMA_HANDLERS(complex, T);
}
PHP_ARMA_START_ME_1(complex, T)
PHP_ARMA_ME(Complex, __construct, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR, T)
PHP_ARMA_END_ME_1(complex, T)
template <typename T>
void complex_write_property(zval* obj, zval* member, zval* value, void** unused)
void complex<T>::write_property(zval *obj, zval *member, zval *value, void **unused)
{
// Theoretically we won't get non-string values here, add type check just in case.
if (UNEXPECTED(Z_TYPE_P(member) != IS_STRING)) {
@ -78,26 +74,19 @@ namespace php_arma
}
template <typename T>
void complex_ce_init(zend_class_entry *parent_ce)
void complex<T>::ce_init(zend_class_entry *parent_ce)
{
PHP_ARMA_CE(complex, T) = class_register("CxDouble", parent_ce, PHP_ARMA_METHODS(complex, T));
property_declare(PHP_ARMA_CE(complex, T), "real");
property_declare(PHP_ARMA_CE(complex, T), "imag");
object_handlers_init(PHP_ARMA_HANDLERS(complex, T));
PHP_ARMA_HANDLER_SET(complex, write_property, complex_write_property, T);
ce = class_register("CxDouble", parent_ce, me);
property_declare(ce, "real");
property_declare(ce, "imag");
object_handlers_init(&handlers);
handlers.write_property = write_property;
}
void complex_init()
{
complex_ce = abstract_class_register("Complex");
complex_ce_init<double>(complex_ce);
complex<double>::ce_init(complex_ce);
}
zend_class_entry *complex_ce;
template <typename T>
PHP_ARMA_DEFINE_CE(complex, T);
template <typename T>
PHP_ARMA_DEFINE_OBJECT_HANDLER(complex, T);
}

View File

@ -11,19 +11,34 @@
#include <complex>
typedef std::complex<double> cx_double;
using cx_double = std::complex<double>;
namespace php_arma
{
template <typename T>
PHP_METHOD(Complex, __construct);
struct complex
{
friend void complex_init();
PHP_ARMA_CE_DECLARE();
PHP_ARMA_HANDLERS_DECLARE();
private:
static ZEND_NAMED_FUNCTION(__construct);
static void write_property(zval*, zval*, zval*, void**);
zend_always_inline
static void ce_init(zend_class_entry*);
PHP_ARMA_START_ME()
PHP_ARMA_ME(__construct, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
PHP_ARMA_END_ME();
};
void complex_init();
extern zend_class_entry *complex_ce;
template <typename T>
PHP_ARMA_DECLARE_CE_HANDLERS(complex);
inline zend_class_entry *complex_ce;
/// Helper functions for handling cx_double types.
@ -34,8 +49,9 @@ namespace php_arma
}
template <>
struct scalar_type<cx_double> {
static const int value = IS_OBJECT;
struct scalar_type<cx_double>
{
static constexpr auto value = IS_OBJECT;
};
template <> zend_always_inline

View File

@ -19,6 +19,4 @@ namespace php_arma
const_declare(fill_ce, "RANDU", fill_randu);
const_declare(fill_ce, "RANDN", fill_randn);
}
zend_class_entry *fill_ce;
}

View File

@ -20,7 +20,7 @@ namespace php_arma
void fill_init();
extern zend_class_entry *fill_ce;
inline zend_class_entry *fill_ce;
}
#endif //!PHP_ARMA_FILL_HH

View File

@ -5,11 +5,26 @@
//
#include "interfaces.hh"
#include "php_arma.hh"
#define N_INT "Internal\\"
namespace php_arma
{
zend_always_inline
zend_class_entry *interface_register(const char *name)
{
// Although methods are declared in interfaces as you see in the stubs,
// nothing is declared in the internal interface implementation.
return ce_init(name, nullptr, zend_register_internal_class);
}
template <typename... Ts>
zend_class_entry *interface_register(const char *name, Ts... parents)
{
return ce_init(name, nullptr, zend_register_internal_class, parents...);
}
void internal_interfaces_init()
{
base_ce = interface_register(N_INT "Base");
@ -63,26 +78,4 @@ namespace php_arma
scalar_ce = interface_register(N_INT "Scalar",
subview_ce);
}
zend_class_entry *base_ce;
zend_class_entry *vector_ce, *matrix_ce;
zend_class_entry *dense_ce, *sparse_ce;
zend_class_entry *dense_vector_ce, *dense_matrix_ce;
zend_class_entry *sparse_vector_ce, *sparse_matrix_ce;
zend_class_entry *resizable_ce;
zend_class_entry *resizable_vector_ce, *resizable_matrix_ce;
zend_class_entry *dense_resizable_vector_ce, *dense_resizable_matrix_ce;
zend_class_entry *sparse_resizable_vector_ce, *sparse_resizable_matrix_ce;
zend_class_entry *non_resizable_ce;
zend_class_entry *non_resizable_vector_ce, *non_resizable_matrix_ce;
zend_class_entry *dense_non_resizable_vector_ce, *dense_non_resizable_matrix_ce;
zend_class_entry *sparse_non_resizable_matrix_ce;
zend_class_entry *subview_ce;
zend_class_entry *diagonal_ce;
zend_class_entry *scalar_ce;
}

View File

@ -7,47 +7,33 @@
#ifndef PHP_ARMA_INTERFACES_HH
#define PHP_ARMA_INTERFACES_HH
#include "php_arma.hh"
#include <php.h>
namespace php_arma
{
zend_always_inline
zend_class_entry *interface_register(const char *name)
{
// Although methods are declared in interfaces as you see in the stubs,
// nothing is declared in the internal interface implementation.
return ce_init(name, nullptr, zend_register_internal_class);
}
template <typename... Ts>
zend_class_entry *interface_register(const char *name, Ts... parents)
{
return ce_init(name, nullptr, zend_register_internal_class, parents...);
}
void internal_interfaces_init();
extern zend_class_entry *base_ce;
inline zend_class_entry *base_ce;
extern zend_class_entry *vector_ce, *matrix_ce;
inline zend_class_entry *vector_ce, *matrix_ce;
extern zend_class_entry *dense_ce, *sparse_ce;
extern zend_class_entry *dense_vector_ce, *dense_matrix_ce;
extern zend_class_entry *sparse_vector_ce, *sparse_matrix_ce;
inline zend_class_entry *dense_ce, *sparse_ce;
inline zend_class_entry *dense_vector_ce, *dense_matrix_ce;
inline zend_class_entry *sparse_vector_ce, *sparse_matrix_ce;
extern zend_class_entry *resizable_ce;
extern zend_class_entry *resizable_vector_ce, *resizable_matrix_ce;
extern zend_class_entry *dense_resizable_vector_ce, *dense_resizable_matrix_ce;
extern zend_class_entry *sparse_resizable_vector_ce, *sparse_resizable_matrix_ce;
inline zend_class_entry *resizable_ce;
inline zend_class_entry *resizable_vector_ce, *resizable_matrix_ce;
inline zend_class_entry *dense_resizable_vector_ce, *dense_resizable_matrix_ce;
inline zend_class_entry *sparse_resizable_vector_ce, *sparse_resizable_matrix_ce;
extern zend_class_entry *non_resizable_ce;
extern zend_class_entry *non_resizable_vector_ce, *non_resizable_matrix_ce;
extern zend_class_entry *dense_non_resizable_vector_ce, *dense_non_resizable_matrix_ce;
extern zend_class_entry *sparse_non_resizable_matrix_ce;
inline zend_class_entry *non_resizable_ce;
inline zend_class_entry *non_resizable_vector_ce, *non_resizable_matrix_ce;
inline zend_class_entry *dense_non_resizable_vector_ce, *dense_non_resizable_matrix_ce;
inline zend_class_entry *sparse_non_resizable_matrix_ce;
extern zend_class_entry *subview_ce;
extern zend_class_entry *diagonal_ce;
extern zend_class_entry *scalar_ce;
inline zend_class_entry *subview_ce;
inline zend_class_entry *diagonal_ce;
inline zend_class_entry *scalar_ce;
}
#endif //!PHP_ARMA_INTERFACES_HH

View File

@ -26,57 +26,35 @@ extern zend_module_entry arma_module_entry;
/// Helper macros for method entry.
#define Z_OBJNAME_P(zval_p) \
#define Z_OBJNAME_P(zval_p) \
ZSTR_VAL(Z_OBJCE_P(zval_p)->name)
#define PHP_ARMA_START_ME_1(name, t1) \
template <typename t1> \
struct name##_me \
{ \
static constexpr zend_function_entry const val[] = {
#define PHP_ARMA_START_ME() \
static constexpr zend_function_entry const me[] = {
#define PHP_ARMA_END_ME_1(name, t1) \
PHP_FE_END \
}; \
}; \
template <typename t1> \
constexpr zend_function_entry const name##_me<t1>::val[];
#define PHP_ARMA_END_ME() \
PHP_FE_END \
}
#define PHP_ARMA_ME(cls, func, flags, ...) \
ZEND_FENTRY(func, ZEND_MN(cls##_##func<__VA_ARGS__>), nullptr, flags)
#define PHP_ARMA_ME(func, flags) \
ZEND_FENTRY(func, func, nullptr, flags)
#define PHP_ARMA_METHODS(name, ...) \
name##_me<__VA_ARGS__>::val
#define PHP_ARMA_METHODS(cls, ...) \
cls<__VA_ARGS__>::me
/// Helper macros for class entry and object handlers
#define PHP_ARMA_DECLARE_CE(name) \
struct name##_components \
{ \
static zend_class_entry *ce; \
}
#define PHP_ARMA_CE_DECLARE() \
static inline zend_class_entry *ce
#define PHP_ARMA_DECLARE_CE_HANDLERS(name) \
struct name##_components \
{ \
static zend_class_entry *ce; \
static zend_object_handlers handlers; \
}
#define PHP_ARMA_HANDLERS_DECLARE() \
static inline zend_object_handlers handlers
#define PHP_ARMA_DEFINE_CE(name, ...) \
zend_class_entry *name##_components<__VA_ARGS__>::ce
#define PHP_ARMA_CE(cls, ...) \
cls<__VA_ARGS__>::ce
#define PHP_ARMA_DEFINE_OBJECT_HANDLER(name, ...) \
zend_object_handlers name##_components<__VA_ARGS__>::handlers
#define PHP_ARMA_CE(name, ...) \
name##_components<__VA_ARGS__>::ce
#define PHP_ARMA_HANDLERS(name, ...) \
&name##_components<__VA_ARGS__>::handlers
#define PHP_ARMA_HANDLER_SET(name, handler, func, ...) \
(PHP_ARMA_HANDLERS(name, __VA_ARGS__))->handler = func<__VA_ARGS__>
#define PHP_ARMA_HANDLERS(cls, ...) \
&cls<__VA_ARGS__>::handlers
namespace php_arma
{
@ -153,7 +131,7 @@ namespace php_arma
return reinterpret_cast<T*>(zobj) - 1;
}
/// Helper functions for creating objects.
/// Helper functions for handling objects.
template <typename T, typename F>
zend_object *object_create(zend_class_entry *ce, F init)
@ -178,6 +156,13 @@ namespace php_arma
return zobj;
}
zend_always_inline
void object_set_property(zend_object *obj, size_t idx, zval *val)
{
zval_ptr_dtor(OBJ_PROP_NUM(obj, idx));
ZVAL_COPY(OBJ_PROP_NUM(obj, idx), val);
}
/// Helper functions for handling scalar types.
template <typename>
@ -211,13 +196,15 @@ namespace php_arma
struct scalar_type {};
template <>
struct scalar_type<double> {
static const int value = IS_DOUBLE;
struct scalar_type<double>
{
static constexpr auto value = IS_DOUBLE;
};
template <>
struct scalar_type<zend_long> {
static const int value = IS_LONG;
struct scalar_type<zend_long>
{
static constexpr auto value = IS_LONG;
};
template <typename T>
@ -295,13 +282,6 @@ namespace php_arma
}
return true;
}
zend_always_inline
void object_set_property(zend_object *obj, size_t idx, zval *val)
{
zval_ptr_dtor(OBJ_PROP_NUM(obj, idx));
ZVAL_COPY(OBJ_PROP_NUM(obj, idx), val);
}
}
#endif // !PHP_ARMA_HH