24#ifndef PROTOBUF_TO_BB_H_
25#define PROTOBUF_TO_BB_H_
27#include "protoboard_types.h"
29#include <blackboard/blackboard.h>
30#include <google/protobuf/message.h>
31#include <logging/logger.h>
33#include <boost/bimap.hpp>
34#include <boost/core/demangle.hpp>
39template <
class IfaceT>
40std::string iface_id_for_type();
46pb_conversion_map make_receiving_interfaces_map();
53class pb_convert :
public std::enable_shared_from_this<pb_convert>
73#pragma GCC diagnostic push
74#pragma GCC diagnostic ignored "-Woverloaded-virtual"
78 virtual void handle(std::shared_ptr<google::protobuf::Message> msg);
82 virtual void handle(
const google::protobuf::Message &msg);
84#pragma GCC diagnostic pop
98template <
class ProtoT,
class IfaceT>
109 :
pb_convert(), interface_(nullptr), name_(boost::core::demangle(typeid(*this).
name()))
126 interface_(std::move(o.interface_)),
127 name_(boost::core::demangle(typeid(*this).
name()))
129 o.interface_ =
nullptr;
139 this->interface_ = o.interface_;
140 o.interface_ =
nullptr;
141 name_ = boost::core::demangle(
typeid(*this).name());
159 std::string iface_id = iface_id_for_type<IfaceT>();
162 if (iface_id.back() !=
'/')
168 logger->
log_info(
name(),
"Initialized %s.", iface_id.c_str());
172 handle(
const google::protobuf::Message &msg)
override
174 handle(
dynamic_cast<const ProtoT &
>(msg));
200 interface_ =
nullptr;
224 return name_.c_str();
232 virtual void handle(
const ProtoT &msg, IfaceT *iface);
244template <
class ProtoT,
class OutputT>
248 typedef google::protobuf::RepeatedPtrField<typename OutputT::input_type> sequence_type;
260 handle(
const google::protobuf::Message &msg)
override
267 typename sequence_type::const_iterator field_it = fields.begin();
269 for (; field_it != fields.end(); ++field_it) {
270 std::string seq_id = OutputT::get_sequence_id(*field_it);
271 auto map_it = sub_converters_.find(seq_id);
272 if (map_it == sub_converters_.end()) {
273 sub_converters_.insert({seq_id, OutputT()});
274 map_it = sub_converters_.find(seq_id);
277 if (!map_it->second.is_open())
279 map_it->second.handle(*field_it);
289 std::unordered_map<std::string, OutputT> sub_converters_;
The BlackBoard abstract class.
virtual Interface * open_for_writing(const char *interface_type, const char *identifier, const char *owner=NULL)=0
Open interface for writing.
virtual void close(Interface *interface)=0
Close interface.
virtual void log_info(const char *component, const char *format,...)
Log informational message.
Default ProtoBuf to blackboard converter.
fawkes::Logger * logger_
Logger from the main thread.
pb_convert()
Empty-init constructor.
virtual void init(fawkes::BlackBoard *blackboard, fawkes::Logger *logger, const std::string &="")
Deferred initialization.
pb_convert(const pb_convert &)=default
Default copy constructor.
virtual void handle(std::shared_ptr< google::protobuf::Message > msg)
Dereference msg and pass it on to handle it by reference.
virtual ~pb_convert()
Destructor. Does nothing since members aren't owned by this class.
pb_convert & operator=(const pb_convert &)=default
Default copy assignment.
fawkes::BlackBoard * blackboard_
Blackboard used by the main thread.
The workhorse of the ProtoBuf to Blackboard conversion.
virtual void handle(const ProtoT &msg, IfaceT *iface)
Write the contents of a ProtoBuf message into the appropriate blackboard interface.
pb_converter(const pb_converter< ProtoT, IfaceT > &)=delete
Copying this is prohibited.
virtual void handle(const google::protobuf::Message &msg) override
Handle a ProtoBuf message by reference.
pb_converter< ProtoT, IfaceT > & operator=(pb_converter< ProtoT, IfaceT > &&o)
Move assignment.
static std::string get_sequence_id(const ProtoT &)
pb_converter< ProtoT, IfaceT > & operator=(const pb_converter< ProtoT, IfaceT > &)=delete
Copying this is prohibited.
pb_converter(pb_converter< ProtoT, IfaceT > &&o)
Move construction.
virtual void init(fawkes::BlackBoard *blackboard, fawkes::Logger *logger, const std::string &id="") override
Deferred initialization, coincides with main thread initialization.
ProtoT input_type
The ProtoBuf message type that goes in.
virtual void handle(const ProtoT &msg)
Handle a ProtoBuf message with known type.
virtual ~pb_converter()
Close blackboard interface on destruction.
virtual void close()
Give up the current blackboard interface (closes it)
IfaceT output_type
The blackboard interface type that the ProtoBuf contents are written to.
pb_converter()
Empty-init.
A special handler for repeated ProtoBuf fields.
virtual void handle(const google::protobuf::Message &msg) override
Handle a repeated field inside a ProtoBuf message, where the individual repeated sub-messages should ...
pb_sequence_converter()
Default constructor.
virtual const sequence_type & extract_sequence(const ProtoT &msg)
Must be implemented by the user.