COMMS
Template library intended to help with implementation of communication protocols.
Loading...
Searching...
No Matches
MaxSizeOf.h
1//
2// Copyright 2023 - 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#pragma once
9
10#include <cstddef>
11#include <limits>
12#include <type_traits>
13
14#include "comms/details/tag.h"
16#include "comms/util/detect.h"
17
18namespace comms
19{
20
21namespace util
22{
23
24namespace details
25{
26
27class MaxSizeOfHelper
28{
29public:
30 template <typename T>
31 static std::size_t maxSizeOf(const T& val)
32 {
33 using DecayedType = typename std::decay<decltype(val)>::type;
34 using Tag =
35 typename comms::util::LazyShallowConditional<
36 comms::util::detect::hasMaxSizeFunc<DecayedType>()
37 >::template Type<
38 HasMaxSizeTag,
39 NoMaxSizeTag
40 >;
41
42 return maxSizeOfInternal(val, Tag());
43 }
44
45private:
46 template <typename... TParams>
47 using HasMaxSizeTag = comms::details::tag::Tag1<>;
48
49 template <typename... TParams>
50 using NoMaxSizeTag = comms::details::tag::Tag2<>;
51
52 template <typename T, typename... TParams>
53 static std::size_t maxSizeOfInternal(const T& val, HasMaxSizeTag<>)
54 {
55 return val.max_size();
56 }
57
58 template <typename T, typename... TParams>
59 static std::size_t maxSizeOfInternal(const T& val, NoMaxSizeTag<>)
60 {
61 static_cast<void>(val);
62 return std::numeric_limits<std::size_t>::max();
63 }
64};
65
66} // namespace details
67
69template <typename T>
70std::size_t maxSizeOf(const T& val)
71{
72 return details::MaxSizeOfHelper::maxSizeOf(val);
73}
74
76
77} // namespace util
78
79} // namespace comms
80
81
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.