| 1 | /*
 | 
|---|
| 2 |  * StreamOperators.hpp
 | 
|---|
| 3 |  *
 | 
|---|
| 4 |  *  Created on: Apr 16, 2012
 | 
|---|
| 5 |  *      Author: ankele, heber
 | 
|---|
| 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 | 
 | 
|---|
| 16 | #include <boost/tokenizer.hpp>
 | 
|---|
| 17 | #include <string>
 | 
|---|
| 18 | #include <sstream>
 | 
|---|
| 19 | #include <iosfwd>
 | 
|---|
| 20 | 
 | 
|---|
| 21 | #include "LinearAlgebra/Vector.hpp"
 | 
|---|
| 22 | #include "LinearAlgebra/RealSpaceMatrix.hpp"
 | 
|---|
| 23 | #include "Box.hpp"
 | 
|---|
| 24 | #include "Descriptors/AtomIdDescriptor.hpp"
 | 
|---|
| 25 | #include "Descriptors/MoleculeIdDescriptor.hpp"
 | 
|---|
| 26 | #include "Element/periodentafel.hpp"
 | 
|---|
| 27 | #include "World.hpp"
 | 
|---|
| 28 | 
 | 
|---|
| 29 | #include "CodePatterns/Assert.hpp"
 | 
|---|
| 30 | 
 | 
|---|
| 31 | class atom;
 | 
|---|
| 32 | class element;
 | 
|---|
| 33 | class molecule;
 | 
|---|
| 34 | 
 | 
|---|
| 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)
 | 
|---|
| 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 | 
 | 
|---|
| 50 |   return line;
 | 
|---|
| 51 | }
 | 
|---|
| 52 | 
 | 
|---|
| 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];
 | 
|---|
| 70 |   return ist;
 | 
|---|
| 71 | };
 | 
|---|
| 72 | 
 | 
|---|
| 73 | inline std::istream& operator>>(std::istream& ist, RealSpaceMatrix& m)
 | 
|---|
| 74 | {
 | 
|---|
| 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]);
 | 
|---|
| 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 | 
 | 
|---|
| 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 * const a = const_cast<const World &>(World::getInstance()).
 | 
|---|
| 116 |       getAtom(AtomById(temp_value));
 | 
|---|
| 117 |   ASSERT( a != NULL,
 | 
|---|
| 118 |       "operator>>(std::istream& ist, const atom* & m) - atom molecule id "
 | 
|---|
| 119 |       +toString(temp_value)+".");
 | 
|---|
| 120 |   m = a;
 | 
|---|
| 121 |   return ist;
 | 
|---|
| 122 | }
 | 
|---|
| 123 | 
 | 
|---|
| 124 | inline std::istream& operator>>(std::istream& ist, const molecule* & m)
 | 
|---|
| 125 | {
 | 
|---|
| 126 |   std::string line = getLine(ist);
 | 
|---|
| 127 | 
 | 
|---|
| 128 |   ConvertTo<unsigned int> converter;
 | 
|---|
| 129 |   const moleculeId_t temp_value = converter(line);
 | 
|---|
| 130 |   const molecule * const mol = const_cast<const World &>(World::getInstance()).
 | 
|---|
| 131 |       getMolecule(MoleculeById(temp_value));
 | 
|---|
| 132 |   ASSERT( mol != NULL,
 | 
|---|
| 133 |       "operator>>(std::istream& ist, const molecule* & m) - invalid molecule id "
 | 
|---|
| 134 |       +toString(temp_value)+".");
 | 
|---|
| 135 |   m = mol;
 | 
|---|
| 136 |   return ist;
 | 
|---|
| 137 | }
 | 
|---|
| 138 | 
 | 
|---|
| 139 | 
 | 
|---|
| 140 | inline std::istream& operator>>(std::istream& ist, const element* & m)
 | 
|---|
| 141 | {
 | 
|---|
| 142 |   std::string line = getLine(ist);
 | 
|---|
| 143 | 
 | 
|---|
| 144 |   ConvertTo<unsigned int> converter;
 | 
|---|
| 145 |   const atomicNumber_t temp_value = converter(line);
 | 
|---|
| 146 |   const element *elem = World::getInstance().getPeriode()->FindElement(temp_value);
 | 
|---|
| 147 |   ASSERT( elem != NULL,
 | 
|---|
| 148 |       "operator>>(std::istream& ist, const element* & m) - invalid atomic number "
 | 
|---|
| 149 |       +toString(temp_value)+".");
 | 
|---|
| 150 |   m = elem;
 | 
|---|
| 151 |   return ist;
 | 
|---|
| 152 | }
 | 
|---|
| 153 | 
 | 
|---|
| 154 | 
 | 
|---|
| 155 | inline std::istream& operator>>(std::istream& ist, std::vector<const molecule*> & m)
 | 
|---|
| 156 | {
 | 
|---|
| 157 |   std::string line = getLine(ist);
 | 
|---|
| 158 | 
 | 
|---|
| 159 |   // dissect by " " or ","
 | 
|---|
| 160 |   boost::char_separator<char> sep(", ");
 | 
|---|
| 161 |   boost::tokenizer<boost::char_separator<char> > tok(line, sep);
 | 
|---|
| 162 |   ConvertTo<unsigned int> converter;
 | 
|---|
| 163 |   for(boost::tokenizer<boost::char_separator<char> >::const_iterator iter = tok.begin();
 | 
|---|
| 164 |       iter != tok.end(); ++iter) {
 | 
|---|
| 165 |     const moleculeId_t temp_value = converter(*iter);
 | 
|---|
| 166 |     const molecule *mol = const_cast<const World &>(World::getInstance()).
 | 
|---|
| 167 |         getMolecule(MoleculeById(temp_value));
 | 
|---|
| 168 |     ASSERT( mol != NULL,
 | 
|---|
| 169 |         "operator>>(std::istream& ist, std::vector<const molecule*> & m) - invalid molecule id "
 | 
|---|
| 170 |         +toString(temp_value)+".");
 | 
|---|
| 171 |     m.push_back(mol);
 | 
|---|
| 172 |   }
 | 
|---|
| 173 |   return ist;
 | 
|---|
| 174 | }
 | 
|---|
| 175 | 
 | 
|---|
| 176 | 
 | 
|---|
| 177 | inline std::istream& operator>>(std::istream& ist, std::vector<const atom*> & m)
 | 
|---|
| 178 | {
 | 
|---|
| 179 |   std::string line = getLine(ist);
 | 
|---|
| 180 | 
 | 
|---|
| 181 |   // dissect by " " or ","
 | 
|---|
| 182 |   boost::char_separator<char> sep(", ");
 | 
|---|
| 183 |   boost::tokenizer<boost::char_separator<char> > tok(line, sep);
 | 
|---|
| 184 |   ConvertTo<unsigned int> converter;
 | 
|---|
| 185 |   for(boost::tokenizer<boost::char_separator<char> >::const_iterator iter = tok.begin();
 | 
|---|
| 186 |       iter != tok.end(); ++iter) {
 | 
|---|
| 187 |     const atomId_t temp_value = converter(line);
 | 
|---|
| 188 |     const atom * const a = const_cast<const World &>(World::getInstance()).
 | 
|---|
| 189 |         getAtom(AtomById(temp_value));
 | 
|---|
| 190 |     ASSERT( a != NULL,
 | 
|---|
| 191 |         "operator>>(std::istream& ist, std::vector<const atom*> & m) - invalid atom id "
 | 
|---|
| 192 |         +toString(temp_value)+".");
 | 
|---|
| 193 |     m.push_back(a);
 | 
|---|
| 194 |   }
 | 
|---|
| 195 |   return ist;
 | 
|---|
| 196 | }
 | 
|---|
| 197 | 
 | 
|---|
| 198 | inline std::istream& operator>>(std::istream& ist, std::vector<const element*> & m)
 | 
|---|
| 199 | {
 | 
|---|
| 200 |   std::string line = getLine(ist);
 | 
|---|
| 201 | 
 | 
|---|
| 202 |   // dissect by " " or ","
 | 
|---|
| 203 |   boost::char_separator<char> sep(", ");
 | 
|---|
| 204 |   boost::tokenizer<boost::char_separator<char> > tok(line, sep);
 | 
|---|
| 205 |   ConvertTo<unsigned int> converter;
 | 
|---|
| 206 |   for(boost::tokenizer<boost::char_separator<char> >::const_iterator iter = tok.begin();
 | 
|---|
| 207 |       iter != tok.end(); ++iter) {
 | 
|---|
| 208 |     const atomicNumber_t temp_value = converter(*iter);
 | 
|---|
| 209 |     const element * const elem = World::getInstance().getPeriode()->FindElement(temp_value);
 | 
|---|
| 210 |     ASSERT( elem != NULL,
 | 
|---|
| 211 |         "operator>>(std::istream& ist, std::vector<const element*> & m) - invalid atomic number "
 | 
|---|
| 212 |         +toString(temp_value)+".");
 | 
|---|
| 213 |     m.push_back(elem);
 | 
|---|
| 214 |   }
 | 
|---|
| 215 |   return ist;
 | 
|---|
| 216 | }
 | 
|---|
| 217 | 
 | 
|---|
| 218 | 
 | 
|---|
| 219 | inline std::istream& operator>>(std::istream& ist, std::vector<std::string> & m)
 | 
|---|
| 220 | {
 | 
|---|
| 221 |   std::string line = getLine(ist);
 | 
|---|
| 222 | 
 | 
|---|
| 223 |   // dissect by " " or ","
 | 
|---|
| 224 |   boost::char_separator<char> sep(", ");
 | 
|---|
| 225 |   boost::tokenizer<boost::char_separator<char> > tok(line, sep);
 | 
|---|
| 226 |   for(boost::tokenizer<boost::char_separator<char> >::const_iterator iter = tok.begin();
 | 
|---|
| 227 |       iter != tok.end(); ++iter)
 | 
|---|
| 228 |     m.push_back(*iter);
 | 
|---|
| 229 |   return ist;
 | 
|---|
| 230 | }
 | 
|---|
| 231 | 
 | 
|---|
| 232 | inline std::istream& operator>>(std::istream& ist, std::vector<unsigned int> & m)
 | 
|---|
| 233 | {
 | 
|---|
| 234 |   std::string line = getLine(ist);
 | 
|---|
| 235 | 
 | 
|---|
| 236 |   // dissect by " " or ","
 | 
|---|
| 237 |   boost::char_separator<char> sep(", ");
 | 
|---|
| 238 |   boost::tokenizer<boost::char_separator<char> > tok(line, sep);
 | 
|---|
| 239 |   ConvertTo<unsigned int> converter;
 | 
|---|
| 240 |   for(boost::tokenizer<boost::char_separator<char> >::const_iterator iter = tok.begin();
 | 
|---|
| 241 |       iter != tok.end(); ++iter)
 | 
|---|
| 242 |     m.push_back(converter(*iter));
 | 
|---|
| 243 |   return ist;
 | 
|---|
| 244 | }
 | 
|---|
| 245 | 
 | 
|---|
| 246 | 
 | 
|---|
| 247 | #endif /* STREAMOPERATORS_HPP_ */
 | 
|---|