Changeset View
Changeset View
Standalone View
Standalone View
src/shared/akranges.h
Show First 20 Lines • Show All 336 Lines • ▼ Show 20 Line(s) | |||||
337 | }; | 337 | }; | ||
338 | 338 | | |||
339 | template<typename EachFun> | 339 | template<typename EachFun> | ||
340 | struct ForEach_ | 340 | struct ForEach_ | ||
341 | { | 341 | { | ||
342 | EachFun mFn; | 342 | EachFun mFn; | ||
343 | }; | 343 | }; | ||
344 | 344 | | |||
345 | template<typename Predicate> | ||||
346 | struct All_ | ||||
347 | { | ||||
348 | Predicate mFn; | ||||
349 | }; | ||||
350 | | ||||
351 | template<typename Predicate> | ||||
352 | struct Any_ | ||||
353 | { | ||||
354 | Predicate mFn; | ||||
355 | }; | ||||
356 | | ||||
345 | } // namespace detail | 357 | } // namespace detail | ||
346 | } // namespace Akonadi | 358 | } // namespace Akonadi | ||
347 | 359 | | |||
348 | // Generic operator| for To_<> convertor | 360 | // Generic operator| for To_<> convertor | ||
349 | template<typename InContainer, | 361 | template<typename InContainer, | ||
350 | template<typename> class OutContainer, | 362 | template<typename> class OutContainer, | ||
351 | typename T = typename InContainer::value_type | 363 | typename T = typename InContainer::value_type | ||
352 | > | 364 | > | ||
▲ Show 20 Lines • Show All 53 Lines • ▼ Show 20 Line(s) | |||||
406 | { | 418 | { | ||
407 | std::for_each(std::cbegin(in), std::cend(in), | 419 | std::for_each(std::cbegin(in), std::cend(in), | ||
408 | [&fun](const auto &val) { | 420 | [&fun](const auto &val) { | ||
409 | Akonadi::invoke(fun.mFn, val); | 421 | Akonadi::invoke(fun.mFn, val); | ||
410 | }); | 422 | }); | ||
411 | return in; | 423 | return in; | ||
412 | } | 424 | } | ||
413 | 425 | | |||
426 | // Generic operator| for all | ||||
427 | template<typename InContainer, | ||||
428 | typename PredicateFn | ||||
429 | > | ||||
430 | auto operator|(const InContainer &in, | ||||
431 | Akonadi::detail::All_<PredicateFn> fun) | ||||
432 | { | ||||
433 | return std::all_of(std::cbegin(in), std::cend(in), fun.mFn); | ||||
434 | } | ||||
435 | | ||||
436 | // Generic operator| for any | ||||
437 | template<typename InContainer, | ||||
438 | typename PredicateFn | ||||
439 | > | ||||
440 | auto operator|(const InContainer &in, | ||||
441 | Akonadi::detail::Any_<PredicateFn> fun) | ||||
442 | { | ||||
443 | return std::any_of(std::cbegin(in), std::cend(in), fun.mFn); | ||||
444 | } | ||||
414 | 445 | | |||
415 | // Generic operator| for keys | 446 | // Generic operator| for keys | ||
416 | template<typename InContainer> | 447 | template<typename InContainer> | ||
417 | auto operator|(const InContainer &in, Akonadi::detail::Keys_) | 448 | auto operator|(const InContainer &in, Akonadi::detail::Keys_) | ||
418 | { | 449 | { | ||
419 | using namespace Akonadi::detail; | 450 | using namespace Akonadi::detail; | ||
420 | using OutIt = AssociativeContainerKeyIterator<InContainer>; | 451 | using OutIt = AssociativeContainerKeyIterator<InContainer>; | ||
421 | return Range<OutIt>(OutIt(in.constKeyValueBegin(), in), | 452 | return Range<OutIt>(OutIt(in.constKeyValueBegin(), in), | ||
422 | OutIt(in.constKeyValueEnd(), in)); | 453 | OutIt(in.constKeyValueEnd(), in)); | ||
423 | } | 454 | } | ||
424 | 455 | | |||
425 | 456 | | |||
426 | // Generic operator| for values | 457 | // Generic operator| for values | ||
427 | template<typename InContainer> | 458 | template<typename InContainer> | ||
428 | auto operator|(const InContainer &in, Akonadi::detail::Values_) | 459 | auto operator|(const InContainer &in, Akonadi::detail::Values_) | ||
429 | { | 460 | { | ||
430 | using namespace Akonadi::detail; | 461 | using namespace Akonadi::detail; | ||
431 | using OutIt = AssociativeContainerValueIterator<InContainer>; | 462 | using OutIt = AssociativeContainerValueIterator<InContainer>; | ||
432 | return Range<OutIt>(OutIt(in.constKeyValueBegin(), in), | 463 | return Range<OutIt>(OutIt(in.constKeyValueBegin(), in), | ||
433 | OutIt(in.constKeyValueEnd(), in)); | 464 | OutIt(in.constKeyValueEnd(), in)); | ||
434 | } | 465 | } | ||
435 | 466 | | |||
467 | | ||||
436 | namespace Akonadi { | 468 | namespace Akonadi { | ||
437 | 469 | | |||
438 | /// Non-lazily convert given range or container to QVector | 470 | /// Non-lazily convert given range or container to QVector | ||
439 | static constexpr auto toQVector = detail::To_<QVector>{}; | 471 | static constexpr auto toQVector = detail::To_<QVector>{}; | ||
440 | /// Non-lazily convert given range or container to QSet | 472 | /// Non-lazily convert given range or container to QSet | ||
441 | static constexpr auto toQSet = detail::To_<QSet>{}; | 473 | static constexpr auto toQSet = detail::To_<QSet>{}; | ||
442 | /// Non-lazily convert given range or container to QList | 474 | /// Non-lazily convert given range or container to QList | ||
443 | static constexpr auto toQList = detail::To_<QList>{}; | 475 | static constexpr auto toQList = detail::To_<QList>{}; | ||
Show All 27 Lines | |||||
471 | template<typename Iterator1, typename Iterator2, | 503 | template<typename Iterator1, typename Iterator2, | ||
472 | typename It = std::remove_reference_t<Iterator1> | 504 | typename It = std::remove_reference_t<Iterator1> | ||
473 | > | 505 | > | ||
474 | detail::Range<It> range(Iterator1 begin, Iterator2 end) | 506 | detail::Range<It> range(Iterator1 begin, Iterator2 end) | ||
475 | { | 507 | { | ||
476 | return detail::Range<It>(std::move(begin), std::move(end)); | 508 | return detail::Range<It>(std::move(begin), std::move(end)); | ||
477 | } | 509 | } | ||
478 | 510 | | |||
511 | /// Non-lazily check that all elements in the range satisfy given predicate | ||||
512 | template<typename Predicate> | ||||
513 | detail::All_<Predicate> all(Predicate &&fn) | ||||
514 | { | ||||
515 | return detail::All_<Predicate>{std::forward<Predicate>(fn)}; | ||||
516 | } | ||||
517 | | ||||
518 | /// Non-lazily check that at least one element in range satisfies the given predicate | ||||
519 | template<typename Predicate> | ||||
520 | detail::Any_<Predicate> any(Predicate &&fn) | ||||
521 | { | ||||
522 | return detail::Any_<Predicate>{std::forward<Predicate>(fn)}; | ||||
523 | } | ||||
524 | | ||||
479 | } // namespace Akonadi | 525 | } // namespace Akonadi | ||
480 | 526 | | |||
481 | #endif | 527 | #endif |