map.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // Downloaded from https://github.com/swansontec/map-macro
  2. /*
  3. * Copyright (C) 2012 William Swanson
  4. *
  5. * Permission is hereby granted, free of charge, to any person
  6. * obtaining a copy of this software and associated documentation
  7. * files (the "Software"), to deal in the Software without
  8. * restriction, including without limitation the rights to use, copy,
  9. * modify, merge, publish, distribute, sublicense, and/or sell copies
  10. * of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be
  14. * included in all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
  20. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  21. * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  22. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. *
  24. * Except as contained in this notice, the names of the authors or
  25. * their institutions shall not be used in advertising or otherwise to
  26. * promote the sale, use or other dealings in this Software without
  27. * prior written authorization from the authors.
  28. */
  29. #ifndef MAP_H_INCLUDED
  30. #define MAP_H_INCLUDED
  31. #define EVAL0(...) __VA_ARGS__
  32. #define EVAL1(...) EVAL0(EVAL0(EVAL0(__VA_ARGS__)))
  33. #define EVAL2(...) EVAL1(EVAL1(EVAL1(__VA_ARGS__)))
  34. #define EVAL3(...) EVAL2(EVAL2(EVAL2(__VA_ARGS__)))
  35. #define EVAL4(...) EVAL3(EVAL3(EVAL3(__VA_ARGS__)))
  36. #define EVAL(...) EVAL4(EVAL4(EVAL4(__VA_ARGS__)))
  37. #define MAP_END(...)
  38. #define MAP_OUT
  39. #define MAP_COMMA ,
  40. #define MAP_GET_END2() 0, MAP_END
  41. #define MAP_GET_END1(...) MAP_GET_END2
  42. #define MAP_GET_END(...) MAP_GET_END1
  43. #define MAP_NEXT0(test, next, ...) next MAP_OUT
  44. #define MAP_NEXT1(test, next) MAP_NEXT0(test, next, 0)
  45. #define MAP_NEXT(test, next) MAP_NEXT1(MAP_GET_END test, next)
  46. #define MAP0(f, x, peek, ...) f(x) MAP_NEXT(peek, MAP1)(f, peek, __VA_ARGS__)
  47. #define MAP1(f, x, peek, ...) f(x) MAP_NEXT(peek, MAP0)(f, peek, __VA_ARGS__)
  48. #define MAP_LIST_NEXT1(test, next) MAP_NEXT0(test, MAP_COMMA next, 0)
  49. #define MAP_LIST_NEXT(test, next) MAP_LIST_NEXT1(MAP_GET_END test, next)
  50. #define MAP_LIST0(f, x, peek, ...) f(x) MAP_LIST_NEXT(peek, MAP_LIST1)(f, peek, __VA_ARGS__)
  51. #define MAP_LIST1(f, x, peek, ...) f(x) MAP_LIST_NEXT(peek, MAP_LIST0)(f, peek, __VA_ARGS__)
  52. /**
  53. * Applies the function macro `f` to each of the remaining parameters.
  54. */
  55. #define MAP(f, ...) EVAL(MAP1(f, __VA_ARGS__, ()()(), ()()(), ()()(), 0))
  56. /**
  57. * Applies the function macro `f` to each of the remaining parameters and
  58. * inserts commas between the results.
  59. */
  60. #define MAP_LIST(f, ...) EVAL(MAP_LIST1(f, __VA_ARGS__, ()()(), ()()(), ()()(), 0))
  61. #endif