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] Getter interface for opaque collection types #1405

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
335 changes: 225 additions & 110 deletions distr/flecs.c

Large diffs are not rendered by default.

156 changes: 150 additions & 6 deletions distr/flecs.h
Original file line number Diff line number Diff line change
Expand Up @@ -15400,6 +15400,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 @@ -15554,13 +15601,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 @@ -15865,6 +15921,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 @@ -18770,9 +18827,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 @@ -19034,6 +19096,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
63 changes: 60 additions & 3 deletions include/flecs/addons/meta.h
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,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 @@ -611,13 +658,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 @@ -922,6 +978,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
Loading
Loading