Skip to content

Commit

Permalink
Rewrite FOR macro, to allow zero fixed arguments if desired
Browse files Browse the repository at this point in the history
  • Loading branch information
agagniere committed Sep 6, 2024
1 parent 4ffb2f5 commit daef0cc
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 239 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
*.pyc
.zig-cache
zig-out
4 changes: 4 additions & 0 deletions book/pages/reference/enum.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Enum utilities

:::{doxygenfile} enum.h
:::
1 change: 1 addition & 0 deletions book/pages/reference/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
internal
color
log
enum
:::
2 changes: 1 addition & 1 deletion build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ pub fn build(b: *std.Build) void {
b.installDirectory(.{
.source_dir = b.path("include/blackmagic"),
.install_dir = .header,
.install_subdir = "blackmagic"
.install_subdir = "blackmagic",
});
}
6 changes: 3 additions & 3 deletions build.zig.zon
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.{
.name = "Blackmagic",
.version = "0.2.0",
.paths = .{"include"}
.name = "blackmagic",
.version = "0.2.0",
.paths = .{ "build.zig", "build.zig.zon", "include", "README.md", "LICENSE" },
}
6 changes: 3 additions & 3 deletions include/blackmagic/blackmagic.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
* @author Antoine GAGNIERE
*/

#include "blackmagic/token.h"
#include "blackmagic/log.h"
#include "blackmagic/for.h"
#include "blackmagic/color.h"
#include "blackmagic/enum.h"
#include "blackmagic/fold.h"
#include "blackmagic/for.h"
#include "blackmagic/log.h"
#include "blackmagic/token.h"

/**
* @dir include
Expand Down
2 changes: 2 additions & 0 deletions include/blackmagic/enum.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <stdbool.h> // bool

///@cond
#define PP_PAIR_APPLY(F, P) F P
#define PP_PAIR_ASSIGN(K, V) K = V
#define PP_PAIR_FIRST(K, V) K
Expand All @@ -17,6 +18,7 @@

#define _ECNT(N) _MERGE(N, count)
#define _EMAX(N) _MERGE(N, upper_bound)
///@endcond

/**
@code{.c}
Expand Down
43 changes: 37 additions & 6 deletions include/blackmagic/for.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#pragma once

#include "blackmagic/arg_count.h" // ARG_COUNT
#include "blackmagic/token.h" // CONCAT

/**
* @file
* Generate code by calling a macro repeatedly
Expand All @@ -9,18 +12,46 @@
#define EACH(...) ARG_COUNT(__VA_ARGS__) __VA_OPT__(,) __VA_ARGS__

/**
* Call @p MACRO on each item of the @p LIST, forwarding extra arguments if any
* # Example
* Call @p MACRO on each item of the @p LIST, forwarding extra arguments if any.
* __Example__
* @code{.c}
* FOR(EACH(A, B, C), F, X, Y)
* @endcode
* will expand to:
* @code{.c}
* F(A, X, Y)
* F(B, X, Y)
* F(C, X, Y)
* F(X, Y, A)
* F(X, Y, B)
* F(X, Y, C)
* @endcode
* @param LIST shall be of the form `EACH(P0, P1, ..., Pn-1)`
* @param MACRO shall be a function-like macro
* @since 0.2
*/
#define FOR(LIST, MACRO, ...) _FOR(LIST, MACRO __VA_OPT__(,) __VA_ARGS__)
#define FOR(LIST, MACRO, ...) FOR_(LIST, MACRO __VA_OPT__(,) __VA_ARGS__)

///@cond
#define FOR_(N, ...) CONCAT(FOR_, N)(__VA_ARGS__)
#define FOR_0(M, ...)
#define FOR_1(A, M, ...) M(__VA_ARGS__ __VA_OPT__(,) A)
#define FOR_2(A, B, M, ...) M(__VA_ARGS__ __VA_OPT__(,) A) M(__VA_ARGS__ __VA_OPT__(,) B)
#define FOR_3(A, B, C, M, ...) M(__VA_ARGS__ __VA_OPT__(,) A) M(__VA_ARGS__ __VA_OPT__(,) B) M(__VA_ARGS__ __VA_OPT__(,) C)
#define FOR_4(A, B, C, D, M, ...) M(__VA_ARGS__ __VA_OPT__(,) A) M(__VA_ARGS__ __VA_OPT__(,) B) M(__VA_ARGS__ __VA_OPT__(,) C) M(__VA_ARGS__ __VA_OPT__(, ) D)
#define FOR_5(A, B, C, D, E, M, ...) \
M(__VA_ARGS__ __VA_OPT__(,) A) \
FOR_4(B, C, D, E, M __VA_OPT__(,) __VA_ARGS__)
#define FOR_6(A, B, C, D, E, F, M, ...) \
FOR_3(A, B, C, M __VA_OPT__(,) __VA_ARGS__) \
FOR_3(D, E, F, M __VA_OPT__(,) __VA_ARGS__)
#define FOR_7(A, B, C, D, E, F, G, M, ...) \
M(__VA_ARGS__ __VA_OPT__(,) A) \
FOR_6(B, C, D, E, F, G, M __VA_OPT__(,) __VA_ARGS__)
#define FOR_8(A, B, C, D, E, F, G, H, M, ...) \
FOR_4(A, B, C, D, M __VA_OPT__(,) __VA_ARGS__) \
FOR_4(E, F, G, H, M __VA_OPT__(,) __VA_ARGS__)
#define FOR_9(A, B, C, D, E, F, G, H, I, M, ...) \
M(__VA_ARGS__ __VA_OPT__(,) A) \
FOR_8(B, C, D, E, F, G, H, I, M __VA_OPT__(,) __VA_ARGS__)
#define FOR_10(A, B, C, D, E, F, G, H, I, J, M, ...) \
FOR_5(A, B, C, D, E, M __VA_OPT__(,) __VA_ARGS__) \
FOR_5(F, G, H, I, J, M __VA_OPT__(,) __VA_ARGS__)
///@endcond
226 changes: 0 additions & 226 deletions include/blackmagic/private_for.h

This file was deleted.

0 comments on commit daef0cc

Please sign in to comment.