Rosetta
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
basic::Emitter Class Referenceabstract

Lightweight class to ease writting YAML documents (http://yaml.org) More...

#include <Emitter.hh>

Inheritance diagram for basic::Emitter:
Inheritance graph
[legend]

Public Member Functions

 Emitter (std::ostream &out)
 
 ~Emitter () override
 
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...
 
- Public Member Functions inherited from utility::VirtualBase
 VirtualBase ()=default
 Default constructor. More...
 
virtual ~VirtualBase ()=default
 The virtual destructor is one of the main reasons for the VirtualBase class. More...
 
 VirtualBase (VirtualBase const &)=default
 
 VirtualBase (VirtualBase &&)=default
 
VirtualBaseoperator= (VirtualBase const &)=default
 
VirtualBaseoperator= (VirtualBase &&)=default
 

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_
 

Detailed Description

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.

Constructor & Destructor Documentation

◆ Emitter() [1/3]

basic::Emitter::Emitter ( std::ostream &  out)
inline

◆ ~Emitter()

basic::Emitter::~Emitter ( )
inlineoverride

◆ Emitter() [2/3]

basic::Emitter::Emitter ( )
protected

◆ Emitter() [3/3]

basic::Emitter::Emitter ( Emitter const &  )
protected

Member Function Documentation

◆ assert_in()

bool basic::Emitter::assert_in ( bool  in_map,
std::string const &  msg 
)
inlineprotected

Check that we're in the expected context (either map or list)

References in_map_, and basic::Warning().

Referenced by end_list(), end_map(), start_list(), start_map(), and write().

◆ depth()

int basic::Emitter::depth ( ) const
inline

Number of closing brackets/braces required to end document (non-negative)

References in_map_.

Referenced by basic::YamlEmitter::do_indent(), basic::JsonEmitter::do_indent(), and end().

◆ do_indent()

virtual void basic::Emitter::do_indent ( bool  write_comma = true)
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().

◆ end()

void basic::Emitter::end ( size_t  desired_depth = 0)

By default, closes all open maps/lists, ending the document.

References depth(), end_raw(), and create_a3b_hbs::i.

Referenced by basic::YamlEmitter::start_doc(), and basic::JsonEmitter::start_doc().

◆ end_list()

void basic::Emitter::end_list ( )

Counterpart to start_list() – writes closing bracket.

References assert_in(), and end_raw().

◆ end_map()

void basic::Emitter::end_map ( )

Counterpart to start_map() – writes closing brace.

References assert_in(), and end_raw().

◆ end_raw()

virtual void basic::Emitter::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().

◆ escape_string()

std::string basic::Emitter::escape_string ( std::string const &  s,
bool &  needs_quotes_out 
)
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.

Parameters
needs_quotes_outwill be set to true if string contains "special" characters.

References basic::options::OptionKeys::out::file::o, and docking::s.

Referenced by basic::YamlEmitter::quote_string(), and basic::JsonEmitter::quote_string().

◆ flush()

void basic::Emitter::flush ( )
inline

Flush the underlying output stream.

References out_.

◆ quote_string()

virtual std::string basic::Emitter::quote_string ( std::string const &  s)
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().

◆ set_indent()

void basic::Emitter::set_indent ( int  num_spaces)
inline

Number of spaces used for indenting. Default is one.

References create_a3b_hbs::i, indent_str_, and docking::s.

◆ start_doc()

virtual void basic::Emitter::start_doc ( )
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.

◆ start_list() [1/3]

void basic::Emitter::start_list ( bool  indent = true)

Write method for use inside lists / arrays.

References assert_in(), do_indent(), binder::indent(), and start_raw().

Referenced by start_list().

◆ start_list() [2/3]

void basic::Emitter::start_list ( const char *  label,
bool  indent = true 
)

Write method for use inside maps / dicts / objects.

References assert_in(), do_indent(), binder::indent(), predPRE::label, start_raw(), and write_label().

◆ start_list() [3/3]

void basic::Emitter::start_list ( std::string const &  label,
bool  indent = true 
)
inline

Write method for use inside maps / dicts / objects.

References binder::indent(), predPRE::label, and start_list().

◆ start_map() [1/3]

void basic::Emitter::start_map ( bool  indent = true)

Write method for use inside lists / arrays.

References assert_in(), do_indent(), binder::indent(), and start_raw().

Referenced by start_map().

◆ start_map() [2/3]

void basic::Emitter::start_map ( const char *  label,
bool  indent = true 
)

Write method for use inside maps / dicts / objects.

References assert_in(), do_indent(), binder::indent(), predPRE::label, start_raw(), and write_label().

◆ start_map() [3/3]

void basic::Emitter::start_map ( std::string const &  label,
bool  indent = true 
)
inline

Write method for use inside maps / dicts / objects.

References binder::indent(), predPRE::label, and start_map().

◆ start_raw()

virtual void basic::Emitter::start_raw ( bool  is_map,
bool  indent 
)
protectedpure virtual

Actual implementation of start_map() and start_list().

Implemented in basic::JsonEmitter, and basic::YamlEmitter.

Referenced by start_list(), and start_map().

◆ write() [1/2]

template<typename T >
void basic::Emitter::write ( std::string const &  label,
T const &  data 
)

Write method for use inside maps / dicts / objects.

References assert_in(), data, do_indent(), predPRE::label, write_label(), and write_raw().

◆ write() [2/2]

template<typename T >
void basic::Emitter::write ( T const &  data)

Write method for use inside lists / arrays.

References assert_in(), data, do_indent(), write_list_marker(), and write_raw().

◆ write_label()

void basic::Emitter::write_label ( std::string const &  label)
inlineprotected

Write the key part of a key-value pair.

References predPRE::label, out_, and quote_string().

Referenced by start_list(), start_map(), and write().

◆ write_list_marker()

virtual void basic::Emitter::write_list_marker ( )
protectedpure virtual

Used for traditional YAML output, writes the "-" marker.

Implemented in basic::JsonEmitter, and basic::YamlEmitter.

Referenced by write().

◆ write_raw() [1/6]

void basic::Emitter::write_raw ( bool  b)
inlineprotected

References compute_difference::b, and out_.

Referenced by write().

◆ write_raw() [2/6]

void basic::Emitter::write_raw ( double  d)
inlineprotected

References out_.

◆ write_raw() [3/6]

void basic::Emitter::write_raw ( float  f)
inlineprotected

References create_a3b_hbs::f, and out_.

◆ write_raw() [4/6]

void basic::Emitter::write_raw ( int  i)
inlineprotected

References create_a3b_hbs::i, and out_.

◆ write_raw() [5/6]

void basic::Emitter::write_raw ( long  l)
inlineprotected

◆ write_raw() [6/6]

void basic::Emitter::write_raw ( std::string const &  s)
inlineprotected

References out_, quote_string(), and docking::s.

Member Data Documentation

◆ first_

std::vector<bool> basic::Emitter::first_
protected

◆ in_map_

std::vector<bool> basic::Emitter::in_map_
protected

◆ indent_

std::vector<bool> basic::Emitter::indent_
protected

◆ indent_depth_

std::vector<int> basic::Emitter::indent_depth_
protected

◆ indent_str_

std::string basic::Emitter::indent_str_
protected

◆ out_

std::ostream& basic::Emitter::out_
protected

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