Fix deprecation warning
Also put iterator into dhandy namespace. I don't remember why I left it out, and it's probably a sign I should have not done so.
This commit is contained in:
parent
d3092f9359
commit
4d904ac689
3 changed files with 70 additions and 3 deletions
|
@ -5,12 +5,12 @@
|
||||||
#if !defined(INFIX_ITERATOR_H_)
|
#if !defined(INFIX_ITERATOR_H_)
|
||||||
#define INFIX_ITERATOR_H_
|
#define INFIX_ITERATOR_H_
|
||||||
#include <ostream>
|
#include <ostream>
|
||||||
#include <iterator>
|
|
||||||
|
namespace dhandy {
|
||||||
template <class T,
|
template <class T,
|
||||||
class charT=char,
|
class charT=char,
|
||||||
class traits=std::char_traits<charT> >
|
class traits=std::char_traits<charT> >
|
||||||
class infix_ostream_iterator :
|
class infix_ostream_iterator
|
||||||
public std::iterator<std::output_iterator_tag,void,void,void,void>
|
|
||||||
{
|
{
|
||||||
std::basic_ostream<charT,traits> *os;
|
std::basic_ostream<charT,traits> *os;
|
||||||
charT const* delimiter;
|
charT const* delimiter;
|
||||||
|
@ -19,6 +19,13 @@ public:
|
||||||
typedef charT char_type;
|
typedef charT char_type;
|
||||||
typedef traits traits_type;
|
typedef traits traits_type;
|
||||||
typedef std::basic_ostream<charT,traits> ostream_type;
|
typedef std::basic_ostream<charT,traits> ostream_type;
|
||||||
|
|
||||||
|
using iterator_category = std::output_iterator_tag;
|
||||||
|
using value_type = void;
|
||||||
|
using difference_type = void;
|
||||||
|
using pointer = void;
|
||||||
|
using reference = void;
|
||||||
|
|
||||||
infix_ostream_iterator(ostream_type& s)
|
infix_ostream_iterator(ostream_type& s)
|
||||||
: os(&s),delimiter(0), first_elem(true)
|
: os(&s),delimiter(0), first_elem(true)
|
||||||
{}
|
{}
|
||||||
|
@ -45,4 +52,5 @@ public:
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
} //namespace dhandy
|
||||||
#endif
|
#endif
|
||||||
|
|
58
test/unit/infix_iterator.cpp
Normal file
58
test/unit/infix_iterator.cpp
Normal file
|
@ -0,0 +1,58 @@
|
||||||
|
/* Copyright 2016-2024 Michele Santullo
|
||||||
|
* This file is part of "duckhandy".
|
||||||
|
*
|
||||||
|
* "duckhandy" is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* "duckhandy" is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with "duckhandy". If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "catch2/catch_test_macros.hpp"
|
||||||
|
#include "duckhandy/infix_iterator.hpp"
|
||||||
|
#include <algorithm>
|
||||||
|
#include <sstream>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
TEST_CASE("infix_iterator tests", "[iterator][infix_iterator]") {
|
||||||
|
using std::vector;
|
||||||
|
using std::string;
|
||||||
|
using std::copy;
|
||||||
|
using std::ostringstream;
|
||||||
|
using dhandy::infix_ostream_iterator;
|
||||||
|
|
||||||
|
{
|
||||||
|
ostringstream oss;
|
||||||
|
vector<string> empty{};
|
||||||
|
copy(empty.begin(), empty.end(), infix_ostream_iterator<string>(oss, "lalala"));
|
||||||
|
CHECK(oss.str() == "");
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
ostringstream oss;
|
||||||
|
vector<string> data{"apple", "pear", "cherry", "mango"};
|
||||||
|
copy(data.begin(), data.end(), infix_ostream_iterator<string>(oss, "lalala"));
|
||||||
|
CHECK(oss.str() == "applelalalapearlalalacherrylalalamango");
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
ostringstream oss;
|
||||||
|
vector<int> data{1, 2, 3, 4, 5, 6, 7};
|
||||||
|
copy(data.begin(), data.end(), infix_ostream_iterator<int>(oss, ", "));
|
||||||
|
CHECK(oss.str() == "1, 2, 3, 4, 5, 6, 7");
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
ostringstream oss;
|
||||||
|
vector<string> data{"just one value"};
|
||||||
|
copy(data.begin(), data.end(), infix_ostream_iterator<string>(oss, ", "));
|
||||||
|
CHECK(oss.str() == "just one value");
|
||||||
|
}
|
||||||
|
}
|
|
@ -9,6 +9,7 @@ unit_test_prog = executable(meson.project_name(),
|
||||||
'resource_pool_test.cpp',
|
'resource_pool_test.cpp',
|
||||||
'version_test.cpp',
|
'version_test.cpp',
|
||||||
'tiger_test.cpp',
|
'tiger_test.cpp',
|
||||||
|
'infix_iterator.cpp',
|
||||||
install: false,
|
install: false,
|
||||||
dependencies: [sprout_dep, catch2_dep],
|
dependencies: [sprout_dep, catch2_dep],
|
||||||
include_directories: [public_incl],
|
include_directories: [public_incl],
|
||||||
|
|
Loading…
Reference in a new issue