#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))

/* warn: beware of double evaluation! */
#define m_opt_from(p_member, p_value, p_member_from) .p_member##_opt     = (p_value).p_member_from##_opt, \
                                                     .p_member##_opt_set = (p_value).p_member_from##_opt_set

#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_10(t0, m0, t1, m1, t2, m2,        \
                         t3, m3, t4, m4)                \
        t0 m0##_opt;                                    \
        t1 m1##_opt;                                    \
        t2 m2##_opt;                                    \
        t3 m3##_opt;                                    \
        t4 m4##_opt;                                    \
        bool m0##_opt_set : 1;                          \
        bool m1##_opt_set : 1;                          \
        bool m2##_opt_set : 1;                          \
        bool m3##_opt_set : 1;                          \
        bool m4##_opt_set : 1;                          \

#define m_option_list_12(t0, m0, t1, m1, t2, m2,        \
                         t3, m3, t4, m4, t5, m5)        \
        t0 m0##_opt;                                    \
        t1 m1##_opt;                                    \
        t2 m2##_opt;                                    \
        t3 m3##_opt;                                    \
        t4 m4##_opt;                                    \
        t5 m5##_opt;                                    \
        bool m0##_opt_set : 1;                          \
        bool m1##_opt_set : 1;                          \
        bool m2##_opt_set : 1;                          \
        bool m3##_opt_set : 1;                          \
        bool m4##_opt_set : 1;                          \
        bool m5##_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
