Make truncating_iterator an output_iterator (#2158)

This commit is contained in:
Jason Cobb 2021-03-04 18:53:08 -05:00 committed by GitHub
parent 772aeca338
commit e718ec3e93
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 3 deletions

View File

@ -469,15 +469,17 @@ template <typename OutputIt> class truncating_iterator_base {
protected:
OutputIt out_;
size_t limit_;
size_t count_;
size_t count_ = 0;
truncating_iterator_base() : out_(), limit_(0) {}
truncating_iterator_base(OutputIt out, size_t limit)
: out_(out), limit_(limit), count_(0) {}
: out_(out), limit_(limit) {}
public:
using iterator_category = std::output_iterator_tag;
using value_type = typename std::iterator_traits<OutputIt>::value_type;
using difference_type = void;
using difference_type = std::ptrdiff_t;
using pointer = void;
using reference = void;
using _Unchecked_type =
@ -502,6 +504,8 @@ class truncating_iterator<OutputIt, std::false_type>
public:
using value_type = typename truncating_iterator_base<OutputIt>::value_type;
truncating_iterator() = default;
truncating_iterator(OutputIt out, size_t limit)
: truncating_iterator_base<OutputIt>(out, limit) {}
@ -525,6 +529,8 @@ template <typename OutputIt>
class truncating_iterator<OutputIt, std::true_type>
: public truncating_iterator_base<OutputIt> {
public:
truncating_iterator() = default;
truncating_iterator(OutputIt out, size_t limit)
: truncating_iterator_base<OutputIt>(out, limit) {}

View File

@ -12,9 +12,11 @@
#include <climits>
#include <cmath>
#include <cstring>
#include <iterator>
#include <list>
#include <memory>
#include <string>
#include <type_traits>
// Check if fmt/format.h compiles with windows.h included before it.
#ifdef _WIN32
@ -157,6 +159,24 @@ TEST(IteratorTest, TruncatingIterator) {
EXPECT_EQ(it.base(), p + 1);
}
TEST(IteratorTest, TruncatingIteratorDefaultConstruct) {
static_assert(
std::is_default_constructible<fmt::detail::truncating_iterator<char*>>::value,
"");
fmt::detail::truncating_iterator<char*> it;
EXPECT_EQ(nullptr, it.base());
EXPECT_EQ(std::size_t{0}, it.count());
}
#ifdef __cpp_lib_ranges
TEST(IteratorTest, TruncatingIteratorOutputIterator) {
static_assert(std::output_iterator<fmt::detail::truncating_iterator<char*>,
char>);
}
#endif
TEST(IteratorTest, TruncatingBackInserter) {
std::string buffer;
auto bi = std::back_inserter(buffer);