Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Wave: a Standard conformant C++ preprocessor library

, 10 Jan 2004
Describes a free and fully Standard conformant C++ preprocessor library
wave_preprocessor_demo.zip
wave_preprocessor_demo1.zip
wave_preprocessor_src.zip
wave
doc
theme
bkd.gif
bkd2.gif
bullet.gif
l_arr.gif
l_arr_disabled.gif
r_arr.gif
r_arr_disabled.gif
uc.gif
u_arr.gif
wave.gif
test
boost-build.jam
cpp_tokens
Jamfile.v2
list_includes
project-root.jam
wave
Jamfile
Jamfile.v2
runtests.sh
test_files
wave
cpplexer
re2clex
cpp.re
test
lextest.in
lextest.output
run_tests.sh
test_lexer.in
test_lexer.output
slex
test
lextest.in
lextest.re2c.output
lextest.slex.output
run_tests.sh
grammars
idllexer
re2clex
idl.re
util
wave_preprocessor_src1.zip
bkd.gif
bkd2.gif
bullet.gif
l_arr.gif
l_arr_disabled.gif
r_arr.gif
r_arr_disabled.gif
u_arr.gif
uc.gif
wave.gif
boost-build.jam
Jamfile.v2
project-root.jam
Jamfile
Jamfile.v2
runtests.sh
cpp.re
lextest.in
lextest.output
run_tests.sh
test_lexer.in
test_lexer.output
lextest.in
lextest.re2c.output
lextest.slex.output
run_tests.sh
/*=============================================================================
    Wave: A Standard compliant C++ preprocessor

    Copyright (c) 2001-2003 Hartmut Kaiser
    Copyright (c) 2003 Paul Mensonides
    http://spirit.sourceforge.net/

    Permission to copy, use, modify, sell and distribute this software
    is granted provided this copyright notice appears in all copies.
    This software is provided "as is" without express or implied
    warranty, and with no claim as to its suitability for any purpose.

    See Copyright.txt for full copyright notices and acknowledgements.
=============================================================================*/

// Tests more complex macro expansion. 

// token-pasting macro

#define CAT(a, b) PRIMITIVE_CAT(a, b)
#define PRIMITIVE_CAT(a, b) a ## b

// splits a value that is about to expand
// into two parameters and returns either
// the zero-th or one-th element.

#define SPLIT(n, im) PRIMITIVE_CAT(SPLIT_, n)(im)
#define SPLIT_0(a, b) a
#define SPLIT_1(a, b) b

// detects if the parameter is nullary parentheses ()
// or something else.  passing non-nullary parenthesis
// is invalid input.

#define IS_NULLARY(expr) \
    SPLIT( \
        0, \
        CAT(IS_NULLARY_R_, IS_NULLARY_T expr) \
    ) \
    /**/
#define IS_NULLARY_T() 1
#define IS_NULLARY_R_1 1, ?
#define IS_NULLARY_R_IS_NULLARY_T 0, ?

// expands to a macro that eats an n-element
// parenthesized expression.

#define EAT(n) PRIMITIVE_CAT(EAT_, n)
#define EAT_0()
#define EAT_1(a)
#define EAT_2(a, b)
#define EAT_3(a, b, c)

// expands to a macro that removes the
// parentheses from an n-element
// parenthesized expression

#define REM(n) PRIMITIVE_CAT(REM_, n)
#define REM_0()
#define REM_1(a) a
#define REM_2(a, b) a, b
#define REM_3(a, b, c) a, b, c

// expands to nothing

#define NIL

// expands to 1 if x is less than y
// otherwise, it expands to 0

#define LESS(x, y) \
    IS_NULLARY( \
        PRIMITIVE_CAT(LESS_, y)( \
            EAT(1), PRIMITIVE_CAT(LESS_, x) \
        )() \
    ) \
    /**/

#define LESS_0(a, b) a(EAT(2)) b(REM(1), NIL)
#define LESS_1(a, b) a(LESS_0) b(REM(1), NIL)
#define LESS_2(a, b) a(LESS_1) b(REM(1), NIL)
#define LESS_3(a, b) a(LESS_2) b(REM(1), NIL)
#define LESS_4(a, b) a(LESS_3) b(REM(1), NIL)
#define LESS_5(a, b) a(LESS_4) b(REM(1), NIL)

// expands to the binary one's compliment
// of a binary input value.  i.e. 0 or 1

#define COMPL(n) PRIMITIVE_CAT(COMPL_, n)
#define COMPL_0 1
#define COMPL_1 0

// these do the obvious...

#define GREATER(x, y) LESS(y, x)
#define LESS_EQUAL(x, y) COMPL(LESS(y, x))
#define GREATER_EQUAL(x, y) COMPL(LESS(x, y))

// causes another rescan...

#define SCAN(x) x

// expands to 1 if x is not equal to y.
// this one contains a workaround...

#define NOT_EQUAL(x, y) \
    IS_NULLARY( \
        SCAN( \
            PRIMITIVE_CAT(LESS_, x)( \
                EAT(1), \
                PRIMITIVE_CAT(LESS_, y) EAT(2) \
            )((), ...) \
        ) \
    ) \
    /**/
#define EQUAL(x, y) COMPL(NOT_EQUAL(x, y))

LESS(2, 3) == 1
//E #line 118 "test_files/test31.cpp"
//E 1 == 1
LESS(3, 2) == 0
//E 0 == 0
LESS(3, 3) == 0
//E 0 == 0

GREATER(2, 3) == 0
//E 0 == 0
GREATER(3, 2) == 1
//E 1 == 1
GREATER(3, 3) == 0
//E 0 == 0

LESS_EQUAL(2, 3) == 1
//E 1 == 1
LESS_EQUAL(3, 2) == 0
//E 0 == 0
LESS_EQUAL(3, 3) == 1
//E 1 == 1

GREATER_EQUAL(2, 3) == 0
//E 0 == 0
GREATER_EQUAL(3, 2) == 1
//E 1 == 1
GREATER_EQUAL(3, 3) == 1
//E 1 == 1

NOT_EQUAL(2, 3) == 1
//E 1 == 1
NOT_EQUAL(3, 2) == 1
//E 1 == 1
NOT_EQUAL(3, 3) == 0
//E 0 == 0

EQUAL(2, 3) == 0
//E 0 == 0
EQUAL(3, 2) == 0
//E 0 == 0
EQUAL(3, 3) == 1
//E 1 == 1

//R 0

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Hartmut Kaiser

United States United States
Actively involved in Boost and the development of the Spirit parser construction framework.

| Advertise | Privacy | Mobile
Web02 | 2.8.140827.1 | Last Updated 11 Jan 2004
Article Copyright 2003 by Hartmut Kaiser
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid