Arduino Libraries
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
unordered_map_support.hpp
Go to the documentation of this file.
1 // Author: Mario S. Könz <mskoenz@gmx.net>
2 // Date: 14.06.2013 20:43:37 EDT
3 // File: unordered_map_support.hpp
4 
5 /* This program is free software. It comes without any warranty, to
6  * the extent permitted by applicable law. You can redistribute it
7  * and/or modify it under the terms of the Do What The Fuck You Want
8  * To Public License, Version 2, as published by Sam Hocevar. See
9  * http://www.wtfpl.net/ or COPYING for more details. */
10 
11 #ifndef __UNORDERED_MAP_SUPPORT_HEADER
12 #define __UNORDERED_MAP_SUPPORT_HEADER
13 
14 #include <unordered_map>
15 #include <algorithm>
16 #include <vector>
17 
18 #include "vector_support.hpp"
19 
20 //------------------- print & serialize -------------------
21 template<typename K, typename V, typename S>
22 S & operator<<(S & os, std::unordered_map<K, V> const & arg) {
23  os << "[";
24  uint16_t i = 0;
25  std::for_each(arg.begin(), arg.end(),
26  [&](std::pair<K const, V> const & p) {
27  os << "{" << p.first << ": " << p.second << "}";
28  if(++i != arg.size())
29  os << ", ";
30  }
31  );
32  os << "]";
33  return os;
34 }
35 namespace com {
36  template<typename K, typename V, typename Archive>
37  void serialize(Archive & ar, std::unordered_map<K, V> & arg) {
38  typedef typename ustd::vector<K>::size_type size_type;
39  size_type size_ = arg.size();
40  if(Archive::type == archive_enum::input) {
41  arg.clear();
42  K key;
43  V val;
44  std::vector<K> keys;
45  std::vector<V> vals;
46  ar & keys;
47  ar & vals;
48 
49  for(int i = keys.size() - 1; i >= 0; --i) {
50  arg[keys[i]] = vals[i];
51  }
52  }
53  else if(Archive::type == archive_enum::output) {
54  ar & size_;
55  std::for_each(arg.begin(), arg.end(),
56  [&](std::pair<K const, V> const & p) {
57  ar & (p.first);
58  }
59  );
60  ar & size_;
61  std::for_each(arg.begin(), arg.end(),
62  [&](std::pair<K const, V> const & p) {
63  ar & (p.second);
64  }
65  );
66  }
67  }
68 }//end namespace com
69 #endif //__UNORDERED_MAP_SUPPORT_HEADER
void serialize(Archive &ar, T &t)
Definition: serializer.hpp:95
uint16_t size_type
Definition: vector.hpp:24
key
Definition: hid_keys.hpp:14