File indexing completed on 2026-09-28 09:20:58
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014 #ifndef NCollection_ForwardRange_HeaderFile
0015 #define NCollection_ForwardRange_HeaderFile
0016
0017 #include <iterator>
0018 #include <type_traits>
0019 #include <utility>
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048 namespace NCollection_ForwardRangeDetail
0049 {
0050
0051
0052 template <typename T, typename = void>
0053 struct HasValue : std::false_type
0054 {
0055 };
0056
0057 template <typename T>
0058 struct HasValue<T, std::void_t<decltype(std::declval<const T&>().Value())>> : std::true_type
0059 {
0060 };
0061
0062
0063 template <typename T, typename = void>
0064 struct HasCurrent : std::false_type
0065 {
0066 };
0067
0068 template <typename T>
0069 struct HasCurrent<T, std::void_t<decltype(std::declval<const T&>().Current())>> : std::true_type
0070 {
0071 };
0072
0073
0074 template <typename T, typename = void>
0075 struct HasCurrentId : std::false_type
0076 {
0077 };
0078
0079 template <typename T>
0080 struct HasCurrentId<T, std::void_t<decltype(std::declval<const T&>().CurrentId())>> : std::true_type
0081 {
0082 };
0083
0084
0085
0086 template <typename T, typename = void>
0087 struct AccessorTraits;
0088
0089
0090 template <typename T>
0091 struct AccessorTraits<T, std::enable_if_t<HasValue<T>::value>>
0092 {
0093 using ReturnType = decltype(std::declval<const T&>().Value());
0094
0095 static ReturnType Get(const T& theIter) { return theIter.Value(); }
0096 };
0097
0098
0099 template <typename T>
0100 struct AccessorTraits<T, std::enable_if_t<!HasValue<T>::value && HasCurrent<T>::value>>
0101 {
0102 using ReturnType = decltype(std::declval<const T&>().Current());
0103
0104 static ReturnType Get(const T& theIter) { return theIter.Current(); }
0105 };
0106
0107
0108 template <typename T>
0109 struct AccessorTraits<
0110 T,
0111 std::enable_if_t<!HasValue<T>::value && !HasCurrent<T>::value && HasCurrentId<T>::value>>
0112 {
0113 using ReturnType = decltype(std::declval<const T&>().CurrentId());
0114
0115 static ReturnType Get(const T& theIter) { return theIter.CurrentId(); }
0116 };
0117
0118
0119 template <typename ValueT>
0120 struct ArrowProxy
0121 {
0122 ValueT myValue;
0123
0124 const ValueT* operator->() const { return &myValue; }
0125 };
0126
0127 }
0128
0129
0130 struct NCollection_ForwardRangeSentinel
0131 {
0132 };
0133
0134
0135
0136
0137
0138
0139
0140 template <typename HostType>
0141 class NCollection_ForwardRangeIterator
0142 {
0143 using Accessor = NCollection_ForwardRangeDetail::AccessorTraits<HostType>;
0144 using RawReturn = typename Accessor::ReturnType;
0145
0146 public:
0147 using value_type = std::remove_cv_t<std::remove_reference_t<RawReturn>>;
0148 using difference_type = std::ptrdiff_t;
0149 using iterator_category = std::input_iterator_tag;
0150 using reference = RawReturn;
0151 using pointer = std::conditional_t<std::is_reference_v<RawReturn>,
0152 std::add_pointer_t<std::remove_reference_t<RawReturn>>,
0153 NCollection_ForwardRangeDetail::ArrowProxy<value_type>>;
0154
0155
0156 explicit NCollection_ForwardRangeIterator(HostType* theHost)
0157 : myHost(theHost)
0158 {
0159 }
0160
0161
0162 RawReturn operator*() const { return Accessor::Get(*myHost); }
0163
0164
0165 pointer operator->() const
0166 {
0167 if constexpr (std::is_reference_v<RawReturn>)
0168 {
0169 return &Accessor::Get(*myHost);
0170 }
0171 else
0172 {
0173 return NCollection_ForwardRangeDetail::ArrowProxy<value_type>{Accessor::Get(*myHost)};
0174 }
0175 }
0176
0177
0178 NCollection_ForwardRangeIterator& operator++()
0179 {
0180 myHost->Next();
0181 return *this;
0182 }
0183
0184
0185
0186 struct PostfixProxy
0187 {
0188 value_type myValue;
0189
0190 const value_type& operator*() const { return myValue; }
0191 };
0192
0193 PostfixProxy operator++(int)
0194 {
0195 PostfixProxy aProxy{Accessor::Get(*myHost)};
0196 myHost->Next();
0197 return aProxy;
0198 }
0199
0200
0201 friend bool operator==(const NCollection_ForwardRangeIterator& theLhs,
0202 NCollection_ForwardRangeSentinel)
0203 {
0204 return !theLhs.myHost->More();
0205 }
0206
0207 friend bool operator!=(const NCollection_ForwardRangeIterator& theLhs,
0208 NCollection_ForwardRangeSentinel)
0209 {
0210 return theLhs.myHost->More();
0211 }
0212
0213 friend bool operator==(NCollection_ForwardRangeSentinel,
0214 const NCollection_ForwardRangeIterator& theRhs)
0215 {
0216 return !theRhs.myHost->More();
0217 }
0218
0219 friend bool operator!=(NCollection_ForwardRangeSentinel,
0220 const NCollection_ForwardRangeIterator& theRhs)
0221 {
0222 return theRhs.myHost->More();
0223 }
0224
0225 private:
0226 HostType* myHost;
0227 };
0228
0229
0230
0231
0232
0233
0234
0235
0236
0237
0238
0239
0240
0241
0242
0243
0244 template <typename HostType>
0245 class NCollection_ForwardRange
0246 {
0247 public:
0248 using iterator = NCollection_ForwardRangeIterator<HostType>;
0249 using const_iterator = iterator;
0250 using sentinel = NCollection_ForwardRangeSentinel;
0251
0252
0253 template <typename U = HostType, typename = std::enable_if_t<std::is_copy_constructible_v<U>>>
0254 explicit NCollection_ForwardRange(const HostType& theHost)
0255 : myHost(theHost)
0256 {
0257 }
0258
0259
0260 explicit NCollection_ForwardRange(HostType&& theHost)
0261 : myHost(std::move(theHost))
0262 {
0263 }
0264
0265 NCollection_ForwardRange(const NCollection_ForwardRange&) = delete;
0266 NCollection_ForwardRange& operator=(const NCollection_ForwardRange&) = delete;
0267 NCollection_ForwardRange(NCollection_ForwardRange&&) = default;
0268 NCollection_ForwardRange& operator=(NCollection_ForwardRange&&) = default;
0269
0270
0271
0272
0273 [[nodiscard]] iterator begin() const { return iterator(&myHost); }
0274
0275
0276 [[nodiscard]] sentinel end() const { return sentinel{}; }
0277
0278
0279 [[nodiscard]] const_iterator cbegin() const { return const_iterator(&myHost); }
0280
0281
0282 [[nodiscard]] sentinel cend() const { return sentinel{}; }
0283
0284 private:
0285 mutable HostType myHost;
0286 };
0287
0288
0289 template <typename T>
0290 NCollection_ForwardRange(T&&) -> NCollection_ForwardRange<std::remove_reference_t<T>>;
0291
0292 #endif