| 1 | // Copyright (C) 2019 T. Zachary Laine |
| 2 | // |
| 3 | // Distributed under the Boost Software License, Version 1.0. (See |
| 4 | // accompanying file LICENSE_1_0.txt or copy at |
| 5 | // http://www.boost.org/LICENSE_1_0.txt) |
| 6 | //[ back_insert_iterator |
| 7 | #include <boost/stl_interfaces/iterator_interface.hpp> |
| 8 | |
| 9 | #include <algorithm> |
| 10 | #include <vector> |
| 11 | |
| 12 | #include <cassert> |
| 13 | |
| 14 | |
| 15 | // This is an output iterator that holds a reference to a container, and calls |
| 16 | // push_back() on that container when it is written to, just like |
| 17 | // std::back_insert_iterator. This is not a lot less code to write than the |
| 18 | // implementation of std::back_insert_iterator, but it demonstrates that |
| 19 | // iterator_interface is flexible enough to handle this odd kind of iterator. |
| 20 | template<typename Container> |
| 21 | struct back_insert_iterator : boost::stl_interfaces::iterator_interface< |
| 22 | #if !BOOST_STL_INTERFACES_USE_DEDUCED_THIS |
| 23 | back_insert_iterator<Container>, |
| 24 | #endif |
| 25 | std::output_iterator_tag, |
| 26 | typename Container::value_type, |
| 27 | back_insert_iterator<Container> &> |
| 28 | { |
| 29 | // For concept requirements. |
| 30 | back_insert_iterator() : c_(nullptr) {} |
| 31 | |
| 32 | // Construct from a container. |
| 33 | explicit back_insert_iterator(Container & c) : c_(std::addressof(c)) {} |
| 34 | |
| 35 | // When writing to *this, copy v into the container via push_back(). |
| 36 | back_insert_iterator & operator=(typename Container::value_type const & v) |
| 37 | { |
| 38 | c_->push_back(v); |
| 39 | return *this; |
| 40 | } |
| 41 | // When writing to *this, move v into the container via push_back(). |
| 42 | back_insert_iterator & operator=(typename Container::value_type && v) |
| 43 | { |
| 44 | c_->push_back(std::move(v)); |
| 45 | return *this; |
| 46 | } |
| 47 | |
| 48 | // Dereferencing *this just returns a reference to *this, so that the |
| 49 | // expression *it = value uses the operator=() overloads above. |
| 50 | back_insert_iterator & operator*() { return *this; } |
| 51 | // There is no underlying sequence over which we are iterating, so there's |
| 52 | // nowhere to go in next(). Do nothing. |
| 53 | back_insert_iterator & operator++() { return *this; } |
| 54 | |
| 55 | using base_type = boost::stl_interfaces::iterator_interface< |
| 56 | #if !BOOST_STL_INTERFACES_USE_DEDUCED_THIS |
| 57 | back_insert_iterator<Container>, |
| 58 | #endif |
| 59 | std::output_iterator_tag, |
| 60 | typename Container::value_type, |
| 61 | back_insert_iterator<Container> &>; |
| 62 | using base_type::operator++; |
| 63 | |
| 64 | private: |
| 65 | Container * c_; |
| 66 | }; |
| 67 | |
| 68 | // A convenience function that creates a back_insert_iterator<Container>. |
| 69 | template<typename Container> |
| 70 | back_insert_iterator<Container> back_inserter(Container & c) |
| 71 | { |
| 72 | return back_insert_iterator<Container>(c); |
| 73 | } |
| 74 | |
| 75 | |
| 76 | int main() |
| 77 | { |
| 78 | std::vector<int> ints = {{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}}; |
| 79 | std::vector<int> ints_copy; |
| 80 | std::copy(first: ints.begin(), last: ints.end(), result: ::back_inserter(c&: ints_copy)); |
| 81 | assert(ints_copy == ints); |
| 82 | } |
| 83 | //] |
| 84 | |