UgMisc 0.3
Miscellaneous C++ header library
Loading...
Searching...
No Matches
SampleOutput< OS, Target > Class Template Reference

#include <samples/samples_output.hpp>

Public Member Functions

 SampleOutput (const SampleOutput &)=default
constexpr SampleOutput (OS &os)
template<typename T, SampleOutputTarget Tgt>
constexpr SampleOutput (const SampleOutput< T, Tgt > &other)
constexpr SampleOutput< OS, SAMPLE_NORMAL_OUTPUT > out ()
constexpr auto normal ()
constexpr SampleOutput< OS, SAMPLE_TEST_OUTPUT > expected ()
constexpr auto expect ()
constexpr SampleOutput< OS, SAMPLE_ALWAYS_OUTPUT > always ()
constexpr auto both ()
template<typename T>
constexpr SampleOutputoperator<< (T &&v)

Detailed Description

template<typename OS, SampleOutputTarget Target>
class SampleOutput< OS, Target >

A SampleOutput<T> object wraps a reference to a stream of type T.

If T is either std::ostream or std::wostream then the default constructor will wrap std::cout or std::wcout respectively.

SampleOutput has deduction guides which allow the template parameters to be avoided in most cases.

// Three ways to wrap std::cout:
SampleOutput cout1;
SampleOutput<std::ostream> cout2;
SampleOutput cout3{std::cout};
// Two ways to wrap std::wcout:
SampleOutput wcout2{std::wcout};
// Wrap another stream:
SampleOutput other1{stream};

The wrapper can be used as a stream with the << operator. It also has methods that can return different wrapper objects:

SampleOutput out; // A "normal" wrapper around std::cout.
out.out(); // The same type of thing again.
out.normal(); // And again.
out.expect(); // Send expected, rather than actual, output here.
out.expected();
out.always(); // This output goes to normal output and the test file.
out.both();

Each of those is a SampleOutput object in its own right, wrapping a reference to the same underlying stream.

SampleOutput out;
auto always = out.always();
auto expected = out.expected();
always << "Doing the twist...\n";
out << getTwistResult();
expected() << "Ouch!";
always << "\n";

When performing the sample program tests, each sample program is built twice, once with the macro UGMISC_SAMPLES_GENERATE_TEST_OUTPUT defined. In the example above, these two programs compile slightly differently:

The normal build is equivalent to this:

std::cout << "Doing the twist...\n";
std::cout << getTwistResult(); // We expect the output to be "Ouch!".
"Ouch!"; // No output!
std::cout << "\n";

The test program is equivalent to:

std::cout << "Doing the twist...\n";
getTwistResult(); // No output!
std::cout << "Ouch!"; // This is what we expect, and we say so.
std::cout << "\n";

Then the two programs' outputs are compared.

Template Parameters
OSThe type of output stream to reference.

Definition at line 127 of file samples_output.hpp.

Constructor & Destructor Documentation

◆ SampleOutput() [1/3]

template<typename OS, SampleOutputTarget Target>
SampleOutput< OS, Target >::SampleOutput ( )
inlineconstexpr

Definition at line 170 of file samples_output.hpp.

◆ SampleOutput() [2/3]

template<typename OS, SampleOutputTarget Target>
SampleOutput< OS, Target >::SampleOutput ( OS & os)
inlineconstexpr

Definition at line 172 of file samples_output.hpp.

◆ SampleOutput() [3/3]

template<typename OS, SampleOutputTarget Target>
template<typename T, SampleOutputTarget Tgt>
SampleOutput< OS, Target >::SampleOutput ( const SampleOutput< T, Tgt > & other)
inlineconstexpr

A SampleOutput will never try to wrap another SampleOutput as if it would another type of stream. It will always try to take the same underlying reference instead.

If that can't be done due to stream type incompatibility, then the attempt will be an error.

Definition at line 183 of file samples_output.hpp.

Member Function Documentation

◆ always()

template<typename OS, SampleOutputTarget Target>
SampleOutput< OS, SAMPLE_ALWAYS_OUTPUT > SampleOutput< OS, Target >::always ( )
inlineconstexpr

Definition at line 194 of file samples_output.hpp.

◆ both()

template<typename OS, SampleOutputTarget Target>
auto SampleOutput< OS, Target >::both ( )
inlineconstexpr

Definition at line 195 of file samples_output.hpp.

◆ expect()

template<typename OS, SampleOutputTarget Target>
auto SampleOutput< OS, Target >::expect ( )
inlineconstexpr

Definition at line 192 of file samples_output.hpp.

◆ expected()

template<typename OS, SampleOutputTarget Target>
SampleOutput< OS, SAMPLE_TEST_OUTPUT > SampleOutput< OS, Target >::expected ( )
inlineconstexpr

Definition at line 191 of file samples_output.hpp.

◆ normal()

template<typename OS, SampleOutputTarget Target>
auto SampleOutput< OS, Target >::normal ( )
inlineconstexpr

Definition at line 189 of file samples_output.hpp.

◆ operator<<()

template<typename OS, SampleOutputTarget Target>
template<typename T>
SampleOutput & SampleOutput< OS, Target >::operator<< ( T && v)
inlineconstexpr

Definition at line 198 of file samples_output.hpp.

◆ out()

template<typename OS, SampleOutputTarget Target>
SampleOutput< OS, SAMPLE_NORMAL_OUTPUT > SampleOutput< OS, Target >::out ( )
inlineconstexpr

Definition at line 188 of file samples_output.hpp.

◆ SampleOutput

template<typename OS, SampleOutputTarget Target>
template<typename, SampleOutputTarget>
friend class SampleOutput
friend

Definition at line 131 of file samples_output.hpp.


The documentation for this class was generated from the following file: