Add helper function `j()`. Fix bug. Refactor code.

This commit is contained in:
CismonX 2019-04-23 17:26:39 +08:00
parent 84717bb121
commit cf213d07c5
9 changed files with 124 additions and 42 deletions

View File

@ -9,6 +9,7 @@ if test "$PHP_ARMA" != "no"; then
ARMA_SRC=" \
src/php_arma.cc \
src/functions.cc \
src/constants.cc \
src/base.cc \
src/complex.cc \

View File

@ -23,8 +23,8 @@ namespace php_arma
zval *real, *imag;
ZEND_PARSE_PARAMETERS_START(0, 2)
Z_PARAM_OPTIONAL
Z_PARAM_ZVAL(real)
Z_PARAM_ZVAL(imag)
Z_PARAM_ZVAL_DEREF(real)
Z_PARAM_ZVAL_DEREF(imag)
ZEND_PARSE_PARAMETERS_END();
auto num_args = EX_NUM_ARGS();
@ -54,8 +54,8 @@ namespace php_arma
{
zval *r, *theta;
ZEND_PARSE_PARAMETERS_START(2, 2)
Z_PARAM_ZVAL(r)
Z_PARAM_ZVAL(theta)
Z_PARAM_ZVAL_DEREF(r)
Z_PARAM_ZVAL_DEREF(theta)
ZEND_PARSE_PARAMETERS_END();
if (!zval_check_scalar<T>(r)) {
@ -74,7 +74,7 @@ namespace php_arma
{
zval *other;
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_ZVAL(other)
Z_PARAM_ZVAL_DEREF(other)
ZEND_PARSE_PARAMETERS_END();
if (!operators::add(getThis(), other, return_value)) {
@ -87,7 +87,7 @@ namespace php_arma
{
zval *other;
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_ZVAL(other)
Z_PARAM_ZVAL_DEREF(other)
ZEND_PARSE_PARAMETERS_END();
if (!operators::sub(getThis(), other, return_value)) {
@ -106,7 +106,7 @@ namespace php_arma
{
zval *other;
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_ZVAL(other)
Z_PARAM_ZVAL_DEREF(other)
ZEND_PARSE_PARAMETERS_END();
if (!operators::mul(getThis(), other, return_value)) {
@ -119,7 +119,7 @@ namespace php_arma
{
zval *other;
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_ZVAL(other)
Z_PARAM_ZVAL_DEREF(other)
ZEND_PARSE_PARAMETERS_END();
if (!operators::div(getThis(), other, return_value)) {
@ -186,7 +186,7 @@ namespace php_arma
{
zval *other;
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_ZVAL(other)
Z_PARAM_ZVAL_DEREF(other)
ZEND_PARSE_PARAMETERS_END();
if (!operators::pow(getThis(), other, return_value)) {

35
src/functions.cc Normal file
View File

@ -0,0 +1,35 @@
//
// php-armadillo/functions.cc
//
// @Author CismonX
//
#include "functions.hh"
#include "complex.hh"
#define PHP_ARMA_FE(func) \
ZEND_RAW_FENTRY("Arma\\" #func, ZEND_FN(func), nullptr, 0)
namespace php_arma
{
#ifdef PHP_ARMA_OPERATORS
PHP_FUNCTION(j)
{
double imag = 0.0;
ZEND_PARSE_PARAMETERS_START(0, 1)
Z_PARAM_OPTIONAL
Z_PARAM_DOUBLE_DEREF(imag)
ZEND_PARSE_PARAMETERS_END();
zval_set_scalar(return_value, std::complex<double>(0.0, imag));
}
#endif // PHP_ARMA_OPERATORS
const zend_function_entry functions[] = {
#ifdef PHP_ARMA_OPERATORS
PHP_ARMA_FE(j)
#endif // PHP_ARMA_OPERATORS
PHP_FE_END
};
}

17
src/functions.hh Normal file
View File

@ -0,0 +1,17 @@
//
// php-armadillo/functions.hh
//
// @Author CismonX
//
#ifndef PHP_ARMA_FUNCTIONS_HH
#define PHP_ARMA_FUNCTIONS_HH
#include "php_arma.hh"
namespace php_arma
{
extern const zend_function_entry functions[];
}
#endif // !PHP_ARMA_FUNCTIONS_HH

View File

@ -34,7 +34,7 @@ namespace php_arma
{
zval *val;
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_ZVAL(val)
Z_PARAM_ZVAL_DEREF(val)
ZEND_PARSE_PARAMETERS_END();
if (!zval_check_scalar<T>(val)) {

View File

@ -9,6 +9,7 @@
#include "complex.hh"
#include "base.hh"
#include "mapval.hh"
#include "functions.hh"
#ifdef PHP_ARMA_OPERATORS
#include "operators.hh"
@ -16,39 +17,45 @@
#include <ext/standard/info.h>
PHP_MINIT_FUNCTION(arma)
namespace php_arma
{
php_arma::constants_init();
php_arma::complex_init();
php_arma::base_init();
php_arma::mapval_init();
int module_init(INIT_FUNC_ARGS)
{
constants_init();
complex_init();
base_init();
mapval_init();
#ifdef PHP_ARMA_OPERATORS
php_arma::operators_init();
operators_init();
#endif // PHP_ARMA_OPERATORS
return SUCCESS;
}
return SUCCESS;
}
PHP_MINFO_FUNCTION(arma)
{
php_info_print_table_start();
php_info_print_table_header(2, "armadillo support", "enabled");
php_info_print_table_end();
}
void module_info(ZEND_MODULE_INFO_FUNC_ARGS)
{
php_info_print_table_start();
php_info_print_table_header(2, "armadillo support", "enabled");
php_info_print_table_end();
}
zend_module_entry arma_module_entry = {
STANDARD_MODULE_HEADER,
"arma",
nullptr,
PHP_MINIT(arma),
nullptr,
nullptr,
nullptr,
PHP_MINFO(arma),
PHP_ARMA_VERSION,
STANDARD_MODULE_PROPERTIES
};
zend_module_entry module_entry = {
STANDARD_MODULE_HEADER,
"arma",
functions,
module_init,
nullptr,
nullptr,
nullptr,
module_info,
PHP_ARMA_VERSION,
STANDARD_MODULE_PROPERTIES
};
}
#ifdef COMPILE_DL_ARMA
ZEND_GET_MODULE(arma)
#endif
extern "C" ZEND_DLEXPORT zend_module_entry *get_module()
{
return &php_arma::module_entry;
}
#endif // COMPILE_DL_ARMA

View File

@ -22,8 +22,6 @@
#define ZEND_ACC_CTOR 0
#endif
extern zend_module_entry arma_module_entry;
/// Helper macros for method entry.
#define PHP_ARMA_START_ME(cls, ...) \
@ -65,6 +63,13 @@ extern zend_module_entry arma_module_entry;
static constexpr const char val[] = name; \
}
/// Helper macros for parameter parsing.
#ifndef Z_PARAM_DOUBLE_DEREF
#define Z_PARAM_DOUBLE_DEREF(dest) \
Z_PARAM_DOUBLE_EX2(dest, _dummy, 0, 1, 0)
#endif // !Z_PARAM_DOUBLE_DEREF
#ifdef PHP_ARMA_OPERATORS
/// Helper macros for handling operator overloading.
@ -82,6 +87,8 @@ extern zend_module_entry arma_module_entry;
namespace php_arma
{
extern zend_module_entry module_entry;
/// Helpers for compile-time string concatenation for better module startup performance.
namespace str
{

13
stubs/functions.php Normal file
View File

@ -0,0 +1,13 @@
<?php
namespace Arma;
/**
* Returns a CxDouble whose real part is zero.
*
* Helper function. Only defined when Features::OPERATORS is true.
*
* @param double $imag[optional]
* @return CxDouble
*/
function j($imag = 0.0) {}

View File

@ -13,8 +13,10 @@ if (is_php_arma_loaded()) {
require_once 'includes/assert.php';
$foo = new Arma\CxDouble(1.2, 1.7);
$bar = new Arma\CxDouble(2.3, 2.6);
use function Arma\j;
$foo = 1.2 + j(1.7);
$bar = 2.3 + j(2.6);
batch_assert('operator overloading of Arma\\Complex',
[$foo->add($bar), $foo + $bar],