From 9eb0e73f461f168fb036c304de13d7501edf6d12 Mon Sep 17 00:00:00 2001 From: Regina Pfeifer Date: Fri, 15 Feb 2019 21:34:44 +0100 Subject: cmRange: Move to dedicated header file --- Source/CTest/cmCTestMultiProcessHandler.cxx | 2 +- Source/CTest/cmCTestP4.cxx | 2 +- Source/cmAddDependenciesCommand.cxx | 2 +- Source/cmAddSubDirectoryCommand.cxx | 2 +- Source/cmAlgorithms.h | 67 +------------------------ Source/cmComputeLinkDepends.cxx | 1 + Source/cmComputeTargetDepends.cxx | 2 +- Source/cmExtraCodeBlocksGenerator.cxx | 1 + Source/cmFLTKWrapUICommand.cxx | 2 +- Source/cmFileCommand.cxx | 1 + Source/cmFindBase.cxx | 1 + Source/cmFindPackageCommand.cxx | 1 + Source/cmForEachCommand.cxx | 1 + Source/cmFunctionCommand.cxx | 1 + Source/cmGeneratorTarget.cxx | 1 + Source/cmGlobalGenerator.cxx | 1 + Source/cmGlobalNinjaGenerator.cxx | 1 + Source/cmInstallCommandArguments.cxx | 2 +- Source/cmInstallFilesCommand.cxx | 2 +- Source/cmListCommand.cxx | 1 + Source/cmLocalUnixMakefileGenerator3.cxx | 1 + Source/cmMacroCommand.cxx | 1 + Source/cmMakefile.cxx | 1 + Source/cmMakefileTargetGenerator.cxx | 1 + Source/cmMessageCommand.cxx | 1 + Source/cmParseArgumentsCommand.cxx | 1 + Source/cmQTWrapCPPCommand.cxx | 2 +- Source/cmQTWrapUICommand.cxx | 2 +- Source/cmRST.cxx | 1 + Source/cmRange.h | 77 +++++++++++++++++++++++++++++ Source/cmSetCommand.cxx | 1 + Source/cmSetPropertyCommand.cxx | 2 +- Source/cmStateDirectory.cxx | 1 + Source/cmStringCommand.cxx | 1 + Source/cmSystemTools.cxx | 1 + Source/cmTarget.cxx | 1 + Source/cmTestGenerator.cxx | 2 +- Source/cmTryRunCommand.cxx | 2 +- Source/cmcmd.cxx | 1 + 39 files changed, 116 insertions(+), 78 deletions(-) create mode 100644 Source/cmRange.h diff --git a/Source/CTest/cmCTestMultiProcessHandler.cxx b/Source/CTest/cmCTestMultiProcessHandler.cxx index 63c2fcc..c66c87a 100644 --- a/Source/CTest/cmCTestMultiProcessHandler.cxx +++ b/Source/CTest/cmCTestMultiProcessHandler.cxx @@ -3,12 +3,12 @@ #include "cmCTestMultiProcessHandler.h" #include "cmAffinity.h" -#include "cmAlgorithms.h" #include "cmCTest.h" #include "cmCTestRunTest.h" #include "cmCTestTestHandler.h" #include "cmDuration.h" #include "cmListFileCache.h" +#include "cmRange.h" #include "cmSystemTools.h" #include "cmWorkingDirectory.h" diff --git a/Source/CTest/cmCTestP4.cxx b/Source/CTest/cmCTestP4.cxx index ac4753c..0d6670a 100644 --- a/Source/CTest/cmCTestP4.cxx +++ b/Source/CTest/cmCTestP4.cxx @@ -2,10 +2,10 @@ file Copyright.txt or https://cmake.org/licensing for details. */ #include "cmCTestP4.h" -#include "cmAlgorithms.h" #include "cmCTest.h" #include "cmCTestVC.h" #include "cmProcessTools.h" +#include "cmRange.h" #include "cmSystemTools.h" #include "cmsys/RegularExpression.hxx" diff --git a/Source/cmAddDependenciesCommand.cxx b/Source/cmAddDependenciesCommand.cxx index 1023808..4956a47 100644 --- a/Source/cmAddDependenciesCommand.cxx +++ b/Source/cmAddDependenciesCommand.cxx @@ -4,9 +4,9 @@ #include -#include "cmAlgorithms.h" #include "cmMakefile.h" #include "cmMessageType.h" +#include "cmRange.h" #include "cmTarget.h" class cmExecutionStatus; diff --git a/Source/cmAddSubDirectoryCommand.cxx b/Source/cmAddSubDirectoryCommand.cxx index c47092a..7947188 100644 --- a/Source/cmAddSubDirectoryCommand.cxx +++ b/Source/cmAddSubDirectoryCommand.cxx @@ -5,8 +5,8 @@ #include #include -#include "cmAlgorithms.h" #include "cmMakefile.h" +#include "cmRange.h" #include "cmSystemTools.h" class cmExecutionStatus; diff --git a/Source/cmAlgorithms.h b/Source/cmAlgorithms.h index 1c4160b..2ff1ed0 100644 --- a/Source/cmAlgorithms.h +++ b/Source/cmAlgorithms.h @@ -5,6 +5,8 @@ #include "cmConfigure.h" // IWYU pragma: keep +#include "cmRange.h" + #include "cm_kwiml.h" #include #include @@ -156,69 +158,12 @@ private: }; } -template -struct cmRange -{ - typedef const_iterator_ const_iterator; - typedef typename std::iterator_traits::value_type value_type; - typedef typename std::iterator_traits::difference_type - difference_type; - cmRange(const_iterator begin_, const_iterator end_) - : Begin(begin_) - , End(end_) - { - } - const_iterator begin() const { return Begin; } - const_iterator end() const { return End; } - bool empty() const { return std::distance(Begin, End) == 0; } - difference_type size() const { return std::distance(Begin, End); } - - cmRange& advance(KWIML_INT_intptr_t amount) & - { - std::advance(this->Begin, amount); - return *this; - } - cmRange advance(KWIML_INT_intptr_t amount) && - { - std::advance(this->Begin, amount); - return std::move(*this); - } - - cmRange& retreat(KWIML_INT_intptr_t amount) & - { - std::advance(End, -amount); - return *this; - } - - cmRange retreat(KWIML_INT_intptr_t amount) && - { - std::advance(End, -amount); - return std::move(*this); - } - -private: - const_iterator Begin; - const_iterator End; -}; - typedef cmRange::const_iterator> cmStringRange; class cmListFileBacktrace; typedef cmRange::const_iterator> cmBacktraceRange; -template -cmRange cmMakeRange(Iter1 begin, Iter2 end) -{ - return cmRange(begin, end); -} - -template -cmRange cmMakeRange(Range const& range) -{ - return cmRange(range.begin(), range.end()); -} - template void cmDeleteAll(Range const& r) { @@ -334,14 +279,6 @@ typename Range::const_iterator cmFindNot(Range const& r, T const& t) return std::find_if(r.begin(), r.end(), [&t](T const& i) { return i != t; }); } -template -cmRange cmReverseRange( - Range const& range) -{ - return cmRange(range.rbegin(), - range.rend()); -} - template std::reverse_iterator cmMakeReverseIterator(Iter it) { diff --git a/Source/cmComputeLinkDepends.cxx b/Source/cmComputeLinkDepends.cxx index 5ed6c29..186deb6 100644 --- a/Source/cmComputeLinkDepends.cxx +++ b/Source/cmComputeLinkDepends.cxx @@ -9,6 +9,7 @@ #include "cmListFileCache.h" #include "cmLocalGenerator.h" #include "cmMakefile.h" +#include "cmRange.h" #include "cmStateTypes.h" #include "cmSystemTools.h" #include "cmTarget.h" diff --git a/Source/cmComputeTargetDepends.cxx b/Source/cmComputeTargetDepends.cxx index 70d1675..01d4c07 100644 --- a/Source/cmComputeTargetDepends.cxx +++ b/Source/cmComputeTargetDepends.cxx @@ -2,7 +2,6 @@ file Copyright.txt or https://cmake.org/licensing for details. */ #include "cmComputeTargetDepends.h" -#include "cmAlgorithms.h" #include "cmComputeComponentGraph.h" #include "cmGeneratorTarget.h" #include "cmGlobalGenerator.h" @@ -12,6 +11,7 @@ #include "cmMakefile.h" #include "cmMessageType.h" #include "cmPolicies.h" +#include "cmRange.h" #include "cmSourceFile.h" #include "cmState.h" #include "cmStateTypes.h" diff --git a/Source/cmExtraCodeBlocksGenerator.cxx b/Source/cmExtraCodeBlocksGenerator.cxx index 9e90547..93ff8f4 100644 --- a/Source/cmExtraCodeBlocksGenerator.cxx +++ b/Source/cmExtraCodeBlocksGenerator.cxx @@ -13,6 +13,7 @@ #include "cmGlobalGenerator.h" #include "cmLocalGenerator.h" #include "cmMakefile.h" +#include "cmRange.h" #include "cmSourceFile.h" #include "cmStateTypes.h" #include "cmSystemTools.h" diff --git a/Source/cmFLTKWrapUICommand.cxx b/Source/cmFLTKWrapUICommand.cxx index 1b509ef..89629c7 100644 --- a/Source/cmFLTKWrapUICommand.cxx +++ b/Source/cmFLTKWrapUICommand.cxx @@ -4,9 +4,9 @@ #include -#include "cmAlgorithms.h" #include "cmCustomCommandLines.h" #include "cmMakefile.h" +#include "cmRange.h" #include "cmSourceFile.h" #include "cmSystemTools.h" diff --git a/Source/cmFileCommand.cxx b/Source/cmFileCommand.cxx index 96c9e15..59ef48d 100644 --- a/Source/cmFileCommand.cxx +++ b/Source/cmFileCommand.cxx @@ -34,6 +34,7 @@ #include "cmMakefile.h" #include "cmMessageType.h" #include "cmPolicies.h" +#include "cmRange.h" #include "cmSystemTools.h" #include "cmTimestamp.h" #include "cm_sys_stat.h" diff --git a/Source/cmFindBase.cxx b/Source/cmFindBase.cxx index f92f66e..2e5e29c 100644 --- a/Source/cmFindBase.cxx +++ b/Source/cmFindBase.cxx @@ -9,6 +9,7 @@ #include "cmAlgorithms.h" #include "cmMakefile.h" +#include "cmRange.h" #include "cmSearchPath.h" #include "cmState.h" #include "cmStateTypes.h" diff --git a/Source/cmFindPackageCommand.cxx b/Source/cmFindPackageCommand.cxx index 0d22ed8..7ebd211 100644 --- a/Source/cmFindPackageCommand.cxx +++ b/Source/cmFindPackageCommand.cxx @@ -23,6 +23,7 @@ #include "cmMakefile.h" #include "cmMessageType.h" #include "cmPolicies.h" +#include "cmRange.h" #include "cmSearchPath.h" #include "cmState.h" #include "cmStateTypes.h" diff --git a/Source/cmForEachCommand.cxx b/Source/cmForEachCommand.cxx index 39430d0..08003eb 100644 --- a/Source/cmForEachCommand.cxx +++ b/Source/cmForEachCommand.cxx @@ -11,6 +11,7 @@ #include "cmExecutionStatus.h" #include "cmMakefile.h" #include "cmMessageType.h" +#include "cmRange.h" #include "cmSystemTools.h" cmForEachFunctionBlocker::cmForEachFunctionBlocker(cmMakefile* mf) diff --git a/Source/cmFunctionCommand.cxx b/Source/cmFunctionCommand.cxx index 264a338..9d75b72 100644 --- a/Source/cmFunctionCommand.cxx +++ b/Source/cmFunctionCommand.cxx @@ -8,6 +8,7 @@ #include "cmExecutionStatus.h" #include "cmMakefile.h" #include "cmPolicies.h" +#include "cmRange.h" #include "cmState.h" // define the class for function commands diff --git a/Source/cmGeneratorTarget.cxx b/Source/cmGeneratorTarget.cxx index 6f5e9bd..c73286c 100644 --- a/Source/cmGeneratorTarget.cxx +++ b/Source/cmGeneratorTarget.cxx @@ -28,6 +28,7 @@ #include "cmMakefile.h" #include "cmMessageType.h" #include "cmPropertyMap.h" +#include "cmRange.h" #include "cmSourceFile.h" #include "cmSourceFileLocation.h" #include "cmState.h" diff --git a/Source/cmGlobalGenerator.cxx b/Source/cmGlobalGenerator.cxx index fa0ffcc..ede5713 100644 --- a/Source/cmGlobalGenerator.cxx +++ b/Source/cmGlobalGenerator.cxx @@ -35,6 +35,7 @@ #include "cmMakefile.h" #include "cmMessageType.h" #include "cmPolicies.h" +#include "cmRange.h" #include "cmSourceFile.h" #include "cmState.h" #include "cmStateDirectory.h" diff --git a/Source/cmGlobalNinjaGenerator.cxx b/Source/cmGlobalNinjaGenerator.cxx index 31bcacf..d9a9d9b 100644 --- a/Source/cmGlobalNinjaGenerator.cxx +++ b/Source/cmGlobalNinjaGenerator.cxx @@ -26,6 +26,7 @@ #include "cmMessageType.h" #include "cmNinjaLinkLineComputer.h" #include "cmOutputConverter.h" +#include "cmRange.h" #include "cmState.h" #include "cmStateDirectory.h" #include "cmStateSnapshot.h" diff --git a/Source/cmInstallCommandArguments.cxx b/Source/cmInstallCommandArguments.cxx index 647c786..c64bd8a 100644 --- a/Source/cmInstallCommandArguments.cxx +++ b/Source/cmInstallCommandArguments.cxx @@ -2,7 +2,7 @@ file Copyright.txt or https://cmake.org/licensing for details. */ #include "cmInstallCommandArguments.h" -#include "cmAlgorithms.h" +#include "cmRange.h" #include "cmSystemTools.h" #include diff --git a/Source/cmInstallFilesCommand.cxx b/Source/cmInstallFilesCommand.cxx index 1b72ce6..b068e46 100644 --- a/Source/cmInstallFilesCommand.cxx +++ b/Source/cmInstallFilesCommand.cxx @@ -2,12 +2,12 @@ file Copyright.txt or https://cmake.org/licensing for details. */ #include "cmInstallFilesCommand.h" -#include "cmAlgorithms.h" #include "cmGeneratorExpression.h" #include "cmGlobalGenerator.h" #include "cmInstallFilesGenerator.h" #include "cmInstallGenerator.h" #include "cmMakefile.h" +#include "cmRange.h" #include "cmSystemTools.h" class cmExecutionStatus; diff --git a/Source/cmListCommand.cxx b/Source/cmListCommand.cxx index 297babf..9322746 100644 --- a/Source/cmListCommand.cxx +++ b/Source/cmListCommand.cxx @@ -19,6 +19,7 @@ #include "cmMakefile.h" #include "cmMessageType.h" #include "cmPolicies.h" +#include "cmRange.h" #include "cmStringReplaceHelper.h" #include "cmSystemTools.h" diff --git a/Source/cmLocalUnixMakefileGenerator3.cxx b/Source/cmLocalUnixMakefileGenerator3.cxx index 7eb4a03..765fa30 100644 --- a/Source/cmLocalUnixMakefileGenerator3.cxx +++ b/Source/cmLocalUnixMakefileGenerator3.cxx @@ -22,6 +22,7 @@ #include "cmMakefile.h" #include "cmMakefileTargetGenerator.h" #include "cmOutputConverter.h" +#include "cmRange.h" #include "cmRulePlaceholderExpander.h" #include "cmSourceFile.h" #include "cmState.h" diff --git a/Source/cmMacroCommand.cxx b/Source/cmMacroCommand.cxx index 7279d5f..6565f02 100644 --- a/Source/cmMacroCommand.cxx +++ b/Source/cmMacroCommand.cxx @@ -10,6 +10,7 @@ #include "cmExecutionStatus.h" #include "cmMakefile.h" #include "cmPolicies.h" +#include "cmRange.h" #include "cmState.h" #include "cmSystemTools.h" diff --git a/Source/cmMakefile.cxx b/Source/cmMakefile.cxx index 560181f..2e4d106 100644 --- a/Source/cmMakefile.cxx +++ b/Source/cmMakefile.cxx @@ -31,6 +31,7 @@ #include "cmInstallSubdirectoryGenerator.h" #include "cmListFileCache.h" #include "cmMessageType.h" +#include "cmRange.h" #include "cmSourceFile.h" #include "cmSourceFileLocation.h" #include "cmState.h" diff --git a/Source/cmMakefileTargetGenerator.cxx b/Source/cmMakefileTargetGenerator.cxx index cb595fd..340e405 100644 --- a/Source/cmMakefileTargetGenerator.cxx +++ b/Source/cmMakefileTargetGenerator.cxx @@ -21,6 +21,7 @@ #include "cmMakefileLibraryTargetGenerator.h" #include "cmMakefileUtilityTargetGenerator.h" #include "cmOutputConverter.h" +#include "cmRange.h" #include "cmRulePlaceholderExpander.h" #include "cmSourceFile.h" #include "cmState.h" diff --git a/Source/cmMessageCommand.cxx b/Source/cmMessageCommand.cxx index 1a21ae4..2724030 100644 --- a/Source/cmMessageCommand.cxx +++ b/Source/cmMessageCommand.cxx @@ -6,6 +6,7 @@ #include "cmMakefile.h" #include "cmMessageType.h" #include "cmMessenger.h" +#include "cmRange.h" #include "cmSystemTools.h" class cmExecutionStatus; diff --git a/Source/cmParseArgumentsCommand.cxx b/Source/cmParseArgumentsCommand.cxx index 6231aab..ab8d103 100644 --- a/Source/cmParseArgumentsCommand.cxx +++ b/Source/cmParseArgumentsCommand.cxx @@ -10,6 +10,7 @@ #include "cmAlgorithms.h" #include "cmMakefile.h" #include "cmMessageType.h" +#include "cmRange.h" #include "cmSystemTools.h" class cmExecutionStatus; diff --git a/Source/cmQTWrapCPPCommand.cxx b/Source/cmQTWrapCPPCommand.cxx index 8b42119..9a764c6 100644 --- a/Source/cmQTWrapCPPCommand.cxx +++ b/Source/cmQTWrapCPPCommand.cxx @@ -2,9 +2,9 @@ file Copyright.txt or https://cmake.org/licensing for details. */ #include "cmQTWrapCPPCommand.h" -#include "cmAlgorithms.h" #include "cmCustomCommandLines.h" #include "cmMakefile.h" +#include "cmRange.h" #include "cmSourceFile.h" #include "cmSystemTools.h" diff --git a/Source/cmQTWrapUICommand.cxx b/Source/cmQTWrapUICommand.cxx index 6a620c1..2223e2d 100644 --- a/Source/cmQTWrapUICommand.cxx +++ b/Source/cmQTWrapUICommand.cxx @@ -2,9 +2,9 @@ file Copyright.txt or https://cmake.org/licensing for details. */ #include "cmQTWrapUICommand.h" -#include "cmAlgorithms.h" #include "cmCustomCommandLines.h" #include "cmMakefile.h" +#include "cmRange.h" #include "cmSourceFile.h" #include "cmSystemTools.h" diff --git a/Source/cmRST.cxx b/Source/cmRST.cxx index 55204d7..5917e27 100644 --- a/Source/cmRST.cxx +++ b/Source/cmRST.cxx @@ -3,6 +3,7 @@ #include "cmRST.h" #include "cmAlgorithms.h" +#include "cmRange.h" #include "cmSystemTools.h" #include "cmVersion.h" diff --git a/Source/cmRange.h b/Source/cmRange.h new file mode 100644 index 0000000..5a2f699 --- /dev/null +++ b/Source/cmRange.h @@ -0,0 +1,77 @@ +/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying + file Copyright.txt or https://cmake.org/licensing for details. */ +#ifndef cmRange_h +#define cmRange_h + +#include "cmConfigure.h" // IWYU pragma: keep + +#include "cm_kwiml.h" +#include +#include + +template +struct cmRange +{ + typedef const_iterator_ const_iterator; + typedef typename std::iterator_traits::value_type value_type; + typedef typename std::iterator_traits::difference_type + difference_type; + cmRange(const_iterator begin_, const_iterator end_) + : Begin(begin_) + , End(end_) + { + } + const_iterator begin() const { return Begin; } + const_iterator end() const { return End; } + bool empty() const { return std::distance(Begin, End) == 0; } + difference_type size() const { return std::distance(Begin, End); } + + cmRange& advance(KWIML_INT_intptr_t amount) & + { + std::advance(this->Begin, amount); + return *this; + } + cmRange advance(KWIML_INT_intptr_t amount) && + { + std::advance(this->Begin, amount); + return std::move(*this); + } + + cmRange& retreat(KWIML_INT_intptr_t amount) & + { + std::advance(End, -amount); + return *this; + } + + cmRange retreat(KWIML_INT_intptr_t amount) && + { + std::advance(End, -amount); + return std::move(*this); + } + +private: + const_iterator Begin; + const_iterator End; +}; + +template +cmRange cmMakeRange(Iter1 begin, Iter2 end) +{ + return cmRange(begin, end); +} + +template +cmRange cmMakeRange(Range const& range) +{ + return cmRange(range.begin(), range.end()); +} + +template +cmRange cmReverseRange( + Range const& range) +{ + return cmRange(range.rbegin(), + range.rend()); +} + +#endif diff --git a/Source/cmSetCommand.cxx b/Source/cmSetCommand.cxx index 6bd071c..41555e8 100644 --- a/Source/cmSetCommand.cxx +++ b/Source/cmSetCommand.cxx @@ -5,6 +5,7 @@ #include "cmAlgorithms.h" #include "cmMakefile.h" #include "cmMessageType.h" +#include "cmRange.h" #include "cmState.h" #include "cmStateTypes.h" #include "cmSystemTools.h" diff --git a/Source/cmSetPropertyCommand.cxx b/Source/cmSetPropertyCommand.cxx index 2a0ccb4..0d8f1cc 100644 --- a/Source/cmSetPropertyCommand.cxx +++ b/Source/cmSetPropertyCommand.cxx @@ -4,11 +4,11 @@ #include -#include "cmAlgorithms.h" #include "cmGlobalGenerator.h" #include "cmInstalledFile.h" #include "cmMakefile.h" #include "cmProperty.h" +#include "cmRange.h" #include "cmSourceFile.h" #include "cmState.h" #include "cmSystemTools.h" diff --git a/Source/cmStateDirectory.cxx b/Source/cmStateDirectory.cxx index 15c8a61..31273cc 100644 --- a/Source/cmStateDirectory.cxx +++ b/Source/cmStateDirectory.cxx @@ -11,6 +11,7 @@ #include "cmAlgorithms.h" #include "cmProperty.h" #include "cmPropertyMap.h" +#include "cmRange.h" #include "cmState.h" #include "cmStatePrivate.h" #include "cmStateTypes.h" diff --git a/Source/cmStringCommand.cxx b/Source/cmStringCommand.cxx index 44ba82a..252d985 100644 --- a/Source/cmStringCommand.cxx +++ b/Source/cmStringCommand.cxx @@ -13,6 +13,7 @@ #include "cmCryptoHash.h" #include "cmGeneratorExpression.h" #include "cmMakefile.h" +#include "cmRange.h" #include "cmStringReplaceHelper.h" #include "cmSystemTools.h" #include "cmTimestamp.h" diff --git a/Source/cmSystemTools.cxx b/Source/cmSystemTools.cxx index f0e8077..254644a 100644 --- a/Source/cmSystemTools.cxx +++ b/Source/cmSystemTools.cxx @@ -5,6 +5,7 @@ #include "cmAlgorithms.h" #include "cmDuration.h" #include "cmProcessOutput.h" +#include "cmRange.h" #include "cm_sys_stat.h" #include "cm_uv.h" diff --git a/Source/cmTarget.cxx b/Source/cmTarget.cxx index d1e4f06..46f930a 100644 --- a/Source/cmTarget.cxx +++ b/Source/cmTarget.cxx @@ -21,6 +21,7 @@ #include "cmMessageType.h" #include "cmMessenger.h" #include "cmProperty.h" +#include "cmRange.h" #include "cmSourceFile.h" #include "cmSourceFileLocation.h" #include "cmSourceFileLocationKind.h" diff --git a/Source/cmTestGenerator.cxx b/Source/cmTestGenerator.cxx index a0e309d..571cd09 100644 --- a/Source/cmTestGenerator.cxx +++ b/Source/cmTestGenerator.cxx @@ -5,7 +5,6 @@ #include #include -#include "cmAlgorithms.h" #include "cmGeneratorExpression.h" #include "cmGeneratorTarget.h" #include "cmListFileCache.h" @@ -13,6 +12,7 @@ #include "cmOutputConverter.h" #include "cmProperty.h" #include "cmPropertyMap.h" +#include "cmRange.h" #include "cmStateTypes.h" #include "cmSystemTools.h" #include "cmTest.h" diff --git a/Source/cmTryRunCommand.cxx b/Source/cmTryRunCommand.cxx index d863339..3a2cd7f 100644 --- a/Source/cmTryRunCommand.cxx +++ b/Source/cmTryRunCommand.cxx @@ -5,10 +5,10 @@ #include "cmsys/FStream.hxx" #include -#include "cmAlgorithms.h" #include "cmDuration.h" #include "cmMakefile.h" #include "cmMessageType.h" +#include "cmRange.h" #include "cmState.h" #include "cmStateTypes.h" #include "cmSystemTools.h" diff --git a/Source/cmcmd.cxx b/Source/cmcmd.cxx index adfce37..1f238d6 100644 --- a/Source/cmcmd.cxx +++ b/Source/cmcmd.cxx @@ -9,6 +9,7 @@ #include "cmMakefile.h" #include "cmQtAutoGeneratorMocUic.h" #include "cmQtAutoGeneratorRcc.h" +#include "cmRange.h" #include "cmState.h" #include "cmStateDirectory.h" #include "cmStateSnapshot.h" -- cgit v0.12 From 17a367e77f373e2781e80e80aaa32644754e6f88 Mon Sep 17 00:00:00 2001 From: Regina Pfeifer Date: Fri, 15 Feb 2019 21:40:41 +0100 Subject: cmRange: Stylistic cleanup --- Source/cmRange.h | 64 ++++++++++++++++++++++++++++++-------------------------- 1 file changed, 34 insertions(+), 30 deletions(-) diff --git a/Source/cmRange.h b/Source/cmRange.h index 5a2f699..a84ccec 100644 --- a/Source/cmRange.h +++ b/Source/cmRange.h @@ -5,73 +5,77 @@ #include "cmConfigure.h" // IWYU pragma: keep -#include "cm_kwiml.h" #include #include -template -struct cmRange +template +class cmRange { - typedef const_iterator_ const_iterator; - typedef typename std::iterator_traits::value_type value_type; - typedef typename std::iterator_traits::difference_type - difference_type; - cmRange(const_iterator begin_, const_iterator end_) - : Begin(begin_) - , End(end_) +public: + using const_iterator = Iter; + using value_type = typename std::iterator_traits::value_type; + using difference_type = typename std::iterator_traits::difference_type; + + cmRange(Iter b, Iter e) + : Begin(std::move(b)) + , End(std::move(e)) + { + } + + Iter begin() const { return this->Begin; } + Iter end() const { return this->End; } + bool empty() const { return this->Begin == this->End; } + + difference_type size() const { + return std::distance(this->Begin, this->End); } - const_iterator begin() const { return Begin; } - const_iterator end() const { return End; } - bool empty() const { return std::distance(Begin, End) == 0; } - difference_type size() const { return std::distance(Begin, End); } - cmRange& advance(KWIML_INT_intptr_t amount) & + cmRange& advance(difference_type amount) & { std::advance(this->Begin, amount); return *this; } - cmRange advance(KWIML_INT_intptr_t amount) && + + cmRange advance(difference_type amount) && { std::advance(this->Begin, amount); return std::move(*this); } - cmRange& retreat(KWIML_INT_intptr_t amount) & + cmRange& retreat(difference_type amount) & { - std::advance(End, -amount); + std::advance(this->End, -amount); return *this; } - cmRange retreat(KWIML_INT_intptr_t amount) && + cmRange retreat(difference_type amount) && { - std::advance(End, -amount); + std::advance(this->End, -amount); return std::move(*this); } private: - const_iterator Begin; - const_iterator End; + Iter Begin; + Iter End; }; template -cmRange cmMakeRange(Iter1 begin, Iter2 end) +auto cmMakeRange(Iter1 begin, Iter2 end) -> cmRange { - return cmRange(begin, end); + return { begin, end }; } template -cmRange cmMakeRange(Range const& range) +auto cmMakeRange(Range const& range) -> cmRange { - return cmRange(range.begin(), range.end()); + return { range.begin(), range.end() }; } template -cmRange cmReverseRange( - Range const& range) +auto cmReverseRange(Range const& range) -> cmRange { - return cmRange(range.rbegin(), - range.rend()); + return { range.rbegin(), range.rend() }; } #endif -- cgit v0.12 From da4773e8b8100f27ee117005c81e81574f5a9868 Mon Sep 17 00:00:00 2001 From: Regina Pfeifer Date: Fri, 15 Feb 2019 21:45:10 +0100 Subject: cmRange: Add functions all_of, any_of, none_of --- Source/cmRange.h | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/Source/cmRange.h b/Source/cmRange.h index a84ccec..b6d161e 100644 --- a/Source/cmRange.h +++ b/Source/cmRange.h @@ -6,6 +6,7 @@ #include "cmConfigure.h" // IWYU pragma: keep #include +#include #include template @@ -55,6 +56,24 @@ public: return std::move(*this); } + template + bool all_of(UnaryPredicate p) const + { + return std::all_of(this->Begin, this->End, std::ref(p)); + } + + template + bool any_of(UnaryPredicate p) const + { + return std::any_of(this->Begin, this->End, std::ref(p)); + } + + template + bool none_of(UnaryPredicate p) const + { + return std::none_of(this->Begin, this->End, std::ref(p)); + } + private: Iter Begin; Iter End; -- cgit v0.12 From a8d51ef8b780644011da8974d99c524e93fe75f3 Mon Sep 17 00:00:00 2001 From: Regina Pfeifer Date: Fri, 15 Feb 2019 21:54:58 +0100 Subject: cmRange: Add functions filter and transform --- Source/cmRange.h | 132 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 132 insertions(+) diff --git a/Source/cmRange.h b/Source/cmRange.h index b6d161e..7ccb143 100644 --- a/Source/cmRange.h +++ b/Source/cmRange.h @@ -9,6 +9,122 @@ #include #include +namespace RangeIterators { + +template +class FilterIterator +{ +public: + using iterator_category = std::bidirectional_iterator_tag; + using value_type = typename std::iterator_traits::value_type; + using difference_type = typename std::iterator_traits::difference_type; + using pointer = typename std::iterator_traits::pointer; + using reference = typename std::iterator_traits::reference; + + FilterIterator(Iter b, Iter e, UnaryPredicate p) + : Cur(std::move(b)) + , End(std::move(e)) + , Pred(std::move(p)) + { + this->SatisfyPredicate(); + } + + FilterIterator& operator++() + { + ++this->Cur; + this->SatisfyPredicate(); + return *this; + } + + FilterIterator& operator--() + { + do { + --this->Cur; + } while (!this->Pred(*this->Cur)); + return *this; + } + + bool operator==(FilterIterator const& other) const + { + return this->Cur == other.Cur; + } + + bool operator!=(FilterIterator const& other) const + { + return !this->operator==(other); + } + + auto operator*() const -> decltype(*std::declval()) + { + return *this->Cur; + } + +private: + void SatisfyPredicate() + { + while (this->Cur != this->End && !this->Pred(*this->Cur)) { + ++this->Cur; + } + } + + Iter Cur; + Iter End; + UnaryPredicate Pred; +}; + +template +class TransformIterator +{ +public: + using iterator_category = std::bidirectional_iterator_tag; + using value_type = + typename std::remove_cv()(*std::declval()))>::type>::type; + using difference_type = typename std::iterator_traits::difference_type; + using pointer = value_type const*; + using reference = value_type const&; + + TransformIterator(Iter i, UnaryFunction f) + : Base(std::move(i)) + , Func(std::move(f)) + { + } + + TransformIterator& operator++() + { + ++this->Base; + return *this; + } + + TransformIterator& operator--() + { + --this->Base; + return *this; + } + + bool operator==(TransformIterator const& other) const + { + return this->Base == other.Base; + } + + bool operator!=(TransformIterator const& other) const + { + return !this->operator==(other); + } + + auto operator*() const + -> decltype(std::declval()(*std::declval())) + { + return this->Func(*this->Base); + } + +private: + Iter Base; + UnaryFunction Func; +}; + +} // namespace RangeIterators + template class cmRange { @@ -74,6 +190,22 @@ public: return std::none_of(this->Begin, this->End, std::ref(p)); } + template + auto filter(UnaryPredicate p) const + -> cmRange> + { + using It = RangeIterators::FilterIterator; + return { It(this->Begin, this->End, p), It(this->End, this->End, p) }; + } + + template + auto transform(UnaryFunction f) const + -> cmRange> + { + using It = RangeIterators::TransformIterator; + return { It(this->Begin, f), It(this->End, f) }; + } + private: Iter Begin; Iter End; -- cgit v0.12 From b8031308f39d66d0dca3b5c59fe62805298dc91f Mon Sep 17 00:00:00 2001 From: Regina Pfeifer Date: Tue, 19 Feb 2019 19:15:17 +0100 Subject: cmRange: Add unit tests --- Source/cmRange.h | 7 +++++++ Tests/CMakeLib/CMakeLists.txt | 1 + Tests/CMakeLib/testRange.cxx | 45 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+) create mode 100644 Tests/CMakeLib/testRange.cxx diff --git a/Source/cmRange.h b/Source/cmRange.h index 7ccb143..3be5193 100644 --- a/Source/cmRange.h +++ b/Source/cmRange.h @@ -212,6 +212,13 @@ private: }; template +bool operator==(cmRange const& left, cmRange const& right) +{ + return left.size() == right.size() && + std::equal(left.begin(), left.end(), right.begin()); +} + +template auto cmMakeRange(Iter1 begin, Iter2 end) -> cmRange { return { begin, end }; diff --git a/Tests/CMakeLib/CMakeLists.txt b/Tests/CMakeLib/CMakeLists.txt index f6a9153..91f7e25 100644 --- a/Tests/CMakeLib/CMakeLists.txt +++ b/Tests/CMakeLib/CMakeLists.txt @@ -7,6 +7,7 @@ include_directories( set(CMakeLib_TESTS testGeneratedFileStream.cxx testRST.cxx + testRange.cxx testString.cxx testSystemTools.cxx testUTF8.cxx diff --git a/Tests/CMakeLib/testRange.cxx b/Tests/CMakeLib/testRange.cxx new file mode 100644 index 0000000..5ae805f --- /dev/null +++ b/Tests/CMakeLib/testRange.cxx @@ -0,0 +1,45 @@ +/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying + file Copyright.txt or https://cmake.org/licensing for details. */ + +#include "cmRange.h" + +#include +#include +#include + +#define ASSERT_TRUE(x) \ + do { \ + if (!(x)) { \ + std::cout << "ASSERT_TRUE(" #x ") failed on line " << __LINE__ << "\n"; \ + return false; \ + } \ + } while (false) + +int testRange(int /*unused*/, char* /*unused*/ []) +{ + std::vector const testData = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; + + ASSERT_TRUE(!cmMakeRange(testData).empty()); + ASSERT_TRUE(cmMakeRange(testData).size() == 10); + + ASSERT_TRUE(!cmMakeRange(testData).advance(5).empty()); + ASSERT_TRUE(cmMakeRange(testData).advance(5).size() == 5); + + ASSERT_TRUE(cmMakeRange(testData).advance(5).retreat(5).empty()); + ASSERT_TRUE(cmMakeRange(testData).advance(5).retreat(5).size() == 0); + + ASSERT_TRUE(cmMakeRange(testData).any_of([](int n) { return n % 3 == 0; })); + ASSERT_TRUE(cmMakeRange(testData).all_of([](int n) { return n < 11; })); + ASSERT_TRUE(cmMakeRange(testData).none_of([](int n) { return n > 11; })); + + std::vector const evenData = { 2, 4, 6, 8, 10 }; + ASSERT_TRUE(cmMakeRange(testData).filter([](int n) { return n % 2 == 0; }) == + cmMakeRange(evenData)); + + std::vector const stringRange = { "1", "2", "3", "4", "5" }; + ASSERT_TRUE(cmMakeRange(testData) + .transform([](int n) { return std::to_string(n); }) + .retreat(5) == cmMakeRange(stringRange)); + + return 0; +} -- cgit v0.12