User Guide
nmodl::printer::CodePrinter Class Reference

Helper class for printing C/C++ code. More...

Detailed Description

Helper class for printing C/C++ code.

This class provides common functionality required by code generation visitor to print C/C++/Cuda code.

Definition at line 42 of file code_printer.hpp.

#include <code_printer.hpp>

Public Member Functions

 CodePrinter (size_t blame_line=0)
 
 CodePrinter (std::ostream &stream, size_t blame_line=0)
 
 CodePrinter (const std::string &filename, size_t blame_line=0)
 
 ~CodePrinter ()
 
void add_indent ()
 print whitespaces for indentation More...
 
void push_block ()
 start a block scope without indentation (i.e. "{\n") More...
 
void push_block (const std::string &expression)
 start a block scope with an expression (i.e. "[indent][expression] {\n") More...
 
void chain_block (std::string const &expression)
 end a block and immediately start a new one (i.e. "[indent-1]} [expression] {\n") More...
 
template<typename... Args>
void add_text (Args &&... args)
 
template<typename... Args>
void add_line (Args &&... args)
 
template<typename... Args>
void fmt_line (Args &&... args)
 fmt_line(x, y, z) is just shorthand for add_line(fmt::format(x, y, z)) More...
 
template<typename... Args>
void fmt_push_block (Args &&... args)
 fmt_push_block(args...) is just shorthand for push_block(fmt::format(args...)) More...
 
template<typename... Args>
void fmt_text (Args &&... args)
 fmt_text(args...) is just shorthand for add_text(fmt::format(args...)) More...
 
void add_multi_line (const std::string &)
 
void add_newline (std::size_t n=1)
 
void increase_indent ()
 
void decrease_indent ()
 
void pop_block ()
 end of current block scope (i.e. end with "}") and adds one NL character More...
 
void pop_block_nl (std::size_t num_newlines=0)
 same as pop_block but control the number of NL characters (0 or more) with num_newlines parameter More...
 
void pop_block (const std::string_view &suffix, std::size_t num_newlines=1)
 end a block with suffix before the newline(s) (i.e. [indent]}[suffix]
*num_newlines) More...
 
int indent_spaces ()
 

Private Member Functions

void blame ()
 Blame when on the requested line. More...
 

Private Attributes

std::ofstream ofs
 
std::streambuf * sbuf = nullptr
 
std::unique_ptr< std::ostream > result
 
size_t current_line = 1
 
size_t blame_line = 0
 
size_t indent_level = 0
 
const size_t NUM_SPACES = 4
 

Constructor & Destructor Documentation

◆ CodePrinter() [1/3]

nmodl::printer::CodePrinter::CodePrinter ( size_t  blame_line = 0)
inline

Definition at line 53 of file code_printer.hpp.

◆ CodePrinter() [2/3]

nmodl::printer::CodePrinter::CodePrinter ( std::ostream &  stream,
size_t  blame_line = 0 
)
inline

Definition at line 57 of file code_printer.hpp.

◆ CodePrinter() [3/3]

nmodl::printer::CodePrinter::CodePrinter ( const std::string &  filename,
size_t  blame_line = 0 
)

Definition at line 18 of file code_printer.cpp.

◆ ~CodePrinter()

nmodl::printer::CodePrinter::~CodePrinter ( )
inline

Definition at line 63 of file code_printer.hpp.

Member Function Documentation

◆ add_indent()

void nmodl::printer::CodePrinter::add_indent ( )

print whitespaces for indentation

Definition at line 56 of file code_printer.cpp.

◆ add_line()

template<typename... Args>
void nmodl::printer::CodePrinter::add_line ( Args &&...  args)
inline

Definition at line 86 of file code_printer.hpp.

◆ add_multi_line()

void nmodl::printer::CodePrinter::add_multi_line ( const std::string &  text)

Definition at line 60 of file code_printer.cpp.

◆ add_newline()

void nmodl::printer::CodePrinter::add_newline ( std::size_t  n = 1)

Definition at line 93 of file code_printer.cpp.

◆ add_text()

