From af77974e91be592348a3f1576e1fea305093d8a7 Mon Sep 17 00:00:00 2001 From: peri4 Date: Mon, 9 May 2022 14:21:38 +0300 Subject: [PATCH] first try --- main.cpp | 382 ++++++++++++++++++++++--------------------------------- 1 file changed, 154 insertions(+), 228 deletions(-) diff --git a/main.cpp b/main.cpp index ca852d33..83784703 100644 --- a/main.cpp +++ b/main.cpp @@ -1,235 +1,161 @@ #include "pip.h" -static const char * smallstr = "abcdef"; - -static const char * bigstr = "zsxdcgfhvbncjdbasljcvavcjadnwnxudvbabdhjlavudvdaljsvclavjlasdhvcjhldsavhjldasvfjlhsavdjhavdjhvfjhldasvfjlasvfhjldasvfhjasvfdjdasfhvjldasvhfjlasvfhjlahsvdfhjfvfvdjalsvfjlhasdvfdjsalvfhhjldasvfdjhaldsvfhjdvsfjhlavfjhlavfladlsvfjlasdvfdhjlavfhjldasvfhjlavfhjldvfhjlalsdvfjlhvasfhjlvchjlavchjladvchjldladvschjlladscvjlhdcahjchjllcahjllvcdjladsvhldbcljadsbcjdhlsachjlvdsa hjlcldajc hljdascbhaldb cldhashd l cajlhs chdsbfhlbfdasdffadsfjkbfkjldsabflhbcldhsbhclabchljadsbchldahsbcladsbhclhabhasbclasbdhl"; - -PIKbdListener kbd(0, 0, false); -#include -#include - - using namespace PICoutManipulators; + +template +class BinaryStream { +public: + BinaryStream(P & parent): p(parent) {} + void append(const void * d, size_t s) {p.appendImp(d, s);} + void append(int i) {append(&i, sizeof(i));} + void append(uchar byte) {p.appendImp(byte);} + void take(void * d, size_t s) {p.takeImp(d, s);} + uchar takeByte() {return p.takeByteImp();} + int takeInt() {int r = 0; take(&r, sizeof(r)); return r;} +private: + P & p; +}; + +template +class BinaryStreamRef { +public: + BinaryStreamRef(BinaryStream

& s): p(s) {} + BinaryStream

& ref() {return p;} + BinaryStream

& p; +}; + + +template inline BinaryStream

& operator <<(BinaryStreamRef

s, const T & v) {s.ref() << v; return s.p;} +template inline BinaryStream

& operator >>(BinaryStreamRef

s, T & v) {s.ref() >> v; return s.p;} + +template inline BinaryStream

& operator <<(BinaryStream

& s, const bool v) {s.append((uchar)v); return s;} +template inline BinaryStream

& operator <<(BinaryStream

& s, const char v) {s.append((uchar)v); return s;} +template inline BinaryStream

& operator <<(BinaryStream

& s, const uchar v) {s.append((uchar)v); return s;} +template::value, int>::type = 0> +inline BinaryStreamRef

operator <<(BinaryStream

& s, const T & v) { + s.append(&v, sizeof(v)); + return s; +} + +template inline BinaryStream

& operator >>(BinaryStream

& s, bool & v) {v = s.takeByte(); return s;} +template inline BinaryStream

& operator >>(BinaryStream

& s, char & v) {v = s.takeByte(); return s;} +template inline BinaryStream

& operator >>(BinaryStream

& s, uchar & v) {v = s.takeByte(); return s;} +template::value, int>::type = 0> +inline BinaryStreamRef

operator >>(BinaryStream

& s, T & v) { + s.take(&v, sizeof(v)); + return s; +} + + +//! \~english Store operator for PIVector of any trivial copyable type +//! \~russian Оператор сохранения для PIVector тривиальных типов +template::value, int>::type = 0, + typename std::enable_if< std::is_same&>() << std::declval()), BinaryStreamRef

>::value, int>::type = 0> +inline BinaryStream

& operator <<(BinaryStream

& s, const PIVector & v) { + piCout << "<< vector trivial default"; + s.append((int)v.size()); + s.append(v.data(), v.size() * sizeof(T)); + return s; +} +template::value, int>::type = 0, + typename std::enable_if&>() << std::declval()), BinaryStreamRef

>::value, int>::type = 0> +inline BinaryStream

& operator <<(BinaryStream

& s, const PIVector & v) { + piCout << "<< vector trivial custom"; + s.append((int)v.size()); + for (const auto & i: v) s << i; + return s; +} + + +//! \~english Restore operator for PIVector of any trivial copyable type +//! \~russian Оператор извлечения для PIVector тривиальных типов +template::value, int>::type = 0, + typename std::enable_if< std::is_same&>() >> std::declval()), BinaryStreamRef

>::value, int>::type = 0> +inline BinaryStream

& operator >>(BinaryStream

& s, PIVector & v) { + piCout << ">> vector trivial default"; + int sz = s.takeInt(); + v._resizeRaw(sz); + if (sz > 0) + s.take(v.data(), sz*sizeof(T)); + return s; +} +template::value, int>::type = 0, + typename std::enable_if&>() >> std::declval()), BinaryStreamRef

