mirror of
https://github.com/paboyle/Grid.git
synced 2024-11-13 01:05:36 +00:00
Format, NAMESPACE
This commit is contained in:
parent
4be31ad1f6
commit
69496482fc
@ -1,4 +1,4 @@
|
|||||||
/*************************************************************************************
|
/*************************************************************************************
|
||||||
|
|
||||||
Grid physics library, www.github.com/paboyle/Grid
|
Grid physics library, www.github.com/paboyle/Grid
|
||||||
|
|
||||||
@ -24,8 +24,8 @@ Author: Peter Boyle <paboyle@ph.ed.ac.uk>
|
|||||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||||
|
|
||||||
See the full license in the file "LICENSE" in the top level distribution directory
|
See the full license in the file "LICENSE" in the top level distribution directory
|
||||||
*************************************************************************************/
|
*************************************************************************************/
|
||||||
/* END LEGAL */
|
/* END LEGAL */
|
||||||
#ifndef GRID_SERIALISATION_BINARY_READER_H
|
#ifndef GRID_SERIALISATION_BINARY_READER_H
|
||||||
#define GRID_SERIALISATION_BINARY_READER_H
|
#define GRID_SERIALISATION_BINARY_READER_H
|
||||||
|
|
||||||
@ -37,83 +37,84 @@ Author: Peter Boyle <paboyle@ph.ed.ac.uk>
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
|
||||||
namespace Grid {
|
NAMESPACE_BEGIN(Grid);
|
||||||
|
|
||||||
class BinaryWriter: public Writer<BinaryWriter>
|
class BinaryWriter: public Writer<BinaryWriter>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
BinaryWriter(const std::string &fileName);
|
BinaryWriter(const std::string &fileName);
|
||||||
virtual ~BinaryWriter(void) = default;
|
virtual ~BinaryWriter(void) = default;
|
||||||
void push(const std::string &s) {};
|
void push(const std::string &s) {};
|
||||||
void pop(void) {};
|
void pop(void) {};
|
||||||
template <typename U>
|
|
||||||
void writeDefault(const std::string &s, const U &x);
|
|
||||||
template <typename U>
|
|
||||||
void writeDefault(const std::string &s, const std::vector<U> &x);
|
|
||||||
void writeDefault(const std::string &s, const char *x);
|
|
||||||
private:
|
|
||||||
std::ofstream file_;
|
|
||||||
};
|
|
||||||
|
|
||||||
class BinaryReader: public Reader<BinaryReader>
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
BinaryReader(const std::string &fileName);
|
|
||||||
virtual ~BinaryReader(void) = default;
|
|
||||||
bool push(const std::string &s) {return true;}
|
|
||||||
void pop(void) {};
|
|
||||||
template <typename U>
|
|
||||||
void readDefault(const std::string &s, U &output);
|
|
||||||
template <typename U>
|
|
||||||
void readDefault(const std::string &s, std::vector<U> &output);
|
|
||||||
private:
|
|
||||||
std::ifstream file_;
|
|
||||||
};
|
|
||||||
|
|
||||||
// Writer template implementation ////////////////////////////////////////////
|
|
||||||
template <typename U>
|
template <typename U>
|
||||||
void BinaryWriter::writeDefault(const std::string &s, const U &x)
|
void writeDefault(const std::string &s, const U &x);
|
||||||
{
|
|
||||||
file_.write((char *)&x, sizeof(U));
|
|
||||||
}
|
|
||||||
|
|
||||||
template <>
|
|
||||||
void BinaryWriter::writeDefault(const std::string &s, const std::string &x);
|
|
||||||
|
|
||||||
template <typename U>
|
template <typename U>
|
||||||
void BinaryWriter::writeDefault(const std::string &s, const std::vector<U> &x)
|
void writeDefault(const std::string &s, const std::vector<U> &x);
|
||||||
{
|
void writeDefault(const std::string &s, const char *x);
|
||||||
uint64_t sz = x.size();
|
private:
|
||||||
|
std::ofstream file_;
|
||||||
|
};
|
||||||
|
|
||||||
|
class BinaryReader: public Reader<BinaryReader>
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
BinaryReader(const std::string &fileName);
|
||||||
|
virtual ~BinaryReader(void) = default;
|
||||||
|
bool push(const std::string &s) {return true;}
|
||||||
|
void pop(void) {};
|
||||||
|
template <typename U>
|
||||||
|
void readDefault(const std::string &s, U &output);
|
||||||
|
template <typename U>
|
||||||
|
void readDefault(const std::string &s, std::vector<U> &output);
|
||||||
|
private:
|
||||||
|
std::ifstream file_;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Writer template implementation ////////////////////////////////////////////
|
||||||
|
template <typename U>
|
||||||
|
void BinaryWriter::writeDefault(const std::string &s, const U &x)
|
||||||
|
{
|
||||||
|
file_.write((char *)&x, sizeof(U));
|
||||||
|
}
|
||||||
|
|
||||||
|
template <>
|
||||||
|
void BinaryWriter::writeDefault(const std::string &s, const std::string &x);
|
||||||
|
|
||||||
|
template <typename U>
|
||||||
|
void BinaryWriter::writeDefault(const std::string &s, const std::vector<U> &x)
|
||||||
|
{
|
||||||
|
uint64_t sz = x.size();
|
||||||
|
|
||||||
write("", sz);
|
write("", sz);
|
||||||
for (uint64_t i = 0; i < sz; ++i)
|
for (uint64_t i = 0; i < sz; ++i)
|
||||||
{
|
{
|
||||||
write("", x[i]);
|
write("", x[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reader template implementation ////////////////////////////////////////////
|
// Reader template implementation ////////////////////////////////////////////
|
||||||
template <typename U>
|
template <typename U>
|
||||||
void BinaryReader::readDefault(const std::string &s, U &output)
|
void BinaryReader::readDefault(const std::string &s, U &output)
|
||||||
{
|
{
|
||||||
file_.read((char *)&output, sizeof(U));
|
file_.read((char *)&output, sizeof(U));
|
||||||
}
|
}
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
void BinaryReader::readDefault(const std::string &s, std::string &output);
|
void BinaryReader::readDefault(const std::string &s, std::string &output);
|
||||||
|
|
||||||
template <typename U>
|
template <typename U>
|
||||||
void BinaryReader::readDefault(const std::string &s, std::vector<U> &output)
|
void BinaryReader::readDefault(const std::string &s, std::vector<U> &output)
|
||||||
{
|
{
|
||||||
uint64_t sz;
|
uint64_t sz;
|
||||||
|
|
||||||
read("", sz);
|
read("", sz);
|
||||||
output.resize(sz);
|
output.resize(sz);
|
||||||
for (uint64_t i = 0; i < sz; ++i)
|
for (uint64_t i = 0; i < sz; ++i)
|
||||||
{
|
{
|
||||||
read("", output[i]);
|
read("", output[i]);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
NAMESPACE_END(Grid);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user