diff --git a/extras/Hadrons/Modules/MContraction/Meson.hpp b/extras/Hadrons/Modules/MContraction/Meson.hpp index 270875a6..a46dd445 100644 --- a/extras/Hadrons/Modules/MContraction/Meson.hpp +++ b/extras/Hadrons/Modules/MContraction/Meson.hpp @@ -36,22 +36,6 @@ See the full license in the file "LICENSE" in the top level distribution directo #include #include -namespace Grid { - // Overload >> to extract gamma pair from "" string. - template - inline std::istringstream &operator>>(std::istringstream &sstr, - std::pair &buf) - { - unsigned int buf1; - unsigned int buf2; - char c; - sstr >> c >> buf1 >> buf2 >> c; - sstr.peek(); - buf = std::make_pair((T1)buf1, (T2)buf2); - return sstr; - } -} - BEGIN_HADRONS_NAMESPACE /* @@ -63,8 +47,8 @@ BEGIN_HADRONS_NAMESPACE - q1: input propagator 1 (string) - q2: input propagator 2 (string) - gammas: gamma products to insert at sink & source, pairs of gamma matrices - (space-separated integers) in square brackets (i.e. [g_sink g_src]), - in a sequence (e.g. "[15 7][7 15][7 7]"). + (space-separated strings) in angled brackets (i.e. ), + in a sequence (e.g. ""). Special values: "all" - perform all possible contractions. - mom: momentum insertion, space-separated float sequence (e.g ".1 .2 1. 0."), diff --git a/lib/serialisation/BaseIO.h b/lib/serialisation/BaseIO.h index 0357915d..36e6fd77 100644 --- a/lib/serialisation/BaseIO.h +++ b/lib/serialisation/BaseIO.h @@ -138,6 +138,54 @@ namespace Grid { unsigned int dimInd_{0}; }; + // Pair IO utilities ///////////////////////////////////////////////////////// + // helper function to parse input in the format "" + template + inline std::istream & operator>>(std::istream &is, std::pair &buf) + { + T1 buf1; + T2 buf2; + char c; + + // Search for "pair" delimiters. + do + { + is.get(c); + } while (c != '<' && !is.eof()); + if (c == '<') + { + int start = is.tellg(); + do + { + is.get(c); + } while (c != '>' && !is.eof()); + if (c == '>') + { + int end = is.tellg(); + int psize = end - start - 1; + + // Only read data between pair limiters. + is.seekg(start); + std::string tmpstr(psize, ' '); + is.read(&tmpstr[0], psize); + std::istringstream temp(tmpstr); + temp >> buf1 >> buf2; + buf = std::make_pair(buf1, buf2); + is.seekg(end); + } + } + is.peek(); + return is; + } + + // output to streams for pairs + template + inline std::ostream & operator<<(std::ostream &os, const std::pair &p) + { + os << "<" << p.first << " " << p.second << ">"; + return os; + } + // Abstract writer/reader classes //////////////////////////////////////////// // static polymorphism implemented using CRTP idiom class Serializable; diff --git a/tests/IO/Test_serialisation.cc b/tests/IO/Test_serialisation.cc index 8204b05b..acafc900 100644 --- a/tests/IO/Test_serialisation.cc +++ b/tests/IO/Test_serialisation.cc @@ -113,6 +113,7 @@ int main(int argc,char **argv) // test serializable class writing myclass obj(1234); // non-trivial constructor std::vector vec; + std::pair pair; std::cout << "-- serialisable class writing to 'bother.xml'..." << std::endl; write(WR,"obj",obj); @@ -120,6 +121,8 @@ int main(int argc,char **argv) vec.push_back(myclass(1234)); vec.push_back(myclass(5678)); vec.push_back(myclass(3838)); + pair = std::make_pair(myenum::red, myenum::blue); + write(WR, "objvec", vec); std::cout << "-- serialisable class writing to std::cout:" << std::endl; std::cout << obj << std::endl; @@ -127,21 +130,30 @@ int main(int argc,char **argv) std::cout << "vec[0] == obj: " << ((vec[0] == obj) ? "true" : "false") << std::endl; std::cout << "vec[1] == obj: " << ((vec[1] == obj) ? "true" : "false") << std::endl; + write(WR, "objpair", pair); + std::cout << "-- pair writing to std::cout:" << std::endl; + std::cout << pair << std::endl; + // read tests std::cout << "\n==== IO self-consistency tests" << std::endl; //// XML ioTest("iotest.xml", obj, "XML (object) "); ioTest("iotest.xml", vec, "XML (vector of objects)"); + ioTest("iotest.xml", pair, "XML (pair of objects)"); //// binary ioTest("iotest.bin", obj, "binary (object) "); ioTest("iotest.bin", vec, "binary (vector of objects)"); + ioTest("iotest.bin", pair, "binary (pair of objects)"); //// text ioTest("iotest.dat", obj, "text (object) "); ioTest("iotest.dat", vec, "text (vector of objects)"); + ioTest("iotest.dat", pair, "text (pair of objects)"); //// HDF5 +#undef HAVE_HDF5 #ifdef HAVE_HDF5 ioTest("iotest.h5", obj, "HDF5 (object) "); ioTest("iotest.h5", vec, "HDF5 (vector of objects)"); + ioTest("iotest.h5", pair, "HDF5 (pair of objects)"); #endif std::cout << "\n==== vector flattening/reconstruction" << std::endl;