Stroika Library 3.0d24
 
Loading...
Searching...
No Matches
ArchtypeClasses.h
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2026. All rights reserved
3 */
4#ifndef _Stroika_Frameworks_Test_ArchtypeClasses_h_
5#define _Stroika_Frameworks_Test_ArchtypeClasses_h_ 1
6
7#include "Stroika/Foundation/StroikaPreComp.h"
8
9#include "Stroika/Foundation/Common/Common.h"
11
12/**
13 * This module needs a lot of work. Need a set of basic archetype classes for things that are not copyable, copyable, default constructible etc
14 * to use in regression test (especially container) test cases...
15 */
17
21
22 /**
23 * \brief IINTeroperable = constructible, and convertible to size_t int as well; for simple use in regression tests
24 */
25 template <typename T>
26 concept IINTeroperable = requires (T t) {
27 { static_cast<size_t> (T{1u}) } -> convertible_to<size_t>;
28 { T{1u} + T{1u} } -> convertible_to<T>;
29 };
30
31 /**
32 * \note Until Stroika v3.0d10 - this was called 'NotCopyable'
33 */
45 static_assert (default_initializable<OnlyDefaultConstructibleAndMoveable>);
46 static_assert (not copyable<OnlyDefaultConstructibleAndMoveable>);
47 static_assert (move_constructible<OnlyDefaultConstructibleAndMoveable>);
48 static_assert (not equality_comparable<OnlyDefaultConstructibleAndMoveable>);
49 static_assert (not totally_ordered<OnlyDefaultConstructibleAndMoveable>);
50 static_assert (not semiregular<OnlyDefaultConstructibleAndMoveable>);
51 static_assert (not regular<OnlyDefaultConstructibleAndMoveable>);
52
53 using NotCopyable [[deprecated ("Since Stroika v3.0d10 use OnlyDefaultConstructibleAndMoveable")]] = OnlyDefaultConstructibleAndMoveable;
54
55 /**
56 * Object NOT default constructible, is copyable (and assignable); supports operator+ and operator==, and operator<, operator <=>, etc
57 *
58 * \note before Stroika v3.0d10 this was called 'SimpleClass'
59 */
61 public:
62 OnlyCopyableMoveableAndTotallyOrdered () noexcept = delete;
67
70
71 [[deprecated ("Since Stroika v3.0d10 use static_cast<size_t>")]] size_t GetValue () const
72 {
73 return fValue_;
74 }
75 static size_t GetTotalLiveCount ();
76
78 explicit operator size_t () const;
79
80 bool operator== (const OnlyCopyableMoveableAndTotallyOrdered& rhs) const;
81 strong_ordering operator<=> (const OnlyCopyableMoveableAndTotallyOrdered& rhs) const;
82
83 private:
84 size_t fValue_;
85 int fConstructed_;
86 static inline size_t sTotalLiveObjects_{0};
87 };
88 static_assert (not default_initializable<OnlyCopyableMoveableAndTotallyOrdered>);
89 static_assert (copyable<OnlyCopyableMoveableAndTotallyOrdered>);
90 static_assert (move_constructible<OnlyCopyableMoveableAndTotallyOrdered>);
91 static_assert (equality_comparable<OnlyCopyableMoveableAndTotallyOrdered>);
92 static_assert (totally_ordered<OnlyCopyableMoveableAndTotallyOrdered>); // @todo allow this to be totally ordered...
93 static_assert (not semiregular<OnlyCopyableMoveableAndTotallyOrdered>);
94 static_assert (not regular<OnlyCopyableMoveableAndTotallyOrdered>);
96
97 using SimpleClass [[deprecated ("Since Stroika v3.0d10 use OnlyCopyableMoveableAndTotallyOrdered")]] = OnlyCopyableMoveableAndTotallyOrdered;
98
99 /**
100 * Object NOT default constructible, is copyable, assignable; supports operator+, but NO COMPARISON operators
101 * \note before Stroika v3.0d10 this was called 'SimpleClassWithoutComparisonOperators'
102 */
104 public:
105 OnlyCopyableMoveable () noexcept = delete;
108 OnlyCopyableMoveable (size_t v);
110
111 OnlyCopyableMoveable& operator= (OnlyCopyableMoveable&& rhs) = default;
112 OnlyCopyableMoveable& operator= (const OnlyCopyableMoveable&) = default;
113
114 [[deprecated ("Since Stroika v3.0d10 use static_cast<size_t>")]] size_t GetValue () const
115 {
116 return fValue_;
117 }
118 static size_t GetTotalLiveCount ();
119
120 explicit operator size_t () const;
121 OnlyCopyableMoveable operator+ (const OnlyCopyableMoveable& rhs) const;
122
123 private:
124 size_t fValue_;
125 int fConstructed_;
126 static inline size_t sTotalLiveObjects_{0};
127 };
128 static_assert (not default_initializable<OnlyCopyableMoveable>);
129 static_assert (copyable<OnlyCopyableMoveable>);
130 static_assert (move_constructible<OnlyCopyableMoveable>);
131 static_assert (not equality_comparable<OnlyCopyableMoveable>);
132 static_assert (not totally_ordered<OnlyCopyableMoveable>); // @todo allow this to be totally ordered...
133 static_assert (not semiregular<OnlyCopyableMoveable>);
134 static_assert (not regular<OnlyCopyableMoveable>);
136
137 using SimpleClassWithoutComparisonOperators [[deprecated ("Since Stroika v3.0d10 use OnlyCopyableMoveable")]] = OnlyCopyableMoveable;
138
139 /**
140 * \brief Copyable, but its copy CTOR throws on demand, and it counts its live instances - for testing the exception safety of containers
141 *
142 * Set sCopiesUntilThrow to N to let the next N copies succeed and make the one after that throw;
143 * -1 (the default) never throws. Restore it to -1 when done, since it is a plain static.
144 *
145 * sLiveCount is what makes a LEAK observable, without any allocator instrumentation: a container
146 * node that was never destroyed holds a T that was never destroyed, so a live count that fails to
147 * return to its starting value IS the leak. Sample it before the operation and compare after:
148 *
149 * \code
150 * const int kLiveBefore = CopyableWithThrowingCopyCTOR::sLiveCount;
151 * CopyableWithThrowingCopyCTOR::sCopiesUntilThrow = 3; // 4th copy throws
152 * EXPECT_THROW (SomeContainer{src}, runtime_error);
153 * CopyableWithThrowingCopyCTOR::sCopiesUntilThrow = -1;
154 * EXPECT_EQ (kLiveBefore, CopyableWithThrowingCopyCTOR::sLiveCount);
155 * \endcode
156 *
157 * \note NOT thread safe - the counters are plain statics, so use from one thread at a time.
158 */
160 public:
161 static inline int sLiveCount = 0;
162 static inline int sCopiesUntilThrow = -1; // -1 => never throw
163
164 public:
165 CopyableWithThrowingCopyCTOR (int value = 0)
166 : fValue_{value}
167 {
168 ++sLiveCount;
169 }
171 : fValue_{src.fValue_}
172 {
173 if (sCopiesUntilThrow == 0) {
174 throw runtime_error{"CopyableWithThrowingCopyCTOR"};
175 }
176 if (sCopiesUntilThrow > 0) {
177 --sCopiesUntilThrow;
178 }
179 ++sLiveCount;
180 }
182 {
183 --sLiveCount;
184 }
185 CopyableWithThrowingCopyCTOR& operator= (const CopyableWithThrowingCopyCTOR& rhs) = default;
186
187 public:
188 nonvirtual int GetValue () const
189 {
190 return fValue_;
191 }
192 nonvirtual bool operator== (const CopyableWithThrowingCopyCTOR& rhs) const
193 {
194 return fValue_ == rhs.fValue_;
195 }
196
197 private:
198 int fValue_;
199 };
200 static_assert (default_initializable<CopyableWithThrowingCopyCTOR>);
201 static_assert (copyable<CopyableWithThrowingCopyCTOR>);
202 static_assert (equality_comparable<CopyableWithThrowingCopyCTOR>);
203
204 /**
205 * \brief alias for 'regular' type (copyable, default constructible, equality_comparable, etc)
206 * @todo maybe do explicit class cuz Regular should NOT be totally_ordered, but int is...
207 */
208 using Regular = size_t;
209 static_assert (regular<Regular>);
210 static_assert (IINTeroperable<Regular>);
211
212 /**
213 * For types that don't have operator== bulletin, this saves a bit of typing producing a generic comparer (using IINTeroperable)
214 */
215 template <IINTeroperable T>
216 struct AsIntsEqualsComparer : ComparisonRelationDeclarationBase<ComparisonRelationType::eEquals> {
217 bool operator() (T v1, T v2) const
218 {
219 return static_cast<size_t> (v1) == static_cast<size_t> (v2);
220 }
221 };
222
223 /**
224 * For types that don't have operator< bulletin, this saves a bit of typing producing a generic comparer (using IINTeroperable)
225 */
226 template <IINTeroperable T>
227 struct AsIntsLessComparer : ComparisonRelationDeclarationBase<ComparisonRelationType::eStrictInOrder> {
228 bool operator() (T v1, T v2) const
229 {
230 return static_cast<size_t> (v1) < static_cast<size_t> (v2);
231 }
232 };
233
234 /**
235 * For types that don't have operator< bulletin, this saves a bit of typing producing a generic comparer (using IINTeroperable)
236 */
237 template <IINTeroperable T>
238 struct AsIntsThreeWayComparer : ComparisonRelationDeclarationBase<ComparisonRelationType::eThreeWayCompare> {
239 strong_ordering operator() (T v1, T v2) const
240 {
241 return static_cast<size_t> (v1) <=> static_cast<size_t> (v2);
242 }
243 };
244
245}
246#endif /* _Stroika_Frameworks_Test_ArchtypeClasses_h_ */
Copyable, but its copy CTOR throws on demand, and it counts its live instances - for testing the exce...
IINTeroperable = constructible, and convertible to size_t int as well; for simple use in regression t...
size_t Regular
alias for 'regular' type (copyable, default constructible, equality_comparable, etc)