Flecs v3.2
A fast entity component system (ECS) for C & C++
Loading...
Searching...
No Matches
Expr

Serialize/deserialize values to string. More...

Classes

struct  ecs_expr_var_t
 Storage for parser variables. More...
 
struct  ecs_expr_var_scope_t
 
struct  ecs_vars_t
 
struct  ecs_parse_expr_desc_t
 Used with ecs_parse_expr. More...
 

Typedefs

typedef struct ecs_expr_var_t ecs_expr_var_t
 Storage for parser variables. More...
 
typedef struct ecs_expr_var_scope_t ecs_expr_var_scope_t
 
typedef struct ecs_vars_t ecs_vars_t
 
typedef struct ecs_parse_expr_desc_t ecs_parse_expr_desc_t
 Used with ecs_parse_expr.
 

Functions

FLECS_API char * ecs_chresc (char *out, char in, char delimiter)
 Write an escaped character. More...
 
const char * ecs_chrparse (const char *in, char *out)
 Parse an escaped character. More...
 
FLECS_API ecs_size_t ecs_stresc (char *out, ecs_size_t size, char delimiter, const char *in)
 Write an escaped string. More...
 
FLECS_API char * ecs_astresc (char delimiter, const char *in)
 Return escaped string. More...
 
FLECS_API void ecs_vars_init (ecs_world_t *world, ecs_vars_t *vars)
 Init variable storage.
 
FLECS_API void ecs_vars_fini (ecs_vars_t *vars)
 Cleanup variable storage.
 
FLECS_API void ecs_vars_push (ecs_vars_t *vars)
 Push variable scope.
 
FLECS_API int ecs_vars_pop (ecs_vars_t *vars)
 Pop variable scope.
 
FLECS_API ecs_expr_var_tecs_vars_declare (ecs_vars_t *vars, const char *name, ecs_entity_t type)
 Declare variable in current scope.
 
FLECS_API ecs_expr_var_tecs_vars_declare_w_value (ecs_vars_t *vars, const char *name, ecs_value_t *value)
 Declare variable in current scope from value. More...
 
FLECS_API ecs_expr_var_tecs_vars_lookup (const ecs_vars_t *vars, const char *name)
 Lookup variable in scope and parent scopes.
 
FLECS_API const char * ecs_parse_expr (ecs_world_t *world, const char *ptr, ecs_value_t *value, const ecs_parse_expr_desc_t *desc)
 Parse expression into value. More...
 
FLECS_API char * ecs_ptr_to_expr (const ecs_world_t *world, ecs_entity_t type, const void *data)
 Serialize value into expression string. More...
 
FLECS_API int ecs_ptr_to_expr_buf (const ecs_world_t *world, ecs_entity_t type, const void *data, ecs_strbuf_t *buf)
 Serialize value into expression buffer. More...
 
FLECS_API char * ecs_ptr_to_str (const ecs_world_t *world, ecs_entity_t type, const void *data)
 Similar as ecs_ptr_to_expr, but serializes values to string. More...
 
FLECS_API int ecs_ptr_to_str_buf (const ecs_world_t *world, ecs_entity_t type, const void *data, ecs_strbuf_t *buf)
 Serialize value into string buffer. More...
 
FLECS_API int ecs_primitive_to_expr_buf (const ecs_world_t *world, ecs_primitive_kind_t kind, const void *data, ecs_strbuf_t *buf)
 Serialize primitive value into string buffer. More...
 
FLECS_API const char * ecs_parse_expr_token (const char *name, const char *expr, const char *ptr, char *token)
 Parse expression token. More...
 
FLECS_API char * ecs_interpolate_string (ecs_world_t *world, const char *str, const ecs_vars_t *vars)
 Evaluate interpolated expressions in string. More...
 
FLECS_API void ecs_iter_to_vars (const ecs_iter_t *it, ecs_vars_t *vars, int offset)
 Convert iterator to vars This operation converts an iterator to a variable array. More...
 

