mirror of
https://github.com/ultimatepp/ultimatepp.git
synced 2026-05-16 14:16:09 -06:00
.tutorial
git-svn-id: svn://ultimatepp.org/upp/trunk@11482 f0d560ea-af0d-0410-9eb7-867de7ffcac7
This commit is contained in:
parent
8fa242b083
commit
08d5d416e0
5 changed files with 210 additions and 0 deletions
77
tutorial/CoreTutorial/Serialize.cpp
Normal file
77
tutorial/CoreTutorial/Serialize.cpp
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
#include "Tutorial.h"
|
||||
|
||||
void Serialize()
|
||||
{
|
||||
/// .Binary serialization
|
||||
|
||||
StringStream ss;
|
||||
|
||||
int x = 123;
|
||||
Color h = White();
|
||||
|
||||
ss % x % h;
|
||||
|
||||
StringStream ss2(ss.GetResult());
|
||||
|
||||
int x2;
|
||||
Color h2;
|
||||
|
||||
ss2 % x2 % h2;
|
||||
|
||||
DUMP(x2);
|
||||
DUMP(h2);
|
||||
|
||||
///
|
||||
|
||||
ss2.Seek(0);
|
||||
ss2.LoadThrowing();
|
||||
try {
|
||||
ss2 % x2 % h2 % x2;
|
||||
}
|
||||
catch(LoadingError) {
|
||||
LOG("Deserialization has failed");
|
||||
}
|
||||
|
||||
///
|
||||
|
||||
struct MyFoo {
|
||||
int number;
|
||||
Color color;
|
||||
|
||||
void Serialize(Stream& s) {
|
||||
int version = 0;
|
||||
s / version; // allow backward compatibility in the future
|
||||
s % number % color;
|
||||
}
|
||||
};
|
||||
|
||||
MyFoo foo;
|
||||
foo.number = 321;
|
||||
foo.color = Blue();
|
||||
|
||||
String data = StoreAsString(foo);
|
||||
MyFoo foo2;
|
||||
LoadFromString(foo2, data);
|
||||
DUMP(foo2.number);
|
||||
DUMP(foo2.color);
|
||||
|
||||
///
|
||||
|
||||
struct MyFoo2 {
|
||||
int number;
|
||||
Color color;
|
||||
String text;
|
||||
|
||||
void Serialize(Stream& s) {
|
||||
int version = 1;
|
||||
s / version;
|
||||
s % number % color;
|
||||
if(version >= 1)
|
||||
s % text;
|
||||
}
|
||||
};
|
||||
MyFoo2 foo3;
|
||||
LoadFromString(foo3, h);
|
||||
DUMP(foo3.number);
|
||||
DUMP(foo3.color);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue