Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[meta] Automatic opaque serialization #1409

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
559 changes: 449 additions & 110 deletions distr/flecs.c

Large diffs are not rendered by default.

165 changes: 159 additions & 6 deletions distr/flecs.h
Original file line number Diff line number Diff line change
Expand Up @@ -15843,6 +15843,53 @@ typedef struct EcsOpaque {
void (*resize)(
void *dst,
size_t count);

/* Getter interface */

/** Get bool value */
bool (*get_bool)(
const void *src);

/** Get char value */
char (*get_char)(
const void *src);

/** Get int value */
int64_t (*get_int)(
const void *src);

/** Get unsigned int value */
uint64_t (*get_uint)(
const void *src);

/** Get float value */
double (*get_float)(
const void *src);

/** Get string value */
const char* (*get_string)(
const void *src);

/** Get entity value */
ecs_entity_t (*get_entity)(
const void *src,
const ecs_world_t *world);

/** Get (component) id value */
ecs_id_t (*get_id)(
const void *src,
const ecs_world_t *world);

/** get collection element */
const void* (*get_element)(
const void *src,
size_t elem);

/** get element */
const void* (*get_member)(
const void *src,
const char *member);

} EcsOpaque;


Expand Down Expand Up @@ -15977,6 +16024,15 @@ typedef struct ecs_meta_cursor_t {
void *lookup_ctx; /**< Context for lookup_action */
} ecs_meta_cursor_t;

/** Serializes an opaque
*/
FLECS_API
bool ecs_meta_serialize_opaque(
const ecs_serializer_t *serializer,
const void *src,
const EcsOpaque *opaque_info,
const ecs_world_t *world);

/** Create meta cursor.
* A meta cursor allows for walking over, reading and writing a value without
* having to know its type at compile time.
Expand All @@ -15997,13 +16053,22 @@ ecs_meta_cursor_t ecs_meta_cursor(
ecs_entity_t type,
void *ptr);

/** Get pointer to current field.
/** Get pointer to current field for writing.
*
* @param cursor The cursor.
* @return A pointer to the current field for writing.
*/
FLECS_API
void* ecs_meta_get_write_ptr(
ecs_meta_cursor_t *cursor);

