Skip to content

Commit

Permalink
version 3
Browse files Browse the repository at this point in the history
  • Loading branch information
uecker committed Aug 21, 2024
1 parent 3ddcc8e commit 397f970
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/cppmap.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/* Copyright 2015. The Regents of the University of California.
* All rights reserved. Use of this source code is governed by
* a BSD-style license which can be found in the LICENSE file.
*
* Authors:
* 2015 Martin Uecker <martin.uecker@med.uni-goettingen.de>
*/


// some classic C preprocessor hackery

/* Ideas from:
*
* https://github.com/swansontec/map-macro
* https://github.com/pfultz2/Cloak/wiki/Is-the-C-preprocessor-Turing-complete%3F
*/

#define EMPTY()
#define DEFER1(...) __VA_ARGS__ EMPTY()
#define DEFER2(...) __VA_ARGS__ DEFER1(EMPTY)()
#define DEFER3(...) __VA_ARGS__ DEFER2(EMPTY)()

#define EXPAND6(...) __VA_ARGS__
#define EXPAND5(...) EXPAND6(EXPAND6(__VA_ARGS__))
#define EXPAND4(...) EXPAND5(EXPAND5(__VA_ARGS__))
#define EXPAND3(...) EXPAND4(EXPAND4(__VA_ARGS__))
#define EXPAND2(...) EXPAND3(EXPAND3(__VA_ARGS__))
#define EXPAND1(...) EXPAND2(EXPAND2(__VA_ARGS__))
#define EXPAND0(...) EXPAND1(EXPAND1(__VA_ARGS__))
#define EXPAND(...) EXPAND0(EXPAND0(__VA_ARGS__))

#define CAT0(x, y) x ## y
#define CAT(x, y) CAT0(x, y)
#define NIL_TEST() DUMMY, TRUE,
#define RET2ND0(a, b, ...) b
#define RET2ND(...) RET2ND0(__VA_ARGS__)
#define NIL_P(x) RET2ND(NIL_TEST x, FALSE)
#define IF_TRUE(a, b) a
#define IF_FALSE(a, b) b
#define IF(x, a, b) CAT(IF_, x)(a, b)
#define MAP1() MAP0
#define MAP0(f, arg, a, b, ...) f(arg, a) IF(NIL_P(b), , DEFER3(MAP1)()(f, arg, b, __VA_ARGS__))
#define MAP(f, arg, ...) EXPAND(MAP0(f, arg, __VA_ARGS__, ()))

7 changes: 7 additions & 0 deletions src/namespace.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

#include "cppmap.h"

#define NAMESPACE_PREFIX(ns, x) ns ## _ ## x
#define NAMESPACE_ENTRY(ns, x) typeof(NAMESPACE_PREFIX(ns, x)) *x;
Expand All @@ -10,3 +11,9 @@ static const struct \

#define NAMESPACE(S) (const struct { S(S, NAMESPACE_ENTRY) }){ S(S, NAMESPACE_INIT) }


// version 2

#define namespace(S, ...) \
static const struct { MAP(NAMESPACE_ENTRY, S, __VA_ARGS__) } S = { MAP(NAMESPACE_INIT, S, __VA_ARGS__) }

0 comments on commit 397f970

Please sign in to comment.