1
0
mirror of https://github.com/paboyle/Grid.git synced 2025-06-11 11:56:56 +01:00

new I/O interface

This commit is contained in:
2015-11-16 18:14:37 +00:00
parent 8709117aea
commit af19118113
11 changed files with 650 additions and 510 deletions

View File

@ -1,36 +1,35 @@
#include <Grid.h>
namespace Grid {
class myclass {
public:
GRID_DECL_CLASS_MEMBERS(myclass,
int, x,
double, y,
bool , b,
std::string, name,
std::vector<double>, array,
std::vector<std::vector<double> >, twodimarray,
);
myclass(){}
myclass(int i) : array(4,5.1), twodimarray(3,std::vector<double>(2,1.23456)) {
x=i;
y=2*i;
b=true;
name="bother said pooh";
}
};
class myclass: Serializable {
public:
GRID_DECL_CLASS_MEMBERS(myclass,
int, x,
double, y,
bool , b,
std::string, name,
std::vector<double>, array,
std::vector<std::vector<double>>, twodimarray,
);
myclass() {}
myclass(int i)
: array(4,5.1), twodimarray(3,std::vector<double>(2,1.23456))
{
x=i;
y=2*i;
b=true;
name="bother said pooh";
}
};
}
int16_t i16 = 1;
int16_t i16 = 1;
uint16_t u16 = 2;
int32_t i32 = 3;
int32_t i32 = 3;
uint32_t u32 = 4;
int64_t i64 = 5;
int64_t i64 = 5;
uint64_t u64 = 6;
float f = M_PI;
double d = 2*M_PI;
@ -41,8 +40,9 @@ using namespace Grid;
int main(int argc,char **argv)
{
{
XMLWriter WR("bother.xml");
XmlWriter WR("bother.xml");
// test basic type writing
push(WR,"BasicTypes");
write(WR,std::string("i16"),i16);
write(WR,"u16",u16);
@ -54,40 +54,54 @@ int main(int argc,char **argv)
write(WR,"d",d);
write(WR,"b",b);
pop(WR);
// test serializable class writing
myclass obj(1234); // non-trivial constructor
write(WR,"obj",obj);
WR.write("obj2", obj);
std::vector<myclass> vec;
vec.push_back(myclass(1234));
vec.push_back(myclass(5678));
vec.push_back(myclass(3838));
write(WR, "objvec", vec);
};
XMLReader RD("bother.xml");
myclass copy1;
myclass copy2;
myclass copy3;
read(RD,"obj",copy1);
std::cout << "Loaded " << copy1<<std::endl;
// read tests
myclass copy1, copy2, copy3;
std::vector<myclass> veccopy1, veccopy2, veccopy3;
//// XML
{
XmlReader RD("bother.xml");
read(RD,"obj",copy1);
read(RD,"objvec", veccopy1);
std::cout << "Loaded (XML) -----------------" << std::endl;
std::cout << copy1 << std::endl << veccopy1 << std::endl;
}
//// binary
{
BinaryWriter BWR("bother.bin");
write(BWR,"discard",copy1 );
write(BWR,"discard",veccopy1 );
}
{
{
BinaryReader BRD("bother.bin");
read (BRD,"discard",copy2 );
std::cout<<copy2<<std::endl;
read (BRD,"discard",veccopy2 );
std::cout << "Loaded (bin) -----------------" << std::endl;
std::cout << copy2 << std::endl << veccopy2 << std::endl;
}
//// text
{
TextWriter TWR("bother.txt");
write(TWR,"discard",copy1 );
write(TWR,"discard",veccopy1 );
}
{
{
TextReader TRD("bother.txt");
read (TRD,"discard",copy3 );
std::cout<<copy3<<std::endl;
read (TRD,"discard",veccopy3 );
std::cout << "Loaded (txt) -----------------" << std::endl;
std::cout << copy3 << std::endl << veccopy3 << std::endl;
}
}