COMMS
Template library intended to help with implementation of communication protocols.
MsgFactory.h
Go to the documentation of this file.
1 //
2 // Copyright 2015 - 2024 (C). Alex Robenko. All rights reserved.
3 //
4 // This Source Code Form is subject to the terms of the Mozilla Public
5 // License, v. 2.0. If a copy of the MPL was not distributed with this
6 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 
10 
11 #pragma once
12 
13 #include <type_traits>
14 #include <algorithm>
15 
16 #include "comms/Assert.h"
18 #include "comms/util/Tuple.h"
19 #include "comms/util/alloc.h"
20 #include "details/MsgFactoryOptionsParser.h"
21 #include "details/MsgFactoryBase.h"
22 
23 namespace comms
24 {
25 
85 template <typename TMsgBase, typename TAllMessages, typename... TOptions>
86 class MsgFactory : private details::MsgFactoryBase<TMsgBase, TAllMessages, TOptions...>
87 {
88  using Base = details::MsgFactoryBase<TMsgBase, TAllMessages, TOptions...>;
89  static_assert(TMsgBase::hasMsgIdType(),
90  "Usage of MsgFactory requires Message interface to provide ID type. "
91  "Use comms::option::def::MsgIdType option in message interface type definition.");
92 
93 public:
95  using ParsedOptions = typename Base::ParsedOptions;
96 
98  using Message = typename Base::Message;
99 
101  using MsgIdParamType = typename Base::MsgIdParamType;
102 
104  using MsgIdType = typename Base::MsgIdType;
105 
109  using MsgPtr = typename Base::MsgPtr;
110 
112  using AllMessages = typename Base::AllMessages;
113 
116 
119  using GenericMessage = typename ParsedOptions::GenericMessage;
120 
138  MsgPtr createMsg(MsgIdParamType id, unsigned idx = 0U, CreateFailureReason* reason = nullptr) const
139  {
140  return Base::createMsg(id, idx, reason);
141  }
142 
155  MsgPtr createGenericMsg(MsgIdParamType id, unsigned idx = 0U) const
156  {
157  return Base::createGenericMsg(id, idx);
158  }
159 
161  bool canAllocate() const
162  {
163  return Base::canAllocate();
164  }
165 
169  std::size_t msgCount(MsgIdParamType id) const
170  {
171  return Base::msgCount(id);
172  }
173 
176  static constexpr bool hasUniqueIds()
177  {
178  return Base::hasUniqueIds();
179  }
180 
186  static constexpr bool isDispatchPolymorphic()
187  {
188  return Base::isDispatchPolymorphic();
189  }
190 
196  static constexpr bool isDispatchStaticBinSearch()
197  {
198  return Base::isDispatchStaticBinSearch();
199  }
200 
206  static constexpr bool isDispatchLinearSwitch()
207  {
208  return Base::isDispatchLinearSwitch();
209  }
210 
213  static constexpr bool hasInPlaceAllocation()
214  {
215  return ParsedOptions::HasInPlaceAllocation;
216  }
217 
219  static constexpr bool hasGenericMessageSupport()
220  {
221  return ParsedOptions::HasSupportGenericMessage;
222  }
223 
225  static constexpr bool hasForcedDispatch()
226  {
227  return ParsedOptions::HasForcedDispatch;
228  }
229 };
230 
231 
232 } // namespace comms
233 
This file contains classes required for generic custom assertion functionality.
Contains definition of comms::MsgFactoryCreateFailureReason enum.
Contains various tuple type manipulation classes and functions.
This file contains various generic allocator classes that may be used to allocate objects using dynam...
Message factory class.
Definition: MsgFactory.h:87
typename Base::MsgIdType MsgIdType
Type of the message ID.
Definition: MsgFactory.h:104
bool canAllocate() const
Inquiry whether allocation is possible.
Definition: MsgFactory.h:161
static constexpr bool hasGenericMessageSupport()
Compile time inquiry whether factory supports comms::GenericMessage allocation.
Definition: MsgFactory.h:219
static constexpr bool isDispatchStaticBinSearch()
Compile time inquiry whether static binary search dispatch is generated internally to map message ID ...
Definition: MsgFactory.h:196
std::size_t msgCount(MsgIdParamType id) const
Get number of message types from AllMessages, that have the specified ID.
Definition: MsgFactory.h:169
static constexpr bool isDispatchLinearSwitch()
Compile time inquiry whether linear switch dispatch is generated internally to map message ID to actu...
Definition: MsgFactory.h:206
MsgPtr createMsg(MsgIdParamType id, unsigned idx=0U, CreateFailureReason *reason=nullptr) const
Create message object given the ID of the message.
Definition: MsgFactory.h:138
typename Base::ParsedOptions ParsedOptions
Parsed options.
Definition: MsgFactory.h:95
static constexpr bool hasUniqueIds()
Compile time inquiry whether all the message classes in the TAllMessages bundle have unique IDs.
Definition: MsgFactory.h:176
static constexpr bool hasInPlaceAllocation()
Compile time inquiry whether factory supports in-place allocation.
Definition: MsgFactory.h:213
MsgPtr createGenericMsg(MsgIdParamType id, unsigned idx=0U) const
Allocate and initialise comms::GenericMessage object.
Definition: MsgFactory.h:155
typename ParsedOptions::GenericMessage GenericMessage
type of generic message.
Definition: MsgFactory.h:119
static constexpr bool hasForcedDispatch()
Compile time inquiry whether factory has forced dispatch method.
Definition: MsgFactory.h:225
typename Base::Message Message
Type of the common base class of all the messages.
Definition: MsgFactory.h:98
typename Base::AllMessages AllMessages
All messages provided as template parameter to this class.
Definition: MsgFactory.h:112
typename Base::MsgIdParamType MsgIdParamType
Type of the message ID when passed as a parameter.
Definition: MsgFactory.h:101
static constexpr bool isDispatchPolymorphic()
Compile time inquiry whether polymorphic dispatch tables are generated internally to map message ID t...
Definition: MsgFactory.h:186
typename Base::MsgPtr MsgPtr
Smart pointer to Message which holds allocated message object.
Definition: MsgFactory.h:109
comms::option::def::MsgIdType< T > MsgIdType
Same as comms::option::def::MsgIdType.
Definition: options.h:1448
Main namespace for all classes / functions of COMMS library.
MsgFactoryCreateFailureReason
Definition: MsgFactoryCreateFailureReason.h:18