template<typename... Args>
void nmodl::printer::CodePrinter::add_text ( Args &&...  args)
inline

Definition at line 80 of file code_printer.hpp.

◆ blame()

void nmodl::printer::CodePrinter::blame ( )
private

Blame when on the requested line.

Definition at line 117 of file code_printer.cpp.

◆ chain_block()

void nmodl::printer::CodePrinter::chain_block ( std::string const &  expression)

end a block and immediately start a new one (i.e. "[indent-1]} [expression] {\n")

Definition at line 48 of file code_printer.cpp.

◆ decrease_indent()

void nmodl::printer::CodePrinter::decrease_indent ( )
inline

Definition at line 118 of file code_printer.hpp.

◆ fmt_line()

template<typename... Args>
void nmodl::printer::CodePrinter::fmt_line ( Args &&...  args)
inline

fmt_line(x, y, z) is just shorthand for add_line(fmt::format(x, y, z))

Definition at line 94 of file code_printer.hpp.

◆ fmt_push_block()

template<typename... Args>
void nmodl::printer::CodePrinter::fmt_push_block ( Args &&...  args)
inline

fmt_push_block(args...) is just shorthand for push_block(fmt::format(args...))

Definition at line 100 of file code_printer.hpp.

◆ fmt_text()

template<typename... Args>
void nmodl::printer::CodePrinter::fmt_text ( Args &&...  args)
inline

fmt_text(args...) is just shorthand for add_text(fmt::format(args...))

Definition at line 106 of file code_printer.hpp.

◆ increase_indent()

void nmodl::printer::CodePrinter::increase_indent ( )
inline

Definition at line 114 of file code_printer.hpp.

◆ indent_spaces()

int nmodl::printer::CodePrinter::indent_spaces ( )
inline

Definition at line 132 of file code_printer.hpp.

◆ pop_block() [1/2]

void nmodl::printer::CodePrinter::pop_block ( )

end of current block scope (i.e. end with "}") and adds one NL character

Definition at line 100 of file code_printer.cpp.

◆ pop_block() [2/2]

void nmodl::printer::CodePrinter::pop_block ( const std::string_view &  suffix,
std::size_t  num_newlines = 1 
)

end a block with suffix before the newline(s) (i.e. [indent]}[suffix]
*num_newlines)

Definition at line 111 of file code_printer.cpp.

◆ pop_block_nl()

void nmodl::printer::CodePrinter::pop_block_nl ( std::size_t  num_newlines = 0)

same as pop_block but control the number of NL characters (0 or more) with num_newlines parameter

Definition at line 104 of file code_printer.cpp.

◆ push_block() [1/2]

void nmodl::printer::CodePrinter::push_block ( )

start a block scope without indentation (i.e. "{\n")

Definition at line 35 of file code_printer.cpp.

◆ push_block() [2/2]

void nmodl::printer::CodePrinter::push_block ( const std::string &  expression)

start a block scope with an expression (i.e. "[indent][expression] {\n")

Definition at line 41 of file code_printer.cpp.

Member Data Documentation

◆ blame_line

size_t nmodl::printer::CodePrinter::blame_line = 0
private

Definition at line 48 of file code_printer.hpp.

◆ current_line

size_t nmodl::printer::CodePrinter::current_line = 1
private

Definition at line 47 of file code_printer.hpp.

◆ indent_level

size_t nmodl::printer::CodePrinter::indent_level = 0
private

Definition at line 49 of file code_printer.hpp.

◆ NUM_SPACES

const size_t nmodl::printer::CodePrinter::NUM_SPACES = 4
private

Definition at line 50 of file code_printer.hpp.

◆ ofs

std::ofstream nmodl::printer::CodePrinter::ofs
private

Definition at line 44 of file code_printer.hpp.

◆ result

std::unique_ptr<std::ostream> nmodl::printer::CodePrinter::result
private

Definition at line 46 of file code_printer.hpp.

◆ sbuf

std::streambuf* nmodl::printer::CodePrinter::sbuf = nullptr
private

Definition at line 45 of file code_printer.hpp.


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