Skip to content

Commit

Permalink
Implement separated stream helper
Browse files Browse the repository at this point in the history
  • Loading branch information
mmore500 committed Oct 14, 2022
1 parent 96b0e88 commit 37ce2f1
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 0 deletions.
40 changes: 40 additions & 0 deletions include/uitsl/utility/SeparatedStream.hpp
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
31 changes: 31 additions & 0 deletions include/uitsl/utility/SetSeparator.hpp
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
1 change: 1 addition & 0 deletions tests/uitsl/utility/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ TARGET_NAMES += get_exec_instance_uuid
TARGET_NAMES += get_proc_instance_uuid
TARGET_NAMES += get_thread_instance_uuid
TARGET_NAMES += NamedArrayElement
TARGET_NAMES += SetSeparator

TO_ROOT := $(shell git rev-parse --show-cdup)

Expand Down
24 changes: 24 additions & 0 deletions tests/uitsl/utility/SetSeparator.cpp
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");
}

0 comments on commit 37ce2f1

Please sign in to comment.