MimIR 0.1
MimIR is my 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  mim::Elem< R, F >
 
class  mim::Tab
 Keeps track of indentation level. More...
 

Namespaces

namespace  mim
 

Functions

out/err

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

template<class... Args>
std::ostream & mim::outf (const char *fmt, Args &&... args)
 
template<class... Args>
std::ostream & mim::errf (const char *fmt, Args &&... args)
 
template<class... Args>
std::ostream & mim::outln (const char *fmt, Args &&... args)
 
template<class... Args>
std::ostream & mim::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 & mim::print (std::ostream &os, const char *s)
 Base case.
 
template<class T , class... Args>
std::ostream & mim::print (std::ostream &os, const char *s, T &&t, Args &&... args)
 
template<class... Args>
std::ostream & mim::println (std::ostream &os, const char *fmt, Args &&... args)
 As above but end with std::endl.
 
template<class... Args>
std::string mim::fmt (const char *s, Args &&... args)
 Wraps mim::print to output a formatted std:string.
 
template<class T = std::logic_error, class... Args>
void mim::error (const char *fmt, Args &&... args)
 Wraps mim::print to throw T with a formatted message.
 

Macro Definition Documentation

◆ assertf

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

Definition at line 172 of file print.h.

Referenced by mim::World::dump().