option macros

This commit is contained in:
veclav talica 2024-07-30 15:22:07 +03:00
parent 142321d458
commit 06ce0eb13a
3 changed files with 82 additions and 0 deletions

8
src/macros/concatenate.h Normal file
View File

@ -0,0 +1,8 @@
#ifndef CONCATENATE_H
#define CONCATENATE_H
#define m_concatenate(p_a, p_b) m_concatenate_(p_a, p_b)
#define m_concatenate_(p_a, p_b) m_concatenate__(p_a, p_b)
#define m_concatenate__(p_a, p_b) p_a##p_b
#endif

65
src/macros/option.h Normal file
View File

@ -0,0 +1,65 @@
#ifndef OPTION_H
#define OPTION_H
#include "concatenate.h"
#include "varargcount.h"
#include <stdbool.h>
/* usage example:
*
* struct result {
* float f;
* m_option_list(
* int, v
* )
* }
*
* struct result = {
* m_set(f, 1.0), -- non options could be set with this
* m_opt(v, 5), -- options are supposed to be initialized like that
* }
*
*/
#define m_set(p_member, p_value) .p_member = (p_value)
#define m_opt(p_member, p_value) .p_member##_opt = (p_value), .p_member##_opt_set = 1
#define m_is_set(p_value, p_member) ((p_value).p_member##_opt_set)
/* warn: beware of double evaluation! */
#define m_or(p_value, p_member, p_default) ((p_value).p_member##_opt_set ? (p_value).p_member##_opt : (p_default))
#define m_option_list_2(t0, m0) \
t0 m0##_opt; \
bool m0##_opt_set : 1; \
#define m_option_list_4(t0, m0, t1, m1) \
t0 m0##_opt; \
t1 m1##_opt; \
bool m0##_opt_set : 1; \
bool m1##_opt_set : 1; \
#define m_option_list_6(t0, m0, t1, m1, t2, m2) \
t0 m0##_opt; \
t1 m1##_opt; \
t2 m1##_opt; \
bool m0##_opt_set : 1; \
bool m1##_opt_set : 1; \
bool m2##_opt_set : 1; \
#define m_option_list_8(t0, m0, t1, m1, t2, m2, t3, m3) \
t0 m0##_opt; \
t1 m1##_opt; \
t2 m2##_opt; \
t3 m3##_opt; \
bool m0##_opt_set : 1; \
bool m1##_opt_set : 1; \
bool m2##_opt_set : 1; \
bool m3##_opt_set : 1; \
#define m_option_list_(p_n, ...) m_concatenate(m_option_list_, p_n)(__VA_ARGS__)
#define m_option_list(...) m_option_list_(m_narg(__VA_ARGS__), __VA_ARGS__)
#endif

9
src/macros/varargcount.h Normal file
View File

@ -0,0 +1,9 @@
#ifndef VARARGCOUNT_H
#define VARARGCOUNT_H
#define m_narg(...) m_narg_(__VA_ARGS__, m_rseq_n_())
#define m_narg_(...) m_arg_n_(__VA_ARGS__)
#define m_arg_n_(_1, _2, _3, _4, _5, _6, _7, _8, N, ...) N
#define m_rseq_n_() 8, 7, 6, 5, 4, 3, 2, 1, 0
#endif