So I decided it was too dangerous.
This commit is contained in:
parent
40bbacce49
commit
329640d1ac
|
@ -99,8 +99,7 @@ typedef struct {
|
|||
} g1m_mcs_cell_t;
|
||||
|
||||
/* mcs file */
|
||||
# define g1m_has_password(F) g1m_get_password(F)[0]
|
||||
//# define g1m_has_password(F) (*(F).password)
|
||||
# define g1m_has_password(F) (F)->password[0]
|
||||
typedef struct {
|
||||
/* directory name */
|
||||
char dirname[9];
|
||||
|
@ -199,39 +198,6 @@ int g1m_fctomb(char *s, FONTCHARACTER fc);
|
|||
size_t g1m_mbstofcs(FONTCHARACTER *dst, const char *src, size_t n);
|
||||
size_t g1m_fcstombs(char *dst, const FONTCHARACTER *src, size_t n);
|
||||
|
||||
/* ************************************************************************** */
|
||||
/* Other things */
|
||||
/* ************************************************************************** */
|
||||
/* Advanced password getting macro.
|
||||
* Okay, so this is an experiment and hack of the C11 `_Generic` feature.
|
||||
*
|
||||
* I wanted people to be able to pass either a pointer to a `g1m_mcsfile_t`
|
||||
* structure, or a `g1m_mcsfile_t` structure directly.
|
||||
* But C11 says `_Generic` branches must all be valid. So it couldn't be just
|
||||
* as simple as:
|
||||
*
|
||||
* _Generic((F), g1m_mcsfile_t: (F).password, g1m_mcsfile_t*: (F)->password)
|
||||
*
|
||||
* So I had to trick the compiler. Getting the adress is valid for the
|
||||
* structure and the pointer to the structure, so we get the double pointer
|
||||
* to the structure, and we send that as a `void*` to a static inline function
|
||||
* that will go through the double pointers to get the password field.
|
||||
*
|
||||
* Note that calculation results will not work here. (e.g. file + 1 - 1)
|
||||
*
|
||||
* Please enjoy the mess. */
|
||||
# if __STDC_VERSION__ >= 201112L
|
||||
static inline char *__g1m_get_password(void *mcsfile) {
|
||||
return ((*((g1m_mcsfile_t**)mcsfile))->password);
|
||||
}
|
||||
# define g1m_get_password(F) (char*)_Generic((F), \
|
||||
g1m_mcsfile_t*: __g1m_get_password(&(F)), \
|
||||
g1m_mcsfile_t: __g1m_get_password((void*[]){&(F)}), \
|
||||
default: NULL)
|
||||
# else
|
||||
# define g1m_get_password(F) (char*)((F)->password)
|
||||
# endif
|
||||
|
||||
# ifdef __cplusplus
|
||||
}
|
||||
# endif
|
||||
|
|
Reference in New Issue