Added support for std::map/std::unordered_map to fr::Packet
This commit is contained in:
parent
20a3bd97d8
commit
4dec5318f7
@ -9,6 +9,8 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <map>
|
||||||
|
#include <unordered_map>
|
||||||
#include "NetworkEncoding.h"
|
#include "NetworkEncoding.h"
|
||||||
#include "Packetable.h"
|
#include "Packetable.h"
|
||||||
#include "Sendable.h"
|
#include "Sendable.h"
|
||||||
@ -142,6 +144,86 @@ namespace fr
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Adds a map to a packet
|
||||||
|
*/
|
||||||
|
template<typename A, typename B>
|
||||||
|
inline Packet &operator<<(const std::map<A, B> &m)
|
||||||
|
{
|
||||||
|
//First store its length
|
||||||
|
*this << static_cast<uint32_t>(m.size());
|
||||||
|
|
||||||
|
//Now each of the elements
|
||||||
|
for(const auto &iter : m)
|
||||||
|
{
|
||||||
|
*this << iter;
|
||||||
|
}
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Extracts a map from the packet
|
||||||
|
*/
|
||||||
|
template<typename A, typename B>
|
||||||
|
inline Packet &operator>>(std::map<A, B> &m)
|
||||||
|
{
|
||||||
|
uint32_t length;
|
||||||
|
|
||||||
|
//First extract the length
|
||||||
|
*this >> length;
|
||||||
|
|
||||||
|
//Now take each of the elements out of the packet
|
||||||
|
for(uint32_t a = 0; a < length; a++)
|
||||||
|
{
|
||||||
|
std::pair<A, B> pair;
|
||||||
|
*this >> pair;
|
||||||
|
m.emplace(std::move(pair));
|
||||||
|
}
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Adds an unordered_map to a packet
|
||||||
|
*/
|
||||||
|
template<typename A, typename B>
|
||||||
|
inline Packet &operator<<(const std::unordered_map<A, B> &m)
|
||||||
|
{
|
||||||
|
//First store its length
|
||||||
|
*this << static_cast<uint32_t>(m.size());
|
||||||
|
|
||||||
|
//Now each of the elements
|
||||||
|
for(const auto &iter : m)
|
||||||
|
{
|
||||||
|
*this << iter;
|
||||||
|
}
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Extracts an unordered_map from the packet
|
||||||
|
*/
|
||||||
|
template<typename A, typename B>
|
||||||
|
inline Packet &operator>>(std::unordered_map<A, B> &m)
|
||||||
|
{
|
||||||
|
uint32_t length;
|
||||||
|
|
||||||
|
//First extract the length
|
||||||
|
*this >> length;
|
||||||
|
|
||||||
|
//Now take each of the elements out of the packet
|
||||||
|
for(uint32_t a = 0; a < length; a++)
|
||||||
|
{
|
||||||
|
std::pair<A, B> pair;
|
||||||
|
*this >> pair;
|
||||||
|
m.emplace(std::move(pair));
|
||||||
|
}
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* For packing pairs
|
* For packing pairs
|
||||||
*/
|
*/
|
||||||
|
|||||||
@ -66,6 +66,26 @@ TEST(PacketTest, pack_and_unpack_stl)
|
|||||||
ASSERT_EQ(c1, c2);
|
ASSERT_EQ(c1, c2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(PacketTest, pack_and_unpack_map)
|
||||||
|
{
|
||||||
|
std::map<std::string, std::string> base = {{"a", "b"}, {"bob", "lob"}};
|
||||||
|
std::map<std::string, std::string> copy;
|
||||||
|
fr::Packet packet;
|
||||||
|
packet << base;
|
||||||
|
packet >> copy;
|
||||||
|
ASSERT_EQ(base, copy);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(PacketTest, pack_and_unpack_unordered_map)
|
||||||
|
{
|
||||||
|
std::unordered_map<std::string, std::string> base = {{"a", "b"}, {"bob", "lob"}};
|
||||||
|
std::unordered_map<std::string, std::string> copy;
|
||||||
|
fr::Packet packet;
|
||||||
|
packet << base;
|
||||||
|
packet >> copy;
|
||||||
|
ASSERT_EQ(base, copy);
|
||||||
|
}
|
||||||
|
|
||||||
TEST(PacketTest, variadic_packet_constructor)
|
TEST(PacketTest, variadic_packet_constructor)
|
||||||
{
|
{
|
||||||
int a1 = 10, a2;
|
int a1 = 10, a2;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user