source: src/CodePatterns/toString.hpp@ 084729c

Action_Thermostats Add_AtomRandomPerturbation Add_RotateAroundBondAction Add_SelectAtomByNameAction Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_StructOpt_integration_tests Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_ChronosMutex Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion GeometryObjects Gui_displays_atomic_force_velocity IndependentFragmentGrids_IntegrationTest JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks RotateToPrincipalAxisSystem_UndoRedo StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg ThirdParty_MPQC_rebuilt_buildsystem TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps Ubuntu_1604_changes stable
Last change on this file since 084729c was 084729c, checked in by Frederik Heber <heber@…>, 8 years ago

Squashed 'ThirdParty/CodePatterns/' content from commit c1e1041

git-subtree-dir: ThirdParty/CodePatterns
git-subtree-split: c1e10418c454f98be2f43d93167642b0008428fc

  • Property mode set to 100644
File size: 6.2 KB
Line 
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 <deque>
22#include <list>
23#include <map>
24#include <queue>
25#include <set>
26#include <vector>
27
28
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 */
35template <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
47/** Operator for output to std::ostream operator of an std::list of arbitrary
48 * type.
49 * @param ost output stream
50 * @param _list list of types to output
51 * @return ost output stream
52 */
53template <class T, class alloc> std::ostream & operator<<(std::ostream &ost, const std::list< T, alloc > &_list)
54{
55 ost << "( ";
56 for (typename std::list< T, alloc >::const_iterator iter = _list.begin();
57 iter != _list.end();
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 */
71template <class T, class S, class comp, class alloc> std::ostream & operator<<(std::ostream &ost, const std::map< T, S, comp, alloc > &_map)
72{
73 ost << "{ ";
74 for (typename std::map< T, S, comp, alloc >::const_iterator iter = _map.begin();
75 iter != _map.end();
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 */
88template <class T, class S, class comp, class alloc> std::ostream & operator<<(std::ostream &ost, const std::multimap< T, S, comp, alloc > &_multimap)
89{
90 ost << "{ ";
91 for (typename std::multimap< T, S, comp, alloc >::const_iterator iter = _multimap.begin();
92 iter != _multimap.end();
93 ++iter)
94 ost << "(" << iter->first << ")=>(" << iter->second << "); ";
95 ost << "}";
96 return ost;
97}
98
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 */
105template <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 */
122template <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 */
134template <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 */
151template <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
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 */
169template <class T, class comp, class alloc> std::ostream & operator<<(std::ostream &ost, const std::set< T, comp, alloc > &_set)
170{
171 ost << "( ";
172 for (typename std::set< T, comp, alloc >::const_iterator iter = _set.begin();
173 iter != _set.end();
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 */
186template <class T, class alloc> std::ostream & operator<<(std::ostream &ost, const std::vector< T, alloc > &_vector)
187{
188 ost << "( ";
189 for (typename std::vector< T, alloc >::const_iterator iter = _vector.begin();
190 iter != _vector.end();
191 ++iter)
192 ost << *iter << "; ";
193 ost << ")";
194 return ost;
195}
196
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 */
202template <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 */
214template <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};
223
224#endif /* TOSTRING_HPP_ */
Note: See TracBrowser for help on using the repository browser.