MAIA bb96820c
Multiphysics at AIA
Loading...
Searching...
No Matches
cpptoml::toml_writer Class Reference

#include <cpptoml.h>

Collaboration diagram for cpptoml::toml_writer:
[legend]

Public Member Functions

 toml_writer (std::ostream &s, std::string indent_space="\t")
 
template<class T >
void visit (const value< T > &v, MBool=false)
 
void visit (const table &t, MBool in_array=false)
 
void visit (const array &a, MBool=false)
 
void visit (const table_array &t, MBool=false)
 

Static Public Member Functions

static std::string escape_string (const std::string &str)
 

Protected Member Functions

void write (const value< std::string > &v)
 
void write (const value< double > &v)
 
template<class T >
std::enable_if< is_one_of< T, int64_t, local_date, local_time, local_datetime, offset_datetime >::value >::type write (const value< T > &v)
 
void write (const value< MBool > &v)
 
void write_table_header (MBool in_array=false)
 
void write_table_item_header (const base &b)
 

Private Member Functions

void indent ()
 
template<class T >
void write (const T &v)
 
void endline ()
 

Private Attributes

std::ostream & stream_
 
const std::string indent_
 
std::vector< std::string > path_
 
MBool has_naked_endline_
 

Detailed Description

Writer that can be passed to accept() functions of cpptoml objects and will output valid TOML to a stream.

Definition at line 2569 of file cpptoml.h.

Constructor & Destructor Documentation

◆ toml_writer()

cpptoml::toml_writer::toml_writer ( std::ostream &  s,
std::string  indent_space = "\t" 
)
inlineexplicit

Construct a toml_writer that will write to the given stream

Definition at line 2574 of file cpptoml.h.

2575 : stream_(s), indent_(std::move(indent_space)), has_naked_endline_(false) {
2576 // nothing
2577 }
MBool has_naked_endline_
Definition: cpptoml.h:2822
const std::string indent_
Definition: cpptoml.h:2820
std::ostream & stream_
Definition: cpptoml.h:2819

Member Function Documentation

◆ endline()

void cpptoml::toml_writer::endline ( )
inlineprivate

Write an endline out to the stream

Definition at line 2811 of file cpptoml.h.

2811 {
2812 if(!has_naked_endline_) {
2813 stream_ << "\n";
2814 has_naked_endline_ = true;
2815 }
2816 }

◆ escape_string()

static std::string cpptoml::toml_writer::escape_string ( const std::string &  str)
inlinestatic

Escape a string for output.

Definition at line 2662 of file cpptoml.h.

2662 {
2663 std::string res;
2664 for(char it : str) {
2665 if(it == '\b') {
2666 res += "\\b";
2667 } else if(it == '\t') {
2668 res += "\\t";
2669 } else if(it == '\n') {
2670 res += "\\n";
2671 } else if(it == '\f') {
2672 res += "\\f";
2673 } else if(it == '\r') {
2674 res += "\\r";
2675 } else if(it == '"') {
2676 res += "\\\"";
2677 } else if(it == '\\') {
2678 res += "\\\\";
2679 } else if((uint32_t)it <= 0x001f) {
2680 res += "\\u";
2681 std::stringstream ss;
2682 ss << std::hex << static_cast<uint32_t>(it);
2683 res += ss.str();
2684 } else {
2685 res += it;
2686 }
2687 }
2688 return res;
2689 }

◆ indent()

void cpptoml::toml_writer::indent ( )
inlineprivate

Indent the proper number of tabs given the size of the path.

Definition at line 2794 of file cpptoml.h.

2794 {
2795 for(std::size_t i = 1; i < path_.size(); ++i)
2796 write(indent_);
2797 }
std::vector< std::string > path_
Definition: cpptoml.h:2821
void write(const value< std::string > &v)
Definition: cpptoml.h:2695

◆ visit() [1/4]

void cpptoml::toml_writer::visit ( const array a,
MBool  = false 
)
inline

Output an array element of the TOML tree

Definition at line 2630 of file cpptoml.h.

2630 {
2631 write("[");
2632
2633 for(MUint i = 0; i < a.get().size(); ++i) {
2634 if(i > 0) write(", ");
2635
2636 if(a.get()[i]->is_array()) {
2637 a.get()[i]->as_array()->accept(*this, true);
2638 } else {
2639 a.get()[i]->accept(*this, true);
2640 }
2641 }
2642
2643 write("]");
2644 }
uint32_t MUint
Definition: maiatypes.h:63
Definition: contexttypes.h:19

◆ visit() [2/4]

void cpptoml::toml_writer::visit ( const table t,
MBool  in_array = false 
)
inline

Output a table element of the TOML tree

Definition at line 2591 of file cpptoml.h.

2591 {
2592 write_table_header(in_array);
2593 std::vector<std::string> values;
2594 std::vector<std::string> tables;
2595
2596 for(const auto& i : t) {
2597 if(i.second->is_table() || i.second->is_table_array()) {
2598 tables.push_back(i.first);
2599 } else {
2600 values.push_back(i.first);
2601 }
2602 }
2603
2604 for(MUint i = 0; i < values.size(); ++i) {
2605 path_.push_back(values[i]);
2606
2607 if(i > 0) endline();
2608
2609 write_table_item_header(*t.get(values[i]));
2610 t.get(values[i])->accept(*this, false);
2611 path_.pop_back();
2612 }
2613
2614 for(MUint i = 0; i < tables.size(); ++i) {
2615 path_.push_back(tables[i]);
2616
2617 if(!values.empty() || i > 0) endline();
2618
2619 write_table_item_header(*t.get(tables[i]));
2620 t.get(tables[i])->accept(*this, false);
2621 path_.pop_back();
2622 }
2623
2624 endline();
2625 }
void write_table_item_header(const base &b)
Definition: cpptoml.h:2770
void write_table_header(MBool in_array=false)
Definition: cpptoml.h:2731

