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