Commit ff71198b authored by David Gibson's avatar David Gibson

cppmagic: New module

A module for some of the awesome / horrifying techniques described at:
    http://jhnet.co.uk/articles/cpp_magic
    https://github.com/pfultz2/Cloak/wiki/C-Preprocessor-tricks,-tips,-and-idioms

Start off with just some simple things.
Signed-off-by: default avatarDavid Gibson <david@gibson.dropbear.id.au>
parent c43be6b8
../../licenses/BSD-MIT
\ No newline at end of file
#include "config.h"
#include <stdio.h>
#include <string.h>
/**
* cppmagic - Abuse of the C preprocessor
*
* This contains a bunch of fancy macro techniques such as
* preprocessor-time evaluated conditionals and (quasi) recursion and
* iteration.
*
* It's based on these articles:
* - http://jhnet.co.uk/articles/cpp_magic
* - https://github.com/pfultz2/Cloak/wiki/C-Preprocessor-tricks,-tips,-and-idioms
* and code from the Boost C++ library.
*
* License: BSD-MIT
*/
int main(int argc, char *argv[])
{
/* Expect exactly one argument */
if (argc != 2)
return 1;
if (strcmp(argv[1], "depends") == 0) {
return 0;
}
return 1;
}
/* MIT (BSD) license - see LICENSE file for details */
#ifndef CCAN_CPPMAGIC_H
#define CCAN_CPPMAGIC_H
/**
* CPPMAGIC_NOTHING - expands to nothing
*/
#define CPPMAGIC_NOTHING()
/**
* CPPMAGIC_STRINGIFY - convert arguments to a string literal
*/
#define _CPPMAGIC_STRINGIFY(...) #__VA_ARGS__
#define CPPMAGIC_STRINGIFY(...) _CPPMAGIC_STRINGIFY(__VA_ARGS__)
/**
* CPPMAGIC_GLUE2 - glue arguments together
*
* CPPMAGIC_GLUE2(@a_, @b_)
* expands to the expansion of @a_ followed immediately
* (combining tokens) by the expansion of @b_
*/
#define _CPPMAGIC_GLUE2(a_, b_) a_##b_
#define CPPMAGIC_GLUE2(a_, b_) _CPPMAGIC_GLUE2(a_, b_)
/**
* CPPMAGIC_1ST - return 1st argument
*
* CPPMAGIC_1ST(@a_, ...)
* expands to the expansion of @a_
*/
#define CPPMAGIC_1ST(a_, ...) a_
/**
* CPPMAGIC_2ND - return 2nd argument
*
* CPPMAGIC_2ST(@a_, @b_, ...)
* expands to the expansion of @b_
*/
#define CPPMAGIC_2ND(a_, b_, ...) b_
#endif /* CCAN_CPPMAGIC_H */
#include "config.h"
#include <string.h>
#include <ccan/cppmagic/cppmagic.h>
#include <ccan/tap/tap.h>
static inline void check1(const char *orig, const char *expand,
const char *match)
{
ok(strcmp(expand, match) == 0,
"%s => %s : %s", orig, expand, match);
}
#define CHECK1(orig, match) \
check1(#orig, CPPMAGIC_STRINGIFY(orig), match)
int main(void)
{
plan_tests(7);
CHECK1(CPPMAGIC_NOTHING(), "");
CHECK1(CPPMAGIC_GLUE2(a, b), "ab");
CHECK1(CPPMAGIC_1ST(a), "a");
CHECK1(CPPMAGIC_1ST(a, b), "a");
CHECK1(CPPMAGIC_1ST(a, b, c), "a");
CHECK1(CPPMAGIC_2ND(a, b), "b");
CHECK1(CPPMAGIC_2ND(a, b, c), "b");
/* This exits depending on whether all tests passed */
return exit_status();
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment