Add MsgPack class
This commit is contained in:
parent
e9f1d30df6
commit
60b02faa65
|
@ -82,6 +82,10 @@ namespace carla {
|
|||
copy_from(source);
|
||||
}
|
||||
|
||||
Buffer(const value_type *data, size_type size) {
|
||||
copy_from(data, size);
|
||||
}
|
||||
|
||||
Buffer(const Buffer &) = delete;
|
||||
|
||||
Buffer(Buffer &&rhs) noexcept
|
||||
|
|
|
@ -6,4 +6,34 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include "carla/Buffer.h"
|
||||
|
||||
#include <rpc/msgpack.hpp>
|
||||
|
||||
namespace carla {
|
||||
|
||||
class MsgPack {
|
||||
public:
|
||||
|
||||
template <typename T>
|
||||
static Buffer Pack(const T &obj) {
|
||||
namespace mp = ::clmdep_msgpack;
|
||||
mp::sbuffer sbuf;
|
||||
mp::pack(sbuf, obj);
|
||||
return Buffer(reinterpret_cast<const unsigned char *>(sbuf.data()), sbuf.size());
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static T UnPack(const Buffer &buffer) {
|
||||
namespace mp = ::clmdep_msgpack;
|
||||
return mp::unpack(reinterpret_cast<const char *>(buffer.data()), buffer.size()).template as<T>();
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static T UnPack(const unsigned char *data, size_t size) {
|
||||
namespace mp = ::clmdep_msgpack;
|
||||
return mp::unpack(reinterpret_cast<const char *>(data), size).template as<T>();
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace carla
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
#include "test.h"
|
||||
|
||||
#include <carla/ThreadGroup.h>
|
||||
#include <carla/rpc/Actor.h>
|
||||
#include <carla/rpc/Client.h>
|
||||
#include <carla/rpc/Server.h>
|
||||
|
||||
|
@ -56,3 +57,21 @@ TEST(rpc, server_bind_sync_run_on_game_thread) {
|
|||
std::cout << "game thread: run " << i << " slices.\n";
|
||||
ASSERT_TRUE(done);
|
||||
}
|
||||
|
||||
TEST(rpc, msgpack) {
|
||||
namespace c = carla;
|
||||
namespace cg = carla::geom;
|
||||
Actor actor;
|
||||
actor.id = 42u;
|
||||
actor.description.uid = 2u;
|
||||
actor.description.id = "actor.random.whatever";
|
||||
actor.bounding_box = cg::BoundingBox{cg::Vector3D{1.0f, 2.0f, 3.0f}};
|
||||
|
||||
auto buffer = c::MsgPack::Pack(actor);
|
||||
auto result = c::MsgPack::UnPack<Actor>(buffer);
|
||||
|
||||
ASSERT_EQ(result.id, actor.id);
|
||||
ASSERT_EQ(result.description.uid, actor.description.uid);
|
||||
ASSERT_EQ(result.description.id, actor.description.id);
|
||||
ASSERT_EQ(result.bounding_box, actor.bounding_box);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue