/* \file toString.hpp * * contains template functions that allow for casting various types to strings. * * Created on: Nov 14, 2010 * Author: heber */ #ifndef TOSTRING_HPP_ #define TOSTRING_HPP_ // include config.h #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #include #include #include #include #include /** Operator for output to std::ostream operator of an std::deque of arbitrary * type. * @param ost output stream * @param _deque deque of types to output * @return ost output stream */ template std::ostream & operator<<(std::ostream &ost, const std::deque< T, alloc > &_deque) { ost << "( "; for (typename std::deque< T, alloc >::const_iterator iter = _deque.begin(); iter != _deque.end(); ++iter) ost << *iter << "; "; ost << ")"; return ost; } /** Operator for output to std::ostream operator of an std::list of arbitrary * type. * @param ost output stream * @param _list list of types to output * @return ost output stream */ template std::ostream & operator<<(std::ostream &ost, const std::list< T, alloc > &_list) { ost << "( "; for (typename std::list< T, alloc >::const_iterator iter = _list.begin(); iter != _list.end(); ++iter) ost << *iter << "; "; ost << ")"; return ost; } /** Operator for output to std::ostream operator of an std::map of arbitrary * type. * @param ost output stream * @param vector map of types to output * @return ost output stream */ template std::ostream & operator<<(std::ostream &ost, const std::map< T, S, comp, alloc > &_map) { ost << "{ "; for (typename std::map< T, S, comp, alloc >::const_iterator iter = _map.begin(); iter != _map.end(); ++iter) ost << "(" << iter->first << ")=>(" << iter->second << "); "; ost << "}"; return ost; } /** Operator for output to std::ostream operator of an std::multimap of arbitrary * type. * @param ost output stream * @param multimap map of types to output * @return ost output stream */ template std::ostream & operator<<(std::ostream &ost, const std::multimap< T, S, comp, alloc > &_multimap) { ost << "{ "; for (typename std::multimap< T, S, comp, alloc >::const_iterator iter = _multimap.begin(); iter != _multimap.end(); ++iter) ost << "(" << iter->first << ")=>(" << iter->second << "); "; ost << "}"; return ost; } /** Operator for output to std::ostream operator of an std::multiset of arbitrary * type. * @param ost output stream * @param multiset list of types to output * @return ost output stream */ template std::ostream & operator<<(std::ostream &ost, const std::multiset< T, comp, alloc > &_multiset) { ost << "( "; for (typename std::multiset< T, comp, alloc >::const_iterator iter = _multiset.begin(); iter != _multiset.end(); ++iter) ost << *iter << "; "; ost << ")"; return ost; } /** Operator for output to std::ostream operator of an std::pair of arbitrary * type. * @param ost output stream * @param _pair pair of types to output * @return ost output stream */ template std::ostream & operator<<(std::ostream &ost, const std::pair< T, S > &_pair) { ost << "[" << _pair.first << "," << _pair.second << "]"; return ost; } /** Operator for output to std::ostream operator of an std::priority_queue of arbitrary * type. * @param ost output stream * @param _priority_queue queue of types to output * @return ost output stream */ template std::ostream & operator<<(std::ostream &ost, const std::priority_queue< T, alloc > &_priority_queue) { ost << "( "; for (typename std::priority_queue< T, alloc >::const_iterator iter = _priority_queue.begin(); iter != _priority_queue.end(); ++iter) ost << *iter << "; "; ost << ")"; return ost; } /** Operator for output to std::ostream operator of an std::queue of arbitrary * type. * @param ost output stream * @param _queue queue of types to output * @return ost output stream */ template std::ostream & operator<<(std::ostream &ost, const std::queue< T, alloc > &_queue) { ost << "( "; for (typename std::queue< T, alloc >::const_iterator iter = _queue.begin(); iter != _queue.end(); ++iter) ost << *iter << "; "; ost << ")"; return ost; } /** Operator for output to std::ostream operator of an std::set of arbitrary * type. * @param ost output stream * @param set list of types to output * @return ost output stream */ template std::ostream & operator<<(std::ostream &ost, const std::set< T, comp, alloc > &_set) { ost << "( "; for (typename std::set< T, comp, alloc >::const_iterator iter = _set.begin(); iter != _set.end(); ++iter) ost << *iter << "; "; ost << ")"; return ost; } /** Operator for output to std::ostream operator of an std::vector of arbitrary * type. * @param ost output stream * @param vector vector of types to output * @return ost output stream */ template std::ostream & operator<<(std::ostream &ost, const std::vector< T, alloc > &_vector) { ost << "( "; for (typename std::vector< T, alloc >::const_iterator iter = _vector.begin(); iter != _vector.end(); ++iter) ost << *iter << "; "; ost << ")"; return ost; } /** Converter for any class to string. * We use default conversion via stringstream as suggested by [Stroustrup]. * \param _&_object reference to object to convert to string. * \return string converted \a _object */ template std::string toString(T const &_object) { std::stringstream s; s << _object; return s.str(); } /** Converter for a string to any class * We use default conversion via stringstream as suggested by [Stroustrup]. * \param _&_object reference to object to convert. * \return converted \a _object of templated type */ template struct ConvertTo { T operator()(const std::string &_object) { std::stringstream s; T object; s << _object; s >> object; return object; } }; #endif /* TOSTRING_HPP_ */