specialize.hpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*! \file specialize.hpp
  2. \brief Serialization disambiguation */
  3. /*
  4. Copyright (c) 2014, Randolph Voorhies, Shane Grant
  5. All rights reserved.
  6. Redistribution and use in source and binary forms, with or without
  7. modification, are permitted provided that the following conditions are met:
  8. * Redistributions of source code must retain the above copyright
  9. notice, this list of conditions and the following disclaimer.
  10. * Redistributions in binary form must reproduce the above copyright
  11. notice, this list of conditions and the following disclaimer in the
  12. documentation and/or other materials provided with the distribution.
  13. * Neither the name of the copyright holder nor the
  14. names of its contributors may be used to endorse or promote products
  15. derived from this software without specific prior written permission.
  16. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  17. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
  20. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #ifndef CEREAL_SPECIALIZE_HPP_
  28. #define CEREAL_SPECIALIZE_HPP_
  29. namespace cereal
  30. {
  31. // Forward declaration of access class that users can become friends with
  32. class access;
  33. // ######################################################################
  34. //! A specifier used in conjunction with cereal::specialize to disambiguate
  35. //! serialization in special cases
  36. /*! @relates specialize
  37. @ingroup Access */
  38. enum class specialization
  39. {
  40. member_serialize, //!< Force the use of a member serialize function
  41. member_load_save, //!< Force the use of a member load/save pair
  42. member_load_save_minimal, //!< Force the use of a member minimal load/save pair
  43. non_member_serialize, //!< Force the use of a non-member serialize function
  44. non_member_load_save, //!< Force the use of a non-member load/save pair
  45. non_member_load_save_minimal //!< Force the use of a non-member minimal load/save pair
  46. };
  47. //! A class used to disambiguate cases where cereal cannot detect a unique way of serializing a class
  48. /*! cereal attempts to figure out which method of serialization (member vs. non-member serialize
  49. or load/save pair) at compile time. If for some reason cereal cannot find a non-ambiguous way
  50. of serializing a type, it will produce a static assertion complaining about this.
  51. This can happen because you have both a serialize and load/save pair, or even because a base
  52. class has a serialize (public or private with friend access) and a derived class does not
  53. overwrite this due to choosing some other serialization type.
  54. Specializing this class will tell cereal to explicitly use the serialization type you specify
  55. and it will not complain about ambiguity in its compile time selection. However, if cereal detects
  56. an ambiguity in specializations, it will continue to issue a static assertion.
  57. @code{.cpp}
  58. class MyParent
  59. {
  60. friend class cereal::access;
  61. template <class Archive>
  62. void serialize( Archive & ar ) {}
  63. };
  64. // Although serialize is private in MyParent, to cereal::access it will look public,
  65. // even through MyDerived
  66. class MyDerived : public MyParent
  67. {
  68. public:
  69. template <class Archive>
  70. void load( Archive & ar ) {}
  71. template <class Archive>
  72. void save( Archive & ar ) {}
  73. };
  74. // The load/save pair in MyDerived is ambiguous because serialize in MyParent can
  75. // be accessed from cereal::access. This looks the same as making serialize public
  76. // in MyParent, making it seem as though MyDerived has both a serialize and a load/save pair.
  77. // cereal will complain about this at compile time unless we disambiguate:
  78. namespace cereal
  79. {
  80. // This struct specialization will tell cereal which is the right way to serialize the ambiguity
  81. template <class Archive> struct specialize<Archive, MyDerived, cereal::specialization::member_load_save> {};
  82. // If we only had a disambiguation for a specific archive type, it would look something like this
  83. template <> struct specialize<cereal::BinaryOutputArchive, MyDerived, cereal::specialization::member_load_save> {};
  84. }
  85. @endcode
  86. You can also choose to use the macros CEREAL_SPECIALIZE_FOR_ALL_ARCHIVES or
  87. CEREAL_SPECIALIZE_FOR_ARCHIVE if you want to type a little bit less.
  88. @tparam T The type to specialize the serialization for
  89. @tparam S The specialization type to use for T
  90. @ingroup Access */
  91. template <class Archive, class T, specialization S>
  92. struct specialize : public std::false_type {};
  93. //! Convenient macro for performing specialization for all archive types
  94. /*! This performs specialization for the specific type for all types of archives.
  95. This macro should be placed at the global namespace.
  96. @code{cpp}
  97. struct MyType {};
  98. CEREAL_SPECIALIZE_FOR_ALL_ARCHIVES( MyType, cereal::specialization::member_load_save );
  99. @endcode
  100. @relates specialize
  101. @ingroup Access */
  102. #define CEREAL_SPECIALIZE_FOR_ALL_ARCHIVES( Type, Specialization ) \
  103. namespace cereal { template <class Archive> struct specialize<Archive, Type, Specialization> {}; }
  104. //! Convenient macro for performing specialization for a single archive type
  105. /*! This performs specialization for the specific type for a single type of archive.
  106. This macro should be placed at the global namespace.
  107. @code{cpp}
  108. struct MyType {};
  109. CEREAL_SPECIALIZE_FOR_ARCHIVE( cereal::XMLInputArchive, MyType, cereal::specialization::member_load_save );
  110. @endcode
  111. @relates specialize
  112. @ingroup Access */
  113. #define CEREAL_SPECIALIZE_FOR_ARCHIVE( Archive, Type, Specialization ) \
  114. namespace cereal { template <> struct specialize<Archive, Type, Specialization> {}; }
  115. }
  116. #endif