This repository has been archived on 2020-06-07. You can view files and clone it, but cannot push or open issues or pull requests.
php-armadillo/src/mapval.hh
2019-04-13 18:17:01 +08:00

96 lines
2.4 KiB
C++

//
// php-armadillo/mapval.hh
//
// @Author CismonX
//
#ifndef PHP_ARMA_MAPVAL_HH
#define PHP_ARMA_MAPVAL_HH
#include "php_arma.hh"
// Forward declarations.
namespace arma
{
template <typename T>
class SpMat_MapMat_val;
template <typename T>
class SpSubview_MapMat_val;
}
namespace php_arma
{
template <typename T, bool IsSparse, bool IsSubview>
struct mapval
{
using sp_mapval_t = std::conditional_t<IsSubview,
arma::SpSubview_MapMat_val<T>, arma::SpMat_MapMat_val<T>>;
using native_t = std::conditional_t<IsSparse, sp_mapval_t, T*>;
friend void mapval_init();
zend_always_inline
static void set_val(zend_object *zobj, T val)
{
if constexpr (IsSparse) {
*to_native_object<native_t>(zobj) = val;
} else {
**to_native_object<native_t>(zobj) = val;
}
}
zend_always_inline
static zend_object *create(native_t init_val)
{
if constexpr (IsSparse) {
return object_create<native_t>(ce, [&init_val](auto obj) {
memcpy(obj, &init_val, sizeof(native_t));
return &handlers;
});
} else {
return object_create<native_t>(ce, [init_val](auto obj) {
*obj = init_val;
return &handlers;
});
}
}
PHP_ARMA_CE_HANDLRES_DECLARE();
private:
PHP_ARMA_COMMON_DECLARE();
static PHP_FUNCTION(val);
static PHP_FUNCTION(setTo);
static T get_val(zend_object*);
static void ce_init(zend_class_entry*);
};
template <typename T>
using mapval_dense = mapval<T, false, false>;
template <typename T>
using mapval_spmat = mapval<T, true, false>;
template <typename T>
using mapval_sp_subview = mapval<T, true, true>;
void mapval_init();
constexpr const char scalar_php_name[] = "Scalar";
constexpr const char mapval_php_name[] = "MapVal";
constexpr const char sp_mapval_php_name[] = "SpMapVal";
constexpr const char spsv_mapval_php_name[] = "SpSvMapVal";
inline zend_class_entry *scalar_ce;
inline zend_class_entry *mapval_ce;
inline zend_class_entry *sp_mapval_ce;
inline zend_class_entry *spsv_mapval_ce;
}
#endif // !PHP_ARMA_MAPVAL_HH