| 1 | /* \file toString.hpp
|
|---|
| 2 | *
|
|---|
| 3 | * contains template functions that allow for casting various types to strings.
|
|---|
| 4 | *
|
|---|
| 5 | * Created on: Nov 14, 2010
|
|---|
| 6 | * Author: heber
|
|---|
| 7 | */
|
|---|
| 8 |
|
|---|
| 9 | #ifndef TOSTRING_HPP_
|
|---|
| 10 | #define TOSTRING_HPP_
|
|---|
| 11 |
|
|---|
| 12 | // include config.h
|
|---|
| 13 | #ifdef HAVE_CONFIG_H
|
|---|
| 14 | #include <config.h>
|
|---|
| 15 | #endif
|
|---|
| 16 |
|
|---|
| 17 | #include <iostream>
|
|---|
| 18 | #include <string>
|
|---|
| 19 | #include <sstream>
|
|---|
| 20 |
|
|---|
| 21 | #include <list>
|
|---|
| 22 | #include <map>
|
|---|
| 23 | #include <set>
|
|---|
| 24 | #include <vector>
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 | /** Operator for output to std::ostream operator of an std::list of arbitrary
|
|---|
| 28 | * type.
|
|---|
| 29 | * @param ost output stream
|
|---|
| 30 | * @param vector list of types to output
|
|---|
| 31 | * @return ost output stream
|
|---|
| 32 | */
|
|---|
| 33 | template <class T> std::ostream & operator<<(std::ostream &ost, const std::list< T > &_vector)
|
|---|
| 34 | {
|
|---|
| 35 | ost << "( ";
|
|---|
| 36 | for (typename std::list< T >::const_iterator iter = _vector.begin();
|
|---|
| 37 | iter != _vector.end();
|
|---|
| 38 | ++iter)
|
|---|
| 39 | ost << *iter << "; ";
|
|---|
| 40 | ost << ")";
|
|---|
| 41 | return ost;
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 | /** Operator for output to std::ostream operator of an std::map of arbitrary
|
|---|
| 46 | * type.
|
|---|
| 47 | * @param ost output stream
|
|---|
| 48 | * @param vector map of types to output
|
|---|
| 49 | * @return ost output stream
|
|---|
| 50 | */
|
|---|
| 51 | template <class T, class S> std::ostream & operator<<(std::ostream &ost, const std::map< T, S > &_vector)
|
|---|
| 52 | {
|
|---|
| 53 | ost << "{ ";
|
|---|
| 54 | for (typename std::map< T, S >::const_iterator iter = _vector.begin();
|
|---|
| 55 | iter != _vector.end();
|
|---|
| 56 | ++iter)
|
|---|
| 57 | ost << "(" << iter->first << ")=>(" << iter->second << "); ";
|
|---|
| 58 | ost << "}";
|
|---|
| 59 | return ost;
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 |
|
|---|
| 63 | /** Operator for output to std::ostream operator of an std::multimap of arbitrary
|
|---|
| 64 | * type.
|
|---|
| 65 | * @param ost output stream
|
|---|
| 66 | * @param multimap map of types to output
|
|---|
| 67 | * @return ost output stream
|
|---|
| 68 | */
|
|---|
| 69 | template <class T, class S> std::ostream & operator<<(std::ostream &ost, const std::multimap< T, S > &_vector)
|
|---|
| 70 | {
|
|---|
| 71 | ost << "{ ";
|
|---|
| 72 | for (typename std::multimap< T, S >::const_iterator iter = _vector.begin();
|
|---|
| 73 | iter != _vector.end();
|
|---|
| 74 | ++iter)
|
|---|
| 75 | ost << "(" << iter->first << ")=>(" << iter->second << "); ";
|
|---|
| 76 | ost << "}";
|
|---|
| 77 | return ost;
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 |
|
|---|
| 81 | /** Operator for output to std::ostream operator of an std::set of arbitrary
|
|---|
| 82 | * type.
|
|---|
| 83 | * @param ost output stream
|
|---|
| 84 | * @param set list of types to output
|
|---|
| 85 | * @return ost output stream
|
|---|
| 86 | */
|
|---|
| 87 | template <class T> std::ostream & operator<<(std::ostream &ost, const std::set< T > &_vector)
|
|---|
| 88 | {
|
|---|
| 89 | ost << "( ";
|
|---|
| 90 | for (typename std::set< T >::const_iterator iter = _vector.begin();
|
|---|
| 91 | iter != _vector.end();
|
|---|
| 92 | ++iter)
|
|---|
| 93 | ost << *iter << "; ";
|
|---|
| 94 | ost << ")";
|
|---|
| 95 | return ost;
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 | /** Operator for output to std::ostream operator of an std::vector of arbitrary
|
|---|
| 100 | * type.
|
|---|
| 101 | * @param ost output stream
|
|---|
| 102 | * @param vector vector of types to output
|
|---|
| 103 | * @return ost output stream
|
|---|
| 104 | */
|
|---|
| 105 | template <class T> std::ostream & operator<<(std::ostream &ost, const std::vector< T > &_vector)
|
|---|
| 106 | {
|
|---|
| 107 | ost << "( ";
|
|---|
| 108 | for (typename std::vector< T >::const_iterator iter = _vector.begin();
|
|---|
| 109 | iter != _vector.end();
|
|---|
| 110 | ++iter)
|
|---|
| 111 | ost << *iter << "; ";
|
|---|
| 112 | ost << ")";
|
|---|
| 113 | return ost;
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | /** Converter for any class to string.
|
|---|
| 117 | * We use default conversion via stringstream as suggested by [Stroustrup].
|
|---|
| 118 | * \param _&_object reference to object to convert to string.
|
|---|
| 119 | * \return string converted \a _object
|
|---|
| 120 | */
|
|---|
| 121 | template <class T> std::string toString(T const &_object)
|
|---|
| 122 | {
|
|---|
| 123 | std::stringstream s;
|
|---|
| 124 | s << _object;
|
|---|
| 125 | return s.str();
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | /** Converter for a string to any class
|
|---|
| 129 | * We use default conversion via stringstream as suggested by [Stroustrup].
|
|---|
| 130 | * \param _&_object reference to object to convert.
|
|---|
| 131 | * \return converted \a _object of templated type
|
|---|
| 132 | */
|
|---|
| 133 | template <class T> struct ConvertTo {
|
|---|
| 134 | T operator()(const std::string &_object) {
|
|---|
| 135 | std::stringstream s;
|
|---|
| 136 | T object;
|
|---|
| 137 | s << _object;
|
|---|
| 138 | s >> object;
|
|---|
| 139 | return object;
|
|---|
| 140 | }
|
|---|
| 141 | };
|
|---|
| 142 |
|
|---|
| 143 | #endif /* TOSTRING_HPP_ */
|
|---|