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