Skip to content

Commit

Permalink
odb: Adds support to serialize and de-serialize std::unordered_map
Browse files Browse the repository at this point in the history
This is based on the already existing case for std::map

Signed-off-by: Felipe Garay <[email protected]>
  • Loading branch information
fgaray committed Dec 27, 2023
1 parent 56a562f commit 0915035
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/odb/include/odb/dbStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include <istream>
#include <ostream>
#include <string>
#include <unordered_map>

#include "ZException.h"
#include "dbObject.h"
Expand Down Expand Up @@ -192,6 +193,18 @@ class dbOStream
return *this;
}

template <class T1, class T2>
dbOStream& operator<<(const std::unordered_map<T1, T2>& m)
{
uint sz = m.size();
*this << sz;
for (auto const& [key, val] : m) {
*this << key;
*this << val;
}
return *this;
}

template <class T, std::size_t SIZE>
dbOStream& operator<<(const std::array<T, SIZE>& a)
{
Expand Down Expand Up @@ -342,6 +355,20 @@ class dbIStream
}
return *this;
}
template <class T1, class T2>
dbIStream& operator>>(std::unordered_map<T1, T2>& m)
{
uint sz;
*this >> sz;
for (uint i = 0; i < sz; i++) {
T1 key;
T2 val;
*this >> key;
*this >> val;
m[key] = val;
}
return *this;
}
template <class T, std::size_t SIZE>
dbIStream& operator>>(std::array<T, SIZE>& a)
{
Expand Down

0 comments on commit 0915035

Please sign in to comment.