macros.hpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*! \file macros.hpp
  2. \brief Preprocessor macros that can customise the cereal library
  3. By default, cereal looks for serialization functions with very
  4. specific names, that is: serialize, load, save, load_minimal,
  5. or save_minimal.
  6. This file allows an advanced user to change these names to conform
  7. to some other style or preference. This is implemented using
  8. preprocessor macros.
  9. As a result of this, in internal cereal code you will see macros
  10. used for these function names. In user code, you should name
  11. the functions like you normally would and not use the macros
  12. to improve readability.
  13. \ingroup utility */
  14. /*
  15. Copyright (c) 2014, Randolph Voorhies, Shane Grant
  16. All rights reserved.
  17. Redistribution and use in source and binary forms, with or without
  18. modification, are permitted provided that the following conditions are met:
  19. * Redistributions of source code must retain the above copyright
  20. notice, this list of conditions and the following disclaimer.
  21. * Redistributions in binary form must reproduce the above copyright
  22. notice, this list of conditions and the following disclaimer in the
  23. documentation and/or other materials provided with the distribution.
  24. * Neither the name of the copyright holder nor the
  25. names of its contributors may be used to endorse or promote products
  26. derived from this software without specific prior written permission.
  27. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  28. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  29. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  30. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
  31. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  32. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  33. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  34. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  35. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  36. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  37. */
  38. #ifndef CEREAL_MACROS_HPP_
  39. #define CEREAL_MACROS_HPP_
  40. #ifndef CEREAL_THREAD_SAFE
  41. //! Whether cereal should be compiled for a threaded environment
  42. /*! This macro causes cereal to use mutexes to control access to
  43. global internal state in a thread safe manner.
  44. Note that even with this enabled you must still ensure that
  45. archives are accessed by only one thread at a time; it is safe
  46. to use multiple archives in parallel, but not to access one archive
  47. from many places simultaneously. */
  48. #define CEREAL_THREAD_SAFE 0
  49. #endif // CEREAL_THREAD_SAFE
  50. #ifndef CEREAL_SIZE_TYPE
  51. //! Determines the data type used for size_type
  52. /*! cereal uses size_type to ensure that the serialized size of
  53. dynamic containers is compatible across different architectures
  54. (e.g. 32 vs 64 bit), which may use different underlying types for
  55. std::size_t.
  56. More information can be found in cereal/details/helpers.hpp.
  57. If you choose to modify this type, ensure that you use a fixed
  58. size type (e.g. uint32_t). */
  59. #define CEREAL_SIZE_TYPE uint64_t
  60. #endif // CEREAL_SIZE_TYPE
  61. // ######################################################################
  62. #ifndef CEREAL_SERIALIZE_FUNCTION_NAME
  63. //! The serialization/deserialization function name to search for.
  64. /*! You can define @c CEREAL_SERIALIZE_FUNCTION_NAME to be different assuming
  65. you do so before this file is included. */
  66. #define CEREAL_SERIALIZE_FUNCTION_NAME serialize
  67. #endif // CEREAL_SERIALIZE_FUNCTION_NAME
  68. #ifndef CEREAL_LOAD_FUNCTION_NAME
  69. //! The deserialization (load) function name to search for.
  70. /*! You can define @c CEREAL_LOAD_FUNCTION_NAME to be different assuming you do so
  71. before this file is included. */
  72. #define CEREAL_LOAD_FUNCTION_NAME load
  73. #endif // CEREAL_LOAD_FUNCTION_NAME
  74. #ifndef CEREAL_SAVE_FUNCTION_NAME
  75. //! The serialization (save) function name to search for.
  76. /*! You can define @c CEREAL_SAVE_FUNCTION_NAME to be different assuming you do so
  77. before this file is included. */
  78. #define CEREAL_SAVE_FUNCTION_NAME save
  79. #endif // CEREAL_SAVE_FUNCTION_NAME
  80. #ifndef CEREAL_LOAD_MINIMAL_FUNCTION_NAME
  81. //! The deserialization (load_minimal) function name to search for.
  82. /*! You can define @c CEREAL_LOAD_MINIMAL_FUNCTION_NAME to be different assuming you do so
  83. before this file is included. */
  84. #define CEREAL_LOAD_MINIMAL_FUNCTION_NAME load_minimal
  85. #endif // CEREAL_LOAD_MINIMAL_FUNCTION_NAME
  86. #ifndef CEREAL_SAVE_MINIMAL_FUNCTION_NAME
  87. //! The serialization (save_minimal) function name to search for.
  88. /*! You can define @c CEREAL_SAVE_MINIMAL_FUNCTION_NAME to be different assuming you do so
  89. before this file is included. */
  90. #define CEREAL_SAVE_MINIMAL_FUNCTION_NAME save_minimal
  91. #endif // CEREAL_SAVE_MINIMAL_FUNCTION_NAME
  92. // ######################################################################
  93. //! Defines the CEREAL_NOEXCEPT macro to use instead of noexcept
  94. /*! If a compiler we support does not support noexcept, this macro
  95. will detect this and define CEREAL_NOEXCEPT as a no-op
  96. @internal */
  97. #if !defined(CEREAL_HAS_NOEXCEPT)
  98. #if defined(__clang__)
  99. #if __has_feature(cxx_noexcept)
  100. #define CEREAL_HAS_NOEXCEPT
  101. #endif
  102. #else // NOT clang
  103. #if defined(__GXX_EXPERIMENTAL_CXX0X__) && __GNUC__ * 10 + __GNUC_MINOR__ >= 46 || \
  104. defined(_MSC_FULL_VER) && _MSC_FULL_VER >= 190023026
  105. #define CEREAL_HAS_NOEXCEPT
  106. #endif // end GCC/MSVC check
  107. #endif // end NOT clang block
  108. #ifndef CEREAL_NOEXCEPT
  109. #ifdef CEREAL_HAS_NOEXCEPT
  110. #define CEREAL_NOEXCEPT noexcept
  111. #else
  112. #define CEREAL_NOEXCEPT
  113. #endif // end CEREAL_HAS_NOEXCEPT
  114. #endif // end !defined(CEREAL_HAS_NOEXCEPT)
  115. #endif // ifndef CEREAL_NOEXCEPT
  116. // ######################################################################
  117. //! Checks if C++17 is available
  118. //! NOTE: clang v5 has a bug with inline variables, so disable C++17 on that compiler
  119. #if (__cplusplus >= 201703L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201703L)) \
  120. && (!defined(__clang__) || __clang_major__ > 5)
  121. #define CEREAL_HAS_CPP17
  122. #endif
  123. //! Checks if C++14 is available
  124. #if __cplusplus >= 201402L
  125. #define CEREAL_HAS_CPP14
  126. #endif
  127. // ######################################################################
  128. //! Defines the CEREAL_ALIGNOF macro to use instead of alignof
  129. #if defined(_MSC_VER) && _MSC_VER < 1900
  130. #define CEREAL_ALIGNOF __alignof
  131. #else // not MSVC 2013 or older
  132. #define CEREAL_ALIGNOF alignof
  133. #endif // end MSVC check
  134. #endif // CEREAL_MACROS_HPP_