Polymorphic.hpp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. ///
  2. /// \file Archive/Polymorphic.hpp
  3. ///
  4. /// Polymorphic pointer support for serialization.
  5. ///
  6. /// \copyright
  7. /// Copyright (c) 2016 Josh Blum
  8. /// SPDX-License-Identifier: BSL-1.0
  9. ///
  10. #pragma once
  11. #include <Pothos/Config.hpp>
  12. #include <Pothos/Archive/ArchiveEntry.hpp>
  13. #include <Pothos/Archive/Numbers.hpp>
  14. #include <Pothos/Archive/Invoke.hpp>
  15. #include <type_traits>
  16. namespace Pothos {
  17. namespace serialization {
  18. template <typename Archive, typename T>
  19. typename std::enable_if<std::is_polymorphic<T>::value>::type
  20. save(Archive &ar, const T* const &t, const unsigned int)
  21. {
  22. const auto &entry = Pothos::Archive::ArchiveEntry::find(typeid(*t));
  23. ar << entry.getHash();
  24. entry.save(ar, t);
  25. }
  26. template <typename Archive, typename T>
  27. typename std::enable_if<std::is_polymorphic<T>::value>::type
  28. load(Archive &ar, T* &t, const unsigned int)
  29. {
  30. unsigned long long idHash;
  31. ar >> idHash;
  32. const auto &entry = Pothos::Archive::ArchiveEntry::find(idHash);
  33. delete t; //delete previous pointer or its null
  34. t = static_cast<T*>(entry.load(ar));
  35. }
  36. template <typename Archive, typename T>
  37. typename std::enable_if<std::is_polymorphic<T>::value>::type
  38. serialize(Archive &ar, T* &t, const unsigned int ver)
  39. {
  40. Pothos::serialization::invokeSplit(ar, t, ver);
  41. }
  42. }}