Thorin 1.9.0
The Higher ORder INtermediate representation
Loading...
Searching...
No Matches
print.h File Reference
#include <functional>
#include <iostream>
#include <ostream>
#include <ranges>
#include <sstream>
#include <string>
#include <fe/assert.h>
Include dependency graph for print.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  thorin::Elem< R, F >
 Use with print to output complicated std::ranges::ranges. More...
 
class  thorin::Tab
 Keeps track of indentation level. More...
 

Namespaces

namespace  thorin
 

Functions

out/err

thorin::prints to std::cout/std::cerr; the *ln variants emit an additional std::endl.

template<class... Args>
std::ostream & thorin::outf (const char *fmt, Args &&... args)
 
template<class... Args>
std::ostream & thorin::errf (const char *fmt, Args &&... args)
 
template<class... Args>
std::ostream & thorin::outln (const char *fmt, Args &&... args)
 
template<class... Args>
std::ostream & thorin::errln (const char *fmt, Args &&... args)
 

Formatted Output

Provides a printf-like interface to format s with args and puts it into os.

Use {} as a placeholder within your format string s.

  • By default, os << t will be used to stream the appropriate argument:
    print(os, "int: {}, float: {}", 23, 42.f);
  • You can also place a function as argument to inject arbitrary code:
    print(os, "int: {}, fun: {}, float: {}", 23, [&]() { os << "hey :)"}, 42.f);
  • There is also a variant to pass os to the function, if you don't have easy access to it:
    print(os, "int: {}, fun: {}, float: {}", 23, [&](auto& os) { os << "hey :)"}, 42.f);
  • You can put a std::ranges::range as argument. This will output a list - using the given specifier as separator. Each element elem of the range will be output via os << elem:
    std::vector<int> v = {0, 1, 2, 3};
    print(os, "v: {, }", v);
  • If you have a std::ranges::range that needs to be formatted in a more complicated way, bundle it with an Elem:
    std::vector<int> v = {3, 2, 1, 0};
    size_t i = 0;
    print(os, "v: {, }", Elem(v, [&](auto elem) { print(os, "{}: {}", i++, elem); }));
  • You again have the option to pass os to the bundled function:
    std::vector<int> v = {3, 2, 1, 0};
    size_t i = 0;
    print(os, "v: {, }", Elem(v, [&](auto& os, auto elem) { print(os, "{}: {}", i++, elem); }));
    * Use `{{` or `}}` to literally output `{` or `{`:
    print(os, "{{{}}}", 23); // "{23}"
    std::ostream & print(std::ostream &os, const char *s)
    Base case.
    Definition print.cpp:5
    See also
    Tab
#define assertf(condition, ...)
 
std::ostream & thorin::print (std::ostream &os, const char *s)
 Base case.
 
template<class T , class... Args>
std::ostream & thorin::print (std::ostream &os, const char *s, T &&t, Args &&... args)
 
template<class... Args>
std::ostream & thorin::println (std::ostream &os, const char *fmt, Args &&... args)
 As above but end with std::endl.
 
template<class... Args>
std::string thorin::fmt (const char *s, Args &&... args)
 Wraps thorin::print to output a formatted std:string.
 
template<class T = std::logic_error, class... Args>
void thorin::error (const char *fmt, Args &&... args)
 Wraps thorin::print to throw T with a formatted message.
 

Macro Definition Documentation

◆ assertf

#define assertf (   condition,
  ... 
)
Value:
do { \
if (!(condition)) { \
thorin::errf("{}:{}: assertion: ", __FILE__, __LINE__); \
thorin::errln(__VA_ARGS__); \
fe::breakpoint(); \
} \
} while (false)

Definition at line 161 of file print.h.