| [f10b0c] | 1 | /* | 
|---|
|  | 2 | * StreamOperators.hpp | 
|---|
|  | 3 | * | 
|---|
|  | 4 | *  Created on: Apr 16, 2012 | 
|---|
| [16876c] | 5 | *      Author: ankele, heber | 
|---|
| [f10b0c] | 6 | */ | 
|---|
|  | 7 |  | 
|---|
|  | 8 | #ifndef STREAMOPERATORS_HPP_ | 
|---|
|  | 9 | #define STREAMOPERATORS_HPP_ | 
|---|
|  | 10 |  | 
|---|
|  | 11 | // include config.h | 
|---|
|  | 12 | #ifdef HAVE_CONFIG_H | 
|---|
|  | 13 | #include <config.h> | 
|---|
|  | 14 | #endif | 
|---|
|  | 15 |  | 
|---|
| [16876c] | 16 | #include <boost/tokenizer.hpp> | 
|---|
| [f10b0c] | 17 | #include <string> | 
|---|
| [16876c] | 18 | #include <sstream> | 
|---|
| [f10b0c] | 19 | #include <iosfwd> | 
|---|
|  | 20 |  | 
|---|
|  | 21 | #include "LinearAlgebra/Vector.hpp" | 
|---|
|  | 22 | #include "LinearAlgebra/RealSpaceMatrix.hpp" | 
|---|
|  | 23 | #include "Box.hpp" | 
|---|
| [16876c] | 24 | #include "Descriptors/AtomIdDescriptor.hpp" | 
|---|
|  | 25 | #include "Descriptors/MoleculeIdDescriptor.hpp" | 
|---|
|  | 26 | #include "Element/periodentafel.hpp" | 
|---|
|  | 27 | #include "World.hpp" | 
|---|
| [f10b0c] | 28 |  | 
|---|
|  | 29 | #include "CodePatterns/Assert.hpp" | 
|---|
|  | 30 |  | 
|---|
| [16876c] | 31 | class atom; | 
|---|
| [f10b0c] | 32 | class element; | 
|---|
| [16876c] | 33 | class molecule; | 
|---|
| [f10b0c] | 34 |  | 
|---|
| [16876c] | 35 | /** Returns a single line from \a ist without leading and ending brackets. | 
|---|
|  | 36 | * | 
|---|
|  | 37 | * @param ist stream to get line from | 
|---|
|  | 38 | * @return line as string without () | 
|---|
|  | 39 | */ | 
|---|
|  | 40 | inline std::string getLine(std::istream& ist) | 
|---|
| [f10b0c] | 41 | { | 
|---|
|  | 42 | std::string line; | 
|---|
|  | 43 | getline(ist,line); | 
|---|
|  | 44 |  | 
|---|
|  | 45 | if (!line.empty() && line[0] == '(') | 
|---|
|  | 46 | line.erase(0, 1); | 
|---|
|  | 47 | if (!line.empty() && line[line.size() - 1] == ')') | 
|---|
|  | 48 | line.erase(line.size() - 1, 1); | 
|---|
|  | 49 |  | 
|---|
| [16876c] | 50 | return line; | 
|---|
|  | 51 | } | 
|---|
| [f10b0c] | 52 |  | 
|---|
| [16876c] | 53 | inline std::istream& operator>>(std::istream& ist, Vector& m) | 
|---|
|  | 54 | { | 
|---|
|  | 55 | std::string line = getLine(ist); | 
|---|
|  | 56 |  | 
|---|
|  | 57 | // dissect by " " or "," | 
|---|
|  | 58 | ConvertTo<double> converter; | 
|---|
|  | 59 | std::vector<double> temp_vector; | 
|---|
|  | 60 | boost::char_separator<char> sep(", "); | 
|---|
|  | 61 | boost::tokenizer<boost::char_separator<char> > tok(line, sep); | 
|---|
|  | 62 | for(boost::tokenizer<boost::char_separator<char> >::const_iterator iter = tok.begin(); | 
|---|
|  | 63 | iter != tok.end(); ++iter) | 
|---|
|  | 64 | temp_vector.push_back(converter(*iter)); | 
|---|
|  | 65 | ASSERT (temp_vector.size() == (size_t)NDIM, | 
|---|
|  | 66 | "operator>>(std::istream& ist, Vector& m) - not "+toString((size_t)NDIM)+" but " | 
|---|
|  | 67 | +toString(temp_vector.size())+" components given."); | 
|---|
|  | 68 | for (size_t i=0;i<NDIM;++i) | 
|---|
|  | 69 | m[i] = temp_vector[i]; | 
|---|
| [f10b0c] | 70 | return ist; | 
|---|
|  | 71 | }; | 
|---|
|  | 72 |  | 
|---|
|  | 73 | inline std::istream& operator>>(std::istream& ist, RealSpaceMatrix& m) | 
|---|
|  | 74 | { | 
|---|
| [16876c] | 75 | std::string line = getLine(ist); | 
|---|
|  | 76 |  | 
|---|
|  | 77 | // dissect by " " or "," | 
|---|
|  | 78 | ConvertTo<double> converter; | 
|---|
|  | 79 | std::vector<double> temp_vector; | 
|---|
|  | 80 | boost::char_separator<char> sep(", "); | 
|---|
|  | 81 | boost::tokenizer<boost::char_separator<char> > tok(line, sep); | 
|---|
|  | 82 | for(boost::tokenizer<boost::char_separator<char> >::const_iterator iter = tok.begin(); | 
|---|
|  | 83 | iter != tok.end(); ++iter) | 
|---|
|  | 84 | temp_vector.push_back(converter(*iter)); | 
|---|
|  | 85 | ASSERT (temp_vector.size() == (size_t)NDIM*(NDIM+1)/2, | 
|---|
|  | 86 | "operator>>(std::istream& ist, RealSpaceMatrix& m) - not "+toString((size_t)NDIM*((size_t)NDIM+1)/2)+" but " | 
|---|
|  | 87 | +toString(temp_vector.size())+" components given."); | 
|---|
|  | 88 | m.set(0,0, temp_vector[0]); | 
|---|
|  | 89 | m.set(0,1, temp_vector[1]); | 
|---|
|  | 90 | m.set(0,2, temp_vector[2]); | 
|---|
|  | 91 | m.set(1,0, temp_vector[1]); | 
|---|
|  | 92 | m.set(1,1, temp_vector[3]); | 
|---|
|  | 93 | m.set(1,2, temp_vector[4]); | 
|---|
|  | 94 | m.set(2,0, temp_vector[2]); | 
|---|
|  | 95 | m.set(2,1, temp_vector[4]); | 
|---|
|  | 96 | m.set(2,2, temp_vector[5]); | 
|---|
| [f10b0c] | 97 | return ist; | 
|---|
|  | 98 | }; | 
|---|
|  | 99 |  | 
|---|
|  | 100 | inline std::istream& operator>>(std::istream& ist, Box& m) | 
|---|
|  | 101 | { | 
|---|
|  | 102 | RealSpaceMatrix M; | 
|---|
|  | 103 | ist >> M; | 
|---|
|  | 104 | m.setM(M); | 
|---|
|  | 105 | return ist; | 
|---|
|  | 106 | }; | 
|---|
|  | 107 |  | 
|---|
|  | 108 |  | 
|---|
| [16876c] | 109 | inline std::istream& operator>>(std::istream& ist, const atom* & m) | 
|---|
|  | 110 | { | 
|---|
|  | 111 | std::string line = getLine(ist); | 
|---|
|  | 112 |  | 
|---|
|  | 113 | ConvertTo<unsigned int> converter; | 
|---|
|  | 114 | const atomId_t temp_value = converter(line); | 
|---|
|  | 115 | const atom *a = World::getInstance().getAtom(AtomById(temp_value)); | 
|---|
|  | 116 | ASSERT( a != NULL, | 
|---|
|  | 117 | "operator>>(std::istream& ist, const atom* & m) - atom molecule id " | 
|---|
|  | 118 | +toString(temp_value)+"."); | 
|---|
|  | 119 | m = a; | 
|---|
|  | 120 | return ist; | 
|---|
|  | 121 | } | 
|---|
|  | 122 |  | 
|---|
| [f10b0c] | 123 | inline std::istream& operator>>(std::istream& ist, const molecule* & m) | 
|---|
|  | 124 | { | 
|---|
| [16876c] | 125 | std::string line = getLine(ist); | 
|---|
|  | 126 |  | 
|---|
|  | 127 | ConvertTo<unsigned int> converter; | 
|---|
|  | 128 | const moleculeId_t temp_value = converter(line); | 
|---|
|  | 129 | const molecule *mol = World::getInstance().getMolecule(MoleculeById(temp_value)); | 
|---|
|  | 130 | ASSERT( mol != NULL, | 
|---|
|  | 131 | "operator>>(std::istream& ist, const molecule* & m) - invalid molecule id " | 
|---|
|  | 132 | +toString(temp_value)+"."); | 
|---|
|  | 133 | m = mol; | 
|---|
| [f10b0c] | 134 | return ist; | 
|---|
|  | 135 | } | 
|---|
|  | 136 |  | 
|---|
|  | 137 |  | 
|---|
|  | 138 | inline std::istream& operator>>(std::istream& ist, const element* & m) | 
|---|
|  | 139 | { | 
|---|
| [16876c] | 140 | std::string line = getLine(ist); | 
|---|
|  | 141 |  | 
|---|
|  | 142 | ConvertTo<unsigned int> converter; | 
|---|
|  | 143 | const atomicNumber_t temp_value = converter(line); | 
|---|
|  | 144 | const element *elem = World::getInstance().getPeriode()->FindElement(temp_value); | 
|---|
|  | 145 | ASSERT( elem != NULL, | 
|---|
|  | 146 | "operator>>(std::istream& ist, const element* & m) - invalid atomic number " | 
|---|
|  | 147 | +toString(temp_value)+"."); | 
|---|
|  | 148 | m = elem; | 
|---|
| [f10b0c] | 149 | return ist; | 
|---|
|  | 150 | } | 
|---|
|  | 151 |  | 
|---|
|  | 152 |  | 
|---|
|  | 153 | inline std::istream& operator>>(std::istream& ist, std::vector<const molecule*> & m) | 
|---|
|  | 154 | { | 
|---|
| [16876c] | 155 | std::string line = getLine(ist); | 
|---|
|  | 156 |  | 
|---|
|  | 157 | // dissect by " " or "," | 
|---|
|  | 158 | boost::char_separator<char> sep(", "); | 
|---|
|  | 159 | boost::tokenizer<boost::char_separator<char> > tok(line, sep); | 
|---|
|  | 160 | ConvertTo<unsigned int> converter; | 
|---|
|  | 161 | for(boost::tokenizer<boost::char_separator<char> >::const_iterator iter = tok.begin(); | 
|---|
|  | 162 | iter != tok.end(); ++iter) { | 
|---|
|  | 163 | const moleculeId_t temp_value = converter(*iter); | 
|---|
|  | 164 | const molecule *mol = World::getInstance().getMolecule(MoleculeById(temp_value)); | 
|---|
|  | 165 | ASSERT( mol != NULL, | 
|---|
|  | 166 | "operator>>(std::istream& ist, std::vector<const molecule*> & m) - invalid molecule id " | 
|---|
|  | 167 | +toString(temp_value)+"."); | 
|---|
|  | 168 | m.push_back(mol); | 
|---|
|  | 169 | } | 
|---|
| [f10b0c] | 170 | return ist; | 
|---|
|  | 171 | } | 
|---|
|  | 172 |  | 
|---|
|  | 173 |  | 
|---|
| [16876c] | 174 | inline std::istream& operator>>(std::istream& ist, std::vector<const atom*> & m) | 
|---|
|  | 175 | { | 
|---|
|  | 176 | std::string line = getLine(ist); | 
|---|
|  | 177 |  | 
|---|
|  | 178 | // dissect by " " or "," | 
|---|
|  | 179 | boost::char_separator<char> sep(", "); | 
|---|
|  | 180 | boost::tokenizer<boost::char_separator<char> > tok(line, sep); | 
|---|
|  | 181 | ConvertTo<unsigned int> converter; | 
|---|
|  | 182 | for(boost::tokenizer<boost::char_separator<char> >::const_iterator iter = tok.begin(); | 
|---|
|  | 183 | iter != tok.end(); ++iter) { | 
|---|
|  | 184 | const atomId_t temp_value = converter(line); | 
|---|
|  | 185 | const atom *a = World::getInstance().getAtom(AtomById(temp_value)); | 
|---|
|  | 186 | ASSERT( a != NULL, | 
|---|
|  | 187 | "operator>>(std::istream& ist, std::vector<const atom*> & m) - invalid atom id " | 
|---|
|  | 188 | +toString(temp_value)+"."); | 
|---|
|  | 189 | m.push_back(a); | 
|---|
|  | 190 | } | 
|---|
|  | 191 | return ist; | 
|---|
|  | 192 | } | 
|---|
|  | 193 |  | 
|---|
| [f10b0c] | 194 | inline std::istream& operator>>(std::istream& ist, std::vector<const element*> & m) | 
|---|
|  | 195 | { | 
|---|
| [16876c] | 196 | std::string line = getLine(ist); | 
|---|
|  | 197 |  | 
|---|
|  | 198 | // dissect by " " or "," | 
|---|
|  | 199 | boost::char_separator<char> sep(", "); | 
|---|
|  | 200 | boost::tokenizer<boost::char_separator<char> > tok(line, sep); | 
|---|
|  | 201 | ConvertTo<unsigned int> converter; | 
|---|
|  | 202 | for(boost::tokenizer<boost::char_separator<char> >::const_iterator iter = tok.begin(); | 
|---|
|  | 203 | iter != tok.end(); ++iter) { | 
|---|
|  | 204 | const atomicNumber_t temp_value = converter(*iter); | 
|---|
|  | 205 | const element *elem = World::getInstance().getPeriode()->FindElement(temp_value); | 
|---|
|  | 206 | ASSERT( elem != NULL, | 
|---|
|  | 207 | "operator>>(std::istream& ist, std::vector<const element*> & m) - invalid atomic number " | 
|---|
|  | 208 | +toString(temp_value)+"."); | 
|---|
|  | 209 | m.push_back(elem); | 
|---|
|  | 210 | } | 
|---|
| [f10b0c] | 211 | return ist; | 
|---|
|  | 212 | } | 
|---|
|  | 213 |  | 
|---|
|  | 214 |  | 
|---|
|  | 215 | inline std::istream& operator>>(std::istream& ist, std::vector<std::string> & m) | 
|---|
|  | 216 | { | 
|---|
| [16876c] | 217 | std::string line = getLine(ist); | 
|---|
|  | 218 |  | 
|---|
|  | 219 | // dissect by " " or "," | 
|---|
|  | 220 | boost::char_separator<char> sep(", "); | 
|---|
|  | 221 | boost::tokenizer<boost::char_separator<char> > tok(line, sep); | 
|---|
|  | 222 | for(boost::tokenizer<boost::char_separator<char> >::const_iterator iter = tok.begin(); | 
|---|
|  | 223 | iter != tok.end(); ++iter) | 
|---|
|  | 224 | m.push_back(*iter); | 
|---|
|  | 225 | return ist; | 
|---|
|  | 226 | } | 
|---|
|  | 227 |  | 
|---|
|  | 228 | inline std::istream& operator>>(std::istream& ist, std::vector<unsigned int> & m) | 
|---|
|  | 229 | { | 
|---|
|  | 230 | std::string line = getLine(ist); | 
|---|
|  | 231 |  | 
|---|
|  | 232 | // dissect by " " or "," | 
|---|
|  | 233 | boost::char_separator<char> sep(", "); | 
|---|
|  | 234 | boost::tokenizer<boost::char_separator<char> > tok(line, sep); | 
|---|
|  | 235 | ConvertTo<unsigned int> converter; | 
|---|
|  | 236 | for(boost::tokenizer<boost::char_separator<char> >::const_iterator iter = tok.begin(); | 
|---|
|  | 237 | iter != tok.end(); ++iter) | 
|---|
|  | 238 | m.push_back(converter(*iter)); | 
|---|
| [f10b0c] | 239 | return ist; | 
|---|
|  | 240 | } | 
|---|
|  | 241 |  | 
|---|
|  | 242 |  | 
|---|
|  | 243 | #endif /* STREAMOPERATORS_HPP_ */ | 
|---|