common.h
Go to the documentation of this file.
1 /*+-------------------------------------------------------------------------+
2  | MultiVehicle simulator (libmvsim) |
3  | |
4  | Copyright (C) 2014-2020 Jose Luis Blanco Claraco |
5  | Copyright (C) 2017 Borys Tymchenko (Odessa Polytechnic University) |
6  | Distributed under 3-clause BSD License |
7  | See COPYING |
8  +-------------------------------------------------------------------------+ */
9 
10 #pragma once
11 
12 #if defined(MVSIM_HAS_ZMQ) && defined(MVSIM_HAS_PROTOBUF)
13 
14 #include <mrpt/io/CMemoryStream.h>
15 #include <mrpt/serialization/CArchive.h>
16 
17 #include <variant>
18 
19 #include "zmq_fwrds.h"
20 
21 namespace google::protobuf
22 {
23 class MessageLite;
24 }
25 
26 namespace mvsim
27 {
35 void sendMessage(const google::protobuf::MessageLite& m, zmq::socket_t& socket);
36 
38 zmq::message_t receiveMessage(zmq::socket_t& s);
39 
46 void parseMessage(
47  const zmq::message_t& msg, google::protobuf::MessageLite& out);
48 
49 class UnexpectedMessageException : public std::runtime_error
50 {
51  public:
52  UnexpectedMessageException(const char* reason) : std::runtime_error(reason)
53  {
54  }
55 };
56 namespace internal
57 {
58 std::tuple<std::string, std::string> parseMessageToParts(
59  const zmq::message_t& msg);
60 
61 template <typename variant_t, size_t IDX = 0>
62 variant_t recursiveParse(
63  const std::string& typeName, const std::string& serializedData)
64 {
65  if constexpr (IDX < std::variant_size_v<variant_t>)
66  {
67  using this_t = std::variant_alternative_t<IDX, variant_t>;
68  this_t v;
69  const std::string expectedName = v.GetTypeName();
70  if (expectedName == typeName)
71  {
72  bool ok = v.ParseFromString(serializedData);
73  if (!ok)
75  "Format error: protobuf could not decode binary message of "
76  "type '%s'",
77  typeName.c_str());
78  return {v};
79  }
80  else
81  return recursiveParse<variant_t, IDX + 1>(typeName, serializedData);
82  }
83  throw UnexpectedMessageException(
85  "Type '%s' not found in expected list of variant arguments.",
86  typeName.c_str())
87  .c_str());
88 }
89 } // namespace internal
90 
91 std::string get_zmq_endpoint(const zmq::socket_t& s);
92 
98 template <typename variant_t>
99 variant_t parseMessageVariant(const zmq::message_t& msg)
100 {
101  const auto [typeName, serializedData] = internal::parseMessageToParts(msg);
102  return internal::recursiveParse<variant_t>(typeName, serializedData);
103 }
104 
106 template <class... Ts>
107 struct overloaded : Ts...
108 {
109  using Ts::operator()...;
110 };
111 
113 template <class... Ts>
114 overloaded(Ts...)->overloaded<Ts...>;
115 
118 } // namespace mvsim
119 #endif
const GLdouble * v
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
GLdouble s
std::string BASE_IMPEXP format(const char *fmt,...) MRPT_printf_format_check(1
ROSCPP_DECL bool ok()
const GLdouble * m


mvsim
Author(s):
autogenerated on Fri May 7 2021 03:05:51