townengine/src/macros/option.h
2024-07-30 15:22:07 +03:00

66 lines
2.1 KiB
C

#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