FairMQ  1.4.14
C++ Message Queuing Library and Framework
ControlMessages.h
1 /********************************************************************************
2  * Copyright (C) 2018 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
3  * *
4  * This software is distributed under the terms of the *
5  * GNU Lesser General Public Licence (LGPL) version 3, *
6  * copied verbatim in the file "LICENSE" *
7  ********************************************************************************/
8 
9 #ifndef FAIR_MQ_OFI_CONTROLMESSAGES_H
10 #define FAIR_MQ_OFI_CONTROLMESSAGES_H
11 
12 #include <FairMQLogger.h>
13 #include <boost/asio/buffer.hpp>
14 #include <boost/container/pmr/memory_resource.hpp>
15 #include <cstdint>
16 #include <functional>
17 #include <memory>
18 #include <type_traits>
19 
20 namespace boost {
21 namespace asio {
22 
23 template<typename PodType>
24 auto buffer(const PodType& obj) -> boost::asio::const_buffer
25 {
26  return boost::asio::const_buffer(static_cast<const void*>(&obj), sizeof(PodType));
27 }
28 
29 } // namespace asio
30 } // namespace boost
31 
32 namespace fair {
33 namespace mq {
34 namespace ofi {
35 
36 enum class ControlMessageType
37 {
38  Empty = 1,
39  PostBuffer,
40  PostMultiPartStartBuffer
41 };
42 
43 struct Empty
44 {};
45 
46 struct PostBuffer
47 {
48  uint64_t size; // buffer size (size_t)
49 };
50 
51 struct PostMultiPartStartBuffer
52 {
53  uint32_t numParts; // buffer size (size_t)
54  uint64_t size; // buffer size (size_t)
55 };
56 
58 {
59  PostBuffer postBuffer;
60  PostMultiPartStartBuffer postMultiPartStartBuffer;
61 };
62 
64 {
65  ControlMessageType type;
67 };
68 
69 template<typename T>
70 using unique_ptr = std::unique_ptr<T, std::function<void(T*)>>;
71 
72 template<typename T, typename... Args>
73 auto MakeControlMessageWithPmr(boost::container::pmr::memory_resource& pmr, Args&&... args)
74  -> ofi::unique_ptr<ControlMessage>
75 {
76  void* mem = pmr.allocate(sizeof(ControlMessage));
77  ControlMessage* ctrl = new (mem) ControlMessage();
78 
79  if (std::is_same<T, PostBuffer>::value) {
80  ctrl->type = ControlMessageType::PostBuffer;
81  ctrl->msg.postBuffer = PostBuffer(std::forward<Args>(args)...);
82  } else if (std::is_same<T, PostMultiPartStartBuffer>::value) {
83  ctrl->type = ControlMessageType::PostMultiPartStartBuffer;
84  ctrl->msg.postMultiPartStartBuffer = PostMultiPartStartBuffer(std::forward<Args>(args)...);
85  } else if (std::is_same<T, Empty>::value) {
86  ctrl->type = ControlMessageType::Empty;
87  }
88 
89  return ofi::unique_ptr<ControlMessage>(ctrl, [&pmr](ControlMessage* p) {
90  p->~ControlMessage();
91  pmr.deallocate(p, sizeof(T));
92  });
93 }
94 
95 template<typename T, typename... Args>
96 auto MakeControlMessage(Args&&... args) -> ControlMessage
97 {
98  ControlMessage ctrl;
99 
100  if (std::is_same<T, PostBuffer>::value) {
101  ctrl.type = ControlMessageType::PostBuffer;
102  } else if (std::is_same<T, PostMultiPartStartBuffer>::value) {
103  ctrl.type = ControlMessageType::PostMultiPartStartBuffer;
104  } else if (std::is_same<T, Empty>::value) {
105  ctrl.type = ControlMessageType::Empty;
106  }
107  ctrl.msg = T(std::forward<Args>(args)...);
108 
109  return ctrl;
110 }
111 
112 } // namespace ofi
113 } // namespace mq
114 } // namespace fair
115 
116 #endif /* FAIR_MQ_OFI_CONTROLMESSAGES_H */
Definition: ControlMessages.h:20
Definition: ControlMessages.h:63
Definition: ControlMessages.h:57
Definition: ControlMessages.h:46
Tools for interfacing containers to the transport via polymorphic allocators.
Definition: DeviceRunner.h:23
Definition: Traits.h:16
Definition: ControlMessages.h:43

privacy