STLplus C++ Library Collection - strings/string_stlplus.hpp - String Formatting of STLplus Containers
Author: Andy Rushton

# STLplus C++ Library Collection

Website Collection

# strings/string_stlplus.hpp
String Formatting of STLplus Containers

- Introduction
- Conversion of STLplus Containers to String
- Printing STLplus Containers

## Introduction

This is a set of functions that perform string formatting and printing of an STLplus container such as digraph by simply defining formatting or printing functions for the elements. They extend the string formatting and printing functions for basic types and STL containers and have similar interfaces.

There are two separate header files for including these functions:

- string_stlplus.hpp: For the type_to_string functions
- print_stlplus.hpp: For the print_type functions

## Conversion of STLplus Containers to String

There is a set of functions which are called type_to_string and which are templates. They give a convenient way of providing string formatting for the most-commonly uses STL container classes. They rely on you writing a type_to_string or print_type function for the type contained within the container.

The functions for formatting most STLplus containers have additional formatting parameters such as separators for multi-element types. The set of functions applicable to the STLplus containers is:

    template<typename T1, typename T2, typename T3, typename S1, typename S2, typename S3>
    std::string stlplus::triple_to_string(const stlplus::triple<T1,T2,T3>& values,
                                          S1 to_string_fn1,
                                          S2 to_string_fn2,
                                          S3 to_string_fn3,
                                          const std::string& separator);

    template<typename T1, typename T2, typename T3, typename T4, typename S1, typename S2, typename S3, typename S4>
    std::string stlplus::foursome_to_string(const stlplus::foursome<T1,T2,T3,T4>& values,
                                            S1 to_string_fn1,
                                            S2 to_string_fn2,
                                            S3 to_string_fn3,
                                            S4 to_string_fn4,
                                            const std::string& separator);

    template<typename T, typename C, typename S>
    std::string stlplus::smart_ptr_to_string(const stlplus::smart_ptr_base<T,C>& value,
                                             S to_string_fn,
                                             const std::string& null_string,
                                             const std::string& prefix,
                                             const std::string& suffix);

    template<typename T, typename C, typename S>
    std::string stlplus::simple_ptr_to_string(const stlplus::simple_ptr_base<T,C>& value,
                                             S to_string_fn,
                                             const std::string& null_string,
                                             const std::string& prefix,
                                             const std::string& suffix);

    template<typename NT, typename AT, typename NS, typename AS>
    std::string stlplus::digraph_to_string(const stlplus::digraph<NT,AT>& values,
                                           NS node_to_string_fn,
                                           AS arc_to_string_fn,
                                           const std::string& separator);

    template<typename K, typename T, typename H, typename E, typename KS, typename TS>
    std::string stlplus::hash_to_string(const stlplus::hash<K,T,H,E>& values,
                                        KS key_to_string_fn,
                                        TS value_to_string_fn,
                                        const std::string& pair_separator,
                                        const std::string& separator);

    template<typename T, typename S>
    std::string stlplus::matrix_to_string(const stlplus::matrix<T>& values,
                                          S to_string_fn,
                                          const std::string& column_separator,
                                          const std::string& row_separator);

    template<typename T, typename S>
    std::string stlplus::ntree_to_string(const stlplus::ntree<T>& values,
                                         S to_string_fn,
                                         const std::string& separator,
                                         const std::string& indent_string);

## Printing Functions

In parallel with the set of string conversion routines, there is a set of print routines for the same set of types. The convention is to have a print_type function that takes an IOStream output device, followed by the same parameters as the corresponding type_to_string function above.

The set of print functions are:

    template<typename T1, typename T2, typename T3, typename S1, typename S2, typename S3>
    void stlplus::print_triple(std::ostream&, const stlplus::triple<T1,T2,T3>& values,
                               S1 print_fn1,
                               S2 print_fn2,
                               S3 print_fn3,
                               const std::string& separator);

    template<typename T1, typename T2, typename T3, typename T4, typename S1, typename S2, typename S3, typename S4>
    void stlplus::print_foursome(std::ostream&, const stlplus::foursome<T1,T2,T3,T4>& values,
                                 S1 print_fn1,
                                 S2 print_fn2,
                                 S3 print_fn3,
                                 S4 print_fn4,
                                 const std::string& separator);

    template<typename T, typename C, typename S>
    void stlplus::print_smart_ptr(std::ostream&, const stlplus::smart_ptr_base<T,C>& value,
                                  S print_fn,
                                  const std::string& null_string,
                                  const std::string& prefix,
                                  const std::string& suffix);

    template<typename T, typename C, typename S>
    void stlplus::print_simple_ptr(std::ostream&, const stlplus::simple_ptr_base<T,C>& value,
                                  S print_fn,
                                  const std::string& null_string,
                                  const std::string& prefix,
                                  const std::string& suffix);

    template<typename NT, typename AT, typename NS, typename AS>
    void stlplus::print_digraph(std::ostream&, const stlplus::digraph<NT,AT>& values,
                                NS node_print_fn,
                                AS arc_print_fn,
                                const std::string& separator);

    template<typename K, typename T, typename H, typename E, typename KS, typename TS>
    void stlplus::print_hash(std::ostream&, const stlplus::hash<K,T,H,E>& values,
                             KS key_print_fn,
                             TS value_print_fn,
                             const std::string& pair_separator,
                             const std::string& separator);

    template<typename T, typename S>
    void stlplus::print_matrix(std::ostream&, const stlplus::matrix<T>& values,
                               S print_fn,
                               const std::string& column_separator,
                               const std::string& row_separator);

    template<typename T, typename S>
    void stlplus::print_ntree(std::ostream&, const stlplus::ntree<T>& values,
                              S print_fn,
                              const std::string& separator,
                              const std::string& indent_string);