/** Get pointer to current field for reading.
*
* @param cursor The cursor.
* @return A pointer to the current field.
* @return A pointer to the current field. NULL if element does not exist.
*/
FLECS_API
void* ecs_meta_get_ptr(
const void* ecs_meta_get_read_ptr(
ecs_meta_cursor_t *cursor);

/** Move cursor to next field.
Expand Down Expand Up @@ -16308,6 +16373,7 @@ ecs_entity_t ecs_meta_get_entity(
* @param cursor The cursor.
* @return The value of the current field.
*/
FLECS_API
ecs_id_t ecs_meta_get_id(
const ecs_meta_cursor_t *cursor);

Expand Down Expand Up @@ -19205,9 +19271,14 @@ struct cursor {
/** Get unit of value */
flecs::entity get_unit() const;

/** Get untyped pointer to value */
void* get_ptr() {
return ecs_meta_get_ptr(&cursor_);
/** Get untyped pointer to value for writing */
void* get_write_ptr() {
return ecs_meta_get_write_ptr(&cursor_);
}

/** Get untyped pointer to value for reading */
const void* get_read_ptr() {
return ecs_meta_get_read_ptr(&cursor_);
}

/** Set boolean value */
Expand Down Expand Up @@ -19469,6 +19540,88 @@ struct opaque {
return *this;
}

/* Getter interface */

/** Get bool value */
opaque& get_bool(bool (*func)(const T *src)) {
this->desc.type.get_bool =
reinterpret_cast<decltype(
this->desc.type.get_bool)>(func);
return *this;
}

/** Get char value */
opaque& get_char(char (*func)(const T *src)) {
this->desc.type.get_char =
reinterpret_cast<decltype(
this->desc.type.get_char)>(func);
return *this;
}

/** Get int value */
opaque& get_int(int64_t (*func)(const T *src)) {
this->desc.type.get_int =
reinterpret_cast<decltype(
this->desc.type.get_int)>(func);
return *this;
}

/** Get unsigned int value */
opaque& get_uint(uint64_t (*func)(const T *src)) {
this->desc.type.get_uint =
reinterpret_cast<decltype(
this->desc.type.get_uint)>(func);
return *this;
}

/** Get float value */
opaque& get_float(double (*func)(const T *src)) {
this->desc.type.get_float =
reinterpret_cast<decltype(
this->desc.type.get_float)>(func);
return *this;
}

/** Get string value */
opaque& get_string(const char* (*func)(const T *src)) {
this->desc.type.get_string =
reinterpret_cast<decltype(
this->desc.type.get_string)>(func);
return *this;
}

/** Get entity value */
opaque& get_entity(ecs_entity_t (*func)(const T *src, const flecs::world_t *world)) {
this->desc.type.get_entity =
reinterpret_cast<decltype(
this->desc.type.get_entity)>(func);
return *this;
}

/** Get (component) id value */
opaque& get_id(ecs_id_t (*func)(const T *src, const flecs::world_t *world)) {
this->desc.type.get_id =
reinterpret_cast<decltype(
this->desc.type.get_id)>(func);
return *this;
}

/** Get collection element */
opaque& get_element(const void* (*func)(const T *src, size_t elem)) {
this->desc.type.get_element =
reinterpret_cast<decltype(
this->desc.type.get_element)>(func);
return *this;
}

/** get element */
opaque& get_member(const void* (*func)(const T *src, const char *member)) {
this->desc.type.get_member =
reinterpret_cast<decltype(
this->desc.type.get_member)>(func);
return *this;
}

~opaque() {
if (world) {
ecs_opaque_init(world, &desc);
Expand Down
11 changes: 8 additions & 3 deletions include/flecs/addons/cpp/mixins/meta/cursor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,14 @@ struct cursor {
/** Get unit of value */
flecs::entity get_unit() const;

/** Get untyped pointer to value */
void* get_ptr() {
return ecs_meta_get_ptr(&cursor_);
/** Get untyped pointer to value for writing */
void* get_write_ptr() {
return ecs_meta_get_write_ptr(&cursor_);
}

/** Get untyped pointer to value for reading */
const void* get_read_ptr() {
return ecs_meta_get_read_ptr(&cursor_);
}

/** Set boolean value */
Expand Down
82 changes: 82 additions & 0 deletions include/flecs/addons/cpp/mixins/meta/opaque.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,88 @@ struct opaque {
return *this;
}

/* Getter interface */

/** Get bool value */
opaque& get_bool(bool (*func)(const T *src)) {
this->desc.type.get_bool =
reinterpret_cast<decltype(
this->desc.type.get_bool)>(func);
return *this;
}

/** Get char value */
opaque& get_char(char (*func)(const T *src)) {
this->desc.type.get_char =
reinterpret_cast<decltype(
this->desc.type.get_char)>(func);
return *this;
}

/** Get int value */
opaque& get_int(int64_t (*func)(const T *src)) {
this->desc.type.get_int =
reinterpret_cast<decltype(
this->desc.type.get_int)>(func);
return *this;
}

/** Get unsigned int value */
opaque& get_uint(uint64_t (*func)(const T *src)) {
this->desc.type.get_uint =
reinterpret_cast<decltype(
this->desc.type.get_uint)>(func);
return *this;
}

/** Get float value */
opaque& get_float(double (*func)(const T *src)) {
this->desc.type.get_float =
reinterpret_cast<decltype(
this->desc.type.get_float)>(func);
return *this;
}

/** Get string value */
opaque& get_string(const char* (*func)(const T *src)) {
this->desc.type.get_string =
reinterpret_cast<decltype(
this->desc.type.get_string)>(func);
return *this;
}

/** Get entity value */
opaque& get_entity(ecs_entity_t (*func)(const T *src, const flecs::world_t *world)) {
this->desc.type.get_entity =
reinterpret_cast<decltype(
this->desc.type.get_entity)>(func);
return *this;
}

/** Get (component) id value */
opaque& get_id(ecs_id_t (*func)(const T *src, const flecs::world_t *world)) {
this->desc.type.get_id =
reinterpret_cast<decltype(
this->desc.type.get_id)>(func);
return *this;
}

/** Get collection element */
opaque& get_element(const void* (*func)(const T *src, size_t elem)) {
this->desc.type.get_element =
reinterpret_cast<decltype(
this->desc.type.get_element)>(func);
return *this;
}

/** get element */
opaque& get_member(const void* (*func)(const T *src, const char *member)) {
this->desc.type.get_member =
reinterpret_cast<decltype(
this->desc.type.get_member)>(func);
return *this;
}

~opaque() {
if (world) {
ecs_opaque_init(world, &desc);
Expand Down
Loading
Loading