Detailed Description

Serialize/deserialize values to string.

Typedef Documentation

◆ ecs_expr_var_t

Storage for parser variables.

Variables make it possible to parameterize expression strings, and are referenced with the $ operator (e.g. $var).

Function Documentation

◆ ecs_astresc()

FLECS_API char * ecs_astresc ( char  delimiter,
const char *  in 
)

Return escaped string.

Return escaped version of input string. Same as ecs_stresc, but returns an allocated string of the right size.

Parameters
delimiterThe delimiter used (for example '"').
inThe input string.
Returns
Escaped string.

◆ ecs_chresc()

FLECS_API char * ecs_chresc ( char *  out,
char  in,
char  delimiter 
)

Write an escaped character.

Write a character to an output string, insert escape character if necessary.

Parameters
outThe string to write the character to.
inThe input character.
delimiterThe delimiiter used (for example '"')
Returns
Pointer to the character after the last one written.

◆ ecs_chrparse()

const char * ecs_chrparse ( const char *  in,
char *  out 
)

Parse an escaped character.

Parse a character with a potential escape sequence.

Parameters
inPointer to character in input string.
outOutput string.
Returns
Pointer to the character after the last one read.

◆ ecs_interpolate_string()

FLECS_API char * ecs_interpolate_string ( ecs_world_t world,
const char *  str,
const ecs_vars_t vars 
)

Evaluate interpolated expressions in string.

This operation evaluates expressions in a string, and replaces them with their evaluated result. Supported expression formats are:

  • $variable_name
  • {expression}

The $, { and } characters can be escaped with a backslash ().

Parameters
worldThe world.
strThe string to evaluate.
varsThe variables to use for evaluation.

◆ ecs_iter_to_vars()

FLECS_API void ecs_iter_to_vars ( const ecs_iter_t *  it,
ecs_vars_t vars,
int  offset 
)

Convert iterator to vars This operation converts an iterator to a variable array.

This allows for using iterator results in expressions. The operation only converts a single result at a time, and does not progress the iterator.

Iterator fields with data will be made available as variables with as name the field index (e.g. "$1"). The operation does not check if reflection data is registered for a field type. If no reflection data is registered for the type, using the field variable in expressions will fail.

Field variables will only contain single elements, even if the iterator returns component arrays. The offset parameter can be used to specify which element in the component arrays to return. The offset parameter must be smaller than it->count.

The operation will create a variable for query variables that contain a single entity.

The operation will attempt to use existing variables. If a variable does not yet exist, the operation will create it. If an existing variable exists with a mismatching type, the operation will fail.

Accessing variables after progressing the iterator or after the iterator is destroyed will result in undefined behavior.

If vars contains a variable that is not present in the iterator, the variable will not be modified.

Parameters
itThe iterator to convert to variables.
varsThe variables to write to.
offsetThe offset to the current element.

◆ ecs_parse_expr()

FLECS_API const char * ecs_parse_expr ( ecs_world_t world,
const char *  ptr,
ecs_value_t value,
const ecs_parse_expr_desc_t desc 
)

Parse expression into value.

This operation parses a flecs expression into the provided pointer. The memory pointed to must be large enough to contain a value of the used type.

If no type and pointer are provided for the value argument, the operation will discover the type from the expression and allocate storage for the value. The allocated value must be freed with ecs_value_free.

Parameters
worldThe world.
ptrThe pointer to the expression to parse.
valueThe value containing type & pointer to write to.
descConfiguration parameters for deserializer.
Returns
Pointer to the character after the last one read, or NULL if failed.

◆ ecs_parse_expr_token()

FLECS_API const char * ecs_parse_expr_token ( const char *  name,
const char *  expr,
const char *  ptr,
char *  token 
)

Parse expression token.

Expression tokens can contain more characters (such as '|') than tokens parsed by the query (term) parser.

Parameters
nameThe name of the expression (used for debug logs).
exprThe full expression (used for debug logs).
ptrThe pointer to the expression to parse.
tokenThe buffer to write to (must have size ECS_MAX_TOKEN_SIZE)
Returns
Pointer to the character after the last one read, or NULL if failed.

◆ ecs_primitive_to_expr_buf()

FLECS_API int ecs_primitive_to_expr_buf ( const ecs_world_t world,
ecs_primitive_kind_t  kind,
const void *  data,
ecs_strbuf_t *  buf 
)

Serialize primitive value into string buffer.

Serializes a primitive value to an ecs_strbuf_t instance. This operation can be reused by other serializers to avoid having to write boilerplate code that serializes primitive values to a string.

Parameters
worldThe world.
kindThe kind of primitive value.
dataThe value ot serialize
bufThe strbuf to append the string to.
Returns
Zero if success, non-zero if failed.

◆ ecs_ptr_to_expr()

FLECS_API char * ecs_ptr_to_expr ( const ecs_world_t world,
ecs_entity_t  type,
const void *  data 
)

Serialize value into expression string.

This operation serializes a value of the provided type to a string. The memory pointed to must be large enough to contain a value of the used type.

Parameters
worldThe world.
typeThe type of the value to serialize.
dataThe value to serialize.
Returns
String with expression, or NULL if failed.

◆ ecs_ptr_to_expr_buf()

FLECS_API int ecs_ptr_to_expr_buf ( const ecs_world_t world,
ecs_entity_t  type,
const void *  data,
ecs_strbuf_t *  buf 
)

Serialize value into expression buffer.

Same as ecs_ptr_to_expr, but serializes to an ecs_strbuf_t instance.

Parameters
worldThe world.
typeThe type of the value to serialize.
dataThe value to serialize.
bufThe strbuf to append the string to.
Returns
Zero if success, non-zero if failed.

◆ ecs_ptr_to_str()

FLECS_API char * ecs_ptr_to_str ( const ecs_world_t world,
ecs_entity_t  type,
const void *  data 
)

Similar as ecs_ptr_to_expr, but serializes values to string.

Whereas the output of ecs_ptr_to_expr is a valid expression, the output of ecs_ptr_to_str is a string representation of the value. In most cases the output of the two operations is the same, but there are some differences:

  • Strings are not quoted
Parameters
worldThe world.
typeThe type of the value to serialize.
dataThe value to serialize.
Returns
String with result, or NULL if failed.

◆ ecs_ptr_to_str_buf()

FLECS_API int ecs_ptr_to_str_buf ( const ecs_world_t world,
ecs_entity_t  type,
const void *  data,
ecs_strbuf_t *  buf 
)

Serialize value into string buffer.

Same as ecs_ptr_to_str, but serializes to an ecs_strbuf_t instance.

Parameters
worldThe world.
typeThe type of the value to serialize.
dataThe value to serialize.
bufThe strbuf to append the string to.
Returns
Zero if success, non-zero if failed.

◆ ecs_stresc()

FLECS_API ecs_size_t ecs_stresc ( char *  out,
ecs_size_t  size,
char  delimiter,
const char *  in 
)

Write an escaped string.

Write an input string to an output string, escape characters where necessary. To determine the size of the output string, call the operation with a NULL argument for 'out', and use the returned size to allocate a string that is large enough.

Parameters
outPointer to output string (msut be).
sizeMaximum number of characters written to output.
delimiterThe delimiter used (for example '"').
inThe input string.
Returns
The number of characters that (would) have been written.

◆ ecs_vars_declare_w_value()

FLECS_API ecs_expr_var_t * ecs_vars_declare_w_value ( ecs_vars_t vars,
const char *  name,
ecs_value_t value 
)

Declare variable in current scope from value.

This operation takes ownership of the value. The value pointer must be allocated with ecs_value_new.