Hegel 0.18.2
Property-based testing for C++
Loading...
Searching...
No Matches
printer.h
1#pragma once
2
3#include <cstddef>
4#include <cstdint>
5#include <functional>
6#include <limits>
7#include <string>
8#include <string_view>
9#include <utility>
10
12struct hegel_printer_t;
14
15namespace hegel {
16
17 class Speculation;
18
55 class Printer {
56 public:
61 static Printer noop();
62
67 Printer(Printer&& other) noexcept;
74 Printer& operator=(Printer&& other) noexcept;
75 Printer(const Printer&) = delete;
76 Printer& operator=(const Printer&) = delete;
77 ~Printer();
78
87 bool should_print() const;
88
96 void text(std::string_view text);
97
113 template <typename T> void repr(const T& value);
114
125 void reflow(std::string_view text);
126
131 void breakable(std::string_view separator = " ");
132
141 void if_break(std::string_view text);
142
145
155 void begin_group(std::size_t indent, std::string_view open);
156
162 void end_group(std::string_view close);
163
183 template <typename F>
184 void group(std::size_t indent, std::string_view open,
185 std::string_view close, F&& body) {
186 begin_group(indent, open);
187 std::invoke(std::forward<F>(body), *this);
188 end_group(close);
189 }
190
205 template <typename Range, typename F>
206 void seq(std::string_view open, std::string_view close,
207 const Range& items, F&& print_item) {
208 begin_block(open);
209 bool first = true;
210 for (const auto& item : items) {
211 if (!first) {
212 separator();
213 }
214 first = false;
215 std::invoke(print_item, *this, item);
216 }
217 end_block(close);
218 }
219
221 void separator();
222
227 void shift_indent(std::ptrdiff_t delta);
228
237 void comment(std::string_view text);
238
250
265
267 Printer(hegel_printer_t* handle, bool owned)
268 : handle_(handle), owned_(owned) {}
269 hegel_printer_t* handle() const { return handle_; }
270 void begin_block(std::string_view open);
271 void end_block(std::string_view close);
273
274 private:
275 hegel_printer_t* handle_ = nullptr;
276 bool owned_ = false;
277 };
278
286 public:
292 Speculation(Speculation&& other) noexcept;
293 Speculation(const Speculation&) = delete;
294 Speculation& operator=(const Speculation&) = delete;
295 Speculation& operator=(Speculation&&) = delete;
296 ~Speculation();
297
299 void commit();
301 void abort();
302
304 explicit Speculation(hegel_printer_t* handle) : handle_(handle) {}
306
307 private:
308 void close(bool keep);
309
310 hegel_printer_t* handle_ = nullptr;
311 bool resolved_ = false;
312 };
313
326 class Document {
327 public:
333 explicit Document(std::size_t max_width = 79);
334 Document(const Document&) = delete;
335 Document& operator=(const Document&) = delete;
336
341 Printer& printer() { return printer_; }
342
350 std::string finish() &&;
351
352 private:
353 Printer printer_;
354 };
355
356} // namespace hegel
357
358namespace hegel::internal {
360 // A width no value reaches, for rendering on one line.
361 inline constexpr std::size_t unbounded_width =
362 std::numeric_limits<uint32_t>::max();
364} // namespace hegel::internal
std::string finish() &&
Lay out the document and return it.
Document(std::size_t max_width=79)
Make an empty document.
Printer & printer()
The printer that writes to this document.
Definition printer.h:341
Writes to the document that a failure report shows.
Definition printer.h:55
void separator()
Write the separator between two items: , and breakable(" ").
void end_group(std::string_view close)
End the innermost group and write close.
void group(std::size_t indent, std::string_view open, std::string_view close, F &&body)
Write a group: open, what body writes, then close.
Definition printer.h:184
void breakable(std::string_view separator=" ")
Write a point where the line can break.
void repr(const T &value)
Write the C++ representation of a value.
Definition repr.h:713
void if_break(std::string_view text)
Write text only when the innermost group breaks.
Printer deferred()
Make a child region at the current position.
void text(std::string_view text)
Write text that does not break.
void comment(std::string_view text)
Write // <text> at the end of the current line.
void reflow(std::string_view text)
Write text that some other formatter made, with its bracket structure recovered.
static Printer noop()
A printer that discards everything.
Printer(Printer &&other) noexcept
Move-constructs, transferring the region.
Speculation speculate()
Start a speculative region on this printer.
void shift_indent(std::ptrdiff_t delta)
Change the indentation of the break points that follow.
void seq(std::string_view open, std::string_view close, const Range &items, F &&print_item)
Write a list of items, separated by commas.
Definition printer.h:206
void hard_break()
Write a new line, then the current indentation.
bool should_print() const
Whether printing to this printer gives output.
void begin_group(std::size_t indent, std::string_view open)
Write open and start a group.
Printer & operator=(Printer &&other) noexcept
Move-assigns, transferring the region.
An open speculative region; see Printer::speculate().
Definition printer.h:285
void commit()
End the region and keep what was written in it.
void abort()
End the region and discard what was written in it.
Speculation(Speculation &&other) noexcept
Move-constructs, transferring the open region.
Main namespace.
Definition config.h:29