range.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * Copyright 2018 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef FIRESTORE_CORE_SRC_UTIL_RANGE_H_
  17. #define FIRESTORE_CORE_SRC_UTIL_RANGE_H_
  18. #include <iterator>
  19. #include <utility>
  20. namespace firebase {
  21. namespace firestore {
  22. namespace util {
  23. /**
  24. * Adapts a pair of iterators into a range suitable for use with range-based
  25. * for loops.
  26. */
  27. template <typename Iterator>
  28. class range {
  29. public:
  30. using value_type = typename std::iterator_traits<Iterator>::value_type;
  31. using iterator = Iterator;
  32. using const_iterator = Iterator;
  33. /**
  34. * Default range which constructs default begin and end pointers. For most
  35. * implementations where the end pointer is a null pointer or some other
  36. * default value, this ends up constructing the empty range.
  37. */
  38. range() : begin_{}, end_{} {
  39. }
  40. /**
  41. * Creates a half-open range starting from begin up to, but not including end.
  42. */
  43. range(iterator&& begin, iterator&& end)
  44. : begin_{std::move(begin)}, end_{std::move(end)} {
  45. }
  46. iterator begin() const {
  47. return begin_;
  48. }
  49. iterator end() const {
  50. return end_;
  51. }
  52. private:
  53. Iterator begin_;
  54. Iterator end_;
  55. };
  56. /**
  57. * Creates ranges with type deduction.
  58. */
  59. template <typename Iterator>
  60. range<Iterator> make_range(Iterator begin, Iterator end) {
  61. return range<Iterator>{std::move(begin), std::move(end)};
  62. }
  63. } // namespace util
  64. } // namespace firestore
  65. } // namespace firebase
  66. #endif // FIRESTORE_CORE_SRC_UTIL_RANGE_H_