/* * StreamOperators.hpp * * Created on: Apr 16, 2012 * Author: ankele, heber */ #ifndef STREAMOPERATORS_HPP_ #define STREAMOPERATORS_HPP_ // include config.h #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #include "LinearAlgebra/Vector.hpp" #include "LinearAlgebra/RealSpaceMatrix.hpp" #include "Box.hpp" #include "Descriptors/AtomIdDescriptor.hpp" #include "Descriptors/MoleculeIdDescriptor.hpp" #include "Element/periodentafel.hpp" #include "World.hpp" #include "CodePatterns/Assert.hpp" class atom; class element; class molecule; /** Returns a single line from \a ist without leading and ending brackets. * * @param ist stream to get line from * @return line as string without () */ inline std::string getLine(std::istream& ist) { std::string line; getline(ist,line); if (!line.empty() && line[0] == '(') line.erase(0, 1); if (!line.empty() && line[line.size() - 1] == ')') line.erase(line.size() - 1, 1); return line; } inline std::istream& operator>>(std::istream& ist, Vector& m) { std::string line = getLine(ist); // dissect by " " or "," ConvertTo converter; std::vector temp_vector; boost::char_separator sep(", "); boost::tokenizer > tok(line, sep); for(boost::tokenizer >::const_iterator iter = tok.begin(); iter != tok.end(); ++iter) temp_vector.push_back(converter(*iter)); ASSERT (temp_vector.size() == (size_t)NDIM, "operator>>(std::istream& ist, Vector& m) - not "+toString((size_t)NDIM)+" but " +toString(temp_vector.size())+" components given."); for (size_t i=0;i>(std::istream& ist, RealSpaceMatrix& m) { std::string line = getLine(ist); // dissect by " " or "," ConvertTo converter; std::vector temp_vector; boost::char_separator sep(", "); boost::tokenizer > tok(line, sep); for(boost::tokenizer >::const_iterator iter = tok.begin(); iter != tok.end(); ++iter) temp_vector.push_back(converter(*iter)); ASSERT (temp_vector.size() == (size_t)NDIM*(NDIM+1)/2, "operator>>(std::istream& ist, RealSpaceMatrix& m) - not "+toString((size_t)NDIM*((size_t)NDIM+1)/2)+" but " +toString(temp_vector.size())+" components given."); m.set(0,0, temp_vector[0]); m.set(0,1, temp_vector[1]); m.set(0,2, temp_vector[2]); m.set(1,0, temp_vector[1]); m.set(1,1, temp_vector[3]); m.set(1,2, temp_vector[4]); m.set(2,0, temp_vector[2]); m.set(2,1, temp_vector[4]); m.set(2,2, temp_vector[5]); return ist; }; inline std::istream& operator>>(std::istream& ist, Box& m) { RealSpaceMatrix M; ist >> M; m.setM(M); return ist; }; inline std::istream& operator>>(std::istream& ist, const atom* & m) { std::string line = getLine(ist); ConvertTo converter; const atomId_t temp_value = converter(line); const atom *a = World::getInstance().getAtom(AtomById(temp_value)); ASSERT( a != NULL, "operator>>(std::istream& ist, const atom* & m) - atom molecule id " +toString(temp_value)+"."); m = a; return ist; } inline std::istream& operator>>(std::istream& ist, const molecule* & m) { std::string line = getLine(ist); ConvertTo converter; const moleculeId_t temp_value = converter(line); const molecule *mol = World::getInstance().getMolecule(MoleculeById(temp_value)); ASSERT( mol != NULL, "operator>>(std::istream& ist, const molecule* & m) - invalid molecule id " +toString(temp_value)+"."); m = mol; return ist; } inline std::istream& operator>>(std::istream& ist, const element* & m) { std::string line = getLine(ist); ConvertTo converter; const atomicNumber_t temp_value = converter(line); const element *elem = World::getInstance().getPeriode()->FindElement(temp_value); ASSERT( elem != NULL, "operator>>(std::istream& ist, const element* & m) - invalid atomic number " +toString(temp_value)+"."); m = elem; return ist; } inline std::istream& operator>>(std::istream& ist, std::vector & m) { std::string line = getLine(ist); // dissect by " " or "," boost::char_separator sep(", "); boost::tokenizer > tok(line, sep); ConvertTo converter; for(boost::tokenizer >::const_iterator iter = tok.begin(); iter != tok.end(); ++iter) { const moleculeId_t temp_value = converter(*iter); const molecule *mol = World::getInstance().getMolecule(MoleculeById(temp_value)); ASSERT( mol != NULL, "operator>>(std::istream& ist, std::vector & m) - invalid molecule id " +toString(temp_value)+"."); m.push_back(mol); } return ist; } inline std::istream& operator>>(std::istream& ist, std::vector & m) { std::string line = getLine(ist); // dissect by " " or "," boost::char_separator sep(", "); boost::tokenizer > tok(line, sep); ConvertTo converter; for(boost::tokenizer >::const_iterator iter = tok.begin(); iter != tok.end(); ++iter) { const atomId_t temp_value = converter(line); const atom *a = World::getInstance().getAtom(AtomById(temp_value)); ASSERT( a != NULL, "operator>>(std::istream& ist, std::vector & m) - invalid atom id " +toString(temp_value)+"."); m.push_back(a); } return ist; } inline std::istream& operator>>(std::istream& ist, std::vector & m) { std::string line = getLine(ist); // dissect by " " or "," boost::char_separator sep(", "); boost::tokenizer > tok(line, sep); ConvertTo converter; for(boost::tokenizer >::const_iterator iter = tok.begin(); iter != tok.end(); ++iter) { const atomicNumber_t temp_value = converter(*iter); const element *elem = World::getInstance().getPeriode()->FindElement(temp_value); ASSERT( elem != NULL, "operator>>(std::istream& ist, std::vector & m) - invalid atomic number " +toString(temp_value)+"."); m.push_back(elem); } return ist; } inline std::istream& operator>>(std::istream& ist, std::vector & m) { std::string line = getLine(ist); // dissect by " " or "," boost::char_separator sep(", "); boost::tokenizer > tok(line, sep); for(boost::tokenizer >::const_iterator iter = tok.begin(); iter != tok.end(); ++iter) m.push_back(*iter); return ist; } inline std::istream& operator>>(std::istream& ist, std::vector & m) { std::string line = getLine(ist); // dissect by " " or "," boost::char_separator sep(", "); boost::tokenizer > tok(line, sep); ConvertTo converter; for(boost::tokenizer >::const_iterator iter = tok.begin(); iter != tok.end(); ++iter) m.push_back(converter(*iter)); return ist; } #endif /* STREAMOPERATORS_HPP_ */