Rosetta
|
Lightweight class to ease writting YAML documents (http://yaml.org) More...
#include <Emitter.hh>
Public Member Functions | |
Emitter (std::ostream &out) | |
virtual | ~Emitter () |
void | flush () |
Flush the underlying output stream. More... | |
template<typename T > | |
void | write (T const &data) |
Write method for use inside lists / arrays. More... | |
void | start_list (bool indent=true) |
Write method for use inside lists / arrays. More... | |
void | start_map (bool indent=true) |
Write method for use inside lists / arrays. More... | |
template<typename T > | |
void | write (std::string const &label, T const &data) |
Write method for use inside maps / dicts / objects. More... | |
void | start_list (std::string const &label, bool indent=true) |
Write method for use inside maps / dicts / objects. More... | |
void | start_list (const char *label, bool indent=true) |
Write method for use inside maps / dicts / objects. More... | |
void | start_map (std::string const &label, bool indent=true) |
Write method for use inside maps / dicts / objects. More... | |
void | start_map (const char *label, bool indent=true) |
Write method for use inside maps / dicts / objects. More... | |
void | end_list () |
Counterpart to start_list() – writes closing bracket. More... | |
void | end_map () |
Counterpart to start_map() – writes closing brace. More... | |
void | end (size_t desired_depth=0) |
By default, closes all open maps/lists, ending the document. More... | |
int | depth () const |
Number of closing brackets/braces required to end document (non-negative) More... | |
void | set_indent (int num_spaces) |
Number of spaces used for indenting. Default is one. More... | |
virtual void | start_doc ()=0 |
Start a new document, ending the previous one first if necessary. A no-op for some types of output (e.g. JSON) that don't allow multiple documents. More... | |
![]() | |
ReferenceCount () | |
Default constructor. More... | |
virtual | ~ReferenceCount () |
Protected Member Functions | |
Emitter () | |
Emitter (Emitter const &) | |
bool | assert_in (bool in_map, std::string const &msg) |
Check that we're in the expected context (either map or list) More... | |
void | write_label (std::string const &label) |
Write the key part of a key-value pair. More... | |
void | write_raw (bool b) |
void | write_raw (int i) |
void | write_raw (long l) |
void | write_raw (float f) |
void | write_raw (double d) |
void | write_raw (std::string const &s) |
std::string | escape_string (std::string const &s, bool &needs_quotes_out) |
Converts special characters (newlines, etc) to escape codes ( , etc). More... | |
virtual std::string | quote_string (std::string const &s)=0 |
Quotes strings as much as needed for this format (e.g. always for JSON). More... | |
virtual void | do_indent (bool write_comma=true)=0 |
Handle pretty-printing indentation. Don't want to use commas for opening/closing brace/bracket. More... | |
virtual void | write_list_marker ()=0 |
Used for traditional YAML output, writes the "-" marker. More... | |
virtual void | start_raw (bool is_map, bool indent)=0 |
Actual implementation of start_map() and start_list(). More... | |
virtual void | end_raw ()=0 |
Actual implementation of end_map() and end_list(). More... | |
Protected Attributes | |
std::ostream & | out_ |
std::string | indent_str_ |
std::vector< bool > | in_map_ |
std::vector< bool > | first_ |
std::vector< bool > | indent_ |
std::vector< int > | indent_depth_ |
Additional Inherited Members | |
![]() | |
typedef platform::Size | Size |
typedef platform::Size | size_type |
Lightweight class to ease writting YAML documents (http://yaml.org)
YAML is a structured data format, a more human-readable alternative to XML.
I use the terms "map" and "list" here, but you may also see "mapping" and "sequence". The former is a series of key : value pairs, while the latter is a simple series of items. This class is not entirely able to enforce the generation of valid YAML – you can still e.g. write a key : value pair in the middle of a list. It will print a warning message but will otherwise try to continue on.
YAML documents have optional explicit start/end markers; if the emitter supports them, they will be auto-generated when the class is initialized and when you're finished and you call end().
Whitespace is YAML documents is semi-significant. By default, this class will try to pretty-print, showing the depth of nesting through indentation. When starting a new list or map, you can request that it not be indented (i.e., be printed all on one line), but all lists and maps nested inside it will also be printed without linebreaks, regardless of the requested indentation. YAML refers to these two styles as "block" and "flow", respectively.
Definition at line 59 of file Emitter.hh.
|
inline |
Definition at line 63 of file Emitter.hh.
|
inlinevirtual |
Definition at line 72 of file Emitter.hh.
|
protected |
|
protected |
Check that we're in the expected context (either map or list)
Definition at line 129 of file Emitter.hh.
References in_map_, and basic::Warning().
Referenced by end_list(), end_map(), start_list(), start_map(), and write().
|
inline |
Number of closing brackets/braces required to end document (non-negative)
Definition at line 110 of file Emitter.hh.
References in_map_.
Referenced by basic::YamlEmitter::do_indent(), basic::JsonEmitter::do_indent(), and end().
|
protectedpure virtual |
Handle pretty-printing indentation. Don't want to use commas for opening/closing brace/bracket.
Implemented in basic::JsonEmitter, and basic::YamlEmitter.
Referenced by start_list(), start_map(), and write().
void basic::Emitter::end | ( | size_t | desired_depth = 0 | ) |
By default, closes all open maps/lists, ending the document.
Definition at line 67 of file Emitter.cc.
References depth(), and end_raw().
Referenced by basic::YamlEmitter::start_doc(), and basic::JsonEmitter::start_doc().
void basic::Emitter::end_list | ( | ) |
Counterpart to start_list() – writes closing bracket.
Definition at line 57 of file Emitter.cc.
References assert_in(), and end_raw().
void basic::Emitter::end_map | ( | ) |
Counterpart to start_map() – writes closing brace.
Definition at line 62 of file Emitter.cc.
References assert_in(), and end_raw().
|
protectedpure virtual |
Actual implementation of end_map() and end_list().
Implemented in basic::JsonEmitter, and basic::YamlEmitter.
Referenced by end(), end_list(), and end_map().
|
protected |
Converts special characters (newlines, etc) to escape codes (
, etc).
Anything but the most basic characters needs to be quoted and escaped. For normal YAML, very simple text can be output without quotes, though.
needs_quotes_out | will be set to true if string contains "special" characters. |
Definition at line 76 of file Emitter.cc.
References app.pyrosetta_toolkit.modules.SQLPDB::s.
Referenced by basic::YamlEmitter::quote_string(), and basic::JsonEmitter::quote_string().
|
inline |
|
protectedpure virtual |
Quotes strings as much as needed for this format (e.g. always for JSON).
Implemented in basic::JsonEmitter, and basic::YamlEmitter.
Referenced by write_label(), and write_raw().
|
inline |
Number of spaces used for indenting. Default is one.
Definition at line 112 of file Emitter.hh.
References indent_str_, and app.pyrosetta_toolkit.modules.SQLPDB::s.
|
pure virtual |
Start a new document, ending the previous one first if necessary. A no-op for some types of output (e.g. JSON) that don't allow multiple documents.
Implemented in basic::JsonEmitter, and basic::YamlEmitter.
void basic::Emitter::start_list | ( | bool | indent = true | ) |
Write method for use inside lists / arrays.
Definition at line 27 of file Emitter.cc.
References assert_in(), do_indent(), and start_raw().
Referenced by start_list().
|
inline |
Write method for use inside maps / dicts / objects.
Definition at line 93 of file Emitter.hh.
References start_list().
void basic::Emitter::start_list | ( | const char * | label, |
bool | indent = true |
||
) |
Write method for use inside maps / dicts / objects.
Definition at line 41 of file Emitter.cc.
References assert_in(), do_indent(), start_raw(), and write_label().
void basic::Emitter::start_map | ( | bool | indent = true | ) |
Write method for use inside lists / arrays.
Definition at line 34 of file Emitter.cc.
References assert_in(), do_indent(), and start_raw().
Referenced by start_map().
|
inline |
Write method for use inside maps / dicts / objects.
Definition at line 98 of file Emitter.hh.
References start_map().
void basic::Emitter::start_map | ( | const char * | label, |
bool | indent = true |
||
) |
Write method for use inside maps / dicts / objects.
Definition at line 49 of file Emitter.cc.
References assert_in(), do_indent(), start_raw(), and write_label().
Actual implementation of start_map() and start_list().
Implemented in basic::JsonEmitter, and basic::YamlEmitter.
Referenced by start_list(), and start_map().
void basic::Emitter::write | ( | T const & | data | ) |
Write method for use inside lists / arrays.
Definition at line 194 of file Emitter.hh.
References assert_in(), do_indent(), write_list_marker(), and write_raw().
Referenced by app.pyrosetta_toolkit.window_modules.interactive_terminal.interactive_terminal.TkConsoleView::changeLine(), app.pyrosetta_toolkit.window_modules.interactive_terminal.interactive_terminal.TkConsoleView::processTabPress(), app.pyrosetta_toolkit.window_modules.interactive_terminal.interactive_terminal.TkConsoleView::showBanner(), app.pyrosetta_toolkit.window_modules.interactive_terminal.interactive_terminal.TkConsoleView::showPrompt(), and app.pyrosetta_toolkit.window_modules.interactive_terminal.interactive_terminal.TkConsoleView::showReturned().
void basic::Emitter::write | ( | std::string const & | label, |
T const & | data | ||
) |
Write method for use inside maps / dicts / objects.
Definition at line 203 of file Emitter.hh.
References assert_in(), do_indent(), write_label(), and write_raw().
Referenced by app.pyrosetta_toolkit.window_modules.interactive_terminal.interactive_terminal.TkConsoleView::changeLine(), app.pyrosetta_toolkit.window_modules.interactive_terminal.interactive_terminal.TkConsoleView::processTabPress(), app.pyrosetta_toolkit.window_modules.interactive_terminal.interactive_terminal.TkConsoleView::showBanner(), app.pyrosetta_toolkit.window_modules.interactive_terminal.interactive_terminal.TkConsoleView::showPrompt(), and app.pyrosetta_toolkit.window_modules.interactive_terminal.interactive_terminal.TkConsoleView::showReturned().
|
inlineprotected |
Write the key part of a key-value pair.
Definition at line 138 of file Emitter.hh.
References out_, and quote_string().
Referenced by start_list(), start_map(), and write().
|
protectedpure virtual |
Used for traditional YAML output, writes the "-" marker.
Implemented in basic::JsonEmitter, and basic::YamlEmitter.
Referenced by write().
|
inlineprotected |
|
inlineprotected |
Definition at line 145 of file Emitter.hh.
References out_.
|
inlineprotected |
Definition at line 147 of file Emitter.hh.
References out_.
|
inlineprotected |
Definition at line 149 of file Emitter.hh.
References demo.D060_Folding::f, and out_.
|
inlineprotected |
Definition at line 151 of file Emitter.hh.
References out_.
|
inlineprotected |
Definition at line 153 of file Emitter.hh.
References out_, and quote_string().
|
protected |
Definition at line 186 of file Emitter.hh.
Referenced by basic::YamlEmitter::do_indent(), basic::JsonEmitter::do_indent(), basic::YamlEmitter::end_raw(), basic::JsonEmitter::end_raw(), basic::YamlEmitter::start_raw(), and basic::JsonEmitter::start_raw().
|
protected |
Definition at line 185 of file Emitter.hh.
Referenced by assert_in(), depth(), basic::YamlEmitter::end_raw(), basic::JsonEmitter::end_raw(), basic::YamlEmitter::start_raw(), and basic::JsonEmitter::start_raw().
|
protected |
Definition at line 187 of file Emitter.hh.
Referenced by basic::YamlEmitter::do_indent(), basic::JsonEmitter::do_indent(), basic::YamlEmitter::end_raw(), basic::JsonEmitter::end_raw(), basic::YamlEmitter::start_raw(), basic::JsonEmitter::start_raw(), and basic::YamlEmitter::write_list_marker().
|
protected |
Definition at line 188 of file Emitter.hh.
Referenced by basic::YamlEmitter::do_indent(), basic::JsonEmitter::do_indent(), basic::YamlEmitter::end_raw(), basic::JsonEmitter::end_raw(), basic::YamlEmitter::start_raw(), and basic::JsonEmitter::start_raw().
|
protected |
Definition at line 182 of file Emitter.hh.
Referenced by basic::YamlEmitter::do_indent(), basic::JsonEmitter::do_indent(), and set_indent().
|
protected |
Definition at line 181 of file Emitter.hh.
Referenced by basic::YamlEmitter::do_indent(), basic::JsonEmitter::do_indent(), basic::YamlEmitter::end_raw(), basic::JsonEmitter::end_raw(), flush(), basic::YamlEmitter::start_doc(), basic::YamlEmitter::start_raw(), basic::JsonEmitter::start_raw(), write_label(), basic::YamlEmitter::write_list_marker(), and write_raw().