diff options
Diffstat (limited to 'src/serialization/containers.h')
-rw-r--r-- | src/serialization/containers.h | 99 |
1 files changed, 17 insertions, 82 deletions
diff --git a/src/serialization/containers.h b/src/serialization/containers.h index 65e6359b2..28eaa50fb 100644 --- a/src/serialization/containers.h +++ b/src/serialization/containers.h @@ -38,91 +38,26 @@ #include <set> #include "serialization.h" -template <template <bool> class Archive, class T> bool do_serialize(Archive<false> &ar, std::vector<T> &v); -template <template <bool> class Archive, class T> bool do_serialize(Archive<true> &ar, std::vector<T> &v); - -template <template <bool> class Archive, class T> bool do_serialize(Archive<false> &ar, std::deque<T> &v); -template <template <bool> class Archive, class T> bool do_serialize(Archive<true> &ar, std::deque<T> &v); - -template<typename K, typename V> -class serializable_unordered_map: public std::unordered_map<K, V> -{ -public: - typedef typename std::pair<K, V> value_type; - typename std::unordered_map<K, V> &parent() { return *this; } -}; - -template <template <bool> class Archive, typename K, typename V> bool do_serialize(Archive<false> &ar, serializable_unordered_map<K, V> &v); -template <template <bool> class Archive, typename K, typename V> bool do_serialize(Archive<true> &ar, serializable_unordered_map<K, V> &v); - -template<typename K, typename V> -class serializable_map: public std::map<K, V> -{ -public: - typedef typename std::pair<K, V> value_type; - typename std::map<K, V> &parent() { return *this; } -}; - -template <template <bool> class Archive, typename K, typename V> bool do_serialize(Archive<false> &ar, serializable_map<K, V> &v); -template <template <bool> class Archive, typename K, typename V> bool do_serialize(Archive<true> &ar, serializable_map<K, V> &v); - -template<typename K, typename V> -class serializable_unordered_multimap: public std::unordered_multimap<K, V> -{ -public: - typedef typename std::pair<K, V> value_type; - typename std::unordered_multimap<K, V> &parent() { return *this; } -}; - -template <template <bool> class Archive, typename K, typename V> bool do_serialize(Archive<false> &ar, serializable_unordered_multimap<K, V> &v); -template <template <bool> class Archive, typename K, typename V> bool do_serialize(Archive<true> &ar, serializable_unordered_multimap<K, V> &v); - -template <template <bool> class Archive, class T> bool do_serialize(Archive<false> &ar, std::unordered_set<T> &v); -template <template <bool> class Archive, class T> bool do_serialize(Archive<true> &ar, std::unordered_set<T> &v); - -template <template <bool> class Archive, class T> bool do_serialize(Archive<false> &ar, std::set<T> &v); -template <template <bool> class Archive, class T> bool do_serialize(Archive<true> &ar, std::set<T> &v); - namespace serialization { - namespace detail - { - template <typename T> void do_reserve(std::vector<T> &c, size_t N) { c.reserve(N); } - template <typename T> void do_add(std::vector<T> &c, T &&e) { c.emplace_back(std::forward<T>(e)); } - - template <typename T> void do_add(std::deque<T> &c, T &&e) { c.emplace_back(std::forward<T>(e)); } - - template <typename K, typename V> void do_add(serializable_unordered_map<K, V> &c, std::pair<K, V> &&e) { c.insert(std::forward<std::pair<K, V>>(e)); } - - template <typename K, typename V> void do_add(serializable_map<K, V> &c, std::pair<K, V> &&e) { c.insert(std::forward<std::pair<K, V>>(e)); } - - template <typename K, typename V> void do_add(serializable_unordered_multimap<K, V> &c, std::pair<K, V> &&e) { c.insert(std::forward<std::pair<K, V>>(e)); } - - template <typename T> void do_add(std::unordered_set<T> &c, T &&e) { c.insert(std::forward<T>(e)); } - - template <typename T> void do_add(std::set<T> &c, T &&e) { c.insert(std::forward<T>(e)); } - } + //! @brief Is this type a STL-like container? + //! To add a new container to be serialized, partially specialize the template is_container like so: + template <typename T> struct is_container: std::false_type {}; + template <typename... TA> struct is_container<std::deque<TA...>>: std::true_type {}; + template <typename... TA> struct is_container<std::map<TA...>>: std::true_type {}; + template <typename... TA> struct is_container<std::multimap<TA...>>: std::true_type {}; + template <typename... TA> struct is_container<std::set<TA...>>: std::true_type {}; + template <typename... TA> struct is_container<std::unordered_map<TA...>>: std::true_type {}; + template <typename... TA> struct is_container<std::unordered_multimap<TA...>>: std::true_type {}; + template <typename... TA> struct is_container<std::unordered_set<TA...>>: std::true_type {}; + template <typename... TA> struct is_container<std::vector<TA...>>: std::true_type {}; } #include "container.h" -template <template <bool> class Archive, class T> bool do_serialize(Archive<false> &ar, std::vector<T> &v) { return do_serialize_container(ar, v); } -template <template <bool> class Archive, class T> bool do_serialize(Archive<true> &ar, std::vector<T> &v) { return do_serialize_container(ar, v); } - -template <template <bool> class Archive, class T> bool do_serialize(Archive<false> &ar, std::deque<T> &v) { return do_serialize_container(ar, v); } -template <template <bool> class Archive, class T> bool do_serialize(Archive<true> &ar, std::deque<T> &v) { return do_serialize_container(ar, v); } - -template <template <bool> class Archive, typename K, typename V> bool do_serialize(Archive<false> &ar, serializable_unordered_map<K, V> &v) { return do_serialize_container(ar, v); } -template <template <bool> class Archive, typename K, typename V> bool do_serialize(Archive<true> &ar, serializable_unordered_map<K, V> &v) { return do_serialize_container(ar, v); } - -template <template <bool> class Archive, typename K, typename V> bool do_serialize(Archive<false> &ar, serializable_map<K, V> &v) { return do_serialize_container(ar, v); } -template <template <bool> class Archive, typename K, typename V> bool do_serialize(Archive<true> &ar, serializable_map<K, V> &v) { return do_serialize_container(ar, v); } - -template <template <bool> class Archive, typename K, typename V> bool do_serialize(Archive<false> &ar, serializable_unordered_multimap<K, V> &v) { return do_serialize_container(ar, v); } -template <template <bool> class Archive, typename K, typename V> bool do_serialize(Archive<true> &ar, serializable_unordered_multimap<K, V> &v) { return do_serialize_container(ar, v); } - -template <template <bool> class Archive, class T> bool do_serialize(Archive<false> &ar, std::unordered_set<T> &v) { return do_serialize_container(ar, v); } -template <template <bool> class Archive, class T> bool do_serialize(Archive<true> &ar, std::unordered_set<T> &v) { return do_serialize_container(ar, v); } - -template <template <bool> class Archive, class T> bool do_serialize(Archive<false> &ar, std::set<T> &v) { return do_serialize_container(ar, v); } -template <template <bool> class Archive, class T> bool do_serialize(Archive<true> &ar, std::set<T> &v) { return do_serialize_container(ar, v); } +template <class Archive, class Container> +std::enable_if_t<::serialization::is_container<Container>::value, bool> +do_serialize(Archive &ar, Container &c) +{ + return ::do_serialize_container(ar, c); +} |