-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#pragma once | ||
#ifndef UITSL_UTILITY_SEPARATEDSTREAM_HPP_INCLUDE | ||
#define UITSL_UTILITY_SEPARATEDSTREAM_HPP_INCLUDE | ||
|
||
#include <iostream> | ||
#include <string> | ||
#include <utility> | ||
|
||
namespace uitsl { | ||
|
||
// adapted from https://stackoverflow.com/a/30073885 | ||
class SeparatedStream { | ||
|
||
std::ostream& _stream; | ||
std::string _sep; | ||
bool _first; | ||
|
||
public: | ||
|
||
SeparatedStream(std::ostream &stream, std::string sep) | ||
: _stream(stream), _sep(std::move(sep)), _first(true) {} | ||
|
||
template <class Rhs> | ||
SeparatedStream &operator <<(Rhs &&rhs) { | ||
if (_first) _first = false; | ||
else _stream << _sep; | ||
_stream << std::forward<Rhs>(rhs); | ||
return *this; | ||
} | ||
|
||
SeparatedStream& operator<<(std::ostream &(*manip)(std::ostream&)) { | ||
manip(_stream); | ||
return *this; | ||
} | ||
|
||
}; | ||
|
||
} // namespace uitsl | ||
|
||
#endif // #ifndef UITSL_UTILITY_SEPARATEDSTREAM_HPP_INCLUDE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#pragma once | ||
#ifndef UITSL_UTILITY_SETSEPARATOR_HPP_INCLUDE | ||
#define UITSL_UTILITY_SETSEPARATOR_HPP_INCLUDE | ||
|
||
#include <iostream> | ||
#include <utility> | ||
#include <string> | ||
|
||
#include "SeparatedStream.hpp" | ||
|
||
namespace uitsl { | ||
|
||
// adapted from https://stackoverflow.com/a/30073885 | ||
struct SetSeparator { | ||
|
||
std::string sep; | ||
|
||
SetSeparator(const std::string& sep) : sep(sep) {} | ||
|
||
}; | ||
|
||
uitsl::SeparatedStream operator<<( | ||
std::ostream& stream, | ||
uitsl::SetSeparator wsep | ||
) { | ||
return uitsl::SeparatedStream(stream, std::move(wsep.sep)); | ||
} | ||
|
||
} // namespace uitsl | ||
|
||
#endif // #ifndef UITSL_UTILITY_SETSEPARATOR_HPP_INCLUDE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#include <sstream> | ||
|
||
#include "Catch/single_include/catch2/catch.hpp" | ||
#include "stduuid/include/uuid.h" | ||
|
||
#include "uitsl/utility/SetSeparator.hpp" | ||
|
||
TEST_CASE("SetSeparator no items", "[nproc:1]") { | ||
std::stringstream ss; | ||
ss << uitsl::SetSeparator(", "); | ||
REQUIRE(ss.str() == ""); | ||
} | ||
|
||
TEST_CASE("SetSeparator one item", "[nproc:1]") { | ||
std::stringstream ss; | ||
ss << uitsl::SetSeparator(", ") << "hello"; | ||
REQUIRE(ss.str() == "hello"); | ||
} | ||
|
||
TEST_CASE("SetSeparator two items", "[nproc:1]") { | ||
std::stringstream ss; | ||
ss << uitsl::SetSeparator(", ") << "hello" << "friend"; | ||
REQUIRE(ss.str() == "hello, friend"); | ||
} |