COMMS
Template library intended to help with implementation of communication protocols.
Loading...
Searching...
No Matches
AssignHelper.h
1//
2// Copyright 2020 - 2025 (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#pragma once
9
10#include "comms/Assert.h"
11#include "comms/details/tag.h"
12#include "comms/util/detect.h"
14
15#include <iterator>
16#include <type_traits>
17
18namespace comms
19{
20
21namespace util
22{
23
24namespace details
25{
26
27template <typename...>
28class AssignHelper
29{
30public:
31 template <typename T, typename TIter>
32 static void assign(T& obj, TIter from, TIter to)
33 {
34 using ObjType = typename std::decay<decltype(obj)>::type;
35 static_assert(!std::is_same<Tag<ObjType>, UnknownTag<> >::value, "Assignment to provided type is not supported");
36 assignInternal(obj, from, to, Tag<ObjType>());
37 }
38
39private:
40 template <typename... TParams>
41 using UseAssignTag = comms::details::tag::Tag1<>;
42
43 template <typename... TParams>
44 using UsePtrSizeConstructorTag = comms::details::tag::Tag2<>;
45
46 template <typename... TParams>
47 using StdSpanTag = comms::details::tag::Tag3<>;
48
49 template <typename... TParams>
50 using UnknownTag = comms::details::tag::Tag4<>;
51
52 template <typename... TParams>
53 using HasReserveTag = comms::details::tag::Tag5<>;
54
55 template <typename... TParams>
56 using NoReserveTag = comms::details::tag::Tag6<>;
57
58 template <typename T>
59 using ConstructorTag =
60 typename comms::util::LazyShallowConditional<
61 comms::util::detect::hasPtrSizeConstructor<T>()
62 >::template Type<
63 UsePtrSizeConstructorTag,
64 UnknownTag
65 >;
66
67 template <typename T>
68 using SpanConstructorTag =
69 typename comms::util::LazyShallowConditional<
70 comms::util::detect::details::IsStdSpan<T>::Value
71 >::template Type<
72 StdSpanTag,
73 ConstructorTag,
74 T
75 >;
76
77 template <typename T>
78 using Tag =
79 typename comms::util::LazyShallowConditional<
80 comms::util::detect::hasAssignFunc<T>()
81 >::template Type<
82 UseAssignTag,
83 SpanConstructorTag,
84 T
85 >;
86
87 template <typename T>
88 using ReserveTag =
89 typename comms::util::LazyShallowConditional<
90 comms::util::detect::hasReserveFunc<T>()
91 >::template Type<
92 HasReserveTag,
93 NoReserveTag
94 >;
95
96 template <typename T, typename TIter, typename... TParams>
97 static void assignInternal(T& obj, TIter from, TIter to, UseAssignTag<TParams...>)
98 {
99 using ObjType = typename std::decay<decltype(obj)>::type;
100 auto len = static_cast<std::size_t>(std::distance(from, to));
101 reserveInternal(obj, len, ReserveTag<ObjType>());
102 obj.assign(from, to);
103 }
104
105 template <typename T, typename TIter, typename... TParams>
106 static void assignInternal(T& obj, TIter from, TIter to, UsePtrSizeConstructorTag<TParams...>)
107 {
108 using IterType = typename std::decay<TIter>::type;
109 using IterTag = typename std::iterator_traits<IterType>::iterator_category;
110 static_assert(std::is_base_of<std::random_access_iterator_tag, IterTag>::value,
111 "Only random access iterator is supported for provided type assignments");
112
113 auto diff = std::distance(from, to);
114 if (diff < 0) {
115 static constexpr bool Invalid_iterators_used_for_assignment = false;
116 static_cast<void>(Invalid_iterators_used_for_assignment);
117 COMMS_ASSERT(Invalid_iterators_used_for_assignment);
118 return;
119 }
120
121 using ObjType = typename std::decay<decltype(obj)>::type;
122
123 if (diff == 0) {
124 obj = ObjType();
125 return;
126 }
127
128 obj = ObjType(&(*from), static_cast<std::size_t>(diff));
129 }
130
131 template <typename T, typename TIter, typename... TParams>
132 static void assignInternal(T& obj, TIter from, TIter to, StdSpanTag<TParams...>)
133 {
134 using ObjType = typename std::decay<decltype(obj)>::type;
135 using ConstPointerType = typename ObjType::const_pointer;
136 using PointerType = typename ObjType::pointer;
137 auto fromPtr = const_cast<PointerType>(reinterpret_cast<ConstPointerType>(&(*from)));
138 auto toPtr = fromPtr + std::distance(from, to);
139 assignInternal(obj, fromPtr, toPtr, UsePtrSizeConstructorTag<TParams...>());
140 }
141
142 template <typename T, typename... TParams>
143 static void reserveInternal(T& obj, std::size_t len, HasReserveTag<TParams...>)
144 {
145 obj.reserve(len);
146 }
147
148 template <typename T, typename... TParams>
149 static void reserveInternal(T& obj, std::size_t len, NoReserveTag<TParams...>)
150 {
151 static_cast<void>(obj);
152 static_cast<void>(len);
153 }
154};
155
156} // namespace details
157
158} // namespace util
159
160} // namespace comms
This file contains classes required for generic custom assertion functionality.
#define COMMS_ASSERT(expr)
Generic assert macro.
Definition Assert.h:168
void assign(T &obj, TIter from, TIter to)
Assigns a new value to provided object.
Definition assign.h:39
Main namespace for all classes / functions of COMMS library.
Replacement to some types from standard type_traits.
Various compile-time detection functions of whether specific member functions and/or types exist.