Created
January 25, 2019 20:22
-
-
Save IngwiePhoenix/a69813ca5204f0a7995a1e28ff93d710 to your computer and use it in GitHub Desktop.
gravity-c++-wrapper-ideas
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Gravity { | |
<template T> | |
class TypedArray { | |
private: | |
typedef marray_t(T) array_struct; | |
array_struct arr; | |
public: | |
TypedArray() { marray_init(this->arr); } | |
TypedArray(array_struct arr) { this->arr = arr; } | |
~TypedArray() { marray_destroy(this->arr); } | |
uint32_t size() { return marray_size(this->arr); } | |
uint32_t max() { return marray_max(this->arr); } | |
void inc() { marray_inc(this->arr); } | |
void dec() { marray_dec(this->arr); } | |
void n(uint32_t i) { marray_nset(this->arr, i); } | |
void reset() { marray_reset0(this->arr); } | |
void resize(uint32_t i) { marray_resize(T, this->arr, i); } | |
void push(T value) { marray_push(T, this->arr, value); } | |
void pop() { marray_pop(this->arr); } | |
void popMany(uint32_t i) { marray_npop(this->arr, i); } | |
void getLast() { marray_last(this->arr); } | |
void operator [] (uint32_t idx, T value) { | |
marray_set(this->arr, idx, value); | |
} | |
T operator [] (uint32_t idx) { | |
return marray_get(this->arr, idx); | |
} | |
array_struct raw() { return this->arr; } | |
}; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Gravity { | |
class HashTable { | |
private: | |
gravity_hash_t* ht; | |
gravity_hash_compute_fn compute_cb; | |
gravity_hash_isequal_fn isequal_cb; | |
ravity_hash_iterate_fn iterate_cb; | |
void *xdata; | |
public: | |
// Good lord is this UGLY... | |
HashTable( | |
uint32_t size, | |
gravity_hash_compute_fn compare_cb, | |
gravity_hash_isequal_fn isequal_cb, | |
ravity_hash_iterate_fn iterate_cb | |
) | |
: ht(NULL) | |
, compare_cb(compare_cb) | |
, isequal_cb(isequal_cb) | |
, iterate_cb(iterate_cb) | |
{ | |
this->ht = gravity_hash_iterate_fn( | |
size, | |
this->compare_cb, | |
this->isequal_cb, | |
this->iterate_cb | |
); | |
} | |
~HashTable() { | |
gravity_hash_free(this->ht); | |
} | |
bool isEmpty() { | |
return gravity_hash_isempty(this->ht); | |
} | |
bool remove(gravity_value_t key) { | |
return gravity_hash_remove(this->ht, key); | |
} | |
// @TODO Can []-overloaded operator return a bool? o.o | |
void operator [] (gravity_value_t key, gravity_value_t value) { | |
gravity_hash_insert(this->ht, key, value); | |
} | |
gravity_value_t operator [] (gravity_value_t key) { | |
return gravity_hash_lookup(this->ht, key); | |
} | |
gravity_value_t operator [] (const char* key) { | |
return gravity_hash_lookup_cstring(this->ht, key); | |
} | |
// @TODO Utilize something like std::iterator... | |
void iterate(gravity_hash_iterate_fn cb, void *data) { | |
gravity_hash_iterate( | |
this->ht, | |
cb? cb : this->iterate_cb, | |
data? data : this->data | |
); | |
} | |
void iterate(gravity_hash_iterate_fn cb, void *data1, void* data2) { | |
gravity_hash_iterate2( | |
this->ht, | |
cb? cb : this->iterate_cb, | |
data1? data1 : this->data, | |
data2? data2 : this->data | |
); | |
} | |
void transform(gravity_hash_transform cb, void *data) { | |
gravity_hash_transform(this->ht, cb, data? data : this->data); | |
} | |
void append(gravity_hash_t* target) { | |
gravity_hash_append(this->ht, target); | |
} | |
bool compare( | |
gravity_hash_t* target, | |
gravity_hash_compare_fn compare, | |
void *data | |
) { | |
return gravity_hash_compare( | |
this->ht, | |
target, | |
compare, | |
data ? data : this->data | |
); | |
} | |
operator gravity_hash_t*() { | |
return this->ht; | |
} | |
}; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Gravity { | |
// This one is VERY. MUCH. undone. | |
class Value { | |
private: | |
gravity_value_t v; | |
public: | |
// Helper macro | |
#define ENABLE_TYPE(type) \ | |
Value(type); \ | |
operator type(); \ | |
operator =(type) | |
// Prototype methods... | |
ENABLE_TYPE(int); | |
ENABLE_TYPE(double); | |
ENABLE_TYPE(float); | |
ENABLE_TYPE(const char*); | |
ENABLE_TYPE(std::string); | |
ENABLE_TYPE(gravity_class_t); | |
ENABLE_TYPE(gravity_object_t); | |
ENABLE_TYPE(gravity_closure_t); | |
ENABLE_TYPE(gravity_function_t); | |
#undef ENABLE_TYPE | |
bool isNummeric(); | |
bool isString(); | |
bool isObject(); | |
bool isClass(); | |
bool isClosure(); | |
bool isFunction(); | |
char* dump(); | |
std::string dump(); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment