CLibs
Loading...
Searching...
No Matches
swexpr.h File Reference
#include "../structs/dynarr.h"
#include "errors.h"
#include "pointer_utils.h"
#include <stdbool.h>

Go to the source code of this file.

Macros

#define swex_init_val(expr_type, expr)
#define swex_init_ptr(expr, nbytes)
#define swex_init_str(expr)
#define as(VAR_NAME)
#define as_new(NEW_VAR_TYPE, NEW_VAR_NAME)
#define resolve(RESULT_TYPE, RESULT)
#define swex_case_imm(type, expr_case)
#define swex_case_ptr(expr_case, nbytes)
#define swex_case_str(expr_case)
#define swex_default()
#define swex_finish()

Macro Definition Documentation

◆ as

#define as ( VAR_NAME)
Value:
{ \
void *swex_as_var_addr = &( VAR_NAME ); \
_Switch_expression_init_var_( &swex_as_var_addr ); \
}

Uses an existing variable to store the result from resolve

◆ as_new

#define as_new ( NEW_VAR_TYPE,
NEW_VAR_NAME )
Value:
NEW_VAR_TYPE NEW_VAR_NAME = ( NEW_VAR_TYPE ) 0; \
as( NEW_VAR_NAME )

Creates a new local variable to store the result from resolve

◆ resolve

#define resolve ( RESULT_TYPE,
RESULT )
Value:
do \
{ \
*deref_as( RESULT_TYPE *, _Switch_expression_get_varaddr_() ) = RESULT; \
} \
while ( 0 )
#define deref_as(type, var)
Dereferences a pointer as if it was a pointer to type
Definition pointer_utils.h:53

Assigns the result into the swexpr variable

◆ swex_case_imm

#define swex_case_imm ( type,
expr_case )
Value:
{ \
if ( _Swex_aux_variable_ != NULL ) \
{ \
free_n( _Swex_aux_variable_ ); \
_Swex_aux_variable_ = NULL; \
} \
_Swex_aux_variable_ = malloc( sizeof( type ) ); \
if ( _Swex_aux_variable_ == NULL ) \
err( ENOMEM, "swex_case_imm" ); /* todo: <- here */ \
type swex_case_imm_aux_var_ = expr_case; \
memcpy( _Swex_aux_variable_, &swex_case_imm_aux_var_, sizeof( type ) ); \
} \
if ( !_Switch_expression_is_assigned_() \
&& ( _Switch_expression_size_peek_() ) == sizeof( type ) \
&& memcmp( _Switch_expression_value_peek_(), \
_Swex_aux_variable_, \
sizeof( type ) ) \
== 0 ) \
if ( _Switch_expression_assign_() != 1 )

Next statement/block is executed if the currently "switched" value matches this expression

Each case (including default) is only executed if no other case branch has been executed in this swexpr so far

◆ swex_case_ptr

#define swex_case_ptr ( expr_case,
nbytes )
Value:
if ( !_Switch_expression_is_assigned_() \
&& ( _Switch_expression_size_peek_() ) == sizeof( void * ) \
&& memcmp( _Switch_expression_value_peek_(), expr_case, nbytes ) == 0 ) \
if ( _Switch_expression_assign_() != 1 )
See also
swex_case_imm

◆ swex_case_str

#define swex_case_str ( expr_case)
Value:
if ( !_Switch_expression_is_assigned_() \
&& strcmp( _Switch_expression_value_peek_(), expr_case ) == 0 ) \
if ( _Switch_expression_assign_() != 1 )
See also
swex_case_imm

◆ swex_default

#define swex_default ( )
Value:
if ( !_Switch_expression_is_assigned_() && ( _Switch_expression_assign_() != 1 ) )

Executes the following statement if and only if no value has been assigned to the switch–target so far

◆ swex_finish

#define swex_finish ( )
Value:
do \
{ \
_Switch_expression_pop_(); \
free_n( _Swex_aux_variable_ ); \
} \
while ( 0 )

Frees all memory owned by the swexpr and resets all static variables needed for the functioning.

Attention
Invoke this right as you are leaving the swexpr body. Not calling this function will cause undefined behaviour and calling it prematurely will probably cause a NULL pointer dereference.

◆ swex_init_ptr

#define swex_init_ptr ( expr,
nbytes )
Value:
_Switch_expression_push_( nbytes, ( expr ) );

Initializes the swexpr to, in each case, compare it to the data behind the supplied pointer

Attention
must be followed by swex_finish() after the body

◆ swex_init_str

#define swex_init_str ( expr)
Value:
const char *swex_init_imm_tmp_var_char##_##expr##__ = ( expr ); \
_Switch_expression_push_( strlen( expr ), \
&( swex_init_imm_tmp_var_char##_##expr##__ ) );
Attention
must be followed by swex_finish() after the body

◆ swex_init_val

#define swex_init_val ( expr_type,
expr )
Value:
expr_type swex_init_imm_tmp_var_##expr_type##_##expr##__ = ( expr ); \
expr_type *swex_init_imm_tmp_var_##expr_type##_##expr##_ptr_ = \
&( swex_init_imm_tmp_var_##expr_type##_##expr##__ ); \
_Switch_expression_push_( sizeof( expr_type ), \
&( swex_init_imm_tmp_var_##expr_type##_##expr##_ptr_ ) );

Initializes the swexpr to, in each case, compare it to this expression

Attention
must be followed by swex_finish() after the body