my_macros.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License, version 2.0,
  5. as published by the Free Software Foundation.
  6. This program is also distributed with certain software (including
  7. but not limited to OpenSSL) that is licensed under separate terms,
  8. as designated in a particular file or component or in included license
  9. documentation. The authors of MySQL hereby grant you an additional
  10. permission to link the program and your derivative works with the
  11. separately licensed software that they have included with MySQL.
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License, version 2.0, for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
  19. #ifndef MY_MACROS_INCLUDED
  20. #define MY_MACROS_INCLUDED
  21. /**
  22. @file include/my_macros.h
  23. Some common macros. Note that some of these should probably be replaced
  24. with code from \<algorithm\> or similar.
  25. */
  26. #ifndef MYSQL_ABI_CHECK
  27. #include <stddef.h>
  28. #endif
  29. /* Macros to make switching between C and C++ mode easier */
  30. #ifdef __cplusplus
  31. #define C_MODE_START extern "C" {
  32. #define C_MODE_END }
  33. #else
  34. #define C_MODE_START
  35. #define C_MODE_END
  36. #endif
  37. /* Make it easier to add conditional code in _expressions_ */
  38. #ifdef _WIN32
  39. #define IF_WIN(A, B) A
  40. #else
  41. #define IF_WIN(A, B) B
  42. #endif
  43. /*
  44. Two levels of macros are needed to stringify the
  45. result of expansion of a macro argument.
  46. */
  47. #define QUOTE_ARG(x) #x /* Quote argument (before cpp) */
  48. #define STRINGIFY_ARG(x) QUOTE_ARG(x) /* Quote argument, after cpp */
  49. #define MY_TEST(a) ((a) ? 1 : 0)
  50. #define MY_MAX(a, b) ((a) > (b) ? (a) : (b))
  51. #define MY_MIN(a, b) ((a) < (b) ? (a) : (b))
  52. #define set_if_bigger(a, b) \
  53. do { \
  54. if ((a) < (b)) (a) = (b); \
  55. } while (0)
  56. #define set_if_smaller(a, b) \
  57. do { \
  58. if ((a) > (b)) (a) = (b); \
  59. } while (0)
  60. #define test_all_bits(a, b) (((a) & (b)) == (b))
  61. /* Bug in developerstudio: use the C version */
  62. #if defined(__cplusplus) && !defined(__SUNPRO_CC)
  63. template <class T, size_t N>
  64. constexpr size_t array_elements(T (&)[N]) noexcept {
  65. return N;
  66. }
  67. #else
  68. // Less type-safe version that e.g. allows sending in pointers
  69. // or STL containers without an error.
  70. #define array_elements(A) ((size_t)(sizeof(A) / sizeof(A[0])))
  71. #endif
  72. #endif // MY_MACROS_INCLUDED