Skip to content

Instantly share code, notes, and snippets.

@vathpela vathpela/is.h
Created May 18, 2018

Embed
What would you like to do?
I apologize.
/*
* is.h
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of the
* License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
#ifndef IS_H_
#define IS_H_
#if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 201100L
#error C11 or newer is required
#endif
#include <stddef.h>
#include <stdint.h>
#include <inttypes.h>
#include <sys/types.h>
#include <string.h>
#define ptr_is_(type, x, y) type: ((*((type)(uintptr_t)(x))) == (*((type)(uintptr_t)(y))))
#define chr_is_(type, x, y) type: (((type)(uintptr_t)(x)) == ((type)(uintptr_t)(y)))
#define int_is_(type, x, y) chr_is_(type, x, y), ptr_is_(type *, x, y)
#define cmp_is_(type, cmp, x, y) type: (!cmp((type)(uintptr_t)(x), (type)(uintptr_t)(y)))
#define is(x, y) \
(((!(x)) && (!(y))) || \
((x) && (y) && \
_Generic((x), \
chr_is_(char, x, y), \
\
chr_is_(signed char, x, y), \
int_is_(signed short int, x, y), \
int_is_(signed int, x, y), \
int_is_(signed long int, x, y), \
int_is_(signed long long int, x, y), \
int_is_(signed __int128, x, y), \
\
chr_is_(unsigned char, x, y), \
int_is_(unsigned short int, x, y), \
int_is_(unsigned int, x, y), \
int_is_(unsigned long int, x, y), \
int_is_(unsigned long long int, x, y), \
int_is_(unsigned __int128, x, y), \
\
cmp_is_(unsigned char *, strcmp, x, y), \
cmp_is_(signed char *, strcmp, x, y), \
cmp_is_(char *, strcmp, x, y), \
\
void *: (((intptr_t)(x)) == ((intptr_t)(y))) \
) \
) \
)
#endif /* !IS_H_ */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.