COMMS
Template library intended to help with implementation of communication protocols.
Loading...
Searching...
No Matches
ReadIteratorHelper.h
1//
2// Copyright 2019 - 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
8#include "comms/Message.h"
9#include "comms/details/detect.h"
10#include "comms/details/tag.h"
12
13namespace comms
14{
15
16namespace details
17{
18
19template <typename...>
20class ReadIteratorHelper
21{
22 template <typename... TParams>
23 using HasReadIterTag = comms::details::tag::Tag1<>;
24
25 template <typename... TParams>
26 using MsgPointerTag = comms::details::tag::Tag2<>;
27
28 template <typename... TParams>
29 using CastTag = comms::details::tag::Tag3<>;
30
31 template <typename TMsg>
32 using MsgObjTag =
33 typename comms::util::LazyShallowConditional<
34 TMsg::hasRead()
35 >::template Type<
36 HasReadIterTag,
37 CastTag
38 >;
39
40 template <typename TMsg>
41 using MsgPtrOrCastTag =
42 typename comms::util::LazyShallowConditional<
43 hasElementType<TMsg>()
44 >::template Type<
45 MsgPointerTag,
46 CastTag
47 >;
48
49 template <typename TMsg>
50 using Tag =
51 typename comms::util::LazyShallowConditional<
52 comms::isMessage<TMsg>()
53 >::template Type<
54 MsgObjTag,
55 MsgPtrOrCastTag,
56 TMsg
57 >;
58
59 template <typename TMsg, typename TIter, typename... TParams>
60 static auto getInternal(TIter&& iter, HasReadIterTag<TParams...>) -> typename TMsg::ReadIterator
61 {
62 static_assert(std::is_convertible<typename std::decay<decltype(iter)>::type, typename TMsg::ReadIterator>::value,
63 "Provided iterator is not convertible to read iterator type used by message interface");
64 return typename TMsg::ReadIterator(std::forward<TIter>(iter));
65 }
66
67 template <typename TMsg, typename TIter, typename... TParams>
68 static auto getInternal(TIter&& iter, CastTag<TParams...>) -> decltype(std::forward<TIter>(iter))
69 {
70 return std::forward<TIter>(iter);
71 }
72
73 template <typename TMsg, typename TIter, typename... TParams>
74 static auto getInternal(TIter&& iter, MsgPointerTag<TParams...>) ->
75 decltype(getInternal<typename TMsg::element_type>(std::forward<TIter>(iter), Tag<typename TMsg::element_type>()))
76 {
77 return getInternal<typename TMsg::element_type>(std::forward<TIter>(iter), Tag<typename TMsg::element_type>());
78 }
79
80public:
81 template <typename TMsg, typename TIter>
82 static auto get(TIter&& iter) -> decltype(getInternal<TMsg>(std::forward<TIter>(iter), Tag<TMsg>()))
83 {
84 return getInternal<TMsg>(std::forward<TIter>(iter), Tag<TMsg>());
85 }
86};
87
88} // namespace details
89
90} // namespace comms
Contains definition of Message object interface and various base classes for custom messages.
Main namespace for all classes / functions of COMMS library.
Replacement to some types from standard type_traits.