add egg creation to qtgui debug menu
[openc2e.git] / tools / serialtest.cpp
blobb222116d506b5390a3d024b587d83f7d203fe144
1 #include <boost/archive/text_oarchive.hpp>
2 #include <boost/archive/text_iarchive.hpp>
3 #include <boost/serialization/base_object.hpp>
4 #include <boost/serialization/utility.hpp>
5 #include <boost/serialization/version.hpp>
6 #include <boost/serialization/split_member.hpp>
7 #include <boost/serialization/export.hpp>
9 #include <fstream>
10 #include <iostream>
11 #include <sstream>
12 #define CV 1
14 #include "caosVar.h"
15 #include "ser/s_caosVar.h"
16 #include "caosScript.h"
17 #include "ser/s_caosScript.h"
18 #include "ser/s_genome.h"
21 class foo {
22 public:
23 int i;
24 foo() : i(0) {}
25 foo(int j) : i(j) {}
26 private:
27 friend class boost::serialization::access;
29 template<class Archive>
30 void serialize(Archive &ar, const unsigned int version) {
31 ar & BOOST_SERIALIZATION_NVP(i);
35 genomeFile *tryLoadGenome() {
36 genomeFile *f = NULL;
37 try {
38 f = new genomeFile();
39 std::ifstream genestream("data/Genetics/norn.bengal46.gen.brain.gen", std::ios::binary | std::ios::in);
40 // std::ifstream notestream("data/Genetics/norn.bengal46.gen.brain.gno", std::ios::binary | std::ios::in);
41 genestream >> std::noskipws >> *f;
42 // f->readNotes(notestream);
44 std::cout << "pre-save, loaded " << f->genes.size() << " genes." << std::endl;
45 } catch (std::exception &e) {
46 std::cerr << "Warning, genome load failed, skipping that test." << std::endl;
47 std::cerr << "Exception was: " << e.what() << std::endl;
48 if (f)
49 delete f;
50 f = NULL;
52 return f;
55 int main(int argc, char **argv) {
57 registerDelegates();
59 std::ofstream ofs("sertest.dat");
60 caosVar null, str, intv, floatv;
61 caosScript scr("c3", "<test input>");
63 null.reset();
64 str.setString("Hello, world!");
65 intv.setInt(42);
66 floatv.setFloat(0.5);
68 std::istringstream ss("inst sets va00 \"hello world\\n\" outv 42 outs \"\\n\" outs va00 slow stop rscr");
70 if (argc != 2)
71 scr.parse(ss);
72 else {
73 std::ifstream ifs(argv[1]);
74 scr.parse(ifs);
78 const foo v(42);
79 boost::archive::text_oarchive oa(ofs);
80 oa << v;
81 oa << (const caosVar &)null << (const caosVar &)str << (const caosVar &)intv << (const caosVar &)floatv;
82 oa << (const caosScript &)scr;
83 genomeFile *f = tryLoadGenome();
84 oa << (const genomeFile * const)f;
85 delete f;
87 ofs.close();
89 caosVar nnull, nstr, nintv, nfloatv;
90 caosScript si;
92 foo v;
93 std::ifstream ifs("sertest.dat", std::ios::binary);
94 boost::archive::text_iarchive ia(ifs);
95 ia >> v;
96 std::cout << "v.i = " << v.i << std::endl;
97 ia >> nnull >> nstr >> nintv >> nfloatv;
98 ia >> si;
100 genomeFile *f;
101 ia >> f;
102 if (!f)
103 std::cerr << "Warning, no genome class found in output. Load failed maybe?" << std::endl;
104 else
105 std::cout << "f->genes.size() = " << f->genes.size() << std::endl;
106 delete f;
110 #if CV
111 #define D(x) std::cout << #x << " = " << x.dump() << std::endl
112 #else
113 #define D(x) do { } while (0)
114 #endif
115 D(nnull); D(nstr); D(nintv); D(nfloatv);
116 std::cout << si.installer->dump() << std::endl;
117 return 0;