Map.hpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. ///
  2. /// \file Archive/Map.hpp
  3. ///
  4. /// Map support for serialization.
  5. ///
  6. /// \copyright
  7. /// Copyright (c) 2016-2019 Josh Blum
  8. /// SPDX-License-Identifier: BSL-1.0
  9. ///
  10. #pragma once
  11. #include <Pothos/Config.hpp>
  12. #include <Pothos/Archive/Invoke.hpp>
  13. #include <Pothos/Archive/Numbers.hpp>
  14. #include <Pothos/Archive/Pair.hpp>
  15. #include <utility> //move
  16. #include <map>
  17. namespace Pothos {
  18. namespace serialization {
  19. template<typename Archive, typename K, typename T, typename Compare, typename Allocator>
  20. void save(Archive &ar, const std::map<K, T, Compare, Allocator> &t, const unsigned int)
  21. {
  22. ar << unsigned(t.size());
  23. for (const auto &pair : t)
  24. {
  25. ar << pair;
  26. }
  27. }
  28. template<typename Archive, typename K, typename T, typename Compare, typename Allocator>
  29. void load(Archive &ar, std::map<K, T, Compare, Allocator> &t, const unsigned int)
  30. {
  31. t.clear();
  32. unsigned size(0);
  33. ar >> size;
  34. for (size_t i = 0; i < size_t(size); i++)
  35. {
  36. std::pair<K, T> pair;
  37. ar >> pair;
  38. t.emplace_hint(t.end(), std::move(pair));
  39. }
  40. }
  41. template <typename Archive, typename K, typename T, typename Compare, typename Allocator>
  42. void serialize(Archive &ar, std::map<K, T, Compare, Allocator> &t, const unsigned int ver)
  43. {
  44. Pothos::serialization::invokeSplit(ar, t, ver);
  45. }
  46. }}