bitset.hpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*! \file bitset.hpp
  2. \brief Support for types found in \<bitset\>
  3. \ingroup STLSupport */
  4. /*
  5. Copyright (c) 2014, Randolph Voorhies, Shane Grant
  6. All rights reserved.
  7. Redistribution and use in source and binary forms, with or without
  8. modification, are permitted provided that the following conditions are met:
  9. * Redistributions of source code must retain the above copyright
  10. notice, this list of conditions and the following disclaimer.
  11. * Redistributions in binary form must reproduce the above copyright
  12. notice, this list of conditions and the following disclaimer in the
  13. documentation and/or other materials provided with the distribution.
  14. * Neither the name of the copyright holder nor the
  15. names of its contributors may be used to endorse or promote products
  16. derived from this software without specific prior written permission.
  17. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  18. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
  21. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  24. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  26. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. #ifndef CEREAL_TYPES_BITSET_HPP_
  29. #define CEREAL_TYPES_BITSET_HPP_
  30. #include "cereal/cereal.hpp"
  31. #include "cereal/types/string.hpp"
  32. #include <bitset>
  33. namespace cereal
  34. {
  35. namespace bitset_detail
  36. {
  37. //! The type the bitset is encoded with
  38. /*! @internal */
  39. enum class type : uint8_t
  40. {
  41. ulong,
  42. ullong,
  43. string,
  44. bits
  45. };
  46. }
  47. //! Serializing (save) for std::bitset when BinaryData optimization supported
  48. template <class Archive, size_t N,
  49. traits::EnableIf<traits::is_output_serializable<BinaryData<std::uint32_t>, Archive>::value>
  50. = traits::sfinae> inline
  51. void CEREAL_SAVE_FUNCTION_NAME( Archive & ar, std::bitset<N> const & bits )
  52. {
  53. ar( CEREAL_NVP_("type", bitset_detail::type::bits) );
  54. // Serialize 8 bit chunks
  55. std::uint8_t chunk = 0;
  56. std::uint8_t mask = 0x80;
  57. // Set each chunk using a rotating mask for the current bit
  58. for( std::size_t i = 0; i < N; ++i )
  59. {
  60. if( bits[i] )
  61. chunk |= mask;
  62. mask = static_cast<std::uint8_t>(mask >> 1);
  63. // output current chunk when mask is empty (8 bits)
  64. if( mask == 0 )
  65. {
  66. ar( chunk );
  67. chunk = 0;
  68. mask = 0x80;
  69. }
  70. }
  71. // serialize remainder, if it exists
  72. if( mask != 0x80 )
  73. ar( chunk );
  74. }
  75. //! Serializing (save) for std::bitset when BinaryData is not supported
  76. template <class Archive, size_t N,
  77. traits::DisableIf<traits::is_output_serializable<BinaryData<std::uint32_t>, Archive>::value>
  78. = traits::sfinae> inline
  79. void CEREAL_SAVE_FUNCTION_NAME( Archive & ar, std::bitset<N> const & bits )
  80. {
  81. try
  82. {
  83. auto const b = bits.to_ulong();
  84. ar( CEREAL_NVP_("type", bitset_detail::type::ulong) );
  85. ar( CEREAL_NVP_("data", b) );
  86. }
  87. catch( std::overflow_error const & )
  88. {
  89. try
  90. {
  91. auto const b = bits.to_ullong();
  92. ar( CEREAL_NVP_("type", bitset_detail::type::ullong) );
  93. ar( CEREAL_NVP_("data", b) );
  94. }
  95. catch( std::overflow_error const & )
  96. {
  97. ar( CEREAL_NVP_("type", bitset_detail::type::string) );
  98. ar( CEREAL_NVP_("data", bits.to_string()) );
  99. }
  100. }
  101. }
  102. //! Serializing (load) for std::bitset
  103. template <class Archive, size_t N> inline
  104. void CEREAL_LOAD_FUNCTION_NAME( Archive & ar, std::bitset<N> & bits )
  105. {
  106. bitset_detail::type t;
  107. ar( CEREAL_NVP_("type", t) );
  108. switch( t )
  109. {
  110. case bitset_detail::type::ulong:
  111. {
  112. unsigned long b;
  113. ar( CEREAL_NVP_("data", b) );
  114. bits = std::bitset<N>( b );
  115. break;
  116. }
  117. case bitset_detail::type::ullong:
  118. {
  119. unsigned long long b;
  120. ar( CEREAL_NVP_("data", b) );
  121. bits = std::bitset<N>( b );
  122. break;
  123. }
  124. case bitset_detail::type::string:
  125. {
  126. std::string b;
  127. ar( CEREAL_NVP_("data", b) );
  128. bits = std::bitset<N>( b );
  129. break;
  130. }
  131. case bitset_detail::type::bits:
  132. {
  133. // Normally we would use BinaryData to route this at compile time,
  134. // but doing this at runtime doesn't break any old serialization
  135. std::uint8_t chunk = 0;
  136. std::uint8_t mask = 0;
  137. bits.reset();
  138. // Load one chunk at a time, rotating through the chunk
  139. // to set bits in the bitset
  140. for( std::size_t i = 0; i < N; ++i )
  141. {
  142. if( mask == 0 )
  143. {
  144. ar( chunk );
  145. mask = 0x80;
  146. }
  147. if( chunk & mask )
  148. bits[i] = 1;
  149. mask = static_cast<std::uint8_t>(mask >> 1);
  150. }
  151. break;
  152. }
  153. default:
  154. throw Exception("Invalid bitset data representation");
  155. }
  156. }
  157. } // namespace cereal
  158. #endif // CEREAL_TYPES_BITSET_HPP_