COMMS
Template library intended to help with implementation of communication protocols.
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Static Public Member Functions | List of all members
comms::MsgFactory< TMsgBase, TAllMessages, TOptions > Class Template Reference

#include "comms/MsgFactory.h"

Detailed Description

template<typename TMsgBase, typename TAllMessages, typename... TOptions>
class comms::MsgFactory< TMsgBase, TAllMessages, TOptions >

Message factory class.

It is responsible to create message objects given the ID of the message. This class DOESN'T use dynamic memory allocation to store its internal data structures, hence can be used in any bare-metal and other embedded environment.
The types of all messages provided in TAllMessages are analysed at compile time and best "id to message object" mapping strategy is chosen, unless the "dispatch" type forcing options are used (see description below).

Template Parameters
TMsgBaseCommon base class for all the messages, smart pointer to this type is returned when allocation of specify message is requested.
TAllMessagesAll custom message types, that this factory is capable of creating, bundled in std::tuple<>. The message types must be sorted based on their IDs. Different variants of the same message (reporting same ID, but implemented as different classes) are also supported. However they must follow one another in this std::tuple, i.e. be sorted.
TOptionsZero or more options. The supported options are:
Precondition
TMsgBase is a base class for all the messages in TAllMessages.
Message type is TAllMessages must be sorted based on their IDs.
If comms::option::app::InPlaceAllocation option is provided, only one custom message can be allocated. The next one can be allocated only after previous message has been destructed.

Inherits comms::details::MsgFactoryBase< TMsgBase, TAllMessages, TOptions... >.

Public Types

using AllMessages = typename Base::AllMessages
 All messages provided as template parameter to this class.
 
using CreateFailureReason = MsgFactoryCreateFailureReason
 Reason for message creation failure.
 
using GenericMessage = typename ParsedOptions::GenericMessage
 type of generic message.
 
using Message = typename Base::Message
 Type of the common base class of all the messages.
 
using MsgIdParamType = typename Base::MsgIdParamType
 Type of the message ID when passed as a parameter.
 
using MsgIdType = typename Base::MsgIdType
 Type of the message ID.
 
using MsgPtr = typename Base::MsgPtr
 Smart pointer to Message which holds allocated message object.
 
using ParsedOptions = typename Base::ParsedOptions
 Parsed options.
 

Public Member Functions

bool canAllocate () const
 Inquiry whether allocation is possible.
 
MsgPtr createGenericMsg (MsgIdParamType id, unsigned idx=0U) const
 Allocate and initialise comms::GenericMessage object.
 
MsgPtr createMsg (MsgIdParamType id, unsigned idx=0U, CreateFailureReason *reason=nullptr) const
 Create message object given the ID of the message.
 
std::size_t msgCount (MsgIdParamType id) const
 Get number of message types from AllMessages, that have the specified ID.
 

Static Public Member Functions

static constexpr bool hasForcedDispatch ()
 Compile time inquiry whether factory has forced dispatch method.
 
static constexpr bool hasGenericMessageSupport ()
 Compile time inquiry whether factory supports comms::GenericMessage allocation.
 
static constexpr bool hasInPlaceAllocation ()
 Compile time inquiry whether factory supports in-place allocation.
 
static constexpr bool hasUniqueIds ()
 Compile time inquiry whether all the message classes in the TAllMessages bundle have unique IDs.
 
static constexpr bool isDispatchLinearSwitch ()
 Compile time inquiry whether linear switch dispatch is generated internally to map message ID to actual type.
 
static constexpr bool isDispatchPolymorphic ()
 Compile time inquiry whether polymorphic dispatch tables are generated internally to map message ID to actual type.
 
static constexpr bool isDispatchStaticBinSearch ()
 Compile time inquiry whether static binary search dispatch is generated internally to map message ID to actual type.
 

Member Typedef Documentation

◆ GenericMessage

template<typename TMsgBase , typename TAllMessages , typename... TOptions>
using comms::MsgFactory< TMsgBase, TAllMessages, TOptions >::GenericMessage = typename ParsedOptions::GenericMessage

type of generic message.

If supported a variant of comms::GenericMessage, void otherwise

◆ MsgPtr

template<typename TMsgBase , typename TAllMessages , typename... TOptions>
using comms::MsgFactory< TMsgBase, TAllMessages, TOptions >::MsgPtr = typename Base::MsgPtr

Smart pointer to Message which holds allocated message object.

