| [a80f419] | 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 |  | 
|---|
| [70672e3] | 12 | // include config.h | 
|---|
|  | 13 | #ifdef HAVE_CONFIG_H | 
|---|
|  | 14 | #include <config.h> | 
|---|
|  | 15 | #endif | 
|---|
|  | 16 |  | 
|---|
| [a80f419] | 17 | #include <iostream> | 
|---|
|  | 18 | #include <string> | 
|---|
|  | 19 | #include <sstream> | 
|---|
|  | 20 |  | 
|---|
| [d3f9ad] | 21 | #include <deque> | 
|---|
| [a80f419] | 22 | #include <list> | 
|---|
|  | 23 | #include <map> | 
|---|
| [d3f9ad] | 24 | #include <queue> | 
|---|
| [a80f419] | 25 | #include <set> | 
|---|
|  | 26 | #include <vector> | 
|---|
|  | 27 |  | 
|---|
|  | 28 |  | 
|---|
| [d3f9ad] | 29 | /** Operator for output to std::ostream operator of an std::deque of arbitrary | 
|---|
|  | 30 | * type. | 
|---|
|  | 31 | * @param ost output stream | 
|---|
|  | 32 | * @param _deque deque of types to output | 
|---|
|  | 33 | * @return ost output stream | 
|---|
|  | 34 | */ | 
|---|
|  | 35 | template <class T, class alloc> std::ostream & operator<<(std::ostream &ost, const std::deque< T, alloc > &_deque) | 
|---|
|  | 36 | { | 
|---|
|  | 37 | ost << "( "; | 
|---|
|  | 38 | for (typename std::deque< T, alloc >::const_iterator iter = _deque.begin(); | 
|---|
|  | 39 | iter != _deque.end(); | 
|---|
|  | 40 | ++iter) | 
|---|
|  | 41 | ost << *iter << "; "; | 
|---|
|  | 42 | ost << ")"; | 
|---|
|  | 43 | return ost; | 
|---|
|  | 44 | } | 
|---|
|  | 45 |  | 
|---|
|  | 46 |  | 
|---|
| [a80f419] | 47 | /** Operator for output to std::ostream operator of an std::list of arbitrary | 
|---|
|  | 48 | * type. | 
|---|
|  | 49 | * @param ost output stream | 
|---|
| [d3f9ad] | 50 | * @param _list list of types to output | 
|---|
| [a80f419] | 51 | * @return ost output stream | 
|---|
|  | 52 | */ | 
|---|
| [d3f9ad] | 53 | template <class T, class alloc> std::ostream & operator<<(std::ostream &ost, const std::list< T, alloc > &_list) | 
|---|
| [a80f419] | 54 | { | 
|---|
|  | 55 | ost << "( "; | 
|---|
| [d3f9ad] | 56 | for (typename std::list< T, alloc >::const_iterator iter = _list.begin(); | 
|---|
|  | 57 | iter != _list.end(); | 
|---|
| [a80f419] | 58 | ++iter) | 
|---|
|  | 59 | ost << *iter << "; "; | 
|---|
|  | 60 | ost << ")"; | 
|---|
|  | 61 | return ost; | 
|---|
|  | 62 | } | 
|---|
|  | 63 |  | 
|---|
|  | 64 |  | 
|---|
|  | 65 | /** Operator for output to std::ostream operator of an std::map of arbitrary | 
|---|
|  | 66 | * type. | 
|---|
|  | 67 | * @param ost output stream | 
|---|
|  | 68 | * @param vector map of types to output | 
|---|
|  | 69 | * @return ost output stream | 
|---|
|  | 70 | */ | 
|---|
| [d3f9ad] | 71 | template <class T, class S, class comp, class alloc> std::ostream & operator<<(std::ostream &ost, const std::map< T, S, comp, alloc > &_map) | 
|---|
| [a80f419] | 72 | { | 
|---|
|  | 73 | ost << "{ "; | 
|---|
| [d3f9ad] | 74 | for (typename std::map< T, S, comp, alloc >::const_iterator iter = _map.begin(); | 
|---|
|  | 75 | iter != _map.end(); | 
|---|
| [a80f419] | 76 | ++iter) | 
|---|
|  | 77 | ost << "(" << iter->first << ")=>(" << iter->second << "); "; | 
|---|
|  | 78 | ost << "}"; | 
|---|
|  | 79 | return ost; | 
|---|
|  | 80 | } | 
|---|
|  | 81 |  | 
|---|
|  | 82 | /** Operator for output to std::ostream operator of an std::multimap of arbitrary | 
|---|
|  | 83 | * type. | 
|---|
|  | 84 | * @param ost output stream | 
|---|
|  | 85 | * @param multimap map of types to output | 
|---|
|  | 86 | * @return ost output stream | 
|---|
|  | 87 | */ | 
|---|
| [d3f9ad] | 88 | template <class T, class S, class comp, class alloc> std::ostream & operator<<(std::ostream &ost, const std::multimap< T, S, comp, alloc > &_multimap) | 
|---|
| [a80f419] | 89 | { | 
|---|
|  | 90 | ost << "{ "; | 
|---|
| [d3f9ad] | 91 | for (typename std::multimap< T, S, comp, alloc >::const_iterator iter = _multimap.begin(); | 
|---|
|  | 92 | iter != _multimap.end(); | 
|---|
| [a80f419] | 93 | ++iter) | 
|---|
|  | 94 | ost << "(" << iter->first << ")=>(" << iter->second << "); "; | 
|---|
|  | 95 | ost << "}"; | 
|---|
|  | 96 | return ost; | 
|---|
|  | 97 | } | 
|---|
|  | 98 |  | 
|---|
| [d3f9ad] | 99 | /** Operator for output to std::ostream operator of an std::multiset of arbitrary | 
|---|
|  | 100 | * type. | 
|---|
|  | 101 | * @param ost output stream | 
|---|
|  | 102 | * @param multiset list of types to output | 
|---|
|  | 103 | * @return ost output stream | 
|---|
|  | 104 | */ | 
|---|
|  | 105 | template <class T, class comp, class alloc> std::ostream & operator<<(std::ostream &ost, const std::multiset< T, comp, alloc > &_multiset) | 
|---|
|  | 106 | { | 
|---|
|  | 107 | ost << "( "; | 
|---|
|  | 108 | for (typename std::multiset< T, comp, alloc >::const_iterator iter = _multiset.begin(); | 
|---|
|  | 109 | iter != _multiset.end(); | 
|---|
|  | 110 | ++iter) | 
|---|
|  | 111 | ost << *iter << "; "; | 
|---|
|  | 112 | ost << ")"; | 
|---|
|  | 113 | return ost; | 
|---|
|  | 114 | } | 
|---|
|  | 115 |  | 
|---|
|  | 116 | /** Operator for output to std::ostream operator of an std::pair of arbitrary | 
|---|
|  | 117 | * type. | 
|---|
|  | 118 | * @param ost output stream | 
|---|
|  | 119 | * @param _pair pair of types to output | 
|---|
|  | 120 | * @return ost output stream | 
|---|
|  | 121 | */ | 
|---|
|  | 122 | template <class T, class S> std::ostream & operator<<(std::ostream &ost, const std::pair< T, S > &_pair) | 
|---|
|  | 123 | { | 
|---|
|  | 124 | ost << "[" << _pair.first << "," << _pair.second << "]"; | 
|---|
|  | 125 | return ost; | 
|---|
|  | 126 | } | 
|---|
|  | 127 |  | 
|---|
|  | 128 | /** Operator for output to std::ostream operator of an std::priority_queue of arbitrary | 
|---|
|  | 129 | * type. | 
|---|
|  | 130 | * @param ost output stream | 
|---|
|  | 131 | * @param _priority_queue queue of types to output | 
|---|
|  | 132 | * @return ost output stream | 
|---|
|  | 133 | */ | 
|---|
|  | 134 | template <class T, class alloc> std::ostream & operator<<(std::ostream &ost, const std::priority_queue< T, alloc > &_priority_queue) | 
|---|
|  | 135 | { | 
|---|
|  | 136 | ost << "( "; | 
|---|
|  | 137 | for (typename std::priority_queue< T, alloc >::const_iterator iter = _priority_queue.begin(); | 
|---|
|  | 138 | iter != _priority_queue.end(); | 
|---|
|  | 139 | ++iter) | 
|---|
|  | 140 | ost << *iter << "; "; | 
|---|
|  | 141 | ost << ")"; | 
|---|
|  | 142 | return ost; | 
|---|
|  | 143 | } | 
|---|
|  | 144 |  | 
|---|
|  | 145 | /** Operator for output to std::ostream operator of an std::queue of arbitrary | 
|---|
|  | 146 | * type. | 
|---|
|  | 147 | * @param ost output stream | 
|---|
|  | 148 | * @param _queue queue of types to output | 
|---|
|  | 149 | * @return ost output stream | 
|---|
|  | 150 | */ | 
|---|
|  | 151 | template <class T, class alloc> std::ostream & operator<<(std::ostream &ost, const std::queue< T, alloc > &_queue) | 
|---|
|  | 152 | { | 
|---|
|  | 153 | ost << "( "; | 
|---|
|  | 154 | for (typename std::queue< T, alloc >::const_iterator iter = _queue.begin(); | 
|---|
|  | 155 | iter != _queue.end(); | 
|---|
|  | 156 | ++iter) | 
|---|
|  | 157 | ost << *iter << "; "; | 
|---|
|  | 158 | ost << ")"; | 
|---|
|  | 159 | return ost; | 
|---|
|  | 160 | } | 
|---|
|  | 161 |  | 
|---|
| [a80f419] | 162 |  | 
|---|
|  | 163 | /** Operator for output to std::ostream operator of an std::set of arbitrary | 
|---|
|  | 164 | * type. | 
|---|
|  | 165 | * @param ost output stream | 
|---|
|  | 166 | * @param set list of types to output | 
|---|
|  | 167 | * @return ost output stream | 
|---|
|  | 168 | */ | 
|---|
| [d3f9ad] | 169 | template <class T, class comp, class alloc> std::ostream & operator<<(std::ostream &ost, const std::set< T, comp, alloc > &_set) | 
|---|
| [a80f419] | 170 | { | 
|---|
|  | 171 | ost << "( "; | 
|---|
| [d3f9ad] | 172 | for (typename std::set< T, comp, alloc >::const_iterator iter = _set.begin(); | 
|---|
|  | 173 | iter != _set.end(); | 
|---|
| [a80f419] | 174 | ++iter) | 
|---|
|  | 175 | ost << *iter << "; "; | 
|---|
|  | 176 | ost << ")"; | 
|---|
|  | 177 | return ost; | 
|---|
|  | 178 | } | 
|---|
|  | 179 |  | 
|---|
|  | 180 | /** Operator for output to std::ostream operator of an std::vector of arbitrary | 
|---|
|  | 181 | * type. | 
|---|
|  | 182 | * @param ost output stream | 
|---|
|  | 183 | * @param vector vector of types to output | 
|---|
|  | 184 | * @return ost output stream | 
|---|
|  | 185 | */ | 
|---|
| [d3f9ad] | 186 | template <class T, class alloc> std::ostream & operator<<(std::ostream &ost, const std::vector< T, alloc > &_vector) | 
|---|
| [a80f419] | 187 | { | 
|---|
|  | 188 | ost << "( "; | 
|---|
| [d3f9ad] | 189 | for (typename std::vector< T, alloc >::const_iterator iter = _vector.begin(); | 
|---|
| [a80f419] | 190 | iter != _vector.end(); | 
|---|
|  | 191 | ++iter) | 
|---|
|  | 192 | ost << *iter << "; "; | 
|---|
|  | 193 | ost << ")"; | 
|---|
|  | 194 | return ost; | 
|---|
|  | 195 | } | 
|---|
|  | 196 |  | 
|---|
| [73e948] | 197 | /** Converter for any class to string. | 
|---|
|  | 198 | * We use default conversion via stringstream as suggested by [Stroustrup]. | 
|---|
|  | 199 | * \param _&_object reference to object to convert to string. | 
|---|
|  | 200 | * \return string converted \a _object | 
|---|
|  | 201 | */ | 
|---|
|  | 202 | template <class T> std::string toString(T const &_object) | 
|---|
|  | 203 | { | 
|---|
|  | 204 | std::stringstream s; | 
|---|
|  | 205 | s << _object; | 
|---|
|  | 206 | return s.str(); | 
|---|
|  | 207 | } | 
|---|
|  | 208 |  | 
|---|
|  | 209 | /** Converter for a string to any class | 
|---|
|  | 210 | * We use default conversion via stringstream as suggested by [Stroustrup]. | 
|---|
|  | 211 | * \param _&_object reference to object to convert. | 
|---|
|  | 212 | * \return converted \a _object of templated type | 
|---|
|  | 213 | */ | 
|---|
|  | 214 | template <class T> struct ConvertTo { | 
|---|
|  | 215 | T operator()(const std::string &_object) { | 
|---|
|  | 216 | std::stringstream s; | 
|---|
|  | 217 | T object; | 
|---|
|  | 218 | s << _object; | 
|---|
|  | 219 | s >> object; | 
|---|
|  | 220 | return object; | 
|---|
|  | 221 | } | 
|---|
|  | 222 | }; | 
|---|
| [a80f419] | 223 |  | 
|---|
|  | 224 | #endif /* TOSTRING_HPP_ */ | 
|---|