queue.hpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*! \file queue.hpp
  2. \brief Support for types found in \<queue\>
  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_QUEUE_HPP_
  29. #define CEREAL_TYPES_QUEUE_HPP_
  30. #include "cereal/details/helpers.hpp"
  31. #include <queue>
  32. // The default container for queue is deque, so let's include that too
  33. #include "cereal/types/deque.hpp"
  34. // The default comparator for queue is less
  35. #include "cereal/types/functional.hpp"
  36. namespace cereal
  37. {
  38. namespace queue_detail
  39. {
  40. //! Allows access to the protected container in queue
  41. /*! @internal */
  42. template <class T, class C> inline
  43. C const & container( std::queue<T, C> const & queue )
  44. {
  45. struct H : public std::queue<T, C>
  46. {
  47. static C const & get( std::queue<T, C> const & q )
  48. {
  49. return q.*(&H::c);
  50. }
  51. };
  52. return H::get( queue );
  53. }
  54. //! Allows access to the protected container in priority queue
  55. /*! @internal */
  56. template <class T, class C, class Comp> inline
  57. C const & container( std::priority_queue<T, C, Comp> const & priority_queue )
  58. {
  59. struct H : public std::priority_queue<T, C, Comp>
  60. {
  61. static C const & get( std::priority_queue<T, C, Comp> const & pq )
  62. {
  63. return pq.*(&H::c);
  64. }
  65. };
  66. return H::get( priority_queue );
  67. }
  68. //! Allows access to the protected comparator in priority queue
  69. /*! @internal */
  70. template <class T, class C, class Comp> inline
  71. Comp const & comparator( std::priority_queue<T, C, Comp> const & priority_queue )
  72. {
  73. struct H : public std::priority_queue<T, C, Comp>
  74. {
  75. static Comp const & get( std::priority_queue<T, C, Comp> const & pq )
  76. {
  77. return pq.*(&H::comp);
  78. }
  79. };
  80. return H::get( priority_queue );
  81. }
  82. }
  83. //! Saving for std::queue
  84. template <class Archive, class T, class C> inline
  85. void CEREAL_SAVE_FUNCTION_NAME( Archive & ar, std::queue<T, C> const & queue )
  86. {
  87. ar( CEREAL_NVP_("container", queue_detail::container( queue )) );
  88. }
  89. //! Loading for std::queue
  90. template <class Archive, class T, class C> inline
  91. void CEREAL_LOAD_FUNCTION_NAME( Archive & ar, std::queue<T, C> & queue )
  92. {
  93. C container;
  94. ar( CEREAL_NVP_("container", container) );
  95. queue = std::queue<T, C>( std::move( container ) );
  96. }
  97. //! Saving for std::priority_queue
  98. template <class Archive, class T, class C, class Comp> inline
  99. void CEREAL_SAVE_FUNCTION_NAME( Archive & ar, std::priority_queue<T, C, Comp> const & priority_queue )
  100. {
  101. ar( CEREAL_NVP_("comparator", queue_detail::comparator( priority_queue )) );
  102. ar( CEREAL_NVP_("container", queue_detail::container( priority_queue )) );
  103. }
  104. //! Loading for std::priority_queue
  105. template <class Archive, class T, class C, class Comp> inline
  106. void CEREAL_LOAD_FUNCTION_NAME( Archive & ar, std::priority_queue<T, C, Comp> & priority_queue )
  107. {
  108. Comp comparator;
  109. ar( CEREAL_NVP_("comparator", comparator) );
  110. C container;
  111. ar( CEREAL_NVP_("container", container) );
  112. priority_queue = std::priority_queue<T, C, Comp>( comparator, std::move( container ) );
  113. }
  114. } // namespace cereal
  115. #endif // CEREAL_TYPES_QUEUE_HPP_