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