├── .github └── FUNDING.yml ├── .gitattributes ├── Product └── Icon128.pdn ├── Resources └── Icon128.png ├── Source └── IGRanges │ ├── Public │ ├── IGRanges │ │ ├── Impl │ │ │ ├── Epilogue.inl │ │ │ ├── Prologue.inl │ │ │ └── Common.h │ │ ├── NonNull.h │ │ ├── Count.h │ │ ├── CustomizationPoints.h │ │ ├── Select.h │ │ ├── ToSet.h │ │ ├── Cast.h │ │ ├── Accumulate.h │ │ ├── ToArray.h │ │ ├── FirstOrDefault.h │ │ ├── Sum.h │ │ ├── Where.h │ │ ├── OfType.h │ │ ├── Filters │ │ │ └── IsChildOf.h │ │ ├── Selectors │ │ │ └── CDO.h │ │ └── AllAnyNone.h │ └── IGRanges.h │ ├── Private │ ├── IGRangesInternal.h │ ├── IGRangesModule.cpp │ └── Tests │ │ ├── IGRanges_Count.spec.cpp │ │ ├── IGRanges_Accumulate.spec.cpp │ │ ├── IGRanges_ToSet.spec.cpp │ │ ├── IGRanges_CustomizationPoints.spec.cpp │ │ ├── IGRanges_Cast.spec.cpp │ │ ├── IGRanges_NonNull.spec.cpp │ │ ├── IGRanges_Select.spec.cpp │ │ ├── IGRanges_ToArray.spec.cpp │ │ ├── IGRanges_CDO.spec.cpp │ │ ├── IGRanges_IsChildOf.spec.cpp │ │ ├── IGRanges_Sum.spec.cpp │ │ ├── IGRanges_AllAnyNone.spec.cpp │ │ ├── IGRanges_OfType.spec.cpp │ │ ├── IGRanges_FirstOrDefault.spec.cpp │ │ ├── IGRanges_Where.spec.cpp │ │ └── IGRanges_Benchmarks.spec.cpp │ └── IGRanges.Build.cs ├── IGRanges.uplugin ├── LICENSE ├── .gitignore ├── .clang-format └── README.md /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | custom: [paypal.me/IanGoodies] 2 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /Product/Icon128.pdn: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IGood/IGRanges/HEAD/Product/Icon128.pdn -------------------------------------------------------------------------------- /Resources/Icon128.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IGood/IGRanges/HEAD/Resources/Icon128.png -------------------------------------------------------------------------------- /Source/IGRanges/Public/IGRanges/Impl/Epilogue.inl: -------------------------------------------------------------------------------- 1 | // Copyright Ian Good 2 | 3 | #undef _IGR 4 | #undef _IGRP 5 | -------------------------------------------------------------------------------- /Source/IGRanges/Public/IGRanges/Impl/Prologue.inl: -------------------------------------------------------------------------------- 1 | // Copyright Ian Good 2 | 3 | #ifndef _IGR 4 | #define _IGR ::IG::Ranges:: 5 | #endif 6 | 7 | #ifndef _IGRP 8 | #define _IGRP ::IG::Ranges::Private:: 9 | #endif 10 | -------------------------------------------------------------------------------- /Source/IGRanges/Private/IGRangesInternal.h: -------------------------------------------------------------------------------- 1 | // Copyright Ian Good 2 | 3 | #pragma once 4 | 5 | #include "Logging/LogMacros.h" 6 | 7 | DECLARE_LOG_CATEGORY_EXTERN(LogIGRanges, Log, All); 8 | DECLARE_LOG_CATEGORY_EXTERN(LogIGRangesTests, Log, All); 9 | -------------------------------------------------------------------------------- /Source/IGRanges/Private/IGRangesModule.cpp: -------------------------------------------------------------------------------- 1 | // Copyright Ian Good 2 | 3 | #include "CoreMinimal.h" 4 | #include "IGRangesInternal.h" 5 | #include "Modules/ModuleManager.h" 6 | 7 | DEFINE_LOG_CATEGORY(LogIGRanges); 8 | DEFINE_LOG_CATEGORY(LogIGRangesTests); 9 | 10 | IMPLEMENT_MODULE(FDefaultModuleImpl, IGRanges) 11 | -------------------------------------------------------------------------------- /Source/IGRanges/IGRanges.Build.cs: -------------------------------------------------------------------------------- 1 | // Copyright Ian Good 2 | 3 | using UnrealBuildTool; 4 | 5 | public class IGRanges : ModuleRules 6 | { 7 | public IGRanges(ReadOnlyTargetRules Target) : base(Target) 8 | { 9 | PublicDependencyModuleNames.AddRange(new[] 10 | { 11 | "Core", 12 | "CoreUObject", 13 | }); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Source/IGRanges/Public/IGRanges.h: -------------------------------------------------------------------------------- 1 | // Copyright Ian Good 2 | 3 | #pragma once 4 | 5 | #include "IGRanges/Accumulate.h" 6 | #include "IGRanges/AllAnyNone.h" 7 | #include "IGRanges/Cast.h" 8 | #include "IGRanges/Count.h" 9 | #include "IGRanges/CustomizationPoints.h" 10 | #include "IGRanges/Filters/IsChildOf.h" 11 | #include "IGRanges/FirstOrDefault.h" 12 | #include "IGRanges/NonNull.h" 13 | #include "IGRanges/OfType.h" 14 | #include "IGRanges/Select.h" 15 | #include "IGRanges/Selectors/CDO.h" 16 | #include "IGRanges/Sum.h" 17 | #include "IGRanges/ToArray.h" 18 | #include "IGRanges/ToSet.h" 19 | #include "IGRanges/Where.h" 20 | 21 | using namespace IG::Ranges; 22 | -------------------------------------------------------------------------------- /IGRanges.uplugin: -------------------------------------------------------------------------------- 1 | { 2 | "FileVersion": 3, 3 | "Version": 2, 4 | "VersionName": "2.0", 5 | "FriendlyName": "C++ Ranges for Unreal", 6 | "Description": "Library that leverages Ranges (C++20) to provide LINQ (C#) style code patterns.", 7 | "Category": "Programming", 8 | "CreatedBy": "Ian Good", 9 | "CreatedByURL": "https://github.com/IGood", 10 | "DocsURL": "https://github.com/IGood/IGRanges#igranges-c-ranges-for-unreal", 11 | "MarketplaceURL": "", 12 | "SupportURL": "https://github.com/IGood/IGRanges/issues", 13 | "CanContainContent": false, 14 | "IsBetaVersion": false, 15 | "Installed": false, 16 | "Modules": [ 17 | { 18 | "Name": "IGRanges", 19 | "Type": "Runtime", 20 | "LoadingPhase": "Default" 21 | } 22 | ], 23 | "IsExperimentalVersion": false 24 | } -------------------------------------------------------------------------------- /Source/IGRanges/Public/IGRanges/NonNull.h: -------------------------------------------------------------------------------- 1 | // Copyright Ian Good 2 | 3 | #pragma once 4 | 5 | #include "Templates/SharedPointer.h" 6 | #include 7 | 8 | #include "IGRanges/Impl/Prologue.inl" 9 | 10 | namespace IG::Ranges 11 | { 12 | /** 13 | * Filters a sequence of pointer-like values, removing null elements. 14 | * Works on raw pointers (e.g. `UFoo*`) and smart pointers (e.g. `TObjectPtr`, `TWeakPointer`, etc.) and 15 | * anything that can be null-checked. 16 | */ 17 | [[nodiscard]] inline constexpr auto NonNull() 18 | { 19 | return std::views::filter([](T&& x) { 20 | if constexpr (TIsTWeakPtr_V) // Support for `TWeakPtr` 21 | { 22 | return x.IsValid(); 23 | } 24 | else 25 | { 26 | return x != nullptr; 27 | } 28 | }); 29 | } 30 | 31 | } // namespace IG::Ranges 32 | 33 | #include "IGRanges/Impl/Epilogue.inl" 34 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2024 Ian Good 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Visual Studio 2015 user specific files 2 | .vs/ 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | 22 | # Compiled Static libraries 23 | *.lai 24 | *.la 25 | *.a 26 | *.lib 27 | 28 | # Executables 29 | *.exe 30 | *.out 31 | *.app 32 | *.ipa 33 | 34 | # These project files can be generated by the engine 35 | *.xcodeproj 36 | *.xcworkspace 37 | *.sln 38 | *.suo 39 | *.opensdf 40 | *.sdf 41 | *.VC.db 42 | *.VC.opendb 43 | 44 | # Precompiled Assets 45 | SourceArt/**/*.png 46 | SourceArt/**/*.tga 47 | 48 | # Binary Files 49 | Binaries/* 50 | Plugins/*/Binaries/* 51 | 52 | # Builds 53 | Build/* 54 | 55 | # Whitelist PakBlacklist-.txt files 56 | !Build/*/ 57 | Build/*/** 58 | !Build/*/PakBlacklist*.txt 59 | 60 | # Don't ignore icon files in Build 61 | !Build/**/*.ico 62 | 63 | # Built data for maps 64 | *_BuiltData.uasset 65 | 66 | # Configuration files generated by the Editor 67 | Saved/* 68 | 69 | # Compiled source files for the engine to use 70 | Intermediate/* 71 | Plugins/*/Intermediate/* 72 | 73 | # Cache files for the editor to use 74 | DerivedDataCache/* 75 | -------------------------------------------------------------------------------- /Source/IGRanges/Public/IGRanges/Count.h: -------------------------------------------------------------------------------- 1 | // Copyright Ian Good 2 | 3 | #pragma once 4 | 5 | #include "HAL/Platform.h" // `int32` 6 | #include 7 | 8 | #include "IGRanges/Impl/Prologue.inl" 9 | 10 | namespace IG::Ranges 11 | { 12 | namespace Private 13 | { 14 | struct Count_fn 15 | { 16 | template 17 | [[nodiscard]] constexpr auto operator()(RangeType&& Range) const 18 | { 19 | return static_cast(std::ranges::distance(std::forward(Range))); 20 | } 21 | }; 22 | 23 | } // namespace Private 24 | 25 | /** 26 | * Returns the number of elements in a range. 27 | * 28 | * @usage 29 | * int32 NumVulerableActors = SomeActors | Where(&AActor::CanBeDamaged) | Count(); 30 | */ 31 | [[nodiscard]] inline constexpr auto Count() 32 | { 33 | return std::ranges::_Range_closure<_IGRP Count_fn>{}; 34 | } 35 | 36 | /** 37 | * Returns the number of elements in a range that satisfy a predicate. 38 | * Equivalent to `Where(pred) | Count()`. 39 | * 40 | * @usage 41 | * int32 NumVulerableActors = SomeActors | Count(&AActor::CanBeDamaged); 42 | */ 43 | template 44 | [[nodiscard]] constexpr auto Count(_Pr&& _Pred) 45 | { 46 | return std::views::filter(std::forward<_Pr>(_Pred)) 47 | | _IGR Count(); 48 | } 49 | 50 | } // namespace IG::Ranges 51 | 52 | #include "IGRanges/Impl/Epilogue.inl" 53 | -------------------------------------------------------------------------------- /Source/IGRanges/Public/IGRanges/CustomizationPoints.h: -------------------------------------------------------------------------------- 1 | // Copyright Ian Good 2 | 3 | #pragma once 4 | 5 | /** 6 | * This file contains "customization points" required to allow Unreal containers to be used with C++20 Ranges. 7 | * 8 | * At least as of UE5.4 these are required. Future versions of the engine might make these obsolete. 9 | */ 10 | 11 | #include "Containers/Array.h" 12 | #include "Containers/ArrayView.h" 13 | 14 | //--------------------------------------------------------------------------------------- 15 | 16 | template 17 | auto begin(TArray& r) 18 | { 19 | return r.GetData(); 20 | } 21 | 22 | template 23 | auto end(TArray& r) 24 | { 25 | return r.GetData() + r.Num(); 26 | } 27 | 28 | template 29 | auto begin(const TArray& r) 30 | { 31 | return r.GetData(); 32 | } 33 | 34 | template 35 | auto end(const TArray& r) 36 | { 37 | return r.GetData() + r.Num(); 38 | } 39 | 40 | // optional; compiles fine w/o, but probably better to provide this 41 | template 42 | auto size(const TArray& r) 43 | { 44 | return r.Num(); 45 | } 46 | 47 | //--------------------------------------------------------------------------------------- 48 | 49 | // optional; compiles fine w/o, but probably better to provide this 50 | template 51 | auto size(const TArrayView& r) 52 | { 53 | return r.Num(); 54 | } 55 | 56 | //--------------------------------------------------------------------------------------- 57 | -------------------------------------------------------------------------------- /Source/IGRanges/Public/IGRanges/Select.h: -------------------------------------------------------------------------------- 1 | // Copyright Ian Good 2 | 3 | #pragma once 4 | 5 | #include "IGRanges/NonNull.h" 6 | #include 7 | 8 | #include "IGRanges/Impl/Prologue.inl" 9 | 10 | namespace IG::Ranges 11 | { 12 | /** 13 | * Projects each element of a sequence into a new form. 14 | * 15 | * Alias for `std::views::transform`: 16 | * A range adaptor that represents view of an underlying sequence after applying a transformation function to each element. 17 | * 18 | * @usage 19 | * SomeNumbers | Select([](int32 N) { return N * N; }) 20 | * SomeStructs | Select([](const FBar& B) { return B.GetId(); }) 21 | * SomeStructs | Select(&FBar::GetId) 22 | * SomeObjects | Select([](const UFoo* F) { return (F != nullptr) ? F->GetName() : TEXT("???"); }) 23 | */ 24 | template 25 | [[nodiscard]] constexpr auto Select(_Fn&& _Fun) 26 | { 27 | return std::views::transform(std::forward<_Fn>(_Fun)); 28 | } 29 | 30 | /** 31 | * Same as `Select` but intended for projections that produce pointer-like results. 32 | * Projection results that are Null are filtered out. 33 | * Equivalent to `Select(proj) | NonNull()`. 34 | * 35 | * @usage: 36 | * SomeActors | SelectNonNull([](const AActor* A) { 37 | * return (A != nullptr) ? A.GetComponentByClass() : nullptr; 38 | * }) 39 | */ 40 | template 41 | [[nodiscard]] constexpr auto SelectNonNull(_Fn&& _Fun) 42 | { 43 | return std::views::transform(std::forward<_Fn>(_Fun)) | _IGR NonNull(); 44 | } 45 | 46 | } // namespace IG::Ranges 47 | 48 | #include "IGRanges/Impl/Epilogue.inl" 49 | -------------------------------------------------------------------------------- /Source/IGRanges/Public/IGRanges/ToSet.h: -------------------------------------------------------------------------------- 1 | // Copyright Ian Good 2 | 3 | #pragma once 4 | 5 | #include "Containers/Set.h" 6 | #include 7 | 8 | #include "IGRanges/Impl/Prologue.inl" 9 | 10 | namespace IG::Ranges 11 | { 12 | namespace Private 13 | { 14 | struct ToSet_fn 15 | { 16 | template 17 | [[nodiscard]] constexpr auto operator()(RangeType&& Range) const 18 | { 19 | using T = std::ranges::range_value_t; 20 | TSet Set; 21 | 22 | if constexpr (std::ranges::sized_range) 23 | { 24 | Set.Reserve(std::ranges::distance(Range)); 25 | } 26 | 27 | for (auto&& X : Range) 28 | { 29 | Set.Emplace(X); 30 | } 31 | 32 | return Set; 33 | } 34 | }; 35 | 36 | } // namespace Private 37 | 38 | /** 39 | * Creates a `TSet` from a range. 40 | * 41 | * @usage 42 | * TSet UniqueNumbers = SomeNumbers | ToSet(); 43 | */ 44 | [[nodiscard]] inline constexpr auto ToSet() 45 | { 46 | return std::ranges::_Range_closure<_IGRP ToSet_fn>{}; 47 | } 48 | 49 | /** 50 | * Same as `ToSet` (no parameters) but first applies a projection to elements. 51 | * Equivalent to `Select(proj) | ToSet()`. 52 | * 53 | * @usage 54 | * TSet UniqueMeshes = MySkMeshComponents | ToSet(&USkeletalMeshComponent::GetSkeletalMeshAsset); 55 | */ 56 | template 57 | [[nodiscard]] constexpr auto ToSet(TransformT&& Trans) 58 | { 59 | return std::views::transform(std::forward(Trans)) 60 | | IG::Ranges::ToSet(); 61 | } 62 | 63 | } // namespace IG::Ranges 64 | 65 | #include "IGRanges/Impl/Epilogue.inl" 66 | -------------------------------------------------------------------------------- /Source/IGRanges/Public/IGRanges/Cast.h: -------------------------------------------------------------------------------- 1 | // Copyright Ian Good 2 | 3 | #pragma once 4 | 5 | #include "Templates/Casts.h" 6 | #include 7 | 8 | namespace IG::Ranges 9 | { 10 | /** 11 | * Casts the values of a range of UObjects to a specified type using UE's `Cast` function. 12 | * Safe to accept null values; may yield null results. 13 | * 14 | * @see `OfType` 15 | */ 16 | template 17 | [[nodiscard]] constexpr auto Cast() 18 | { 19 | return std::views::transform([](auto&& x) { return ::Cast(x); }); 20 | } 21 | 22 | /** 23 | * Casts the values of a range of UObjects to a specified type using UE's `ExactCast` function. 24 | * Safe to accept null values; may yield null results. 25 | * 26 | * @see `OfExactType` 27 | */ 28 | template 29 | [[nodiscard]] constexpr auto ExactCast() 30 | { 31 | return std::views::transform([](auto&& x) { return ::ExactCast(x); }); 32 | } 33 | 34 | /** 35 | * Casts the values of a range of UObjects to a specified type using UE's `CastChecked` function. 36 | * Not safe to accept null values; never yields null results. 37 | */ 38 | template 39 | [[nodiscard]] constexpr auto CastChecked() 40 | { 41 | return std::views::transform([](auto&& x) { return ::CastChecked(x); }); 42 | } 43 | 44 | /** 45 | * Similar to `CastChecked` (no parameters) but null-check behavior is parameterized. 46 | */ 47 | template 48 | [[nodiscard]] constexpr auto CastChecked(ECastCheckedType::Type CheckType) 49 | { 50 | return std::views::transform([](auto&& x) { return ::CastChecked(x, CheckType); }); 51 | } 52 | 53 | } // namespace IG::Ranges 54 | -------------------------------------------------------------------------------- /Source/IGRanges/Public/IGRanges/Impl/Common.h: -------------------------------------------------------------------------------- 1 | // Copyright Ian Good 2 | 3 | #pragma once 4 | 5 | #include "HAL/Platform.h" // required before `CoreMiscDefines.h` 6 | #include "Misc/CoreMiscDefines.h" // `EForceInit` 7 | #include 8 | 9 | namespace IG::Ranges::Private 10 | { 11 | template 12 | concept HasGet = requires(T t) { 13 | t.Get(); 14 | }; 15 | 16 | struct AlwaysTrue 17 | { 18 | template 19 | [[nodiscard]] constexpr bool operator()(T&&) const noexcept 20 | { 21 | return true; 22 | } 23 | 24 | using is_transparent = int; 25 | }; 26 | 27 | template 28 | [[nodiscard]] T Construct() 29 | { 30 | // For non-fundamental types (bool, int, etc.), use `EForceInit` if possible. 31 | // This is desirable for things like `FVector` & `FQuat`. 32 | if constexpr (!std::is_fundamental_v && std::is_constructible_v) 33 | { 34 | // We're intentionally using `ForceInit` (zero) & not `ForceInitToZero` (one) because `is_constructible_v` is 35 | // True even for types that have constructors like `Foo::Foo(int)`, which is not actually `EForceInit`, but 36 | // implicit conversions are accepted anyhow. So we hope that `Foo(0)` is correct here. 37 | // Also, the distinction between `ForceInit` & `ForceInitToZero` is sometimes important. For example, `FQuat` 38 | // will be "identity" or "zeroes" (respectively) & we probably want "identity" in all cases (like when using 39 | // `Sum` or `FirstOrDefault`). 40 | return T(EForceInit::ForceInit); 41 | } 42 | else 43 | { 44 | return T{}; 45 | } 46 | } 47 | 48 | } // namespace IG::Ranges::Private 49 | -------------------------------------------------------------------------------- /Source/IGRanges/Public/IGRanges/Accumulate.h: -------------------------------------------------------------------------------- 1 | // Copyright Ian Good 2 | 3 | #pragma once 4 | 5 | #include 6 | #include 7 | 8 | #include "IGRanges/Impl/Prologue.inl" 9 | 10 | namespace IG::Ranges 11 | { 12 | namespace Private 13 | { 14 | struct Accumulate_fn 15 | { 16 | template 17 | [[nodiscard]] auto operator()(RangeType&& Range, SeedType&& Seed, FoldType&& Fold) const 18 | { 19 | return std::accumulate(std::begin(Range), std::end(Range), std::forward(Seed), std::forward(Fold)); 20 | } 21 | }; 22 | 23 | } // namespace Private 24 | 25 | /** 26 | * Applies an accumulator function over a range. 27 | * The specified seed value is used as the initial accumulator value. 28 | * 29 | * Alias for `std::accumulate`: 30 | * Initializes an accumulator value with the seed value and then modifies it with `Fold(acc, *i)` for every element in 31 | * the range. 32 | * 33 | * @usage 34 | * FString Results = 35 | * SomeObjects 36 | * | Accumulate(FString(TEXT("Objects:")), [](FString Acc, const UObject* Obj) { 37 | * return Acc + GetNameSafe(Obj) + TEXT(','); 38 | * }); 39 | * // example Results = "Objects:Foo,Bar,None,Blah," 40 | */ 41 | template 42 | [[nodiscard]] constexpr auto Accumulate(T&& Seed, FoldType&& Fold) 43 | { 44 | return std::ranges::_Range_closure< 45 | _IGRP Accumulate_fn, 46 | std::decay_t, 47 | std::decay_t> // 48 | { 49 | std::forward(Seed), 50 | std::forward(Fold), 51 | }; 52 | } 53 | 54 | } // namespace IG::Ranges 55 | 56 | #include "IGRanges/Impl/Epilogue.inl" 57 | -------------------------------------------------------------------------------- /Source/IGRanges/Public/IGRanges/ToArray.h: -------------------------------------------------------------------------------- 1 | // Copyright Ian Good 2 | 3 | #pragma once 4 | 5 | #include "Containers/Array.h" 6 | #include 7 | 8 | #include "IGRanges/Impl/Prologue.inl" 9 | 10 | namespace IG::Ranges 11 | { 12 | namespace Private 13 | { 14 | struct ToArray_fn 15 | { 16 | template 17 | [[nodiscard]] constexpr auto operator()(RangeType&& Range) const 18 | { 19 | using T = std::ranges::range_value_t; 20 | TArray Array; 21 | 22 | if constexpr (std::ranges::sized_range) 23 | { 24 | Array.Reserve(std::ranges::distance(Range)); 25 | } 26 | 27 | for (auto&& X : Range) 28 | { 29 | Array.Emplace(X); 30 | } 31 | 32 | return Array; 33 | } 34 | }; 35 | 36 | } // namespace Private 37 | 38 | /** 39 | * Creates a `TArray` from a range. 40 | * 41 | * @usage 42 | * TArray SquaredNumbers = SomeNumbers | Select([](int32 N) { return N * N; }) | ToArray(); 43 | */ 44 | [[nodiscard]] inline constexpr auto ToArray() 45 | { 46 | return std::ranges::_Range_closure<_IGRP ToArray_fn>{}; 47 | } 48 | 49 | /** 50 | * Same as `ToArray` (no parameters) but first applies a projection to elements. 51 | * Equivalent to `Select(proj) | ToArray()`. 52 | * 53 | * @usage 54 | * TArray SquaredNumbers = SomeNumbers | ToArray([](int32 N) { return N * N; }); 55 | * TArray Names = SomeObjects | ToArray([](auto&& Obj) { return GetNameSafe(Obj); }); 56 | */ 57 | template 58 | [[nodiscard]] constexpr auto ToArray(TransformT&& Trans) 59 | { 60 | return std::views::transform(std::forward(Trans)) 61 | | _IGR ToArray(); 62 | } 63 | 64 | } // namespace IG::Ranges 65 | 66 | #include "IGRanges/Impl/Epilogue.inl" 67 | -------------------------------------------------------------------------------- /Source/IGRanges/Public/IGRanges/FirstOrDefault.h: -------------------------------------------------------------------------------- 1 | // Copyright Ian Good 2 | 3 | #pragma once 4 | 5 | #include "IGRanges/Impl/Common.h" 6 | #include "Templates/SharedPointer.h" 7 | #include 8 | 9 | #include "IGRanges/Impl/Prologue.inl" 10 | 11 | namespace IG::Ranges 12 | { 13 | namespace Private 14 | { 15 | struct FirstOrDefault_fn 16 | { 17 | template 18 | [[nodiscard]] constexpr auto operator()(RangeType&& Range, _Pr _Pred) const 19 | { 20 | using T = std::ranges::range_value_t; 21 | 22 | static_assert(!TIsTSharedRef_V, "`FirstOrDefault` cannot operate on ranges of `TSharedRef`."); 23 | 24 | for (auto&& X : Range) 25 | { 26 | if (std::invoke(_Pred, X)) 27 | { 28 | return X; 29 | } 30 | } 31 | 32 | return _IGRP Construct(); 33 | } 34 | }; 35 | 36 | } // namespace Private 37 | 38 | /** 39 | * Returns the first element of a sequence, or a default-initialized value if the sequence contains no elements. 40 | * 41 | * If a predicate is specified, then returns the first element of the sequence that satisfies the predicate. 42 | * Equivalent to `Where(pred) | FirstOrDefault()`. 43 | * 44 | * @usage 45 | * AActor* Rosie = SomeActors | Where([](const AActor* A) { return GetNameSafe(A) == TEXT("Rosie"); }) | FirstOrDefault(); 46 | * AActor* Rosie = SomeActors | FirstOrDefault([](const AActor* A) { return GetNameSafe(A) == TEXT("Rosie"); }); 47 | */ 48 | template 49 | [[nodiscard]] constexpr auto FirstOrDefault(_Pr&& _Pred = {}) 50 | { 51 | return std::ranges::_Range_closure<_IGRP FirstOrDefault_fn, std::decay_t<_Pr>>{std::forward<_Pr>(_Pred)}; 52 | } 53 | 54 | } // namespace IG::Ranges 55 | 56 | #include "IGRanges/Impl/Epilogue.inl" 57 | -------------------------------------------------------------------------------- /Source/IGRanges/Private/Tests/IGRanges_Count.spec.cpp: -------------------------------------------------------------------------------- 1 | // Copyright Ian Good 2 | 3 | #include "IGRanges/Count.h" 4 | #include "IGRangesInternal.h" 5 | #include "Misc/AutomationTest.h" 6 | #include 7 | 8 | #if WITH_DEV_AUTOMATION_TESTS 9 | 10 | DEFINE_SPEC(FIGRangesCountSpec, "IG.Ranges.Count", EAutomationTestFlags::ApplicationContextMask | EAutomationTestFlags::ProductFilter); 11 | 12 | void FIGRangesCountSpec::Define() 13 | { 14 | using namespace IG::Ranges; 15 | 16 | static constexpr int32 SomeValues[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; 17 | static constexpr int32 NumSomeValues = UE_ARRAY_COUNT(SomeValues); 18 | 19 | It("empty", [this]() { 20 | const int32 ActualCount = std::ranges::empty_view() | Count(); 21 | TestEqual("count", ActualCount, 0); 22 | }); 23 | 24 | It("many", [this]() { 25 | const int32 ActualCount = SomeValues | Count(); 26 | TestEqual("count", ActualCount, NumSomeValues); 27 | }); 28 | 29 | It("many_transformed", [this]() { 30 | const auto Square = [](auto&& x) { 31 | return x * x; 32 | }; 33 | 34 | const int32 ActualCount = SomeValues | std::views::transform(Square) | Count(); 35 | TestEqual("count", ActualCount, NumSomeValues); 36 | }); 37 | 38 | It("many_filtered", [this]() { 39 | const auto IsEven = [](auto&& x) { 40 | return x % 2 == 0; 41 | }; 42 | 43 | int32 ExpectedCount = 0; 44 | for (auto&& X : SomeValues) 45 | { 46 | if (IsEven(X)) 47 | { 48 | ++ExpectedCount; 49 | } 50 | } 51 | 52 | int32 ActualCount = SomeValues | std::views::filter(IsEven) | Count(); 53 | TestEqual("count", ActualCount, ExpectedCount); 54 | 55 | ActualCount = SomeValues | Count(IsEven); 56 | TestEqual("count", ActualCount, ExpectedCount); 57 | }); 58 | } 59 | 60 | #endif // WITH_DEV_AUTOMATION_TESTS 61 | -------------------------------------------------------------------------------- /Source/IGRanges/Private/Tests/IGRanges_Accumulate.spec.cpp: -------------------------------------------------------------------------------- 1 | // Copyright Ian Good 2 | 3 | #include "IGRanges/Accumulate.h" 4 | #include "IGRangesInternal.h" 5 | #include "Misc/AutomationTest.h" 6 | #include 7 | #include 8 | 9 | #if WITH_DEV_AUTOMATION_TESTS 10 | 11 | DEFINE_SPEC(FIGRangesAccumulateSpec, "IG.Ranges.Accumulate", EAutomationTestFlags::ApplicationContextMask | EAutomationTestFlags::ProductFilter); 12 | 13 | void FIGRangesAccumulateSpec::Define() 14 | { 15 | using namespace IG::Ranges; 16 | 17 | static const FString Seed = TEXT("🌱"); 18 | 19 | static const auto Fold = [](FString Acc, int32 Elem) { 20 | Acc 21 | .AppendChar(TEXT(',')) 22 | .AppendInt(Elem); 23 | return Acc; 24 | }; 25 | 26 | It("empty", [this]() { 27 | const int32* Empty = nullptr; 28 | const FString ExpectedAccumulate = std::accumulate(Empty, Empty, Seed, Fold); 29 | const FString ActualAccumulate = std::ranges::empty_view() | Accumulate(Seed, Fold); 30 | TestEqual("accumulate", ActualAccumulate, ExpectedAccumulate); 31 | }); 32 | 33 | It("single", [this]() { 34 | const int32 Single[] = {123}; 35 | const FString ExpectedAccumulate = std::accumulate(Single, Single + UE_ARRAY_COUNT(Single), Seed, Fold); 36 | const FString ActualAccumulate = Single | Accumulate(Seed, Fold); 37 | TestEqual("accumulate", ActualAccumulate, ExpectedAccumulate); 38 | }); 39 | 40 | It("many", [this]() { 41 | const int32 SomeValues[] = {1, 2, 3, 4, 5}; 42 | const FString ExpectedAccumulate = std::accumulate(SomeValues, SomeValues + UE_ARRAY_COUNT(SomeValues), Seed, Fold); 43 | const FString ActualAccumulate = SomeValues | Accumulate(Seed, Fold); 44 | TestEqual("accumulate", ActualAccumulate, ExpectedAccumulate); 45 | }); 46 | } 47 | 48 | #endif // WITH_DEV_AUTOMATION_TESTS 49 | -------------------------------------------------------------------------------- /.clang-format: -------------------------------------------------------------------------------- 1 | --- 2 | AccessModifierOffset: -4 3 | AlignAfterOpenBracket: DontAlign 4 | AlignArrayOfStructures: Right 5 | AlignConsecutiveAssignments: None 6 | AlignConsecutiveBitFields: None 7 | AlignConsecutiveDeclarations: None 8 | AlignEscapedNewlines: Left 9 | AlignOperands: AlignAfterOperator 10 | AlignTrailingComments: true 11 | AllowShortBlocksOnASingleLine: Empty 12 | AllowShortEnumsOnASingleLine: false 13 | AllowShortFunctionsOnASingleLine: Inline 14 | AllowShortLambdasOnASingleLine: Inline 15 | BasedOnStyle: LLVM 16 | BraceWrapping: 17 | AfterCaseLabel: true 18 | AfterClass: true 19 | AfterControlStatement: true 20 | AfterEnum: true 21 | AfterFunction: true 22 | AfterNamespace: true 23 | AfterObjCDeclaration: true 24 | AfterStruct: true 25 | AfterUnion: true 26 | AfterExternBlock: true 27 | BeforeCatch: true 28 | BeforeElse: true 29 | BeforeLambdaBody: false 30 | BeforeWhile: true 31 | IndentBraces: false 32 | BreakBeforeBinaryOperators: NonAssignment 33 | BreakBeforeBraces: Custom 34 | BreakBeforeTernaryOperators: true 35 | BreakConstructorInitializers: BeforeComma 36 | BreakInheritanceList: BeforeComma 37 | BreakStringLiterals: true 38 | ColumnLimit: 0 39 | Cpp11BracedListStyle: true 40 | EmptyLineBeforeAccessModifier: LogicalBlock 41 | FixNamespaceComments: true 42 | IncludeBlocks: Preserve 43 | IndentCaseBlocks: false 44 | IndentCaseLabels: true 45 | IndentPPDirectives: None 46 | IndentWidth: 4 47 | Language: Cpp 48 | NamespaceIndentation: None 49 | PackConstructorInitializers: CurrentLine 50 | PointerAlignment: Left 51 | SortIncludes: true 52 | SpaceBeforeCaseColon: false 53 | Standard: Cpp11 54 | StatementMacros: ['UPROPERTY', 'UFUNCTION', 'UCLASS', 'USTRUCT', 'UENUM', 'UINTERFACE', 'GENERATED_BODY', 'v'] 55 | TabWidth: 4 56 | UseTab: ForContinuationAndIndentation 57 | -------------------------------------------------------------------------------- /Source/IGRanges/Public/IGRanges/Sum.h: -------------------------------------------------------------------------------- 1 | // Copyright Ian Good 2 | 3 | #pragma once 4 | 5 | #include "IGRanges/Impl/Common.h" 6 | #include 7 | 8 | #include "IGRanges/Impl/Prologue.inl" 9 | 10 | namespace IG::Ranges 11 | { 12 | namespace Private 13 | { 14 | struct Sum_fn 15 | { 16 | template 17 | [[nodiscard]] constexpr auto operator()(RangeType&& Range) const 18 | { 19 | using T = std::ranges::range_value_t; 20 | 21 | auto It = std::begin(Range); 22 | const auto& End = std::end(Range); 23 | 24 | // If the range is empty, then return a default-initialized value. 25 | if (It == End) 26 | { 27 | return _IGRP Construct(); 28 | } 29 | 30 | T Result = *It; 31 | 32 | while (++It != End) 33 | { 34 | Result = std::move(Result) + *It; 35 | } 36 | 37 | return Result; 38 | } 39 | }; 40 | 41 | } // namespace Private 42 | 43 | /** 44 | * Computes the sum of a sequence of values by applying `operator+`. 45 | * Empty ranges return a default-initialized value. 46 | * 47 | * @usage 48 | * int32 Total = SomeNumbers | Sum(); 49 | * FVector Offset = SomeVectors | Sum(); 50 | * FString Concatenated = SomeStrings | Sum(); 51 | */ 52 | [[nodiscard]] inline constexpr auto Sum() 53 | { 54 | return std::ranges::_Range_closure<_IGRP Sum_fn>{}; 55 | } 56 | 57 | /** 58 | * Same as `Sum` (no parameters) but first applies a projection to elements. 59 | * Equivalent to `Select(proj) | Sum()`. 60 | * 61 | * @usage 62 | * float TotalWeight = SomeStructs | Sum([](const FBar& B) { return B.Weight; }); 63 | * float TotalWeight = SomeStructs | Sum(&FBar::Weight); 64 | */ 65 | template 66 | [[nodiscard]] constexpr auto Sum(TransformT&& Trans) 67 | { 68 | return std::views::transform(std::forward(Trans)) 69 | | _IGR Sum(); 70 | } 71 | 72 | } // namespace IG::Ranges 73 | 74 | #include "IGRanges/Impl/Epilogue.inl" 75 | -------------------------------------------------------------------------------- /Source/IGRanges/Public/IGRanges/Where.h: -------------------------------------------------------------------------------- 1 | // Copyright Ian Good 2 | 3 | #pragma once 4 | 5 | #include 6 | #include 7 | 8 | #include "IGRanges/Impl/Prologue.inl" 9 | 10 | namespace IG::Ranges 11 | { 12 | /** 13 | * Filters a sequence of values based on a predicate. 14 | * 15 | * Alias for `std::views::filter`: 16 | * A range adaptor that represents view of an underlying sequence without the elements that fail to satisfy a predicate. 17 | * 18 | * @usage 19 | * SomeNumbers | Where([](int32 N) { return N > 0; }) 20 | * SomeStructs | Where([](const FBar& B) { return B.IsGood(); }) 21 | * SomeStructs | Where(&FBar::IsGood) 22 | * SomeObjects | Where([](const UFoo* F) { return F != nullptr && F->IsGood(); }) 23 | */ 24 | template 25 | [[nodiscard]] constexpr auto Where(_Pr&& _Pred) 26 | { 27 | return std::views::filter(std::forward<_Pr>(_Pred)); 28 | } 29 | 30 | /** 31 | * Same as `Where` but intended for pointer-like elements that can be null-checked. 32 | * Elements are null-checked before invoking the predicate. 33 | * Similar to `NonNull() | Where(filter)`. 34 | * 35 | * @usage 36 | * SomeObjects | SafeWhere([](const UFoo* F) { return F->IsGood(); }) 37 | * SomeObjects | SafeWhere(&UFoo::IsGood) 38 | */ 39 | template 40 | [[nodiscard]] constexpr auto SafeWhere(_Pr _Pred) 41 | { 42 | return std::views::filter([Pred = std::move(_Pred)](T&& x) { 43 | return x != nullptr && std::invoke(Pred, std::forward(x)); 44 | }); 45 | } 46 | 47 | /** 48 | * Same as `Where` but the result of the predicate is negated. 49 | */ 50 | template 51 | [[nodiscard]] constexpr auto WhereNot(_Pr&& _Pred) 52 | { 53 | return std::views::filter(std::not_fn(std::forward<_Pr>(_Pred))); 54 | } 55 | 56 | /** 57 | * Same as `SafeWhere` but the result of the predicate is negated. 58 | */ 59 | template 60 | [[nodiscard]] constexpr auto SafeWhereNot(_Pr&& _Pred) 61 | { 62 | return _IGR SafeWhere(std::not_fn(std::forward<_Pr>(_Pred))); 63 | } 64 | 65 | } // namespace IG::Ranges 66 | 67 | #include "IGRanges/Impl/Epilogue.inl" 68 | -------------------------------------------------------------------------------- /Source/IGRanges/Private/Tests/IGRanges_ToSet.spec.cpp: -------------------------------------------------------------------------------- 1 | // Copyright Ian Good 2 | 3 | #include "IGRanges/ToSet.h" 4 | #include "IGRangesInternal.h" 5 | #include "Misc/AutomationTest.h" 6 | #include 7 | 8 | #if WITH_DEV_AUTOMATION_TESTS 9 | 10 | BEGIN_DEFINE_SPEC(FIGRangesToSetSpec, "IG.Ranges.ToSet", EAutomationTestFlags::ApplicationContextMask | EAutomationTestFlags::ProductFilter) 11 | 12 | void TestSet(const TSet& Actual, const TSet& Expected) 13 | { 14 | TestEqual("count", Actual.Num(), Expected.Num()); 15 | TestTrue("equal sets", Actual.Difference(Expected).IsEmpty()); 16 | }; 17 | 18 | END_DEFINE_SPEC(FIGRangesToSetSpec) 19 | 20 | void FIGRangesToSetSpec::Define() 21 | { 22 | using namespace IG::Ranges; 23 | 24 | static constexpr int32 SomeValues[] = {10, 8, 6, 4, 2, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10}; 25 | static constexpr int32 NumSomeValues = UE_ARRAY_COUNT(SomeValues); 26 | 27 | // `ToSet` with an empty range produces a set with zero count. 28 | It("empty", [this]() { 29 | const TSet TestMe = std::ranges::empty_view() | ToSet(); 30 | TestSet(TestMe, {}); 31 | }); 32 | 33 | // `ToSet` produces a set equal to traditional `TSet` usage. 34 | It("many", [this]() { 35 | TSet ExpectedSet; 36 | ExpectedSet.Reserve(NumSomeValues); 37 | for (auto&& X : SomeValues) 38 | { 39 | ExpectedSet.Emplace(X); 40 | } 41 | 42 | const TSet TestMe = SomeValues | ToSet(); 43 | TestSet(TestMe, ExpectedSet); 44 | }); 45 | 46 | // `ToSet` produces a set equal to traditional `TSet` usage. 47 | // (works even with transformations) 48 | It("many_transformed", [this]() { 49 | const auto Square = [](auto&& x) { 50 | return x * x; 51 | }; 52 | 53 | TSet ExpectedSet; 54 | ExpectedSet.Reserve(NumSomeValues); 55 | for (auto&& X : SomeValues) 56 | { 57 | ExpectedSet.Emplace(Square(X)); 58 | } 59 | 60 | TSet TestMe = SomeValues | std::views::transform(Square) | ToSet(); 61 | TestSet(TestMe, ExpectedSet); 62 | 63 | TestMe = SomeValues | ToSet(Square); 64 | TestSet(TestMe, ExpectedSet); 65 | }); 66 | } 67 | 68 | #endif // WITH_DEV_AUTOMATION_TESTS 69 | -------------------------------------------------------------------------------- /Source/IGRanges/Public/IGRanges/OfType.h: -------------------------------------------------------------------------------- 1 | // Copyright Ian Good 2 | 3 | #pragma once 4 | 5 | #include "IGRanges/Cast.h" 6 | #include "UObject/SoftObjectPtr.h" 7 | #include "UObject/WeakObjectPtrTemplates.h" 8 | #include 9 | 10 | #include "IGRanges/Impl/Prologue.inl" 11 | 12 | namespace IG::Ranges 13 | { 14 | namespace Private 15 | { 16 | [[nodiscard]] inline bool IsA(const UObject* Obj, const UClass* Class) 17 | { 18 | return Obj != nullptr && Obj->IsA(Class); 19 | } 20 | 21 | template 22 | [[nodiscard]] bool IsA(const TObjectPtr Obj, const UClass* Class) 23 | { 24 | return Obj.IsA(Class); 25 | } 26 | 27 | template 28 | [[nodiscard]] bool IsA(const TWeakObjectPtr& WeakObj, const UClass* Class) 29 | { 30 | return _IGRP IsA(WeakObj.Get(), Class); 31 | } 32 | 33 | template 34 | [[nodiscard]] bool IsA(const TSoftObjectPtr& SoftObj, const UClass* Class) 35 | { 36 | return _IGRP IsA(SoftObj.Get(), Class); 37 | } 38 | 39 | [[nodiscard]] inline constexpr auto NonNull() 40 | { 41 | return std::views::filter([](const void* x) { return x != nullptr; }); 42 | } 43 | 44 | } // namespace Private 45 | 46 | /** 47 | * Filters the values of a range of UObjects based on a specified type. 48 | * Performs a cast to the specified type and then filters null elements. 49 | * Equivalent to `Cast() | NonNull()`. 50 | * 51 | * All the "Of Type" range adapters are safe to accept null values and never yield null results. 52 | * 53 | * @usage 54 | * SomeActors | OfType() 55 | * SomeComponents | OfType() 56 | */ 57 | template 58 | [[nodiscard]] constexpr auto OfType() 59 | { 60 | return _IGR Cast() | _IGRP NonNull(); 61 | } 62 | 63 | /** 64 | * Similar to `OfType` but uses `ExactCast`. 65 | */ 66 | template 67 | [[nodiscard]] constexpr auto OfExactType() 68 | { 69 | return _IGR ExactCast() | _IGRP NonNull(); 70 | } 71 | 72 | /** 73 | * Similar to `OfType` (checks types) but does not perform a cast. 74 | */ 75 | [[nodiscard]] inline constexpr auto OfType(const UClass* Class) 76 | { 77 | return std::views::filter([Class](auto&& x) { return _IGRP IsA(x, Class); }); 78 | } 79 | 80 | } // namespace IG::Ranges 81 | 82 | #include "IGRanges/Impl/Epilogue.inl" 83 | -------------------------------------------------------------------------------- /Source/IGRanges/Private/Tests/IGRanges_CustomizationPoints.spec.cpp: -------------------------------------------------------------------------------- 1 | // Copyright Ian Good 2 | 3 | #include "IGRanges/CustomizationPoints.h" 4 | #include "IGRangesInternal.h" 5 | #include "Misc/AutomationTest.h" 6 | #include 7 | 8 | #if WITH_DEV_AUTOMATION_TESTS 9 | 10 | DEFINE_SPEC(FIGRangesCPOSpec, "IG.Ranges.CPO", EAutomationTestFlags::ApplicationContextMask | EAutomationTestFlags::ProductFilter); 11 | 12 | /** 13 | * This function pretty much just ensures that things compile when using UE containers with C++ Ranges. 14 | * If this fails to compile, that means there's probably some missing customization points for the container (see 15 | * `CustomizationPoints.h`). 16 | */ 17 | static void CheckCompat(auto&& Container) 18 | { 19 | UE_LOG(LogIGRangesTests, Verbose, TEXT("%hs"), __FUNCSIG__); 20 | 21 | const bool bIsEmpty = std::ranges::empty(Container); 22 | UE_LOG(LogIGRangesTests, Verbose, TEXT("IsEmpty=%s"), bIsEmpty ? TEXT("true") : TEXT("false")); 23 | 24 | const int32 Count = std::ranges::distance(Container); 25 | UE_LOG(LogIGRangesTests, Verbose, TEXT("Count=%d"), Count); 26 | 27 | auto EvenElements = Container | std::views::filter([](auto&& x) { return x % 2 == 0; }); 28 | 29 | auto SquaredElements = Container | std::views::transform([](auto&& x) { return x * x; }); 30 | 31 | auto Sqevens = 32 | Container 33 | | std::views::filter([](auto&& x) { return x % 2 == 0; }) 34 | | std::views::transform([](auto&& x) { return x * x; }); 35 | for (const int32 X : Sqevens) 36 | { 37 | UE_LOG(LogIGRangesTests, Verbose, TEXT("X=%d"), X); 38 | } 39 | } 40 | 41 | #define IGR_CHECK_COMPAT(_ContainerType) \ 42 | { \ 43 | _ContainerType C; \ 44 | CheckCompat(C); \ 45 | CheckCompat(MoveTempIfPossible(C)); \ 46 | } 47 | 48 | void FIGRangesCPOSpec::Define() 49 | { 50 | It("check_compat", [this]() { 51 | IGR_CHECK_COMPAT(TArray); 52 | IGR_CHECK_COMPAT(TArray); 53 | IGR_CHECK_COMPAT(const TArray); 54 | IGR_CHECK_COMPAT(const TArray); 55 | 56 | IGR_CHECK_COMPAT(TArrayView); 57 | IGR_CHECK_COMPAT(TArrayView); 58 | IGR_CHECK_COMPAT(const TArrayView); 59 | IGR_CHECK_COMPAT(const TArrayView); 60 | }); 61 | } 62 | 63 | #endif // WITH_DEV_AUTOMATION_TESTS 64 | -------------------------------------------------------------------------------- /Source/IGRanges/Public/IGRanges/Filters/IsChildOf.h: -------------------------------------------------------------------------------- 1 | // Copyright Ian Good 2 | 3 | #pragma once 4 | 5 | #include "IGRanges/Impl/Common.h" 6 | #include "Templates/SubclassOf.h" 7 | #include "UObject/Class.h" 8 | #include "UObject/SoftObjectPtr.h" 9 | #include "UObject/WeakObjectPtrTemplates.h" 10 | 11 | #include "IGRanges/Impl/Prologue.inl" 12 | 13 | namespace IG::Ranges 14 | { 15 | namespace Private 16 | { 17 | /** 18 | * Gets the input UClass-like value as a `UStruct*`. 19 | * This function is expected to be used with types like... 20 | * - UClass* 21 | * - TObjectPtr 22 | * - TWeakObjectPtr 23 | * - TSubclassOf 24 | * - TSoftClassPtr 25 | * All of the smart pointer types have a `Get` method that we want to use to get the underlying value. 26 | */ 27 | template 28 | [[nodiscard]] const UStruct* AsStructPointer(ClassType&& ClassLike) 29 | { 30 | if constexpr (_IGRP HasGet) 31 | { 32 | return ClassLike.Get(); 33 | } 34 | else 35 | { 36 | return ClassLike; 37 | } 38 | } 39 | 40 | } // namespace Private 41 | 42 | namespace Filters 43 | { 44 | /** 45 | * Filter that tests whether a UClass-like value is a child of the specified class. 46 | * Safe to use with null inputs. 47 | * 48 | * This filter is expected to be used with types like... 49 | * - UClass* 50 | * - TObjectPtr 51 | * - TWeakObjectPtr 52 | * - TSubclassOf 53 | * - TSoftClassPtr 54 | * 55 | * @usage SomeClasses | Where(Filters::IsChildOf(FooClass)) 56 | */ 57 | template 58 | [[nodiscard]] auto IsChildOf(ClassType&& ClassLike) 59 | { 60 | return [SomeBase = _IGRP AsStructPointer(ClassLike)](auto&& x) { 61 | const UStruct* Struct = _IGRP AsStructPointer(x); 62 | return Struct != nullptr && Struct->IsChildOf(SomeBase); 63 | }; 64 | } 65 | 66 | /** 67 | * Filter that tests whether a UClass-like value is a child of the specified class. 68 | * Safe to use with null inputs. 69 | * 70 | * This filter is expected to be used with types like... 71 | * - UClass* 72 | * - TObjectPtr 73 | * - TWeakObjectPtr 74 | * - TSubclassOf 75 | * - TSoftClassPtr 76 | * 77 | * @usage SomeClasses | Where(Filters::IsChildOf()) 78 | */ 79 | template 80 | [[nodiscard]] auto IsChildOf() 81 | { 82 | return _IGR Filters::IsChildOf(T::StaticClass()); 83 | } 84 | 85 | } // namespace Filters 86 | 87 | } // namespace IG::Ranges 88 | 89 | #include "IGRanges/Impl/Epilogue.inl" 90 | -------------------------------------------------------------------------------- /Source/IGRanges/Private/Tests/IGRanges_Cast.spec.cpp: -------------------------------------------------------------------------------- 1 | // Copyright Ian Good 2 | 3 | #include "IGRanges/Cast.h" 4 | #include "IGRangesInternal.h" 5 | #include "Misc/AutomationTest.h" 6 | 7 | #if WITH_DEV_AUTOMATION_TESTS 8 | 9 | BEGIN_DEFINE_SPEC(FIGRangesCastSpec, "IG.Ranges.Cast", EAutomationTestFlags::ApplicationContextMask | EAutomationTestFlags::ProductFilter) 10 | 11 | /** 12 | * Given a range of pointer-like elements, tests that `IG::Ranges::Cast` behaves the same as `::Cast`. 13 | */ 14 | template 15 | bool TestPointers(const RangeType& Range) 16 | { 17 | TArray ExpectedPointers; 18 | for (auto&& X : Range) 19 | { 20 | ExpectedPointers.Emplace(::Cast(X)); 21 | } 22 | 23 | int32 i = -1; 24 | for (auto* X : Range | IG::Ranges::Cast()) 25 | { 26 | ++i; 27 | UTEST_TRUE_EXPR(ExpectedPointers.IsValidIndex(i)); 28 | UTEST_EQUAL("casted element", X, ExpectedPointers[i]); 29 | } 30 | 31 | return true; 32 | } 33 | 34 | /** 35 | * Gets a few CDOs and puts them into an array of type `PointerType[]`. 36 | * `PointerType` is a raw pointer or a smart pointer type (e.g. UObject*, TObjectPtr, TWeakObjectPtr). 37 | * This array of pointer-like things is then tested with `Cast`. 38 | */ 39 | template 40 | void TestPointers() 41 | { 42 | PointerType O = GetDefault(); 43 | PointerType F = GetDefault(); // is a UObject 44 | PointerType E = GetDefault(); // is a UField 45 | PointerType S = GetDefault(); // is a UField 46 | PointerType C = GetDefault(); // is a UStruct 47 | PointerType SomePointers[] = {nullptr, O, F, E, S, C, nullptr, nullptr, C, O, F, F, E, E}; 48 | 49 | TestPointers(SomePointers); 50 | } 51 | 52 | END_DEFINE_SPEC(FIGRangesCastSpec) 53 | 54 | void FIGRangesCastSpec::Define() 55 | { 56 | // `Cast` yields pointers to objects of the specified type or null. 57 | It("yields_objects_of_specified_type_or_null (raw pointer)", [this]() { 58 | TestPointers(); 59 | }); 60 | 61 | // `Cast` yields pointers to objects of the specified type or null. 62 | It("yields_objects_of_specified_type_or_null (TObjectPtr)", [this]() { 63 | TestPointers>(); 64 | }); 65 | 66 | // `Cast` yields pointers to objects of the specified type or null. 67 | It("yields_objects_of_specified_type_or_null (TWeakObjectPtr)", [this]() { 68 | TestPointers>(); 69 | }); 70 | } 71 | 72 | #endif // WITH_DEV_AUTOMATION_TESTS 73 | -------------------------------------------------------------------------------- /Source/IGRanges/Private/Tests/IGRanges_NonNull.spec.cpp: -------------------------------------------------------------------------------- 1 | // Copyright Ian Good 2 | 3 | #include "IGRanges/NonNull.h" 4 | #include "IGRangesInternal.h" 5 | #include "Misc/AutomationTest.h" 6 | #include 7 | 8 | #if WITH_DEV_AUTOMATION_TESTS 9 | 10 | BEGIN_DEFINE_SPEC(FIGRangesNonNullSpec, "IG.Ranges.NonNull", EAutomationTestFlags::ApplicationContextMask | EAutomationTestFlags::ProductFilter) 11 | 12 | /** 13 | * Given a range of pointer-like elements, tests that `NonNull` behaves the same as a null-check. 14 | */ 15 | template 16 | bool TestPointers(const RangeType& Range) 17 | { 18 | using namespace IG::Ranges; 19 | using T = std::ranges::range_value_t; 20 | 21 | TArray ExpectedPointers; 22 | for (auto&& X : Range) 23 | { 24 | if (X != nullptr) 25 | { 26 | ExpectedPointers.Emplace(X); 27 | } 28 | } 29 | 30 | int32 i = -1; 31 | for (auto&& X : Range | NonNull()) 32 | { 33 | ++i; 34 | UTEST_TRUE_EXPR(ExpectedPointers.IsValidIndex(i)); 35 | UTEST_EQUAL("non-null element", X, ExpectedPointers[i]); 36 | } 37 | 38 | return true; 39 | } 40 | 41 | END_DEFINE_SPEC(FIGRangesNonNullSpec) 42 | 43 | void FIGRangesNonNullSpec::Define() 44 | { 45 | // `NonNull` yields only non-null pointers. 46 | It("removes_null_pointers", [this]() { 47 | int32 A = 1; 48 | int32 B = 2; 49 | int32 C = 3; 50 | int32 D = 4; 51 | const int32* SomePointers[] = {nullptr, &A, &B, &C, &D, nullptr, nullptr, &D, &A, &D}; 52 | 53 | TestPointers(SomePointers); 54 | }); 55 | 56 | // `NonNull` yields only non-null shared pointers. 57 | It("removes_null_shared_pointers", [this]() { 58 | TSharedPtr A = MakeShared(1); 59 | TSharedPtr B = MakeShared(2); 60 | TSharedPtr C = MakeShared(3); 61 | TSharedPtr D = MakeShared(4); 62 | const TSharedPtr SomePointers[] = {nullptr, A, B, C, D, nullptr, nullptr, D, A, D}; 63 | 64 | TestPointers(SomePointers); 65 | }); 66 | 67 | // `NonNull` yields only non-null weak pointers. 68 | It("removes_null_weak_pointers", [this]() { 69 | TSharedPtr A = MakeShared(1); 70 | TSharedPtr B = MakeShared(2); 71 | TSharedPtr C = MakeShared(3); 72 | TSharedPtr D = MakeShared(4); 73 | const TSharedPtr SomePointers[] = {nullptr, A, B, C, D, nullptr, nullptr, D, A, D}; 74 | 75 | auto SomeWeakPointers = SomePointers | std::views::transform([](auto&& x) { return x.ToWeakPtr(); }); 76 | TestPointers(SomeWeakPointers); 77 | }); 78 | } 79 | 80 | #endif // WITH_DEV_AUTOMATION_TESTS 81 | -------------------------------------------------------------------------------- /Source/IGRanges/Private/Tests/IGRanges_Select.spec.cpp: -------------------------------------------------------------------------------- 1 | // Copyright Ian Good 2 | 3 | #include "IGRanges/Select.h" 4 | #include "IGRangesInternal.h" 5 | #include "Misc/AutomationTest.h" 6 | #include 7 | 8 | #if WITH_DEV_AUTOMATION_TESTS 9 | 10 | BEGIN_DEFINE_SPEC(FIGRangesSelectSpec, "IG.Ranges.Select", EAutomationTestFlags::ApplicationContextMask | EAutomationTestFlags::ProductFilter) 11 | 12 | static constexpr double SquareD(const int32 X) 13 | { 14 | return static_cast(X) * static_cast(X); 15 | } 16 | 17 | constexpr struct FMyNumber 18 | { 19 | constexpr FMyNumber(int32 InN) 20 | : N(InN) 21 | , Squared(SquareD(InN)) 22 | { 23 | } 24 | 25 | const int32 N = 0; 26 | 27 | const double Squared = 0.0; 28 | 29 | constexpr double Square() const { return SquareD(N); } 30 | }; 31 | 32 | static double StaticSquare(const FMyNumber& X) 33 | { 34 | return SquareD(X.N); 35 | }; 36 | 37 | /** 38 | * Tests that a `Select` tranformation behaves the same as using a transformation directly. 39 | */ 40 | template 41 | bool TestCallable(TransformT&& Transform) 42 | { 43 | using namespace IG::Ranges; 44 | 45 | static constexpr FMyNumber SomeNumbers[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; 46 | static constexpr int32 NumSomeNumbers = UE_ARRAY_COUNT(SomeNumbers); 47 | 48 | TArray ExpectedValues; 49 | ExpectedValues.Reserve(NumSomeNumbers); 50 | for (auto&& X : SomeNumbers) 51 | { 52 | ExpectedValues.Emplace(std::invoke(Transform, X)); 53 | } 54 | 55 | int32 i = -1; 56 | for (auto&& X : SomeNumbers | Select(Transform)) 57 | { 58 | ++i; 59 | UTEST_TRUE_EXPR(ExpectedValues.IsValidIndex(i)); 60 | UTEST_EQUAL("transformed element", X, ExpectedValues[i]); 61 | } 62 | 63 | UTEST_EQUAL("count", i + 1, ExpectedValues.Num()); 64 | 65 | return true; 66 | } 67 | 68 | END_DEFINE_SPEC(FIGRangesSelectSpec) 69 | 70 | void FIGRangesSelectSpec::Define() 71 | { 72 | // `Select` accepts function pointers as a transformation. 73 | It("function_pointer", [this]() { 74 | TestCallable(&StaticSquare); 75 | }); 76 | 77 | // `Select` accepts function objects as a transformation. 78 | It("function_object", [this]() { 79 | const auto Square = [](auto&& x) { 80 | return SquareD(x.N); 81 | }; 82 | 83 | TestCallable(Square); 84 | }); 85 | 86 | // `Select` accepts member field pointers as a transformation. 87 | It("member_field_pointer", [this]() { 88 | TestCallable(&FMyNumber::Squared); 89 | }); 90 | 91 | // `Select` accepts member function pointers as a transformation. 92 | It("member_function_pointer", [this]() { 93 | TestCallable(&FMyNumber::Square); 94 | }); 95 | } 96 | 97 | #endif // WITH_DEV_AUTOMATION_TESTS 98 | -------------------------------------------------------------------------------- /Source/IGRanges/Public/IGRanges/Selectors/CDO.h: -------------------------------------------------------------------------------- 1 | // Copyright Ian Good 2 | 3 | #pragma once 4 | 5 | #include "IGRanges/Impl/Common.h" 6 | #include "Templates/SubclassOf.h" 7 | #include "UObject/Class.h" 8 | #include "UObject/SoftObjectPtr.h" 9 | #include "UObject/WeakObjectPtrTemplates.h" 10 | 11 | #include "IGRanges/Impl/Prologue.inl" 12 | 13 | namespace IG::Ranges 14 | { 15 | namespace Private 16 | { 17 | template 18 | concept HasGetDefaultObject = requires(T t) { 19 | t.GetDefaultObject(); 20 | }; 21 | 22 | template 23 | concept IsSoftPtr = 24 | _IGRP HasGet 25 | && requires { 26 | typename T::ElementType; 27 | } // 28 | && requires(T t) { 29 | t.LoadSynchronous(); 30 | }; 31 | 32 | } // namespace Private 33 | 34 | namespace Selectors 35 | { 36 | /** 37 | * Selector that yields the "class default object" / "constructed default object" (CDO) from UClass-like values. 38 | * Safe to use with null inputs. 39 | * May yield null. 40 | * 41 | * This selector is expected to be used with types like... 42 | * - UClass* 43 | * - TObjectPtr 44 | * - TWeakObjectPtr 45 | * - TSubclassOf 46 | * - TSoftClassPtr 47 | * 48 | * @usage SomeClasses | Select(Selectors::CDO) 49 | */ 50 | inline constexpr auto CDO = [](ClassType&& ClassLike) { 51 | // Remove reference to go from things like (A) to (B) so we can do things with (C): 52 | // (A) const TSoftClassPtr& 53 | // (B) const TSoftClassPtr 54 | // (C) ValueClassType::ElementType 55 | using ValueClassType = std::remove_reference_t; 56 | 57 | // `TSubclassOf::GetDefaultObject` knows the class type & does the null-checks & casting for us. 58 | if constexpr (_IGRP HasGetDefaultObject) 59 | { 60 | return ClassLike.GetDefaultObject(); 61 | } 62 | // `TSoftClassPtr` knows the class type, but we need to perform the cast (returns `UFoo*`). 63 | else if constexpr (_IGRP IsSoftPtr) 64 | { 65 | const UClass* Class = ClassLike.Get(); 66 | return (Class != nullptr) ? static_cast(Class->GetDefaultObject()) : nullptr; 67 | } 68 | // Other UClass-like types don't know the class type, so we cannot cast (just returns `UObject*`). 69 | else if constexpr (_IGRP HasGet) 70 | { 71 | const UClass* Class = ClassLike.Get(); 72 | return (Class != nullptr) ? Class->GetDefaultObject() : nullptr; 73 | } 74 | else 75 | { 76 | const UClass* Class = ClassLike; 77 | return (Class != nullptr) ? Class->GetDefaultObject() : nullptr; 78 | } 79 | }; 80 | 81 | } // namespace Selectors 82 | 83 | } // namespace IG::Ranges 84 | 85 | #include "IGRanges/Impl/Epilogue.inl" 86 | -------------------------------------------------------------------------------- /Source/IGRanges/Private/Tests/IGRanges_ToArray.spec.cpp: -------------------------------------------------------------------------------- 1 | // Copyright Ian Good 2 | 3 | #include "IGRanges/ToArray.h" 4 | #include "IGRangesInternal.h" 5 | #include "Misc/AutomationTest.h" 6 | #include 7 | 8 | #if WITH_DEV_AUTOMATION_TESTS 9 | 10 | BEGIN_DEFINE_SPEC(FIGRangesToArraySpec, "IG.Ranges.ToArray", EAutomationTestFlags::ApplicationContextMask | EAutomationTestFlags::ProductFilter) 11 | 12 | void TestArray(const TArray& Actual, const TArray& Expected) 13 | { 14 | TestEqual("count", Actual.Num(), Expected.Num()); 15 | TestEqual("capacity", Actual.Max(), Expected.Max()); 16 | TestEqual("contents", Actual, Expected); 17 | } 18 | 19 | END_DEFINE_SPEC(FIGRangesToArraySpec) 20 | 21 | void FIGRangesToArraySpec::Define() 22 | { 23 | using namespace IG::Ranges; 24 | 25 | // These values represent our "expected array". 26 | // This number of elements has been chosen because we expect the reallocation strategy for `TArray` will give us 27 | // different values for `Num` & `Max` when adding them one at a time. 28 | static constexpr int32 SomeValues[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; 29 | static constexpr int32 NumSomeValues = UE_ARRAY_COUNT(SomeValues); 30 | 31 | // `ToArray` with an empty range produces an array with zero count & capacity. 32 | It("empty", [this]() { 33 | const TArray TestMe = std::ranges::empty_view() | ToArray(); 34 | TestArray(TestMe, {}); 35 | }); 36 | 37 | // `ToArray` with a range whose size is known produces an array with equal count & capacity. 38 | It("many", [this]() { 39 | TArray ExpectedArray; 40 | ExpectedArray.Append(SomeValues); 41 | 42 | const TArray TestMe = SomeValues | ToArray(); 43 | TestArray(TestMe, ExpectedArray); 44 | }); 45 | 46 | // `ToArray` with a range whose size is known produces an array with equal count & capacity. 47 | // (works even with transformations) 48 | It("many_transformed", [this]() { 49 | const auto Square = [](auto&& x) { 50 | return x * x; 51 | }; 52 | 53 | TArray ExpectedArray; 54 | ExpectedArray.Reserve(NumSomeValues); 55 | for (auto&& X : SomeValues) 56 | { 57 | ExpectedArray.Emplace(Square(X)); 58 | } 59 | 60 | // `ToArray` with 0 arguments (just creates an array from the view). 61 | TArray TestMe = SomeValues | std::views::transform(Square) | ToArray(); 62 | TestArray(TestMe, ExpectedArray); 63 | 64 | // `ToArray` with transformation argument (applies the transformation). 65 | TestMe = SomeValues | ToArray(Square); 66 | TestArray(TestMe, ExpectedArray); 67 | }); 68 | 69 | // `ToArray` with a range whose size is unknown produces an array with count & capacity consistent with traditional 70 | // `TArray` usage. 71 | It("many_filtered", [this]() { 72 | const auto IsEven = [](auto&& x) { 73 | return x % 2 == 0; 74 | }; 75 | 76 | TArray ExpectedArray; 77 | for (auto&& X : SomeValues) 78 | { 79 | if (IsEven(X)) 80 | { 81 | ExpectedArray.Emplace(X); 82 | } 83 | } 84 | 85 | const TArray TestMe = SomeValues | std::views::filter(IsEven) | ToArray(); 86 | TestArray(TestMe, ExpectedArray); 87 | }); 88 | } 89 | 90 | #endif // WITH_DEV_AUTOMATION_TESTS 91 | -------------------------------------------------------------------------------- /Source/IGRanges/Public/IGRanges/AllAnyNone.h: -------------------------------------------------------------------------------- 1 | // Copyright Ian Good 2 | 3 | #pragma once 4 | 5 | #include "IGRanges/Impl/Common.h" 6 | #include 7 | #include 8 | 9 | #include "IGRanges/Impl/Prologue.inl" 10 | 11 | namespace IG::Ranges 12 | { 13 | namespace Private 14 | { 15 | enum class EAlgoChoice 16 | { 17 | AllOf, 18 | AnyOf, 19 | NoneOf, 20 | }; 21 | 22 | template 23 | struct Algo_fn 24 | { 25 | template 26 | [[nodiscard]] constexpr bool operator()(RangeType&& Range, _Pr _Pred) const 27 | { 28 | if constexpr (_Choice == EAlgoChoice::AllOf) 29 | { 30 | return std::all_of(std::begin(Range), std::end(Range), std::move(_Pred)); 31 | } 32 | else if constexpr (_Choice == EAlgoChoice::AnyOf) 33 | { 34 | return std::any_of(std::begin(Range), std::end(Range), std::move(_Pred)); 35 | } 36 | else if constexpr (_Choice == EAlgoChoice::NoneOf) 37 | { 38 | return std::none_of(std::begin(Range), std::end(Range), std::move(_Pred)); 39 | } 40 | } 41 | }; 42 | 43 | template 44 | [[nodiscard]] constexpr auto ChooseAlgo(_Pr&& _Pred) 45 | { 46 | return std::ranges::_Range_closure, std::decay_t<_Pr>>{std::forward<_Pr>(_Pred)}; 47 | } 48 | 49 | } // namespace Private 50 | 51 | /** 52 | * Returns True if all elements in the range satisfy the predicate (or the range is empty); otherwise, False. 53 | * If no predicate is specified, then elements themselves are tested for "truthiness". 54 | * 55 | * Alias for `std::all_of`: 56 | * Checks if a unary predicate returns True for all elements in the range. 57 | * 58 | * @usage 59 | * bool bAllEven = SomeNumbers | All([](int32 N) { return N % 2 == 0; }); 60 | * bool bAllGood = SomeStructs | All(&FBar::IsGood); 61 | */ 62 | template 63 | [[nodiscard]] constexpr auto All(_Pr&& _Pred = {}) 64 | { 65 | return _IGRP ChooseAlgo<_IGRP EAlgoChoice::AllOf>(std::forward<_Pr>(_Pred)); 66 | } 67 | 68 | /** 69 | * Returns True if any element in the range satisfies the predicate; otherwise, False. 70 | * If no predicate is specified, then returns whether there are any elements in the range at all. 71 | * 72 | * Alias for `std::any_of`: 73 | * Checks if a unary predicate returns True for at least one element in the range. 74 | * 75 | * @usage 76 | * bool bSomeGood = SomeStructs | Any(&FBar::IsGood); 77 | */ 78 | template 79 | [[nodiscard]] constexpr auto Any(_Pr&& _Pred = {}) 80 | { 81 | return _IGRP ChooseAlgo<_IGRP EAlgoChoice::AnyOf>(std::forward<_Pr>(_Pred)); 82 | } 83 | 84 | /** 85 | * Returns True if no element in the range satisfies the predicate (or the range is empty); otherwise, False. 86 | * If no predicate is specified, then elements themselves are tested for "truthiness". 87 | * 88 | * Alias for `std::none_of`: 89 | * Checks if a unary predicate returns True for no elements in the range. 90 | * 91 | * @usage 92 | * bool bNoGood = SomeStructs | None(&FBar::IsGood); 93 | */ 94 | template 95 | [[nodiscard]] constexpr auto None(_Pr&& _Pred = {}) 96 | { 97 | return _IGRP ChooseAlgo<_IGRP EAlgoChoice::NoneOf>(std::forward<_Pr>(_Pred)); 98 | } 99 | 100 | } // namespace IG::Ranges 101 | 102 | #include "IGRanges/Impl/Epilogue.inl" 103 | -------------------------------------------------------------------------------- /Source/IGRanges/Private/Tests/IGRanges_CDO.spec.cpp: -------------------------------------------------------------------------------- 1 | // Copyright Ian Good 2 | 3 | #include "IGRanges/Select.h" 4 | #include "IGRanges/Selectors/CDO.h" 5 | #include "IGRangesInternal.h" 6 | #include "Misc/AutomationTest.h" 7 | 8 | #if WITH_DEV_AUTOMATION_TESTS 9 | 10 | BEGIN_DEFINE_SPEC(FIGRangesCDOSpec, "IG.Ranges.CDO", EAutomationTestFlags::ApplicationContextMask | EAutomationTestFlags::ProductFilter) 11 | 12 | /** 13 | * Given a range of pointer-like elements, tests that `CDO` behaves the same as `GetDefaultObject`. 14 | */ 15 | template 16 | bool TestPointers(const RangeType& Range) 17 | { 18 | using namespace IG::Ranges; 19 | 20 | TArray ExpectedPointers; 21 | for (auto&& X : Range) 22 | { 23 | ExpectedPointers.Emplace( 24 | (X != nullptr) 25 | ? static_cast(X->GetDefaultObject()) 26 | : nullptr); 27 | } 28 | 29 | int32 i = -1; 30 | for (ExpectedCDOType X : Range | Select(Selectors::CDO)) 31 | { 32 | ++i; 33 | UTEST_TRUE_EXPR(ExpectedPointers.IsValidIndex(i)); 34 | UTEST_EQUAL("casted element", X, ExpectedPointers[i]); 35 | } 36 | 37 | UTEST_EQUAL("count", i + 1, ExpectedPointers.Num()); 38 | 39 | return true; 40 | } 41 | 42 | /** 43 | * Gets a few UClasses and puts them into an array of type `PointerType[]`. 44 | * `PointerType` is a raw pointer or a smart pointer type (e.g. UClass*, TObjectPtr, TWeakObjectPtr). 45 | * This array of pointer-like things is then tested with `CDO`. 46 | */ 47 | template 48 | void TestPointers() 49 | { 50 | UClass* O = UObject::StaticClass(); 51 | UClass* F = UField::StaticClass(); // is a UObject 52 | UClass* E = UEnum::StaticClass(); // is a UField 53 | UClass* S = UStruct::StaticClass(); // is a UField 54 | UClass* C = UClass::StaticClass(); // is a UStruct 55 | PointerType SomePointers[] = {nullptr, O, F, E, S, C, nullptr, nullptr, C, O, F, F, E, E}; 56 | 57 | // For smart pointer types like `TSubclassOf` & `TSoftClassPtr`, Unreal doesn't protect us from creating bad 58 | // instances like this where the UClasses involved are not correct: 59 | // /* ohno */ TSubclassOf = UStruct::StaticClass(); 60 | // But Unreal does protect us from using bad instances by returning Null from `Get`, so we'll weed those out here 61 | // so our tests don't break. 62 | if constexpr (IG::Ranges::Private::HasGet) 63 | { 64 | for (PointerType& P : SomePointers) 65 | { 66 | if (P.Get() == nullptr) 67 | { 68 | P = nullptr; 69 | } 70 | } 71 | } 72 | 73 | TestPointers(SomePointers); 74 | } 75 | 76 | END_DEFINE_SPEC(FIGRangesCDOSpec) 77 | 78 | void FIGRangesCDOSpec::Define() 79 | { 80 | It("yields_CDOs_from_classes (raw pointer)", [this]() { 81 | TestPointers(); 82 | }); 83 | 84 | It("yields_CDOs_from_classes (TObjectPtr)", [this]() { 85 | TestPointers, UObject*>(); 86 | }); 87 | 88 | It("yields_CDOs_from_classes (TWeakObjectPtr)", [this]() { 89 | TestPointers, UObject*>(); 90 | }); 91 | 92 | It("yields_CDOs_from_classes (TSubclassOf)", [this]() { 93 | TestPointers, const UStruct*>(); 94 | }); 95 | 96 | It("yields_CDOs_from_classes (TSoftClassPtr)", [this]() { 97 | TestPointers, const UStruct*>(); 98 | }); 99 | } 100 | 101 | #endif // WITH_DEV_AUTOMATION_TESTS 102 | -------------------------------------------------------------------------------- /Source/IGRanges/Private/Tests/IGRanges_IsChildOf.spec.cpp: -------------------------------------------------------------------------------- 1 | // Copyright Ian Good 2 | 3 | #include "IGRanges/Filters/IsChildOf.h" 4 | #include "IGRanges/Where.h" 5 | #include "IGRangesInternal.h" 6 | #include "Misc/AutomationTest.h" 7 | 8 | #if WITH_DEV_AUTOMATION_TESTS 9 | 10 | BEGIN_DEFINE_SPEC(FIGRangesIsChildOfSpec, "IG.Ranges.IsChildOf", EAutomationTestFlags::ApplicationContextMask | EAutomationTestFlags::ProductFilter) 11 | 12 | /** 13 | * Given a range of pointer-like elements, tests that `IG::Ranges::Filters::IsChildOf` behaves the same as `UStruct::IsChildOf`. 14 | */ 15 | template 16 | bool TestPointers(const RangeType& Range) 17 | { 18 | using namespace IG::Ranges; 19 | 20 | TArray ExpectedPointers; 21 | for (auto&& X : Range) 22 | { 23 | if (X != nullptr && X->IsChildOf()) 24 | { 25 | ExpectedPointers.Emplace(IG::Ranges::Private::AsStructPointer(X)); 26 | } 27 | } 28 | 29 | int32 i = -1; 30 | for (auto&& X : Range | Where(Filters::IsChildOf())) 31 | { 32 | ++i; 33 | UTEST_TRUE_EXPR(ExpectedPointers.IsValidIndex(i)); 34 | 35 | const UStruct* Actual = IG::Ranges::Private::AsStructPointer(X); 36 | UTEST_EQUAL("casted element", Actual, ExpectedPointers[i]); 37 | } 38 | 39 | UTEST_EQUAL("count", i + 1, ExpectedPointers.Num()); 40 | 41 | return true; 42 | } 43 | 44 | /** 45 | * Gets a few UClasses and puts them into an array of type `PointerType[]`. 46 | * `PointerType` is a raw pointer or a smart pointer type (e.g. UClass*, TObjectPtr, TWeakObjectPtr). 47 | * This array of pointer-like things is then tested with `IsChildOf`. 48 | */ 49 | template 50 | void TestPointers() 51 | { 52 | UClass* O = UObject::StaticClass(); 53 | UClass* F = UField::StaticClass(); // is a UObject 54 | UClass* E = UEnum::StaticClass(); // is a UField 55 | UClass* S = UStruct::StaticClass(); // is a UField 56 | UClass* C = UClass::StaticClass(); // is a UStruct 57 | PointerType SomePointers[] = {nullptr, O, F, E, S, C, nullptr, nullptr, C, O, F, F, E, E}; 58 | 59 | // For smart pointer types like `TSubclassOf` & `TSoftClassPtr`, Unreal doesn't protect us from creating bad 60 | // instances like this where the UClasses involved are not correct: 61 | // /* ohno */ TSubclassOf = UStruct::StaticClass(); 62 | // But Unreal does protect us from using bad instances by returning Null from `Get`, so we'll weed those out here 63 | // so our tests don't break. 64 | if constexpr (IG::Ranges::Private::HasGet) 65 | { 66 | for (PointerType& P : SomePointers) 67 | { 68 | if (P.Get() == nullptr) 69 | { 70 | P = nullptr; 71 | } 72 | } 73 | } 74 | 75 | TestPointers(SomePointers); 76 | } 77 | 78 | END_DEFINE_SPEC(FIGRangesIsChildOfSpec) 79 | 80 | void FIGRangesIsChildOfSpec::Define() 81 | { 82 | It("yields_classes_of_specified_type (raw pointer)", [this]() { 83 | TestPointers(); 84 | }); 85 | 86 | It("yields_classes_of_specified_type (TObjectPtr)", [this]() { 87 | TestPointers>(); 88 | }); 89 | 90 | It("yields_classes_of_specified_type (TWeakObjectPtr)", [this]() { 91 | TestPointers>(); 92 | }); 93 | 94 | It("yields_classes_of_specified_type (TSubclassOf)", [this]() { 95 | TestPointers>(); 96 | }); 97 | 98 | It("yields_classes_of_specified_type (TSoftClassPtr)", [this]() { 99 | TestPointers>(); 100 | }); 101 | } 102 | 103 | #endif // WITH_DEV_AUTOMATION_TESTS 104 | -------------------------------------------------------------------------------- /Source/IGRanges/Private/Tests/IGRanges_Sum.spec.cpp: -------------------------------------------------------------------------------- 1 | // Copyright Ian Good 2 | 3 | #include "IGRanges/Sum.h" 4 | #include "IGRangesInternal.h" 5 | #include "Misc/AutomationTest.h" 6 | #include 7 | #include 8 | #include 9 | 10 | #if WITH_DEV_AUTOMATION_TESTS 11 | 12 | DEFINE_SPEC(FIGRangesSumSpec, "IG.Ranges.Sum", EAutomationTestFlags::ApplicationContextMask | EAutomationTestFlags::ProductFilter); 13 | 14 | void FIGRangesSumSpec::Define() 15 | { 16 | using namespace IG::Ranges; 17 | 18 | It("empty", [this]() { 19 | { 20 | const int32 ActualSum = std::ranges::empty_view() | Sum(); 21 | TestEqual("sum int32", ActualSum, 0); 22 | } 23 | { 24 | const FVector ActualSum = std::ranges::empty_view() | Sum(); 25 | TestEqual("sum FVector", ActualSum, FVector::ZeroVector); 26 | } 27 | { 28 | const FQuat ActualSum = std::ranges::empty_view() | Sum(); 29 | TestEqual("sum FQuat", ActualSum, FQuat::Identity); 30 | } 31 | { 32 | const FString ActualSum = std::ranges::empty_view() | Sum(); 33 | TestEqual("sum FString", ActualSum, FString{}); 34 | } 35 | }); 36 | 37 | It("single", [this]() { 38 | { 39 | const int32 Single[] = {123}; 40 | const int32 ActualSum = Single | Sum(); 41 | TestEqual("sum int32", ActualSum, Single[0]); 42 | } 43 | { 44 | const FVector Single[] = {FVector(1, 2, 3)}; 45 | const FVector ActualSum = Single | Sum(); 46 | TestEqual("sum FVector", ActualSum, Single[0]); 47 | } 48 | { 49 | const FQuat Single[] = {FQuat(1, 2, 3, 4)}; 50 | const FQuat ActualSum = Single | Sum(); 51 | TestEqual("sum FQuat", ActualSum, Single[0]); 52 | } 53 | { 54 | const FString Single[] = {TEXT("123")}; 55 | const FString ActualSum = Single | Sum(); 56 | TestEqual("sum FString", ActualSum, Single[0]); 57 | } 58 | }); 59 | 60 | It("many", [this]() { 61 | { 62 | const int32 SomeValues[] = {1, 2, 3, 4, 5}; 63 | const int32 ExpectedSum = std::accumulate(SomeValues, SomeValues + UE_ARRAY_COUNT(SomeValues), 0); 64 | const int32 ActualSum = SomeValues | Sum(); 65 | TestEqual("sum int32", ActualSum, ExpectedSum); 66 | } 67 | { 68 | const FVector SomeValues[] = {FVector(1.0), FVector(2.0), FVector(3.0), FVector(4.0), FVector(5.0)}; 69 | const FVector ExpectedSum = std::accumulate(SomeValues, SomeValues + UE_ARRAY_COUNT(SomeValues), FVector::ZeroVector); 70 | const FVector ActualSum = SomeValues | Sum(); 71 | TestEqual("sum FVector", ActualSum, ExpectedSum); 72 | } 73 | { 74 | const FQuat SomeValues[] = {FQuat(1.0), FQuat(2.0), FQuat(3.0), FQuat(4.0), FQuat(5.0)}; 75 | const FQuat ExpectedSum = std::accumulate(SomeValues, SomeValues + UE_ARRAY_COUNT(SomeValues), FQuat(EForceInit::ForceInitToZero)); 76 | const FQuat ActualSum = SomeValues | Sum(); 77 | TestEqual("sum FQuat", ActualSum, ExpectedSum); 78 | } 79 | { 80 | const FString SomeValues[] = {TEXT("1"), TEXT("2"), TEXT("3"), TEXT("4"), TEXT("5")}; 81 | const FString ExpectedSum = std::accumulate(SomeValues, SomeValues + UE_ARRAY_COUNT(SomeValues), FString{}); 82 | const FString ActualSum = SomeValues | Sum(); 83 | TestEqual("sum FString", ActualSum, ExpectedSum); 84 | } 85 | }); 86 | 87 | It("many_transformed", [this]() { 88 | const FString SomeValues[] = {TEXT("1"), TEXT("22"), TEXT("333"), TEXT("4444"), TEXT("55555")}; 89 | const auto AddLen = [](int32 Acc, const FString& Elem) { 90 | return Acc + Elem.Len(); 91 | }; 92 | const int32 ExpectedSum = std::accumulate(SomeValues, SomeValues + UE_ARRAY_COUNT(SomeValues), int32{}, AddLen); 93 | const int32 ActualSum = SomeValues | Sum(&FString::Len); 94 | TestEqual("sum int32", ActualSum, ExpectedSum); 95 | }); 96 | } 97 | 98 | #endif // WITH_DEV_AUTOMATION_TESTS 99 | -------------------------------------------------------------------------------- /Source/IGRanges/Private/Tests/IGRanges_AllAnyNone.spec.cpp: -------------------------------------------------------------------------------- 1 | // Copyright Ian Good 2 | 3 | #include "IGRanges/AllAnyNone.h" 4 | #include "IGRanges/Impl/Common.h" 5 | #include "IGRangesInternal.h" 6 | #include "Misc/AutomationTest.h" 7 | #include 8 | #include 9 | 10 | #if WITH_DEV_AUTOMATION_TESTS 11 | 12 | BEGIN_DEFINE_SPEC(FIGRangesAllAnyNoneSpec, "IG.Ranges.AllAnyNone", EAutomationTestFlags::ApplicationContextMask | EAutomationTestFlags::ProductFilter) 13 | 14 | static constexpr auto AlwaysTrue = [](auto&&) { 15 | return true; 16 | }; 17 | 18 | static constexpr auto AlwaysFalse = [](auto&&) { 19 | return false; 20 | }; 21 | 22 | static bool IsThree(int32 X) 23 | { 24 | return X == 3; 25 | } 26 | 27 | static bool IsFour(const TSharedPtr& X) 28 | { 29 | return X != nullptr && *X == 4; 30 | } 31 | 32 | template 33 | void TestRange(const RangeType& Range) 34 | { 35 | using namespace IG::Ranges; 36 | 37 | const bool bExpectedAll = std::ranges::all_of(Range, [](auto&& x) { return !!x; }); 38 | const bool bActualAll = Range | All(); 39 | TestEqual("all", bActualAll, bExpectedAll); 40 | 41 | const bool bExpectedAny = std::ranges::any_of(Range, AlwaysTrue); 42 | const bool bActualAny = Range | Any(); 43 | TestEqual("any", bExpectedAny, bExpectedAny); 44 | 45 | const bool bExpectedNone = std::ranges::none_of(Range, [](auto&& x) { return !!x; }); 46 | const bool bActualNone = Range | None(); 47 | TestEqual("none", bActualNone, bActualNone); 48 | } 49 | 50 | template 51 | void TestRange(const RangeType& Range, const PredicateT& Predicate) 52 | { 53 | using namespace IG::Ranges; 54 | 55 | const bool bExpectedAll = std::ranges::all_of(Range, Predicate); 56 | const bool bActualAll = Range | All(Predicate); 57 | TestEqual("all", bActualAll, bExpectedAll); 58 | 59 | const bool bExpectedAny = std::ranges::any_of(Range, Predicate); 60 | const bool bActualAny = Range | Any(Predicate); 61 | TestEqual("any", bActualAny, bExpectedAny); 62 | 63 | const bool bExpectedNone = std::ranges::none_of(Range, Predicate); 64 | const bool bActualNone = Range | None(Predicate); 65 | TestEqual("none", bActualNone, bActualNone); 66 | } 67 | 68 | END_DEFINE_SPEC(FIGRangesAllAnyNoneSpec) 69 | 70 | void FIGRangesAllAnyNoneSpec::Define() 71 | { 72 | using namespace IG::Ranges; 73 | 74 | static constexpr int32 SomeValues[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; 75 | 76 | const TSharedPtr A = MakeShared(1); 77 | const TSharedPtr B = MakeShared(2); 78 | const TSharedPtr C = MakeShared(3); 79 | const TSharedPtr D = MakeShared(4); 80 | const TSharedPtr SomePointers[] = {nullptr, A, B, C, D, nullptr, nullptr, D, A, D}; 81 | 82 | It("empty (int32)", [this]() { 83 | auto Empty = std::ranges::empty_view(); 84 | TestRange(Empty); 85 | TestRange(Empty, AlwaysTrue); 86 | TestRange(Empty, AlwaysFalse); 87 | TestRange(Empty, &IsThree); 88 | TestRange(Empty, [](int32 X) { return X == 7; }); 89 | }); 90 | 91 | It("empty (shared ptr)", [this]() { 92 | auto Empty = std::ranges::empty_view>(); 93 | TestRange(Empty); 94 | TestRange(Empty, AlwaysTrue); 95 | TestRange(Empty, AlwaysFalse); 96 | TestRange(Empty, std::mem_fn(&TSharedPtr::IsValid)); 97 | TestRange(Empty, &IsFour); 98 | TestRange(Empty, [](const TSharedPtr& X) { return X != nullptr && *X == 7; }); 99 | }); 100 | 101 | It("many (int32)", [this]() { 102 | TestRange(SomeValues); 103 | TestRange(SomeValues, AlwaysTrue); 104 | TestRange(SomeValues, AlwaysFalse); 105 | TestRange(SomeValues, &IsThree); 106 | TestRange(SomeValues, [](int32 X) { return X == 7; }); 107 | }); 108 | 109 | It("many (shared ptr)", [this, SomePointers]() { 110 | TestRange(SomePointers); 111 | TestRange(SomePointers, AlwaysTrue); 112 | TestRange(SomePointers, AlwaysFalse); 113 | TestRange(SomePointers, std::mem_fn(&TSharedPtr::IsValid)); 114 | TestRange(SomePointers, &IsFour); 115 | TestRange(SomePointers, [](const TSharedPtr& X) { return X != nullptr && *X == 7; }); 116 | }); 117 | } 118 | 119 | #endif // WITH_DEV_AUTOMATION_TESTS 120 | -------------------------------------------------------------------------------- /Source/IGRanges/Private/Tests/IGRanges_OfType.spec.cpp: -------------------------------------------------------------------------------- 1 | // Copyright Ian Good 2 | 3 | #include "IGRanges/OfType.h" 4 | #include "IGRangesInternal.h" 5 | #include "Misc/AutomationTest.h" 6 | #include "UObject/MetaData.h" 7 | #include "UObject/Package.h" 8 | 9 | #if WITH_DEV_AUTOMATION_TESTS 10 | 11 | BEGIN_DEFINE_SPEC(FIGRangesOfTypeSpec, "IG.Ranges.OfType", EAutomationTestFlags::ApplicationContextMask | EAutomationTestFlags::ProductFilter) 12 | 13 | /** 14 | * Given a range of pointer-like elements, tests that `OfType` behaves the same as `Cast`. 15 | */ 16 | template 17 | bool TestPointersOfType(const RangeType& Range) 18 | { 19 | using namespace IG::Ranges; 20 | 21 | TArray ExpectedPointers; 22 | for (auto&& X : Range) 23 | { 24 | if (auto* Casted = Cast(X)) 25 | { 26 | ExpectedPointers.Emplace(Casted); 27 | } 28 | } 29 | 30 | int32 i = -1; 31 | for (auto* X : Range | OfType()) 32 | { 33 | ++i; 34 | UTEST_TRUE_EXPR(ExpectedPointers.IsValidIndex(i)); 35 | UTEST_EQUAL("casted element", X, ExpectedPointers[i]); 36 | } 37 | 38 | UTEST_EQUAL("count", i + 1, ExpectedPointers.Num()); 39 | 40 | return true; 41 | } 42 | 43 | /** 44 | * Gets a few CDOs and puts them into an array of type `PointerType[]`. 45 | * `PointerType` is a raw pointer or a smart pointer type (e.g. UObject*, TObjectPtr, TWeakObjectPtr). 46 | * This array of pointer-like things is then tested with `OfType`. 47 | */ 48 | template 49 | void TestPointers() 50 | { 51 | PointerType A = GetDefault(); 52 | PointerType B = GetDefault(); 53 | PointerType C = GetDefault(); 54 | PointerType D = GetDefault(); 55 | PointerType SomePointers[] = {nullptr, A, B, C, D, nullptr, nullptr, D, A, D}; 56 | 57 | TestPointersOfType(SomePointers); 58 | } 59 | 60 | /** 61 | * Given a range of pointer-like elements, tests that `OfType` behaves the same as `IsA`. 62 | */ 63 | template 64 | bool TestPointersIsA(const RangeType& Range, const UClass* Class) 65 | { 66 | using namespace IG::Ranges; 67 | 68 | TArray ExpectedPointers; 69 | for (auto&& X : Range) 70 | { 71 | if (X != nullptr && X->IsA(Class)) 72 | { 73 | ExpectedPointers.Emplace(&*X); 74 | } 75 | } 76 | 77 | int32 i = -1; 78 | for (auto&& X : Range | OfType(Class)) 79 | { 80 | ++i; 81 | UTEST_TRUE_EXPR(ExpectedPointers.IsValidIndex(i)); 82 | UTEST_SAME("element", *X, *ExpectedPointers[i]); 83 | } 84 | 85 | UTEST_EQUAL("count", i + 1, ExpectedPointers.Num()); 86 | 87 | return true; 88 | } 89 | 90 | /** 91 | * Gets a few CDOs and puts them into an array of type `PointerType[]`. 92 | * `PointerType` is a raw pointer or a smart pointer type (e.g. UObject*, TObjectPtr, TWeakObjectPtr). 93 | * This array of pointer-like things is then tested with `OfType`. 94 | */ 95 | template 96 | void TestPointersIsA() 97 | { 98 | PointerType A = GetDefault(); 99 | PointerType B = GetDefault(); 100 | PointerType C = GetDefault(); 101 | PointerType D = GetDefault(); 102 | PointerType SomePointers[] = {nullptr, A, B, C, D, nullptr, nullptr, D, A, D}; 103 | 104 | TestPointersIsA(SomePointers, UMetaData::StaticClass()); 105 | } 106 | 107 | END_DEFINE_SPEC(FIGRangesOfTypeSpec) 108 | 109 | void FIGRangesOfTypeSpec::Define() 110 | { 111 | // `OfType` yields only pointers to objects of the specified type. 112 | It("yields_objects_of_specified_type (raw pointer)", [this]() { 113 | TestPointers(); 114 | }); 115 | 116 | // `OfType` yields only pointers to objects of the specified type. 117 | It("yields_objects_of_specified_type (TObjectPtr)", [this]() { 118 | TestPointers>(); 119 | }); 120 | 121 | // `OfType` yields only pointers to objects of the specified type. 122 | It("yields_objects_of_specified_type (TWeakObjectPtr)", [this]() { 123 | TestPointers>(); 124 | }); 125 | 126 | // `OfType` yields only pointers to objects of the specified class. 127 | It("yields_objects_of_specified_class (raw pointer)", [this]() { 128 | TestPointersIsA(); 129 | }); 130 | 131 | // `OfType` yields only pointers to objects of the specified class. 132 | It("yields_objects_of_specified_class (TObjectPtr)", [this]() { 133 | TestPointersIsA>(); 134 | }); 135 | 136 | // `OfType` yields only pointers to objects of the specified class. 137 | It("yields_objects_of_specified_class (TWeakObjectPtr)", [this]() { 138 | TestPointersIsA>(); 139 | }); 140 | } 141 | 142 | #endif // WITH_DEV_AUTOMATION_TESTS 143 | -------------------------------------------------------------------------------- /Source/IGRanges/Private/Tests/IGRanges_FirstOrDefault.spec.cpp: -------------------------------------------------------------------------------- 1 | // Copyright Ian Good 2 | 3 | #include "IGRanges/FirstOrDefault.h" 4 | #include "IGRangesInternal.h" 5 | #include "Misc/AutomationTest.h" 6 | #include 7 | 8 | #if WITH_DEV_AUTOMATION_TESTS 9 | 10 | BEGIN_DEFINE_SPEC(FIGRangesFirstOrDefaultSpec, "IG.Ranges.FirstOrDefault", EAutomationTestFlags::ApplicationContextMask | EAutomationTestFlags::ProductFilter) 11 | 12 | constexpr struct FMyNumber 13 | { 14 | constexpr FMyNumber() = default; 15 | 16 | constexpr FMyNumber(int32 InN) 17 | : N(InN) 18 | , bIsEven(InN % 2 == 0) 19 | { 20 | } 21 | 22 | FMyNumber& operator=(const FMyNumber& Other) 23 | { 24 | N = Other.N; 25 | bIsEven = Other.bIsEven; 26 | return *this; 27 | } 28 | 29 | bool operator==(const FMyNumber& Other) const 30 | { 31 | return N == Other.N && bIsEven == Other.bIsEven; 32 | } 33 | 34 | int32 N = 0; 35 | 36 | bool bIsEven = true; 37 | 38 | constexpr bool IsEven() const { return N % 2 == 0; } 39 | }; 40 | 41 | static constexpr auto AlwaysFalse = [](auto&&) { 42 | return false; 43 | }; 44 | 45 | template 46 | void TestRange(const FString& What, RangeType&& Range, T Default) 47 | { 48 | using namespace IG::Ranges; 49 | 50 | T Expected = std::move(Default); 51 | for (auto&& X : Range) 52 | { 53 | if (true) // add this to silence warning C4702: unreachable code 54 | { 55 | Expected = X; 56 | break; 57 | } 58 | } 59 | 60 | const T Actual = Range | FirstOrDefault(); 61 | TestEqual(What, Actual, Expected); 62 | } 63 | 64 | template 65 | void TestFilteredRange(const FString& What, RangeType&& Range, _Pr&& _Pred, T Default) 66 | { 67 | using namespace IG::Ranges; 68 | 69 | T Expected = std::move(Default); 70 | for (auto&& X : Range) 71 | { 72 | if (std::invoke(_Pred, X)) 73 | { 74 | Expected = X; 75 | break; 76 | } 77 | } 78 | 79 | const T Actual = Range | FirstOrDefault(_Pred); 80 | TestEqual(What, Actual, Expected); 81 | } 82 | 83 | END_DEFINE_SPEC(FIGRangesFirstOrDefaultSpec) 84 | 85 | void FIGRangesFirstOrDefaultSpec::Define() 86 | { 87 | static constexpr int32 SomeValues[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; 88 | 89 | static constexpr FMyNumber SomeNumbers[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; 90 | 91 | const TSharedPtr A = MakeShared(1); 92 | const TSharedPtr B = MakeShared(2); 93 | const TSharedPtr C = MakeShared(3); 94 | const TSharedPtr D = MakeShared(4); 95 | const TSharedPtr SomePointers[] = {nullptr, A, B, C, D, nullptr, nullptr, D, A, D}; 96 | 97 | It("empty (int32)", [this]() { 98 | TestRange("default", std::ranges::empty_view(), 0); 99 | }); 100 | 101 | It("empty (struct)", [this]() { 102 | TestRange("default FMyNumber", std::ranges::empty_view(), FMyNumber{}); 103 | // Test that `FVector` returns "zero" instead of uninitialized. 104 | TestRange("default FVector", std::ranges::empty_view(), FVector::ZeroVector); 105 | // Test that `FQuat` returns "identity" instead of uninitialized. 106 | TestRange("default FQuat", std::ranges::empty_view(), FQuat::Identity); 107 | }); 108 | 109 | It("empty (shared ptr)", [this]() { 110 | TestRange("default", std::ranges::empty_view>(), TSharedPtr{}); 111 | }); 112 | 113 | It("many (int32)", [this]() { 114 | TestRange("first", SomeValues, 0); 115 | }); 116 | 117 | It("many (struct)", [this]() { 118 | TestRange("first", SomeNumbers, FMyNumber{}); 119 | }); 120 | 121 | It("many (shared ptr)", [this, SomePointers]() { 122 | TestRange("first", SomePointers, TSharedPtr{}); 123 | }); 124 | 125 | It("many_filtered (int32)", [this]() { 126 | const auto IsEven = [](auto&& x) { 127 | return x % 2 == 0; 128 | }; 129 | 130 | TestFilteredRange("default", SomeValues, AlwaysFalse, 0); 131 | TestFilteredRange("filter+first", SomeValues, IsEven, 0); 132 | }); 133 | 134 | It("many_filtered (struct)", [this]() { 135 | TestFilteredRange("default", SomeNumbers, AlwaysFalse, FMyNumber{}); 136 | TestFilteredRange("filter+first", SomeNumbers, &FMyNumber::bIsEven, FMyNumber{}); 137 | TestFilteredRange("filter+first", SomeNumbers, &FMyNumber::IsEven, FMyNumber{}); 138 | }); 139 | 140 | It("many_filtered (shared ptr)", [this, SomePointers]() { 141 | const auto IsEven = [](auto&& x) { 142 | return x.IsValid() && (*x % 2 == 0); 143 | }; 144 | 145 | TestFilteredRange("default", SomePointers, AlwaysFalse, TSharedPtr{}); 146 | TestFilteredRange("filter+first", SomePointers, IsEven, TSharedPtr{}); 147 | }); 148 | } 149 | 150 | #endif // WITH_DEV_AUTOMATION_TESTS 151 | -------------------------------------------------------------------------------- /Source/IGRanges/Private/Tests/IGRanges_Where.spec.cpp: -------------------------------------------------------------------------------- 1 | // Copyright Ian Good 2 | 3 | #include "IGRanges/Where.h" 4 | #include "IGRangesInternal.h" 5 | #include "Misc/AutomationTest.h" 6 | #include 7 | 8 | #if WITH_DEV_AUTOMATION_TESTS 9 | 10 | BEGIN_DEFINE_SPEC(FIGRangesWhereSpec, "IG.Ranges.Where", EAutomationTestFlags::ApplicationContextMask | EAutomationTestFlags::ProductFilter) 11 | 12 | constexpr struct FMyNumber 13 | { 14 | constexpr FMyNumber(int32 InN) 15 | : N(InN) 16 | , bIsEven(InN % 2 == 0) 17 | { 18 | } 19 | 20 | const int32 N = 0; 21 | 22 | const bool bIsEven = true; 23 | 24 | constexpr bool IsEven() const { return N % 2 == 0; } 25 | }; 26 | 27 | static bool StaticIsEven(const FMyNumber& X) 28 | { 29 | return X.N % 2 == 0; 30 | }; 31 | 32 | template 33 | bool TestCallable(PredicateT&& Predicate) 34 | { 35 | using namespace IG::Ranges; 36 | return TestCallableImpl(std::forward(Predicate), Where(std::forward(Predicate))); 37 | } 38 | 39 | template 40 | bool TestCallableNot(PredicateT&& Predicate) 41 | { 42 | using namespace IG::Ranges; 43 | return TestCallableImpl(std::not_fn(std::forward(Predicate)), WhereNot(std::forward(Predicate))); 44 | } 45 | 46 | /** 47 | * Tests that a `Where` or `WhereNot` filter behaves the same as using a predicate directly. 48 | */ 49 | template 50 | bool TestCallableImpl(PredicateT&& Predicate, WhereFilterT&& WhereFilter) 51 | { 52 | static constexpr FMyNumber SomeNumbers[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; 53 | static constexpr int32 NumSomeNumbers = UE_ARRAY_COUNT(SomeNumbers); 54 | 55 | TArray ExpectedValues; 56 | for (auto&& X : SomeNumbers) 57 | { 58 | if (std::invoke(Predicate, X)) 59 | { 60 | ExpectedValues.Emplace(X.N); 61 | } 62 | } 63 | 64 | int32 i = -1; 65 | for (auto&& X : SomeNumbers | WhereFilter) 66 | { 67 | ++i; 68 | UTEST_TRUE_EXPR(ExpectedValues.IsValidIndex(i)); 69 | UTEST_EQUAL("filtered element", X.N, ExpectedValues[i]); 70 | } 71 | 72 | UTEST_EQUAL("count", i + 1, ExpectedValues.Num()); 73 | 74 | return true; 75 | } 76 | 77 | END_DEFINE_SPEC(FIGRangesWhereSpec) 78 | 79 | void FIGRangesWhereSpec::Define() 80 | { 81 | // `Where` accepts function pointers as a predicate. 82 | It("function_pointer", [this]() { 83 | TestCallable(&StaticIsEven); 84 | }); 85 | 86 | // `Where` accepts function objects as a predicate. 87 | It("function_object", [this]() { 88 | const auto IsEven = [](auto&& x) { 89 | return x.N % 2 == 0; 90 | }; 91 | 92 | TestCallable(IsEven); 93 | }); 94 | 95 | // `Where` accepts member field pointers as a predicate. 96 | It("member_field_pointer", [this]() { 97 | TestCallable(&FMyNumber::bIsEven); 98 | }); 99 | 100 | // `Where` accepts member function pointers as a predicate. 101 | It("member_function_pointer", [this]() { 102 | TestCallable(&FMyNumber::IsEven); 103 | }); 104 | 105 | // `WhereNot` accepts function pointers as a predicate. 106 | It("function_pointer (not)", [this]() { 107 | TestCallableNot(&StaticIsEven); 108 | }); 109 | 110 | // `WhereNot` accepts function objects as a predicate. 111 | It("function_object (not)", [this]() { 112 | const auto IsEven = [](auto&& x) { 113 | return x.N % 2 == 0; 114 | }; 115 | 116 | TestCallableNot(IsEven); 117 | }); 118 | 119 | // `WhereNot` accepts member field pointers as a predicate. 120 | It("member_field_pointer (not)", [this]() { 121 | TestCallableNot(&FMyNumber::bIsEven); 122 | }); 123 | 124 | // `WhereNot` accepts member function pointers as a predicate. 125 | It("member_function_pointer (not)", [this]() { 126 | TestCallableNot(&FMyNumber::IsEven); 127 | }); 128 | 129 | // `SafeWhere` expects pointer-like elements & performs a null-check before the invoking the predicate. 130 | It("pointer safe", [this]() { 131 | using namespace IG::Ranges; 132 | 133 | int32 A = 1; 134 | int32 B = 2; 135 | int32 C = 3; 136 | int32 D = 4; 137 | int32* SomePointers[] = {nullptr, &A, &B, &C, &D, nullptr, nullptr, &D, &A, &D}; 138 | 139 | const auto IsEven = [](const int32* x) { 140 | return *x % 2 == 0; 141 | }; 142 | 143 | TArray ExpectedValues; 144 | for (const int32* X : SomePointers) 145 | { 146 | if (X != nullptr && IsEven(X)) 147 | { 148 | ExpectedValues.Emplace(X); 149 | } 150 | } 151 | 152 | int32 i = -1; 153 | for (const int32* X : SomePointers | SafeWhere(IsEven)) 154 | { 155 | ++i; 156 | UTEST_TRUE_EXPR(ExpectedValues.IsValidIndex(i)); 157 | UTEST_EQUAL("filtered element", X, ExpectedValues[i]); 158 | } 159 | 160 | UTEST_EQUAL("count", i + 1, ExpectedValues.Num()); 161 | 162 | return true; 163 | }); 164 | } 165 | 166 | #endif // WITH_DEV_AUTOMATION_TESTS 167 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ![Logo](Resources/Icon128.png) for Unreal 2 | 3 | [![MIT License](https://img.shields.io/badge/license-MIT-green.svg?style=flat-square)](/LICENSE) 4 | 5 | **IGRanges** is an Unreal Engine 5 plugin that leverages the [Ranges library (C++20)](https://en.cppreference.com/w/cpp/ranges) to provide [LINQ (C#)](https://learn.microsoft.com/en-us/dotnet/csharp/linq/) style code patterns. 6 | 7 | Unreal's container types (e.g. `TArray`) do not support Ranges out of the box (yet?), so this plugin first adds the necessary customization point objects to make them compatible.\ 8 | Beyond that, a handful of common mapping & filtering operations have been implemented in ways that are familiar to programmers acquainted with UE & LINQ. 9 | 10 | ---- 11 | 12 | ## C++20 Ranges, But Think LINQ 13 | 14 | A quick primer for Ranges from [cppreference.com](https://en.cppreference.com/w/cpp/ranges): 15 | > The ranges library is an extension and generalization of the algorithms and iterator libraries that makes them more powerful by making them composable and less error-prone.\ 16 | The library creates and manipulates range views, lightweight objects that indirectly represent iterable sequences (ranges). 17 | 18 | ### 👩‍💻 Example A: 19 | The following example "pipes" an array of integers through views to generate a sequence that is a modified subset of the original values. 20 | 21 | #### 👩‍💻 Example A.1: 22 | ```cpp 23 | int myInts[] = { 0, 1, 2, 3, 4, 5 }; 24 | auto even = [](int i) { return i % 2 == 0; }; 25 | auto square = [](int i) { return i * i; }; 26 | // the "pipe" syntax of composing the views: 27 | for (int i : myInts | std::views::filter(even) | std::views::transform(square)) 28 | std::cout << i << ' '; 29 | // Output: 0 4 16 30 | ``` 31 | 32 | This example is very *C++*-looking. It's not very *UE*-looking & it doesn't read quite as clearly as LINQ.\ 33 | If we were to rewrite it in a UE style with IGRanges, then it might look something like this: 34 | 35 | #### 👩‍💻 Example A.2: 36 | ```cpp 37 | TArray MyInts = { 0, 1, 2, 3, 4, 5 }; 38 | auto IsEven = [](int i) { return i % 2 == 0; }; 39 | auto Square = [](int i) { return i * i; }; 40 | FString Result; 41 | for (int32 i : MyInts | Where(IsEven) | Select(Square)) 42 | { 43 | Result.AppendInt(i); 44 | Result += TEXT(", "); 45 | } 46 | UE_LOG(LogTemp, Log, TEXT("Result{%s}"), *Result); 47 | // Output: Result{0, 4, 16,} 48 | ``` 49 | 50 | Or, depending on style preferences, it might look something like this: 51 | 52 | #### 👩‍💻 Example A.3: 53 | ```cpp 54 | TArray MyInts = { 0, 1, 2, 3, 4, 5 }; 55 | auto Sqevens = MyInts 56 | | Where([](int i) { return i % 2 == 0; }) 57 | | Select([](int i) { return i * i; }); 58 | FString Result; 59 | for (int32 i : Sqevens) 60 | { 61 | Result.AppendInt(i); 62 | Result += TEXT(", "); 63 | } 64 | ``` 65 | 66 | ### 👩‍💻 Example B: 67 | 68 | A better demonstration of IGRanges is shown in the following example.\ 69 | The declarative syntax can make it easier to understand the intent of the code.\ 70 | In this case, we have a collection of pointers to Actors (some of which might be null) & we want to get the World from one of them. 71 | 72 | 73 | 74 | 75 | 92 | 109 | 110 |
ImperativeDeclarative
76 | 77 | ```cpp 78 | TArray MaybeActors = ...; 79 | UWorld* World = nullptr; 80 | for (const AActor* Actor : MaybeActors) 81 | { 82 | if (Actor != nullptr) 83 | { 84 | World = Actor->GetWorld(); 85 | break; 86 | } 87 | } 88 | if (World != nullptr) ... 89 | ``` 90 | 91 | 93 | 94 | ```cpp 95 | TArray MaybeActors = ...; 96 | UWorld* World = MaybeActors 97 | | NonNull() 98 | | Select(&AActor::GetWorld) 99 | | FirstOrDefault(); 100 | if (World != nullptr) ... 101 | 102 | 103 | 104 | 105 | 106 | ``` 107 | 108 |
111 | 112 | ### 👩‍💻 Example C: 113 | 114 | Filtering & collecting elements also becomes very easy with IGRanges. 115 | 116 | 117 | 118 | 119 | 134 | 149 | 150 |
ImperativeDeclarative
120 | 121 | ```cpp 122 | TArray SomeObjects = ...; 123 | TArray Foos; 124 | for (UObject* Obj : SomeObjects) 125 | { 126 | if (UFoo* Foo = Cast(Obj)) 127 | { 128 | Foos.Add(Foo); 129 | } 130 | } 131 | ``` 132 | 133 | 135 | 136 | ```cpp 137 | TArray SomeObjects = ...; 138 | TArray Foos = SomeObjects | OfType() | ToArray(); 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | ``` 147 | 148 |
151 | 152 | ---- 153 | 154 | ### ✨ Features 155 | 156 | - `Where`, `WhereNot`, `SafeWhere`, `SafeWhereNot` 157 | - `NonNull`, `NonNullRef` 158 | - `Select`, `SelectNonNull` 159 | - `Cast`, `CastExact`, `CastChecked`, `CastCheckedRef` 160 | - `OfType`, `OfTypeRef`, `OfTypeExact`, `OfTypeExactRef`, `OfType`, `OfTypeRef` 161 | - `FirstOrDefault` 162 | - `Count` 163 | - `Sum` 164 | - `Accumulate` 165 | - `ToArray` 166 | - `ToSet` 167 | - `All`, `Any`, `None` 168 | - `Selectors::CDO` 169 | - `Filters::IsChildOf`, `Filters::IsChildOf` 170 | 171 | ---- 172 | 173 | ### 🔗 Related Links 174 | 175 | - [Ranges library (C++20)](https://en.cppreference.com/w/cpp/ranges) 176 | - [\ | Microsoft Learn](https://learn.microsoft.com/en-us/cpp/standard-library/ranges) 177 | - [Language Integrated Query (LINQ) - C# | Microsoft Learn](https://learn.microsoft.com/en-us/dotnet/csharp/linq/) 178 | -------------------------------------------------------------------------------- /Source/IGRanges/Private/Tests/IGRanges_Benchmarks.spec.cpp: -------------------------------------------------------------------------------- 1 | // Copyright Ian Good 2 | 3 | #include "IGRanges.h" 4 | #include "IGRangesInternal.h" 5 | #include "Misc/AutomationTest.h" 6 | #include "Tests/Benchmark.h" 7 | #include "UObject/MetaData.h" 8 | #include "UObject/Package.h" 9 | #include 10 | 11 | #if WITH_DEV_AUTOMATION_TESTS 12 | 13 | DEFINE_SPEC(FIGRangesBenchmarksSpec, "IG.Ranges.Benchmarks", EAutomationTestFlags::ApplicationContextMask | EAutomationTestFlags::ProductFilter); 14 | 15 | static TArray MakeObjectsArray() 16 | { 17 | const UObject* ObjectCDO = GetDefault(); 18 | const UObject* ClassCDO = GetDefault(); 19 | const UObject* PackageCDO = GetDefault(); 20 | const UObject* MetaDataCDO = GetDefault(); 21 | 22 | const UObject* SomeObjects[] = { 23 | ObjectCDO, 24 | ClassCDO, 25 | PackageCDO, 26 | MetaDataCDO, 27 | nullptr, 28 | ObjectCDO->GetClass(), 29 | ClassCDO->GetClass(), 30 | PackageCDO->GetClass(), 31 | MetaDataCDO->GetClass(), 32 | nullptr, 33 | ObjectCDO->GetPackage(), 34 | ClassCDO->GetPackage(), 35 | PackageCDO->GetPackage(), 36 | MetaDataCDO->GetPackage(), 37 | nullptr, 38 | }; 39 | 40 | constexpr int32 NumCopies = 500'000; 41 | 42 | TArray SomeObjectsManyTimes; 43 | SomeObjectsManyTimes.Reserve(UE_ARRAY_COUNT(SomeObjects) * NumCopies); 44 | 45 | for (int32 i = 0; i < NumCopies; ++i) 46 | { 47 | SomeObjectsManyTimes.Append(SomeObjects); 48 | } 49 | 50 | return SomeObjectsManyTimes; 51 | } 52 | 53 | void FIGRangesBenchmarksSpec::Define() 54 | { 55 | It("complex_chain", [this]() { 56 | const TArray MyObjects = MakeObjectsArray(); 57 | 58 | // Discard every 3rd thing. 59 | int32 Skip3sValue = 0; 60 | const auto DiscardMe = [&Skip3sValue](auto&&) { 61 | ++Skip3sValue; 62 | return Skip3sValue % 3 == 0; 63 | }; 64 | 65 | // Later, keep every other thing. 66 | bool bFlipFlop = false; 67 | const auto FlipFlop = [&bFlipFlop](auto&&) { 68 | bFlipFlop = !bFlipFlop; 69 | return bFlipFlop; 70 | }; 71 | 72 | const auto BaselineVersion = [&]() { 73 | Skip3sValue = 0; 74 | bFlipFlop = false; 75 | int32 Results = 0; 76 | TArray Names; 77 | for (const UObject* Obj : MyObjects) 78 | { 79 | if (DiscardMe(Obj)) 80 | { 81 | continue; 82 | } 83 | 84 | if (const UMetaData* MetaData = Cast(Obj)) 85 | { 86 | if (FlipFlop(MetaData)) 87 | { 88 | const FName Name = MetaData->GetFName(); 89 | FString NameStr = Name.ToString(); 90 | NameStr.AppendInt(++Results); 91 | Names.Emplace(MoveTemp(NameStr)); 92 | } 93 | } 94 | } 95 | 96 | return Names; 97 | }; 98 | 99 | const auto IGRangesVersion = [&]() { 100 | Skip3sValue = 0; 101 | bFlipFlop = false; 102 | int32 Results = 0; 103 | return MyObjects 104 | | WhereNot(DiscardMe) 105 | | OfType() 106 | | Where(FlipFlop) 107 | | Select(&UMetaData::GetFName) 108 | | Select([&Results](auto&& Name) { 109 | FString NameStr = Name.ToString(); 110 | NameStr.AppendInt(++Results); 111 | return NameStr; 112 | }) 113 | | ToArray(); 114 | }; 115 | 116 | // Sanity check that these versions produce the same results. 117 | { 118 | const TArray ExpectedNames = BaselineVersion(); 119 | const TArray ActualNames = IGRangesVersion(); 120 | if (!TestEqual("version results", ActualNames, ExpectedNames)) 121 | { 122 | return; 123 | } 124 | 125 | UE_LOG(LogIGRangesTests, Log, TEXT("%d elements were filtered & transformed into %d elements."), MyObjects.Num(), ActualNames.Num()); 126 | } 127 | 128 | constexpr int32 NumRuns = 7; 129 | UE_BENCHMARK(NumRuns, BaselineVersion); 130 | UE_BENCHMARK(NumRuns, IGRangesVersion); 131 | }); 132 | 133 | It("accumulate", [this]() { 134 | const TArray MyObjects = MakeObjectsArray(); 135 | 136 | const auto BaselineVersion = [&]() { 137 | uint64 Result = 0; 138 | for (const UObject* Elem : MyObjects) 139 | { 140 | Result += (Elem != nullptr) ? Elem->GetName().Len() : 0; 141 | } 142 | 143 | return Result; 144 | }; 145 | 146 | const auto Fold = [](uint64 Acc, const UObject* Elem) { 147 | return Acc + ((Elem != nullptr) ? Elem->GetName().Len() : 0); 148 | }; 149 | 150 | const auto StdVersion = [&]() { 151 | return std::accumulate(MyObjects.begin(), MyObjects.end(), uint64{}, Fold); 152 | }; 153 | 154 | const auto IGRangesVersion = [&]() { 155 | return MyObjects | Accumulate(uint64{}, Fold); 156 | }; 157 | 158 | // Sanity check that these versions produce the same results. 159 | { 160 | const int32 Expected = BaselineVersion(); 161 | const int32 StdActual = StdVersion(); 162 | const int32 IgrActual = IGRangesVersion(); 163 | const bool bSuccess = 164 | TestEqual("std version results", StdActual, Expected) 165 | && TestEqual("igr version results", IgrActual, Expected); 166 | if (!bSuccess) 167 | { 168 | return; 169 | } 170 | 171 | UE_LOG(LogIGRangesTests, Log, TEXT("%d elements were accumulated."), MyObjects.Num()); 172 | } 173 | 174 | constexpr int32 NumRuns = 7; 175 | UE_BENCHMARK(NumRuns, BaselineVersion); 176 | UE_BENCHMARK(NumRuns, StdVersion); 177 | UE_BENCHMARK(NumRuns, IGRangesVersion); 178 | }); 179 | 180 | It("sum", [this]() { 181 | const TArray MyObjects = MakeObjectsArray(); 182 | 183 | const auto BaselineVersion = [&]() { 184 | int32 Result = 0; 185 | for (const UObject* Elem : MyObjects) 186 | { 187 | Result += (Elem != nullptr) ? Elem->GetName().Len() : 0; 188 | } 189 | 190 | return Result; 191 | }; 192 | 193 | const auto Fold = [](int32 Acc, const UObject* Elem) { 194 | return Acc + ((Elem != nullptr) ? Elem->GetName().Len() : 0); 195 | }; 196 | 197 | const auto StdVersion = [&]() { 198 | return std::accumulate(MyObjects.begin(), MyObjects.end(), int32{}, Fold); 199 | }; 200 | 201 | const auto SumSelector = [](const UObject* Elem) { 202 | return (Elem != nullptr) ? Elem->GetName().Len() : 0; 203 | }; 204 | 205 | const auto IGRangesVersion = [&]() { 206 | return MyObjects | Sum(SumSelector); 207 | }; 208 | 209 | // Sanity check that these versions produce the same results. 210 | { 211 | const int32 Expected = BaselineVersion(); 212 | const int32 StdActual = StdVersion(); 213 | const int32 IgrActual = IGRangesVersion(); 214 | const bool bSuccess = 215 | TestEqual("std version results", StdActual, Expected) 216 | && TestEqual("igr version results", IgrActual, Expected); 217 | if (!bSuccess) 218 | { 219 | return; 220 | } 221 | 222 | UE_LOG(LogIGRangesTests, Log, TEXT("%d elements were summed."), MyObjects.Num()); 223 | } 224 | 225 | constexpr int32 NumRuns = 7; 226 | UE_BENCHMARK(NumRuns, BaselineVersion); 227 | UE_BENCHMARK(NumRuns, StdVersion); 228 | UE_BENCHMARK(NumRuns, IGRangesVersion); 229 | }); 230 | } 231 | 232 | #endif // WITH_DEV_AUTOMATION_TESTS 233 | --------------------------------------------------------------------------------