COMMS
Template library intended to help with implementation of communication protocols.
Loading...
Searching...
No Matches
MsgFactory.h
Go to the documentation of this file.
1//
2// Copyright 2015 - 2026 (C). Alex Robenko. All rights reserved.
3//
4// SPDX-License-Identifier: MPL-2.0
5//
6// This Source Code Form is subject to the terms of the Mozilla Public
7// License, v. 2.0. If a copy of the MPL was not distributed with this
8// file, You can obtain one at http://mozilla.org/MPL/2.0/.
9
12
13#pragma once
14
15#include "comms/Assert.h"
16#include "comms/details/MsgFactoryBase.h"
17#include "comms/details/MsgFactoryOptionsParser.h"
19#include "comms/util/Tuple.h"
20#include "comms/util/alloc.h"
21
22#include <cstddef>
23
24namespace comms
25{
26
86template <typename TMsgBase, typename TAllMessages, typename... TOptions>
87class MsgFactory : private details::MsgFactoryBase<TMsgBase, TAllMessages, TOptions...>
88{
89 using Base = details::MsgFactoryBase<TMsgBase, TAllMessages, TOptions...>;
90 static_assert(TMsgBase::hasMsgIdType(),
91 "Usage of MsgFactory requires Message interface to provide ID type. "
92 "Use comms::option::def::MsgIdType option in message interface type definition.");
93
94public:
96 using ParsedOptions = typename Base::ParsedOptions;
97
99 using Message = typename Base::Message;
100
102 using MsgIdParamType = typename Base::MsgIdParamType;
103
105 using MsgIdType = typename Base::MsgIdType;
106
110 using MsgPtr = typename Base::MsgPtr;
111
113 using AllMessages = typename Base::AllMessages;
114
117
120 using GenericMessage = typename ParsedOptions::GenericMessage;
121
139 MsgPtr createMsg(MsgIdParamType id, unsigned idx = 0U, CreateFailureReason* reason = nullptr) const
140 {
141 return Base::createMsg(id, idx, reason);
142 }
143
156 MsgPtr createGenericMsg(MsgIdParamType id, unsigned idx = 0U) const
157 {
158 return Base::createGenericMsg(id, idx);
159 }
160
162 bool canAllocate() const
163 {
164 return Base::canAllocate();
165 }
166
170 std::size_t msgCount(MsgIdParamType id) const
171 {
172 return Base::msgCount(id);
173 }
174
177 static constexpr bool hasUniqueIds()
178 {
179 return Base::hasUniqueIds();
180 }
181
187 static constexpr bool isDispatchPolymorphic()
188 {
189 return Base::isDispatchPolymorphic();
190 }
191
197 static constexpr bool isDispatchStaticBinSearch()
198 {
199 return Base::isDispatchStaticBinSearch();
200 }
201
207 static constexpr bool isDispatchLinearSwitch()
208 {
209 return Base::isDispatchLinearSwitch();
210 }
211
214 static constexpr bool hasInPlaceAllocation()
215 {
216 return ParsedOptions::HasInPlaceAllocation;
217 }
218
220 static constexpr bool hasGenericMessageSupport()
221 {
222 return ParsedOptions::HasSupportGenericMessage;
223 }
224
226 static constexpr bool hasForcedDispatch()
227 {
228 return ParsedOptions::HasForcedDispatch;
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:88
typename Base::MsgIdType MsgIdType
Type of the message ID.
Definition MsgFactory.h:105
bool canAllocate() const
Inquiry whether allocation is possible.
Definition MsgFactory.h:162
static constexpr bool hasGenericMessageSupport()
Compile time inquiry whether factory supports comms::GenericMessage allocation.
Definition MsgFactory.h:220
static constexpr bool isDispatchStaticBinSearch()
Compile time inquiry whether static binary search dispatch is generated internally to map message ID ...
Definition MsgFactory.h:197
std::size_t msgCount(MsgIdParamType id) const
Get number of message types from AllMessages, that have the specified ID.
Definition MsgFactory.h:170
static constexpr bool isDispatchLinearSwitch()
Compile time inquiry whether linear switch dispatch is generated internally to map message ID to actu...
Definition MsgFactory.h:207
MsgPtr createMsg(MsgIdParamType id, unsigned idx=0U, CreateFailureReason *reason=nullptr) const
Create message object given the ID of the message.
Definition MsgFactory.h:139
typename Base::ParsedOptions ParsedOptions
Parsed options.
Definition MsgFactory.h:96
static constexpr bool hasUniqueIds()
Compile time inquiry whether all the message classes in the TAllMessages bundle have unique IDs.
Definition MsgFactory.h:177
static constexpr bool hasInPlaceAllocation()
Compile time inquiry whether factory supports in-place allocation.
Definition MsgFactory.h:214
MsgPtr createGenericMsg(MsgIdParamType id, unsigned idx=0U) const
Allocate and initialise comms::GenericMessage object.
Definition MsgFactory.h:156
typename ParsedOptions::GenericMessage GenericMessage
type of generic message.
Definition MsgFactory.h:120
static constexpr bool hasForcedDispatch()
Compile time inquiry whether factory has forced dispatch method.
Definition MsgFactory.h:226
typename Base::Message Message
Type of the common base class of all the messages.
Definition MsgFactory.h:99
typename Base::AllMessages AllMessages
All messages provided as template parameter to this class.
Definition MsgFactory.h:113
typename Base::MsgIdParamType MsgIdParamType
Type of the message ID when passed as a parameter.
Definition MsgFactory.h:102
static constexpr bool isDispatchPolymorphic()
Compile time inquiry whether polymorphic dispatch tables are generated internally to map message ID t...
Definition MsgFactory.h:187
typename Base::MsgPtr MsgPtr
Smart pointer to Message which holds allocated message object.
Definition MsgFactory.h:110
Main namespace for all classes / functions of COMMS library.
MsgFactoryCreateFailureReason
Definition MsgFactoryCreateFailureReason.h:20