source: src/Helpers/toString.hpp@ dfafe7

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Added_ParseSaveFragmentResults AddingActions_SaveParseParticleParameters Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_ParticleName_to_Atom Adding_StructOpt_integration_tests AtomFragments Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator CombiningParticlePotentialParsing Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_BoundInBox_CenterInBox_MoleculeActions Fix_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions Fix_PopActions Fix_QtFragmentList_sorted_selection Fix_Restrictedkeyset_FragmentMolecule Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns Fix_fitting_potentials Fixes ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion FragmentAction_writes_AtomFragments FragmentMolecule_checks_bonddegrees GeometryObjects Gui_Fixes Gui_displays_atomic_force_velocity ImplicitCharges IndependentFragmentGrids IndependentFragmentGrids_IndividualZeroInstances IndependentFragmentGrids_IntegrationTest IndependentFragmentGrids_Sole_NN_Calculation JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix MoreRobust_FragmentAutomation ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PdbParser_setsAtomName PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks Rewrite_FitPartialCharges RotateToPrincipalAxisSystem_UndoRedo SaturateAtoms_findBestMatching SaturateAtoms_singleDegree StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg Switchable_LogView ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps TremoloParser_setsAtomName Ubuntu_1604_changes stable
Last change on this file since dfafe7 was 1fb318, checked in by Frederik Heber <heber@…>, 14 years ago

Added helper template function: ConvertTo() that converts a string to a desired value.

  • Property mode set to 100644
File size: 3.5 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 <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 */
26template <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 */
38template <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 */
55template <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 */
73template <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 */
91template <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 */
109template <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 */
127template <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_ */
Note: See TracBrowser for help on using the repository browser.