◆ visit() [3/4]

void cpptoml::toml_writer::visit ( const table_array t,
MBool  = false 
)
inline

Output a table_array element of the TOML tree

Definition at line 2649 of file cpptoml.h.

2649 {
2650 for(MUint j = 0; j < t.get().size(); ++j) {
2651 if(j > 0) endline();
2652
2653 t.get()[j]->accept(*this, true);
2654 }
2655
2656 endline();
2657 }

◆ visit() [4/4]

template<class T >
void cpptoml::toml_writer::visit ( const value< T > &  v,
MBool  = false 
)
inline

Output a base value of the TOML tree.

Definition at line 2584 of file cpptoml.h.

2584 {
2585 write(v);
2586 }

◆ write() [1/5]

template<class T >
void cpptoml::toml_writer::write ( const T &  v)
inlineprivate

Write a value out to the stream.

Definition at line 2803 of file cpptoml.h.

2803 {
2804 stream_ << v;
2805 has_naked_endline_ = false;
2806 }

◆ write() [2/5]

void cpptoml::toml_writer::write ( const value< double > &  v)
inlineprotected

Write out a double.

Definition at line 2704 of file cpptoml.h.

2704 {
2705 std::ios::fmtflags flags{stream_.flags()};
2706
2707 stream_ << std::showpoint;
2708 write(v.get());
2709
2710 stream_.flags(flags);
2711 }

◆ write() [3/5]

void cpptoml::toml_writer::write ( const value< MBool > &  v)
inlineprotected

Write out a boolean.

Definition at line 2726 of file cpptoml.h.

2726{ write((v.get() ? "true" : "false")); }

◆ write() [4/5]

void cpptoml::toml_writer::write ( const value< std::string > &  v)
inlineprotected

Write out a string.

Definition at line 2695 of file cpptoml.h.

2695 {
2696 write("\"");
2697 write(escape_string(v.get()));
2698 write("\"");
2699 }
static std::string escape_string(const std::string &str)
Definition: cpptoml.h:2662

◆ write() [5/5]

template<class T >
std::enable_if< is_one_of< T, int64_t, local_date, local_time, local_datetime, offset_datetime >::value >::type cpptoml::toml_writer::write ( const value< T > &  v)
inlineprotected

Write out an integer, local_date, local_time, local_datetime, or offset_datetime.

Definition at line 2719 of file cpptoml.h.

2719 {
2720 write(v.get());
2721 }

◆ write_table_header()

void cpptoml::toml_writer::write_table_header ( MBool  in_array = false)
inlineprotected

Write out the header of a table.

Definition at line 2731 of file cpptoml.h.

2731 {
2732 if(!path_.empty()) {
2733 indent();
2734
2735 write("[");
2736
2737 if(in_array) {
2738 write("[");
2739 }
2740
2741 for(MUint i = 0; i < path_.size(); ++i) {
2742 if(i > 0) {
2743 write(".");
2744 }
2745
2746 if(path_[i].find_first_not_of("ABCDEFGHIJKLMNOPQRSTUVWXYZabcde"
2747 "fghijklmnopqrstuvwxyz0123456789"
2748 "_-")
2749 == std::string::npos) {
2750 write(path_[i]);
2751 } else {
2752 write("\"");
2754 write("\"");
2755 }
2756 }
2757
2758 if(in_array) {
2759 write("]");
2760 }
2761
2762 write("]");
2763 endline();
2764 }
2765 }

◆ write_table_item_header()

void cpptoml::toml_writer::write_table_item_header ( const base b)
inlineprotected

Write out the identifier for an item in a table.

Definition at line 2770 of file cpptoml.h.

2770 {
2771 if(!b.is_table() && !b.is_table_array()) {
2772 indent();
2773
2774 if(path_.back().find_first_not_of("ABCDEFGHIJKLMNOPQRSTUVWXYZabcde"
2775 "fghijklmnopqrstuvwxyz0123456789"
2776 "_-")
2777 == std::string::npos) {
2778 write(path_.back());
2779 } else {
2780 write("\"");
2781 write(escape_string(path_.back()));
2782 write("\"");
2783 }
2784
2785 write(" = ");
2786 }
2787 }

Member Data Documentation

◆ has_naked_endline_

MBool cpptoml::toml_writer::has_naked_endline_
private

Definition at line 2822 of file cpptoml.h.

◆ indent_

const std::string cpptoml::toml_writer::indent_
private

Definition at line 2820 of file cpptoml.h.

◆ path_

std::vector<std::string> cpptoml::toml_writer::path_
private

Definition at line 2821 of file cpptoml.h.

◆ stream_

std::ostream& cpptoml::toml_writer::stream_
private

Definition at line 2819 of file cpptoml.h.


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