Belle II Software development
ZMQModuleMessage.h
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8#pragma once
9
10#include <framework/pcore/zmq/messages/ZMQMessageHelper.h>
11
12#include <zmq.hpp>
13#include <memory>
14#include <array>
15
16namespace Belle2 {
23 template <unsigned int AMessageFrameNumber>
25 friend class ZMQMessageFactory;
26
27 public:
29 static constexpr unsigned int c_messageParts = AMessageFrameNumber;
30
32 using MessageParts = std::array<zmq::message_t, ZMQModuleMessage::c_messageParts>;
33
35 static void toSocket(std::unique_ptr<ZMQModuleMessage> message, const std::unique_ptr<zmq::socket_t>& socket)
36 {
37 for (unsigned int i = 0; i < c_messageParts - 1; i++) {
38 socket->send(message->m_messageParts[i], zmq::send_flags::sndmore);
39 }
40 socket->send(message->m_messageParts[c_messageParts - 1], zmq::send_flags::none);
41 }
42
46 void operator=(const ZMQModuleMessage&) = delete;
47
50 {
51 return m_messageParts;
52 };
53
56 {
57 return m_messageParts;
58 };
59
61 template <unsigned int index>
62 const zmq::message_t& getMessagePart() const
63 {
64 return m_messageParts[index];
65 }
66
68 template <unsigned int index>
69 const char* getMessagePartAsCharArray() const
70 {
71 const auto& messagePart = getMessagePart<index>();
72 return static_cast<const char*>(messagePart.data());
73 }
74
76 template <unsigned int index>
77 std::string getMessagePartAsString() const
78 {
79 const auto& messagePart = getMessagePart<index>();
80 return std::string(static_cast<const char*>(messagePart.data()), messagePart.size());
81 }
82
84 template <unsigned int index>
85 zmq::message_t& getMessagePart()
86 {
87 return m_messageParts[index];
88 }
89
91 template <unsigned int index>
93 {
94 auto& messagePart = getMessagePart<index>();
95 return static_cast<char*>(messagePart.data());
96 }
97
98 protected:
100 ZMQModuleMessage() = default;
101
103 template <class ...T>
104 explicit ZMQModuleMessage(T&& ... arguments) :
105 m_messageParts({ZMQMessageHelper::createZMQMessage(std::forward<T>(arguments)) ... })
106 {
107 }
108
109 private:
112 };
114}
Helper class for creating new ID/No-ID messages.
static zmq::message_t createZMQMessage(zmq::message_t message)
Just pass a zmq message.
A general message with as many parts as given as template argument.
static void toSocket(std::unique_ptr< ZMQModuleMessage > message, const std::unique_ptr< zmq::socket_t > &socket)
Send the message to the given socket. As the message is nullified, you have to move it in here.
const char * getMessagePartAsCharArray() const
Get the message part with the given index as char* (const version)
const zmq::message_t & getMessagePart() const
Get the message part with the given index (const version)
zmq::message_t & getMessagePart()
Get the message part with the given index.
void operator=(const ZMQModuleMessage &)=delete
Do not allow to copy a message.
ZMQModuleMessage(const ZMQModuleMessage &)=delete
Do not allow to copy a message.
const MessageParts & getMessageParts() const
Get a const reference to the message parts.
ZMQModuleMessage()=default
Do not allow to create a new message from scratch publicly.
static constexpr unsigned int c_messageParts
The number of message parts this message carries.
std::string getMessagePartAsString() const
Get the message part with the given index as string (const version)
MessageParts m_messageParts
The content of this message as an array of zmq messages. Will be set during constructor or when comin...
char * getMessagePartAsCharArray()
Get the message part with the given index as char*.
std::array< zmq::message_t, ZMQModuleMessage::c_messageParts > MessageParts
The base class of the message parts.
ZMQModuleMessage(T &&... arguments)
Constructor out of different parts.
MessageParts & getMessageParts()
Get a reference to the message parts.
Abstract base class for different kinds of events.