Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
CountedValue.h
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4#ifndef _Stroika_Foundation_Common_CountedValue_h_
5#define _Stroika_Foundation_Common_CountedValue_h_ 1
6
7#include "Stroika/Foundation/StroikaPreComp.h"
8
9#include <compare>
10#include <utility>
11
12#include "Stroika/Foundation/Common/Common.h"
13#include "Stroika/Foundation/Common/Concepts.h"
15
16/**
17 * TODO:
18 * @todo add default CTOR iff VALUE_TYPE has default CTOR, and same for other
19 * ops like assignment.
20 *
21 * @todo Use typename Common::ArgByValueType<> more (one missing CTOR and Equals). On VS2k13 generates
22 * compiler errors I don't understand. Low pri...
23 */
24
26
27 /**
28 * CountedValue is the same as a value, but with a count as well.
29 *
30 * Essentially the same as pair<VALUE_TYPE,COUNTER_TYPE> but with more clearly named data elements
31 *
32 * \note <a href="Design-Overview.md#Comparisons">Comparisons</a>:
33 * o static_assert (totally_ordered<VALUE_TYPE> IMPLIES totally_ordered<CountedValue<...>>)
34 * o @todo COULD add EqualsComparer/ThreeWayComparer members which take explicit 'T' comparer argument
35 */
36 template <typename VALUE_TYPE, typename COUNTER_TYPE = unsigned int>
37 requires (default_initializable<COUNTER_TYPE> and unsigned_integral<COUNTER_TYPE>)
38 struct CountedValue {
39 public:
40 /**
41 */
42 using ValueType = VALUE_TYPE;
43
44 public:
45 /**
46 */
47 using CounterType = COUNTER_TYPE;
48
49 public:
50 /**
51 */
52 constexpr CountedValue ()
53 requires (default_initializable<VALUE_TYPE>);
54 constexpr CountedValue (ArgByValueType<ValueType> value, CounterType count = 1);
55 template <convertible_to<VALUE_TYPE> VALUE2_TYPE, convertible_to<COUNTER_TYPE> COUNTER2_TYPE>
56 constexpr CountedValue (const pair<VALUE2_TYPE, COUNTER2_TYPE>& src);
57 template <convertible_to<VALUE_TYPE> VALUE2_TYPE, convertible_to<COUNTER_TYPE> COUNTER2_TYPE>
59
60 public:
61 ValueType fValue;
62 CounterType fCount;
63
64 public:
65 /**
66 */
67 constexpr auto operator<=> (const CountedValue&) const
68 requires (three_way_comparable<VALUE_TYPE>);
69
70 public:
71 /**
72 */
73 constexpr bool operator== (const CountedValue&) const
74 requires (equality_comparable<VALUE_TYPE>);
75 };
76
77 namespace Private_ {
78#if qCompilerAndStdLib_template_concept_matcher_requires_Buggy
79 template <typename T1, typename T2 = void>
80 struct is_CV_ : std::false_type {};
81 template <typename T1, typename T2>
82 struct is_CV_<CountedValue<T1, T2>> : std::true_type {};
83#endif
84 }
85
86 /**
87 */
88 template <typename T>
89 concept ICountedValue =
90#if qCompilerAndStdLib_template_concept_matcher_requires_Buggy
91 Private_::is_CV_<T>::value
92#else
93 requires (T t) {
94 {
95 []<typename VALUE_TYPE, typename COUNTER_TYPE> (CountedValue<VALUE_TYPE, COUNTER_TYPE>) {}(t)
96 };
97 }
98#endif
99 ;
100 static_assert (not ICountedValue<int>);
101 static_assert (ICountedValue<CountedValue<int>>);
102
103}
104
105/*
106 ********************************************************************************
107 ***************************** Implementation Details ***************************
108 ********************************************************************************
109 */
110#include "CountedValue.inl"
111
112#endif /*_Stroika_Foundation_Common_CountedValue_h_*/
conditional_t<(sizeof(CHECK_T)<=2 *sizeof(void *)) and is_trivially_copyable_v< CHECK_T >, CHECK_T, const CHECK_T & > ArgByValueType
This is an alias for 'T' - but how we want to pass it on stack as formal parameter.
Definition TypeHints.h:32