libcruft-util/iterator/unordered_insert.hpp

51 lines
1.4 KiB
C++
Raw Normal View History

/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Copyright 2021, Danny Robson <danny@nerdcruft.net>
*/
#pragma once
#include <iterator>
#include <type_traits>
#include <utility>
///////////////////////////////////////////////////////////////////////////////
namespace cruft::iterator {
/// An output iterator that calls insert on a supplied container
template <typename ContainerT>
struct unordered_insert {
public:
using iterator_category = std::output_iterator_tag;
using value_type = void;
using difference_type = void;
using pointer = void;
using reference = void;
unordered_insert (ContainerT &_target)
: m_target (&_target)
{ ; }
unordered_insert& operator++ () { return *this; }
unordered_insert& operator * () { return *this; }
unordered_insert& operator-> () { return this; }
template <typename ValueT>
unordered_insert&
operator= (ValueT &&val)
{
m_target->insert (std::forward<ValueT> (val));
return *this;
}
private:
ContainerT *m_target;
};
template <typename ContainerT>
unordered_insert (ContainerT &) -> unordered_insert<std::remove_cvref_t<ContainerT>>;
}