Ark Server API (ASA) - Wiki
Loading...
Searching...
No Matches
RangeBound.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "CoreTypes.h"
6#include "Misc/AssertionMacros.h"
7#include "Templates/TypeHash.h"
8#include "Containers/EnumAsByte.h"
9#include "Misc/FrameNumber.h"
10#include "Misc/DateTime.h"
11
12namespace ERangeBoundTypes
13{
14 /**
15 * Enumerates the valid types of range bounds.
16 */
17 enum Type
18 {
19 /** The range excludes the bound. */
21
22 /** The range includes the bound. */
24
25 /** The bound is open. */
26 Open
27 };
28}
29
30
31/**
32 * Template for range bounds.
33 */
34template<typename ElementType>
36{
37public:
38
39 /*~ Typedef used to pass/return small element types by value rather than const& */
40 typedef typename TCallTraits<ElementType>::ParamType ElementValueOrConstRef;
41
42 /**
43 * Default constructor.
44 *
45 * @see Exclusive, Inclusive, Open
46 */
49 , Value()
50 { }
51
52 /**
53 * Creates a closed bound that includes the specified value.
54 *
55 * @param InValue The bound's value.
56 * @see Exclusive, Inclusive, Open
57 */
60 , Value(InValue)
61 { }
62
63public:
64
65 /**
66 * Compares this bound with the specified bound for equality.
67 *
68 * @param Other The bound to compare with.
69 * @return true if the bounds are equal, false otherwise.
70 */
71 bool operator==(const TRangeBound& Other) const
72 {
73 return ((Type == Other.Type) && (IsOpen() || (Value == Other.Value)));
74 }
75
76 /**
77 * Compares this range with the specified bound for inequality.
78 *
79 * @param Other The bound to compare with.
80 * @return true if the bounds are not equal, false otherwise.
81 */
82 bool operator!=(const TRangeBound& Other) const
83 {
84 return ((Type != Other.Type) || (!IsOpen() && (Value != Other.Value)));
85 }
86
87public:
88
89 /**
90 * Gets the bound's value.
91 *
92 * Use IsClosed() to verify that this bound is closed before calling this method.
93 *
94 * @return Bound value.
95 * @see IsOpen
96 */
98 {
100
101 return Value;
102 }
103
104
105 /**
106 * Sets the bound's value, maintining the inclusivity of the bound
107 *
108 * Use IsClosed() to verify that this bound is closed before calling this method.
109 *
110 * @param NewValue New bound value.
111 * @see IsClosed
112 */
114 {
116
117 Value = NewValue;
118 }
119
120 /**
121 * Checks whether the bound is closed.
122 *
123 * @return true if the bound is closed, false otherwise.
124 */
126 {
127 return (Type != ERangeBoundTypes::Open);
128 }
129
130 /**
131 * Checks whether the bound is exclusive.
132 *
133 * @return true if the bound is exclusive, false otherwise.
134 */
136 {
137 return (Type == ERangeBoundTypes::Exclusive);
138 }
139
140 /**
141 * Checks whether the bound is inclusive.
142 *
143 * @return true if the bound is inclusive, false otherwise.
144 */
146 {
147 return (Type == ERangeBoundTypes::Inclusive);
148 }
149
150 /**
151 * Checks whether the bound is open.
152 *
153 * @return true if the bound is open, false otherwise.
154 */
156 {
157 return (Type == ERangeBoundTypes::Open);
158 }
159
160public:
161
162 /**
163 * Serializes the given bound from or into the specified archive.
164 *
165 * @param Ar The archive to serialize from or into.
166 * @param Bound The bound to serialize.
167 * @return The archive.
168 */
169 friend class FArchive& operator<<(class FArchive& Ar, TRangeBound& Bound)
170 {
171 return Ar << (uint8&)Bound.Type << Bound.Value;
172 }
173
174 /**
175 * Gets the hash for the specified bound.
176 *
177 * @param Bound The bound to get the hash for.
178 * @return Hash value.
179 */
180 friend uint32 GetTypeHash(const TRangeBound& Bound)
181 {
182 return (GetTypeHash((uint8)Bound.Type) + 23 * GetTypeHash(Bound.Value));
183 }
184
185public:
186
187 /**
188 * Returns a closed bound that excludes the specified value.
189 *
190 * @param Value The bound value.
191 * @return An exclusive closed bound.
192 */
194 {
196
199
200 return Result;
201 }
202
203 /**
204 * Returns a closed bound that includes the specified value.
205 *
206 * @param Value The bound value.
207 * @return An inclusive closed bound.
208 */
210 {
212
215
216 return Result;
217 }
218
219 /**
220 * Returns an open bound.
221 *
222 * @return An open bound.
223 */
225 {
227
229
230 return Result;
231 }
232
233public:
234
235 /**
236 * Returns the given bound with its inclusion flipped between inclusive and exclusive.
237 *
238 * If the bound is open it is returned unchanged.
239 *
240 * @return A new bound.
241 */
243 {
244 if (Bound.IsExclusive())
245 {
246 return Inclusive(Bound.Value);
247 }
248
249 if (Bound.IsInclusive())
250 {
251 return Exclusive(Bound.Value);
252 }
253
254 return Bound;
255 }
256
257 /**
258 * Returns the greater of two lower bounds.
259 *
260 * @param A The first lower bound.
261 * @param B The second lower bound.
262 * @return The greater lower bound.
263 */
264 static FORCEINLINE const TRangeBound& MaxLower(const TRangeBound& A, const TRangeBound& B)
265 {
266 if (A.IsOpen()) { return B; }
267 if (B.IsOpen()) { return A; }
268 if (A.Value > B.Value) { return A; }
269 if (B.Value > A.Value) { return B; }
270 if (A.IsExclusive()) { return A; }
271
272 return B;
273 }
274
275 /**
276 * Returns the greater of two upper bounds.
277 *
278 * @param A The first upper bound.
279 * @param B The second upper bound.
280 * @return The greater upper bound.
281 */
282 static FORCEINLINE const TRangeBound& MaxUpper(const TRangeBound& A, const TRangeBound& B)
283 {
284 if (A.IsOpen()) { return A; }
285 if (B.IsOpen()) { return B; }
286 if (A.Value > B.Value) { return A; }
287 if (B.Value > A.Value) { return B; }
288 if (A.IsInclusive()) { return A; }
289
290 return B;
291 }
292
293 /**
294 * Returns the lesser of two lower bounds.
295 *
296 * @param A The first lower bound.
297 * @param B The second lower bound.
298 * @return The lesser lower bound.
299 */
300 static FORCEINLINE const TRangeBound& MinLower(const TRangeBound& A, const TRangeBound& B)
301 {
302 if (A.IsOpen()) { return A; }
303 if (B.IsOpen()) { return B; }
304 if (A.Value < B.Value) { return A; }
305 if (B.Value < A.Value) { return B; }
306 if (A.IsInclusive()) { return A; }
307
308 return B;
309 }
310
311 /**
312 * Returns the lesser of two upper bounds.
313 *
314 * @param A The first upper bound.
315 * @param B The second upper bound.
316 * @return The lesser upper bound.
317 */
318 static FORCEINLINE const TRangeBound& MinUpper(const TRangeBound& A, const TRangeBound& B)
319 {
320 if (A.IsOpen()) { return B; }
321 if (B.IsOpen()) { return A; }
322 if (A.Value < B.Value) { return A; }
323 if (B.Value < A.Value) { return B; }
324 if (A.IsExclusive()) { return A; }
325
326 return B;
327 }
328
329private:
330
331 /** Holds the type of the bound. */
332 TEnumAsByte<ERangeBoundTypes::Type> Type;
333
334 /** Holds the bound's value. */
335 ElementType Value;
336};
337
338
339/* Default range bounds for built-in types (for FProperty support)
340 *****************************************************************************/
341
342#define DEFINE_RANGEBOUND_WRAPPER_STRUCT(Name, ElementType)
343 struct Name : TRangeBound<ElementType>
344 {
345 private:
346 typedef TRangeBound<ElementType> Super;
347
348 public:
349 Name()
350 : Super()
351 { }
352
353 Name(const Super& Other)
354 : Super(Other)
355 { }
356
357 Name(const ElementType InValue)
358 : Super(InValue)
359 { }
360
361 static FORCEINLINE Name Exclusive(ElementValueOrConstRef Value)
362 {
363 return static_cast<const Name&>(Super::Exclusive(Value));
364 }
365
366 static FORCEINLINE Name Inclusive(ElementValueOrConstRef Value)
367 {
368 return static_cast<const Name&>(Super::Inclusive(Value));
369 }
370
371 static FORCEINLINE Name Open()
372 {
373 return static_cast<const Name&>(Super::Open());
374 }
375
376 static FORCEINLINE Name FlipInclusion(const Name& Bound)
377 {
378 return static_cast<const Name&>(Super::FlipInclusion(Bound));
379 }
380
381 static FORCEINLINE const Name& MaxLower(const Name& A, const Name& B)
382 {
383 return static_cast<const Name&>(Super::MaxLower(A, B));
384 }
385
386 static FORCEINLINE const Name& MaxUpper(const Name& A, const Name& B)
387 {
388 return static_cast<const Name&>(Super::MaxUpper(A, B));
389 }
390
391 static FORCEINLINE const Name& MinLower(const Name& A, const Name& B)
392 {
393 return static_cast<const Name&>(Super::MinLower(A, B));
394 }
395
396 static FORCEINLINE const Name& MinUpper(const Name& A, const Name& B)
397 {
398 return static_cast<const Name&>(Super::MinUpper(A, B));
399 }
400 };
401
402 template <>
403 struct TIsBitwiseConstructible<Name, TRangeBound<ElementType>>
404 {
405 enum { Value = true };
406 };
407
408 template <>
409 struct TIsBitwiseConstructible<TRangeBound<ElementType>, Name>
410 {
411 enum { Value = true };
412 };
413
414
416DEFINE_RANGEBOUND_WRAPPER_STRUCT(FDoubleRangeBound, double)
417DEFINE_RANGEBOUND_WRAPPER_STRUCT(FFloatRangeBound, float)
418DEFINE_RANGEBOUND_WRAPPER_STRUCT(FInt8RangeBound, int8)
419DEFINE_RANGEBOUND_WRAPPER_STRUCT(FInt16RangeBound, int16)
420DEFINE_RANGEBOUND_WRAPPER_STRUCT(FInt32RangeBound, int32)
421DEFINE_RANGEBOUND_WRAPPER_STRUCT(FInt64RangeBound, int64)
422DEFINE_RANGEBOUND_WRAPPER_STRUCT(FFrameNumberRangeBound, FFrameNumber)
#define check(expr)
#define FORCEINLINE_DEBUGGABLE
#define FORCEINLINE
Definition Platform.h:644
#define DEFINE_RANGEBOUND_WRAPPER_STRUCT(Name, ElementType)
Definition RangeBound.h:342
static FORCEINLINE TRangeBound FlipInclusion(const TRangeBound &Bound)
Definition RangeBound.h:242
static FORCEINLINE TRangeBound Open()
Definition RangeBound.h:224
static FORCEINLINE const TRangeBound & MinLower(const TRangeBound &A, const TRangeBound &B)
Definition RangeBound.h:300
friend uint32 GetTypeHash(const TRangeBound &Bound)
Definition RangeBound.h:180
static FORCEINLINE const TRangeBound & MaxUpper(const TRangeBound &A, const TRangeBound &B)
Definition RangeBound.h:282
static FORCEINLINE const TRangeBound & MaxLower(const TRangeBound &A, const TRangeBound &B)
Definition RangeBound.h:264
FORCEINLINE_DEBUGGABLE bool IsExclusive() const
Definition RangeBound.h:135
FORCEINLINE_DEBUGGABLE bool IsClosed() const
Definition RangeBound.h:125
static FORCEINLINE const TRangeBound & MinUpper(const TRangeBound &A, const TRangeBound &B)
Definition RangeBound.h:318
FORCEINLINE_DEBUGGABLE bool IsOpen() const
Definition RangeBound.h:155
ElementType Value
Definition RangeBound.h:335
TRangeBound(ElementValueOrConstRef InValue)
Definition RangeBound.h:58
static FORCEINLINE TRangeBound Inclusive(ElementValueOrConstRef Value)
Definition RangeBound.h:209
FORCEINLINE_DEBUGGABLE bool IsInclusive() const
Definition RangeBound.h:145
bool operator==(const TRangeBound &Other) const
Definition RangeBound.h:71
TCallTraits< ElementType >::ParamType ElementValueOrConstRef
Definition RangeBound.h:40
bool operator!=(const TRangeBound &Other) const
Definition RangeBound.h:82
FORCEINLINE_DEBUGGABLE void SetValue(ElementValueOrConstRef NewValue)
Definition RangeBound.h:113
static FORCEINLINE TRangeBound Exclusive(ElementValueOrConstRef Value)
Definition RangeBound.h:193
FORCEINLINE_DEBUGGABLE ElementValueOrConstRef GetValue() const
Definition RangeBound.h:97