fx: pointer: implement hash() and compare()
This commit is contained in:
@@ -1,17 +1,51 @@
|
|||||||
|
#include <fx/comparable.h>
|
||||||
#include <fx/pointer.h>
|
#include <fx/pointer.h>
|
||||||
#include <fx/value-type.h>
|
#include <fx/value-type.h>
|
||||||
|
#include <fx/value.h>
|
||||||
|
|
||||||
|
static fx_status hash(const fx_value *v, uint64_t *out_hash)
|
||||||
|
{
|
||||||
|
*out_hash = (uint64_t)v->v_pointer;
|
||||||
|
return FX_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
static i32 compare(const fx_value *left, const fx_value *right)
|
||||||
|
{
|
||||||
|
void *left_v = left->v_pointer;
|
||||||
|
void *right_v = 0;
|
||||||
|
if (!FX_OK(fx_value_get_pointer(right, &right_v))) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
uintptr_t left_p = (uintptr_t)left_v;
|
||||||
|
uintptr_t right_p = (uintptr_t)right_v;
|
||||||
|
|
||||||
|
if (left_p < right_p) {
|
||||||
|
return -1;
|
||||||
|
} else if (left_p > right_p) {
|
||||||
|
return 1;
|
||||||
|
} else {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/*** CLASS DEFINITION *********************************************************/
|
/*** CLASS DEFINITION *********************************************************/
|
||||||
|
|
||||||
FX_TYPE_CLASS_BEGIN(fx_pointer)
|
FX_TYPE_CLASS_BEGIN(fx_pointer)
|
||||||
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
|
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
|
||||||
FX_INTERFACE_ENTRY(to_string) = NULL;
|
FX_INTERFACE_ENTRY(to_string) = NULL;
|
||||||
|
FX_INTERFACE_ENTRY(hash) = hash;
|
||||||
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
|
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
|
||||||
|
|
||||||
|
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_comparable, FX_TYPE_COMPARABLE)
|
||||||
|
FX_INTERFACE_ENTRY(c_compare) = compare;
|
||||||
|
FX_TYPE_VTABLE_INTERFACE_END(fx_comparable, FX_TYPE_COMPARABLE)
|
||||||
FX_TYPE_CLASS_END(fx_pointer)
|
FX_TYPE_CLASS_END(fx_pointer)
|
||||||
|
|
||||||
FX_TYPE_DEFINITION_BEGIN(fx_pointer)
|
FX_TYPE_DEFINITION_BEGIN(fx_pointer)
|
||||||
__FX_VALUE_TYPE_ID(POINTER);
|
__FX_VALUE_TYPE_ID(POINTER);
|
||||||
FX_TYPE_EXTENDS(FX_TYPE_VALUE_TYPE);
|
FX_TYPE_EXTENDS(FX_TYPE_VALUE_TYPE);
|
||||||
|
FX_TYPE_IMPLEMENTS(FX_TYPE_COMPARABLE);
|
||||||
FX_TYPE_NAME("fx.pointer");
|
FX_TYPE_NAME("fx.pointer");
|
||||||
FX_TYPE_CLASS(fx_pointer_class);
|
FX_TYPE_CLASS(fx_pointer_class);
|
||||||
FX_TYPE_INSTANCE_PRIVATE(void *);
|
FX_TYPE_INSTANCE_PRIVATE(void *);
|
||||||
|
|||||||
Reference in New Issue
Block a user