>::value, int>::type = 0> +inline BinaryStream

& operator >>(BinaryStream

& s, PIVector & v) { + piCout << ">> vector trivial custom"; + int sz = s.takeInt(); + v._resizeRaw(sz); + for (int i = 0; i < sz; ++i) s >> v[i]; + return s; +} + + +class ByteArray: public BinaryStream { +public: + ByteArray(): BinaryStream(*this) {} + PIByteArray data; + void appendImp(const void * d, size_t s) { + data.append(d, s); + } + void appendImp(uchar byte) { + data.append(byte); + } + void takeImp(void * d, size_t s) { + memcpy(d, data.data(), s); + data.remove(0, s); + } + uchar takeByteImp() { + return data.take_front(); + } +}; + +class File: public BinaryStream { +public: + File(): BinaryStream(*this) {} + PIFile file; + void appendImp(const void * d, size_t s) { + file.write(d, s); + } + void appendImp(uchar byte) { + file.writeBinary(byte); + } + void takeImp(void * d, size_t s) { + file.read(d, s); + } + uchar takeByteImp() { + return (uchar)file.readChar(); + } +}; + + +struct TS { + TS(int v0 = -1, float v1 = -1){i = v0; f = v1;} + int i; + float f; +}; +PICout operator << (PICout c, const TS & v) {c << '{' << v.i << ", " << v.f << '}'; return c;} + +template inline BinaryStream

& operator <<(BinaryStream

& s, const TS & v) {s << v.i; return s;} +template inline BinaryStream

& operator >>(BinaryStream

& s, TS & v) {s >> v.i; return s;} + int main(int argc, char * argv[]) { - /*PIVariant vi; - piCout << vi; - vi = 1; - piCout << vi << vi.toString(); - vi = "string"; - piCout << vi << vi.toStringList(); - vi = PIStringList("2"); - piCout << vi << vi.toInt();*/ - - /*auto cmp = [](const PIConstChars & c0, const PIConstChars & c1){ - PICout(DefaultControls) << c0 << "==" << c1 << "->" << (c0 != c1); - }; - PIConstChars s; - PICout(DefaultControls | AddQuotes) << s; - s = PIConstChars("str", 2); - PICout(DefaultControls | AddQuotes) << s; - cmp(PIConstChars(), PIConstChars()); - cmp(PIConstChars("str"), PIConstChars()); - cmp(PIConstChars(), PIConstChars("s_tr")); - cmp(PIConstChars("str1"), PIConstChars("str")); - cmp(PIConstChars("str"), PIConstChars("str2")); - cmp(PIConstChars("str"), PIConstChars("sTr")); - cmp(PIConstChars("str"), PIConstChars("str")); - cmp(PIConstChars("1a"), PIConstChars("1b")); - cmp(PIConstChars("1c"), PIConstChars("1b"));*/ - - /*PIMap map; - map[PIConstChars()] = 0; - map[PIConstChars()] = -1; - map["_2"] = 22; - map["1"] = 11; - map["__3"] = 33; - map["10"] = 10; - map[""] = 999; - piCout << map; - - piCout << PIConstChars().toString(); - piCout << PIConstChars("").toString(); - piCout << PIConstChars("str").toString(); - PIConstChars s = " 1 2 \t"; - piCout << "trim:"; - PICout(DefaultControls | AddQuotes) << s.trimmed(); - PICout(DefaultControls | AddQuotes) << s; - PICout(DefaultControls | AddQuotes) << s.isEmpty();*/ - - /*piCout << PIIODevice::availablePrefixes(); - auto * d = PIIODevice::createFromFullPath("ser://COM1"); - piCout << ""; - piCout << d; - d->dump(); - d = PIIODevice::createFromFullPath("eth://udp:127.0.0.1:5000"); - piCout << ""; - piCout << d; - d->dump();*/ - - piCout << ""; - dumpApplication(); - - return 0; - - auto rstr = PIString::fromUTF8("ascii русский!"); - /*for (PIChar c: rstr) - std::wcout << c.toWChar(); - std::wcout << std::endl;*/ - piCout << PIChar::fromUTF8("│"); - piCout << PICoutManipulators::Hex << (int)PIChar::fromUTF8("│").toWChar(); - piCout << rstr; - - /*char rs[] = "й"; - piCout << PIString(PIChar::fromUTF8(rs)); - std::cout << sizeof(rs) << " chars "; - for (int i = 0; i < sizeof(rs); ++i) - std::cout << "'" << (char)(rs[i]) << "' " << (int)(uchar)(rs[i]); - std::cout << std::endl; - CONNECTL(&kbd, keyPressed, ([](PIKbdListener::KeyEvent k, void*){ - ; - piCout << k.key << PIChar((ushort)k.key); - })); - kbd.start(); - WAIT_FOR_EXIT;*/ - //return 0; - static const int cc = 1000000; - PITimeMeasurer tm; - int l = 0; - tm.reset(); - for(int i=0; i vi({TS(1,20), TS(3,40)}); + f << vi; + //piCout << "s" << ba.data; + vi.fill(TS()); + vi.clear(); + f.file.seekToBegin(); + f >> vi; + piCout << "data" << vi; + //piCout << "r" << ba.data; return 0; }