It is a variant of std::unique_ptr, based on whether comms::option::app::InPlaceAllocation option was used.

Member Function Documentation

◆ createGenericMsg()

template<typename TMsgBase , typename TAllMessages , typename... TOptions>
MsgPtr comms::MsgFactory< TMsgBase, TAllMessages, TOptions >::createGenericMsg ( MsgIdParamType  id,
unsigned  idx = 0U 
) const

Allocate and initialise comms::GenericMessage object.

If comms::option::app::SupportGenericMessage option hasn't been provided, this function will return empty MsgPtr pointer. Otherwise the relevant allocator will be used to allocate comms::GenericMessage.

Parameters
[in]idID of the message, will be passed as a parameter to the constructor of the comms::GenericMessage class
idxRelative index (or offset) of the message with the same ID. In case protocol implementation contains multiple distinct message types that report same ID value, it must be possible to choose the relative index of such message from the first message type reporting the same ID. This parameter provides such an ability. However, most protocols will implement single message class for single ID.

◆ createMsg()

template<typename TMsgBase , typename TAllMessages , typename... TOptions>
MsgPtr comms::MsgFactory< TMsgBase, TAllMessages, TOptions >::createMsg ( MsgIdParamType  id,
unsigned  idx = 0U,
CreateFailureReason reason = nullptr 
) const

Create message object given the ID of the message.

The id to mapping is performed using the chosen (or default) dispatch policy described in the class options.

Parameters
idID of the message.
idxRelative index (or offset) of the message with the same ID. In case protocol implementation contains multiple distinct message types that report same ID value, it must be possible to choose the relative index of such message from the first message type reporting the same ID. This parameter provides such an ability. However, most protocols will implement single message class for single ID. For such implementations, use default value of this parameter.
[out]reasonFailure reason in case creation has failed. May be nullptr.
Returns
Smart pointer (variant of std::unique_ptr) to Message type, which is a common base class of all the messages (provided as first template parameter to this class). If comms::option::app::InPlaceAllocation option was used and previously allocated message wasn't de-allocated yet, the empty (null) pointer will be returned.

◆ hasInPlaceAllocation()

template<typename TMsgBase , typename TAllMessages , typename... TOptions>
static constexpr bool comms::MsgFactory< TMsgBase, TAllMessages, TOptions >::hasInPlaceAllocation ( )
staticconstexpr

Compile time inquiry whether factory supports in-place allocation.

Returns
true in case of in-place allocation, false in case of dynamic memory use.

◆ isDispatchLinearSwitch()

template<typename TMsgBase , typename TAllMessages , typename... TOptions>
static constexpr bool comms::MsgFactory< TMsgBase, TAllMessages, TOptions >::isDispatchLinearSwitch ( )
staticconstexpr

Compile time inquiry whether linear switch dispatch is generated internally to map message ID to actual type.

See also
Advanced Guide to Message Dispatching
comms::MsgFactory::isDispatchStaticBinSearch()
comms::MsgFactory::isDispatchLinearSwitch()

◆ isDispatchPolymorphic()

template<typename TMsgBase , typename TAllMessages , typename... TOptions>
static constexpr bool comms::MsgFactory< TMsgBase, TAllMessages, TOptions >::isDispatchPolymorphic ( )
staticconstexpr

Compile time inquiry whether polymorphic dispatch tables are generated internally to map message ID to actual type.

See also
Advanced Guide to Message Dispatching
comms::MsgFactory::isDispatchStaticBinSearch()
comms::MsgFactory::isDispatchLinearSwitch()

◆ isDispatchStaticBinSearch()

template<typename TMsgBase , typename TAllMessages , typename... TOptions>
static constexpr bool comms::MsgFactory< TMsgBase, TAllMessages, TOptions >::isDispatchStaticBinSearch ( )
staticconstexpr

Compile time inquiry whether static binary search dispatch is generated internally to map message ID to actual type.

See also
Advanced Guide to Message Dispatching
comms::MsgFactory::isDispatchPolymorphic()
comms::MsgFactory::isDispatchLinearSwitch()

◆ msgCount()

template<typename TMsgBase , typename TAllMessages , typename... TOptions>
std::size_t comms::MsgFactory< TMsgBase, TAllMessages, TOptions >::msgCount ( MsgIdParamType  id) const

Get number of message types from AllMessages, that have the specified ID.

Parameters
idID of the message.
Returns
Number of message classes that report same ID.

The documentation for this class was generated from the following file: