Skip to content

Instantly share code, notes, and snippets.

@wavewave
Created January 30, 2013 08:30
Show Gist options
  • Select an option

  • Save wavewave/4671651 to your computer and use it in GitHub Desktop.

Select an option

Save wavewave/4671651 to your computer and use it in GitHub Desktop.
Pattern Match using C99 Macro
#ifndef __MACROPATTERNMATCH__
#define __MACROPATTERNMATCH__
#define CAT(a,...) PRIMITIVE_CAT(a, __VA_ARGS__ )
#define PRIMITIVE_CAT(a,...) a ## __VA_ARGS__
#define IIF(c) PRIMITIVE_CAT(IIF_, c)
#define IIF_0(t, ...) __VA_ARGS__
#define IIF_1(t, ...) t
#define CHECK_N(x, n, ... ) n
#define CHECK(...) CHECK_N (__VA_ARGS__, 0, )
#define PROBE(x) x, 1,
#define IS_PAREN(x) CHECK(IS_PAREN_PROBE x)
#define IS_PAREN_PROBE(...) PROBE(~)
#endif // __MACROPATTERNMATCH__
#include "MacroPatternMatch.h"
#include <stdio.h>
#define IS_TH2_FILL_PROTECTED ()
#define CHECKPROTECT(x,y) IS_PAREN(IS_ ## x ## _ ## y ## _PROTECTED)
#define FUNC(x,y) \
IIF( CHECKPROTECT(x,y) ) ( 300, 400 )
int main ()
{
printf("hello\n");
// printf("%d", IS_PAREN(IS_TH2_FILL_PROTECTED));
printf("%d\n", FUNC(TH2,FILL));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment