//********************************************************
// The following code example is taken from the book
//  C++23 - The Complete Guide
//  by Nicolai M. Josuttis (www.josuttis.com)
//  http://www.cppstd23.com
//
// The code is licensed under a
//  Creative Commons Attribution 4.0 International License
//  http://creativecommons.org/licenses/by/4.0/
//********************************************************


#include <ranges>
#include <format>

// new view type for all elements:
template<std::ranges::range InnerRgT>
struct SepNewlineView : std::ranges::view_interface<SepNewlineView<InnerRgT>>
{
  private:
    InnerRgT innerRange;
  public:
    template<std::ranges::range RgT>
    SepNewlineView(RgT&& rg) 
     : innerRange(std::forward<RgT>(rg)) {
    }
    auto begin() { return innerRange.begin(); }
    auto end() { return innerRange.end(); }
};

// deduction guide to deal with lvalues and rvalues:
template<std::ranges::range RgT> 
SepNewlineView(RgT&& rg) -> SepNewlineView<std::ranges::views::all_t<RgT>>;

// add special formatter for this view:
template<typename RgT>
struct std::formatter<SepNewlineView<RgT>> 
 : std::range_formatter<std::ranges::range_value_t<RgT>>
{
  constexpr formatter() {
    this->set_brackets("[", "]");
    this->set_separator("\n ");
  }
};

