.tutorial

git-svn-id: svn://ultimatepp.org/upp/trunk@11482 f0d560ea-af0d-0410-9eb7-867de7ffcac7
This commit is contained in:
cxl 2017-11-19 16:44:22 +00:00
parent 8fa242b083
commit 08d5d416e0
5 changed files with 210 additions and 0 deletions

View file

@ -47,6 +47,11 @@ file
Async.cpp,
CoPartition.cpp,
Parallel.cpp,
Stream readonly separator,
Stream.cpp,
SpecialStream.cpp,
Serialize.cpp,
Tutorial readonly separator,
tutorial2.cpp,
help.qtf;

View 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);
}

View file

@ -0,0 +1,49 @@
#include "Tutorial.h"
void SpecialStream()
{
/// .Special streams
SizeStream szs;
szs << "1234567";
DUMP(szs.GetSize());
///
StringStream in("123456");
CompareStream cs(in);
cs.Put("12345");
DUMP(cs.IsEqual());
cs.Put("7");
DUMP(cs.IsEqual());
///
struct MyOutStream : OutStream {
virtual void Out(const void *data, dword size) {
DUMPHEX(String((const char *)data, size));
}
};
MyOutStream os;
os << "This is a test " << 12345;
os.Close();
///
StringStream ss1;
StringStream ss2;
TeeStream tee(ss1, ss2);
tee << "Tee stream test";
tee.Close();
DUMP(ss1.GetResult());
DUMP(ss2.GetResult());
///
static const char s[] = "Some line\nAnother line";
MemReadStream ms(s, sizeof(os));
while(!ms.IsEof())
DUMP(ms.GetLine());
///
}

View file

@ -0,0 +1,73 @@
#include "Tutorial.h"
void Stream()
{
/// .Streams basics
FileIn in(GetDataFile("test.txt"));
if(!in) {
LOG("Failed to open the file");
return;
}
String h;
int c;
while((c = in.Get()) >= 0)
h.Cat(c);
DUMP(h);
in.Seek(0);
while(!in.IsEof())
DUMP(in.GetLine());
in.Seek(0);
DUMP(in.Get(10));
in.Seek(0);
DUMP(in.Get(999999).GetCount());
in.Seek(0);
h = in.GetAll(100);
DUMP(h.GetCount());
h = in.GetAll(999999);
DUMP(h.IsVoid());
in.LoadThrowing();
try {
in.GetAll(999999);
}
catch(LoadingError) {
LOG("Loading error");
}
String fn = GetHomeDirFile("test");
FileOut out(fn);
if(!out) {
LOG("Failed to open the file");
return;
}
out << "Some number " << 321 << " and Point " << Point(1, 2);
out.Close();
if(out.IsError()) { // check whether file was properly written
LOG("Error");
return;
}
DUMP(LoadFile(fn));
FileAppend out2(fn);
out2 << "\nSomething more";
out2.Close();
DUMP(LoadFile(fn));
StringStream ss;
ss.Put32le(0x12345678);
ss.Put32be(0x12345678);
StringStream ss2(ss.GetResult());
DUMP(ss.Get32le());
DUMP(ss.Get32be());
DUMPHEX(ss.GetResult());
///
}

View file

@ -2,6 +2,7 @@
GUI_APP_MAIN
{
#if 0
SECTION("Basics");
DO(Logging);
DO(StringTutorial);
@ -52,6 +53,11 @@ GUI_APP_MAIN
DO(AsyncTutorial);
DO(CoPartitionTutorial);
DO(CoAlgoTutorial);
#endif
SECTION("Streams");
DO(Stream);
DO(SpecialStream);
MakeTutorial();
}