macros.H 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // -*- mode: c++; coding: utf-8 -*-
  2. /// @file macros.H
  3. /// @brief Fundamental macros and types.
  4. // (c) Daniel Llorens - 2005, 2012, 2019
  5. // This library is free software; you can redistribute it and/or modify it under
  6. // the terms of the GNU Lesser General Public License as published by the Free
  7. // Software Foundation; either version 3 of the License, or (at your option) any
  8. // later version.
  9. #pragma once
  10. #include <cstddef>
  11. #include <cassert>
  12. #include <type_traits>
  13. #include <utility>
  14. #define STRINGIZE1( x ) #x
  15. #define STRINGIZE( x ) STRINGIZE1( x )
  16. #define JOIN1( x, y ) x##y
  17. #define JOIN( x, y ) JOIN1( x, y )
  18. // by G. Pakosz @ http://stackoverflow.com/a/1872506
  19. #define FOR_EACH_1(what, x, ...) what(x)
  20. #define FOR_EACH_2(what, x, ...) what(x) FOR_EACH_1(what, __VA_ARGS__)
  21. #define FOR_EACH_3(what, x, ...) what(x) FOR_EACH_2(what, __VA_ARGS__)
  22. #define FOR_EACH_4(what, x, ...) what(x) FOR_EACH_3(what, __VA_ARGS__)
  23. #define FOR_EACH_5(what, x, ...) what(x) FOR_EACH_4(what, __VA_ARGS__)
  24. #define FOR_EACH_6(what, x, ...) what(x) FOR_EACH_5(what, __VA_ARGS__)
  25. #define FOR_EACH_7(what, x, ...) what(x) FOR_EACH_6(what, __VA_ARGS__)
  26. #define FOR_EACH_8(what, x, ...) what(x) FOR_EACH_7(what, __VA_ARGS__)
  27. #define FOR_EACH_9(what, x, ...) what(x) FOR_EACH_8(what, __VA_ARGS__)
  28. #define FOR_EACH_NARG(...) FOR_EACH_NARG_(__VA_ARGS__, FOR_EACH_RSEQ_N())
  29. #define FOR_EACH_NARG_(...) FOR_EACH_ARG_N(__VA_ARGS__)
  30. #define FOR_EACH_ARG_N(_1, _2, _3, _4, _5, _6, _7, _8, _9, N, ...) N
  31. #define FOR_EACH_RSEQ_N() 9, 8, 7, 6, 5, 4, 3, 2, 1, 0
  32. #define FOR_EACH_(N, what, ...) JOIN(FOR_EACH_, N)(what, __VA_ARGS__)
  33. #define FOR_EACH(what, ...) FOR_EACH_(FOR_EACH_NARG(__VA_ARGS__), what, __VA_ARGS__)
  34. // FIXME use __VA_OPT__(,) or __VA_OPT__(&&) in c++20
  35. #ifndef RA_ASSERT
  36. #define RA_ASSERT(cond, ...) assert(cond)
  37. #endif
  38. namespace mp {
  39. template <class B> constexpr bool exists = true;
  40. struct identity
  41. {
  42. template<class T>
  43. constexpr decltype(auto) operator()(T && t) const noexcept
  44. {
  45. return std::forward<T>(t);
  46. }
  47. };
  48. #define RA_IS_DEF(NAME, PRED) \
  49. template <class A, class Enable=void> constexpr bool JOIN(NAME, _def) = false; \
  50. template <class A> constexpr bool JOIN(NAME, _def) < A, std::enable_if_t< PRED >> = true; \
  51. template <class A> constexpr bool NAME = JOIN(NAME, _def)< std::decay_t< A >>;
  52. } // namespace mp