From 3575cbc543236abf6201b35a0c7020f03ae6b8f3 Mon Sep 17 00:00:00 2001 From: IlyaShurupov Date: Tue, 25 Jul 2023 18:47:00 +0300 Subject: [PATCH] Archiver Initial --- CMakeLists.txt | 2 +- CMakeSettings.json | 15 ++ CommandLine/private/CommandLine.cpp | 2 +- CommandLine/public/CommandLine.hpp | 2 +- Containers/public/Buffer.hpp | 67 ++++++++- Containers/public/Buffer2D.hpp | 34 ++++- Containers/public/List.hpp | 27 ++-- Containers/public/Map.hpp | 33 ++--- Containers/public/Tree.hpp | 31 +++- Containers/tests/ListTest.cpp | 14 +- Containers/tests/MapTest.cpp | 10 +- Containers/tests/Tests.hpp | 31 ---- Math/private/Camera.cpp | 2 +- Math/public/Vec.hpp | 32 ++--- Modules/.vs/slnx.sqlite | Bin 0 -> 90112 bytes Modules/CMakeLists.txt | 3 +- Modules/public/Archiver.hpp | 146 +++++++++++++++++++ Modules/public/Common.hpp | 29 +++- Modules/public/SizeCounter.hpp | 50 +++++++ Modules/tests/TestArchiver.cpp | 202 +++++++++++++++++++++++++++ Modules/tests/Tests.cpp | 12 +- TODO | 22 +-- Tokenizer/public/RegularExpression.h | 4 +- Tokenizer/public/Tokenizer.hpp | 4 +- Utils/tests/Tests.cpp | 2 +- 25 files changed, 652 insertions(+), 124 deletions(-) create mode 100644 CMakeSettings.json create mode 100644 Modules/.vs/slnx.sqlite create mode 100644 Modules/public/Archiver.hpp create mode 100644 Modules/public/SizeCounter.hpp create mode 100644 Modules/tests/TestArchiver.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index d0d6658..147af07 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -25,4 +25,4 @@ add_subdirectory(Externals) add_subdirectory(Connection) add_subdirectory(Graphics) -add_subdirectory(Objects) +#add_subdirectory(Objects) diff --git a/CMakeSettings.json b/CMakeSettings.json new file mode 100644 index 0000000..03cbf8d --- /dev/null +++ b/CMakeSettings.json @@ -0,0 +1,15 @@ +{ + "configurations": [ + { + "name": "x64-Debug", + "generator": "Ninja", + "configurationType": "Debug", + "inheritEnvironments": [ "msvc_x64_x64" ], + "buildRoot": "${projectDir}\\out\\build\\${name}", + "installRoot": "${projectDir}\\out\\install\\${name}", + "cmakeCommandArgs": "", + "buildCommandArgs": "", + "ctestCommandArgs": "" + } + ] +} \ No newline at end of file diff --git a/CommandLine/private/CommandLine.cpp b/CommandLine/private/CommandLine.cpp index f937e2a..35bfb35 100644 --- a/CommandLine/private/CommandLine.cpp +++ b/CommandLine/private/CommandLine.cpp @@ -93,7 +93,7 @@ CommandLine::Arg::~Arg() { } } -CommandLine::CommandLine(const init_list& args) { +CommandLine::CommandLine(const InitialierList& args) { bool optional_start = false; for (auto& arg : args) { diff --git a/CommandLine/public/CommandLine.hpp b/CommandLine/public/CommandLine.hpp index b0ad457..9573dc9 100644 --- a/CommandLine/public/CommandLine.hpp +++ b/CommandLine/public/CommandLine.hpp @@ -67,7 +67,7 @@ namespace tp { operator bool() { return mDescr != nullptr; } } mError; - CommandLine(const init_list& args); + CommandLine(const InitialierList& args); ~CommandLine(); void resetError() { mError.mDescr = nullptr; } diff --git a/Containers/public/Buffer.hpp b/Containers/public/Buffer.hpp index b889348..7888002 100644 --- a/Containers/public/Buffer.hpp +++ b/Containers/public/Buffer.hpp @@ -20,7 +20,7 @@ namespace tp { template class ConstSizeBuffer { - typedef SelCopyArg Arg; + typedef SelectValueOrReference Arg; private: tType mBuff[tSize]; @@ -29,6 +29,12 @@ namespace tp { public: ConstSizeBuffer() = default; + ~ConstSizeBuffer() { + for (auto i = 0; i < mLoad; i++) { + mBuff[i].~tType(); + } + } + public: [[nodiscard]] ualni size() const { return mLoad; } [[nodiscard]] ualni getBuffSize() const { return tSize; } @@ -75,13 +81,18 @@ namespace tp { mLoad--; } - ConstSizeBuffer& operator=(const tp::init_list& init) { + ConstSizeBuffer& operator=(const tp::InitialierList& init) { for (auto arg : init) { append(arg); } return *this; } + void clear() { + this->~ConstSizeBuffer(); + new (this) ConstSizeBuffer(); + } + public: class IteratorPointer { @@ -125,6 +136,24 @@ namespace tp { [[nodiscard]] Iterator end() const { return Iterator(mBuff + mLoad); } + + public: + template + void archiveWrite(tArchiver& ar) const { + ar << mLoad; + for (auto item : *this) { + ar << item.data(); + } + } + + template + void archiveRead(tArchiver& ar) { + clear(); + ar >> mLoad; + for (auto i = 0; i < mLoad; i++) { + ar >> mBuff[i]; + } + } }; template< @@ -135,7 +164,7 @@ namespace tp { ualni tMinSize = 4 > class Buffer { - typedef SelCopyArg Arg; + typedef SelectValueOrReference Arg; private: tType* mBuff; @@ -157,10 +186,14 @@ namespace tp { for (ualni i = 0; i < mLoad; i++) new (&mBuff[i]) tType(in.mBuff[i]); } + void clear() { + this->~Buffer(); + new (this) Buffer(); + } + Buffer& operator=(const Buffer& in) { if (this == &in) return *this; - this->~Buffer(); - new (this) Buffer(in); + clear(); return *this; } @@ -219,7 +252,7 @@ namespace tp { } public: - Buffer& operator=(const tp::init_list& init) { + Buffer& operator=(const tp::InitialierList& init) { // TODO : optimize for (auto arg : init) { append(arg); @@ -307,6 +340,28 @@ namespace tp { return Iterator(mBuff + mLoad); } + public: + template + void archiveWrite(tArchiver& ar) const { + ar << mLoad; + for (auto item : *this) { + ar << item.data(); + } + } + + template + void archiveRead(tArchiver& ar) { + clear(); + decltype(mLoad) len; + ar >> len; + for (auto i = len; i; i--) { + // TODO : optimize + if (mLoad == mSize) resizeBuffer(tResizePolicy(mSize)); + ar >> mBuff[mLoad]; + mLoad++; + } + } + private: void resizeBuffer(ualni newSize) { DEBUG_ASSERT(newSize >= mLoad) diff --git a/Containers/public/Buffer2D.hpp b/Containers/public/Buffer2D.hpp index d8e5205..32ab6cb 100644 --- a/Containers/public/Buffer2D.hpp +++ b/Containers/public/Buffer2D.hpp @@ -15,7 +15,7 @@ namespace tp { public: typedef ualni Index; typedef Pair Index2D; - typedef SelCopyArg tTypeArg; + typedef SelectValueOrReference tTypeArg; private: tAllocator mAlloc; @@ -40,6 +40,7 @@ namespace tp { ~Buffer2D() { deleteBuffer(); + mSize = { 0, 0 }; } explicit Buffer2D(Index2D aSize) { @@ -55,17 +56,17 @@ namespace tp { } inline tType& get(const Index2D& at) { - DEBUG_ASSERT(at.x < mSize.x && at.y < mSize.y && at.x >= 0 && at.y >= 0) + DEBUG_ASSERT(mBuff && at.x < mSize.x && at.y < mSize.y && at.x >= 0 && at.y >= 0) return *(mBuff + mSize.x * at.y + at.x); } inline const tType& get(const Index2D& at) const { - DEBUG_ASSERT(at.x < mSize.x && at.y < mSize.y && at.x >= 0 && at.y >= 0) + DEBUG_ASSERT(mBuff && at.x < mSize.x && at.y < mSize.y && at.x >= 0 && at.y >= 0) return *(mBuff + mSize.x * at.y + at.x); } inline void set(const Index2D& at, tTypeArg value) { - DEBUG_ASSERT(at.x < mSize.x && at.y < mSize.y && at.x >= 0 && at.y >= 0) + DEBUG_ASSERT(mBuff && at.x < mSize.x && at.y < mSize.y && at.x >= 0 && at.y >= 0) *(mBuff + mSize.x * at.y + at.x) = value; } @@ -77,10 +78,35 @@ namespace tp { } void assign(tType value) { + DEBUG_ASSERT(mBuff); Index len = mSize.x * mSize.y; for (Index i = 0; i < len; i++) { mBuff[i] = value; } } + + public: + template + void archiveWrite(tArchiver& ar) const { + ar << mSize; + for (auto i = 0; i < mSize.x; i++) { + for (auto j = 0; j < mSize.y; j++) { + ar << get(i, j); + } + } + } + + template + void archiveRead(tArchiver& ar) { + decltype(mSize) size; + deleteBuffer(); + ar >> size; + reserve(size); + for (auto i = 0; i < mSize.x; i++) { + for (auto j = 0; j < mSize.y; j++) { + ar >> get(i, j); + } + } + } }; } \ No newline at end of file diff --git a/Containers/public/List.hpp b/Containers/public/List.hpp index 470781b..2da3168 100644 --- a/Containers/public/List.hpp +++ b/Containers/public/List.hpp @@ -10,7 +10,7 @@ namespace tp { template class List { - typedef SelCopyArg TypeArg; + typedef SelectValueOrReference TypeArg; typedef ualni Index; public: @@ -73,7 +73,7 @@ namespace tp { List() = default; - List(const init_list& list) { operator=(list); } + List(const InitialierList& list) { operator=(list); } [[nodiscard]] inline Node* first() const { return mFirst; } [[nodiscard]] inline Node* last() const { return mLast; } @@ -221,7 +221,7 @@ namespace tp { return *this; } - List& operator+=(const init_list& list) { + List& operator+=(const InitialierList& list) { for (auto item : list) { pushBack(item); } @@ -235,7 +235,7 @@ namespace tp { return *this; } - List& operator=(const init_list& list) { + List& operator=(const InitialierList& list) { removeAll(); *this += list; return *this; @@ -295,22 +295,23 @@ namespace tp { } } - template - void write(Saver& file) const { - file.write(mLength); + public: + template + void archiveWrite(tArchiver& ar) const { + ar << mLength; for (auto item : *this) { - file.write(item.data()); + ar << item.data(); } } - template - void read(Loader& file) { + template + void archiveRead(tArchiver& ar) { removeAll(); - ualni len; - file.read(len); + decltype(mLength) len; + ar >> len; for (auto i = len; i; i--) { auto node = newNodeNotConstructed(); - file.read(node->data); + ar >> node->data; pushBack(node); } } diff --git a/Containers/public/Map.hpp b/Containers/public/Map.hpp index cac899e..eb80279 100644 --- a/Containers/public/Map.hpp +++ b/Containers/public/Map.hpp @@ -7,11 +7,11 @@ namespace tp { template - ualni DefaultHashFunc(SelCopyArg key) { + ualni DefaultHashFunc(SelectValueOrReference key) { return hash(key); } - template) = DefaultHashFunc, int tTableInitialSize = 4> + template) = DefaultHashFunc, int tTableInitialSize = 4> class Map { enum { @@ -20,8 +20,8 @@ namespace tp { MAP_MAX_LOAD_PERCENTAGE = 66, }; - typedef SelCopyArg KeyArg; - typedef SelCopyArg ValArg; + typedef SelectValueOrReference KeyArg; + typedef SelectValueOrReference ValArg; public: @@ -35,7 +35,8 @@ namespace tp { struct Idx { alni idx = -1; - operator bool() { return idx != -1; } + [[nodiscard]] bool isValid() const { return bool(*this); } + explicit operator bool() const { return idx != -1; } }; private: @@ -365,24 +366,24 @@ namespace tp { return mNSlots; } - template - void write(Saver& file) { - file.write(mNEntries); + template + void archiveWrite(Archiver& ar) const { + ar << mNEntries; for (auto item : *this) { - file.write(item->val); - file.write(item->key); + ar << item->val; + ar << item->key; } } - template - void read(Loader& file) { + template + void archiveRead(Archiver& ar) { removeAll(); - ualni len; - file.read(len); + decltype(mNSlots) len; + ar >> len; for (auto i = len; i; i--) { auto node = newNodeNotConstructed(); - file.read(node->val); - file.read(node->key); + ar >> node->val; + ar >> node->key; put(node); } } diff --git a/Containers/public/Tree.hpp b/Containers/public/Tree.hpp index dfeefc2..2dd1ec7 100644 --- a/Containers/public/Tree.hpp +++ b/Containers/public/Tree.hpp @@ -25,8 +25,8 @@ namespace tp { template class AvlTree { - typedef SelCopyArg KeyArg; - typedef SelCopyArg DataArg; + typedef SelectValueOrReference KeyArg; + typedef SelectValueOrReference DataArg; public: class Node { @@ -369,5 +369,32 @@ namespace tp { } bool isValid() { return findInvalidNode(head()) == nullptr; } + + template + void traverse(Node* node, bool after, tFunctor functor) { + if (!after) functor(node); + if (node->mLeft) traverse(node->mLeft, after, functor); + if (node->mRight) traverse(node->mRight, after, functor); + if (after) functor(node); + } + + void removeAll() { + traverse(mRoot, true, [this](Node* node) { + deleteNode(node); + }); + mRoot = nullptr; + mSize = 0; + } + + public: + template + void archiveWrite(tArchiver& file) const { + FAIL("not implemented") + } + + template + void archiveRead(tArchiver&) { + FAIL("not implemented") + } }; } \ No newline at end of file diff --git a/Containers/tests/ListTest.cpp b/Containers/tests/ListTest.cpp index e23deb4..394826f 100644 --- a/Containers/tests/ListTest.cpp +++ b/Containers/tests/ListTest.cpp @@ -2,6 +2,7 @@ #include "Tests.hpp" #include "Testing.hpp" +#include "Archiver.hpp" using namespace tp; @@ -53,18 +54,19 @@ TEST_DEF_STATIC(Copy) { list2.removeAll(); } -TEST_DEF_STATIC(SaveLoad) { +TEST_DEF_STATIC(Serialization) { tp::List list = { TestClass(1), TestClass(2), TestClass(3), TestClass(4) }; - TestFile file; + ArchiverExample<1024, false> write; + ArchiverExample<1024, true> read; - list.write(file); + write % list; list.removeAll(); - file.setAddress(0); + memCopy(read.mBuff, write.mBuff, sizeof(write.mBuff)); - list.read(file); + read % list; ualni i = 0; for (auto iter : list) { @@ -79,5 +81,5 @@ TEST_DEF_STATIC(SaveLoad) { TEST_DEF(List) { testSimplePointer(); testSimpleReference(); - testSaveLoad(); + testSerialization(); } \ No newline at end of file diff --git a/Containers/tests/MapTest.cpp b/Containers/tests/MapTest.cpp index d5ce1f7..1afd47e 100644 --- a/Containers/tests/MapTest.cpp +++ b/Containers/tests/MapTest.cpp @@ -3,6 +3,7 @@ #include "Tests.hpp" #include "Testing.hpp" #include "Map.hpp" +#include "Archiver.hpp" using namespace tp; @@ -100,17 +101,18 @@ TEST_DEF_STATIC(SaveLoad) { map.put(i, TestClass(i)); } - TestFile file; + ArchiverExample<1024, false> write; + ArchiverExample<1024, true> read; - map.write(file); + write % map; map.removeAll(); TEST(map.size() == 0); - file.setAddress(0); + memCopy(read.mBuff, write.mBuff, sizeof(write.mBuff)); - map.read(file); + read % map; TEST(map.size() == 10); diff --git a/Containers/tests/Tests.hpp b/Containers/tests/Tests.hpp index 3e47e0b..20f09ef 100644 --- a/Containers/tests/Tests.hpp +++ b/Containers/tests/Tests.hpp @@ -28,37 +28,6 @@ public: void setVal(tp::ualni val) { val1 = val; } }; -class TestFile { - tp::ualni mem[1024] = { 0 }; - tp::ualni address = 0; - -public: - TestFile() = default; - - template - void write(const Type& val) { - val.write(*this); - } - - template<> - void write(const tp::ualni& val) { - mem[address] = val; - address++; - } - - void setAddress(tp::ualni addr) { address = addr; } - - template - void read(Type& val) { - val.read(*this); - } - - template<> - void read(tp::ualni& val) { - val = mem[address]; - address++; - } -}; void testList(); void testMap(); diff --git a/Math/private/Camera.cpp b/Math/private/Camera.cpp index d024aab..a2c6800 100644 --- a/Math/private/Camera.cpp +++ b/Math/private/Camera.cpp @@ -67,7 +67,7 @@ Vec3F Camera::project(Vec2F normalized) { halnf w = halnf((mTarget - mPos).length()); Vec4 world_pos4(normalized.x * w, normalized.y * w, z, w); - return inv * world_pos4; + return Vec3F(inv * world_pos4); } Vec2F Camera::project(const Vec3F& world) { diff --git a/Math/public/Vec.hpp b/Math/public/Vec.hpp index 5c165e1..3f5b1d4 100644 --- a/Math/public/Vec.hpp +++ b/Math/public/Vec.hpp @@ -6,7 +6,7 @@ namespace tp { template class Vec { - typedef SelCopyArg TypeArg; + typedef SelectValueOrReference TypeArg; private: Type mBuff[tSize]; @@ -21,7 +21,7 @@ namespace tp { MODULE_SANITY_CHECK(gModuleMath) } - Vec(TypeArg val) { + explicit Vec(TypeArg val) { assign(val); } @@ -185,7 +185,7 @@ namespace tp { return dot(in); } - alnf length2() const { + [[nodiscard]] alnf length2() const { alnf sum = 0; for (ualni i = 0; i < tSize; i++) { Type val = get(i); @@ -194,7 +194,7 @@ namespace tp { return sum; } - alnf length() const { + [[nodiscard]] alnf length() const { return sqrt(length2()); } @@ -250,18 +250,18 @@ namespace tp { } template - Vec(TypeIn Vec[2]) { + explicit Vec(TypeIn Vec[2]) { x = (Type) Vec[0]; y = (Type) Vec[1]; } template - Vec(TypeIn val) { + explicit Vec(TypeIn val) { x = y = (Type) val; } template - Vec(Vec& Vec) { + explicit Vec(Vec& Vec) { x = (Type) Vec.x; y = (Type) Vec.y; } @@ -381,11 +381,11 @@ namespace tp { return { -y, x }; } - alnf length2() const { + [[nodiscard]] alnf length2() const { return (x * x + y * y); } - alnf length() const { + [[nodiscard]] alnf length() const { Type const tmp = (Type) (x * x + y * y); return sqrt(tmp); } @@ -419,9 +419,9 @@ namespace tp { Type z; // Initialization - Vec() {} + Vec() = default; - Vec(const Vec& in) { + explicit Vec(const Vec& in) { x = in[0]; y = in[1]; z = in[2]; @@ -433,13 +433,13 @@ namespace tp { z = aZ; } - Vec(Type Vec[3]) { + explicit Vec(Type Vec[3]) { x = Vec[0]; y = Vec[1]; z = Vec[2]; } - Vec(Type x) { + explicit Vec(Type x) { assign(x); } @@ -584,7 +584,7 @@ namespace tp { return (x * in.x + y * in.y + z * in.z); } - alnf length() const { + [[nodiscard]] alnf length() const { return sqrt((halnf) (x * x + y * y + z * z)); } @@ -632,11 +632,11 @@ namespace tp { z = (Type) (tmp * sinA + z * cosA); } - halnf angelX() const { + [[nodiscard]] halnf angelX() const { return (halnf) atan2(y, z); } - halnf angelY() const { + [[nodiscard]] halnf angelY() const { return (halnf) atan2(x, z); } }; diff --git a/Modules/.vs/slnx.sqlite b/Modules/.vs/slnx.sqlite new file mode 100644 index 0000000000000000000000000000000000000000..7b621f2557188917c5604ecb8b552dbc0710bfff GIT binary patch literal 90112 zcmeI4Piz}kdcbEi6e&?6pAto84HfYONMNz{$l`x;gA}Eyk_>TF|Zpn#rD=)+so#%heZ$V0=p=BD0)~FMYo3n4T>JRD1rh-fgaihn?v85 znaBA{Q!BSBtb7DC1rz98656Q8lPeo7?x#zi{}}JnZ|!t z@E?6G;tv6OgWp-(=W!pWnbo}%sfb0tBr>m~U(SCqzc6<%{Q2x>Ghc?d;12iwsjsJM zQ}2(f0@7a@0y9L@)3>9c*aLw*-Rbrf^-29?zdh)!tDSDYt91wcmhrLwXrtRx%Sx;4 zCxl8hvDgs#a&=w&fZsb)9nDCX$FHrbSH8)w>^Yll#!r_bp;!s4dBT3J*X z17P)ju~xcUtgR$7nfORy7gt(jvc2Q7CGTZzSs_L$}tWmz2# zyX|g|ba8~$E#|U8kDS$IJ3!(n^^>8d?vD_?ELfzWl zDQfQyZK&YLcxi&i|NWi3adLp8B#l(G3F>)`?!G$Ir2T^?Pl89Iyon^&rm?+R8a~pDNTRK$xT8o5jcixQe__?L*HQ0vIEk-5`eoyC{_;IGI;4VOH z`Sf-$6#EcIz7s3**}Twf+FQ~V|7_wRvp?n($X#qgZ4>(Tf=(<=+owgIW(gI4g@Y3m zwn|P=&JTS2+1-Ess%Fi$p89N46tkdHdnNN|| zIi>_J7val8!ludJE_sf00h2$1g>xa z<`ycG^EteqM-DhjgQLEdc$S=+X5M{XAbS9Mu5l(+H+K1lDOHu%vPoG;q_Zg@oy)HY zQc6|?HJeGOYlTcMtK#H#Dk*34IU$)($U<7qDuR@h5<*JJ=hE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{Q%P15}rP$AG zyCqK2K1>(Jx%-HTug{Niy^ec^RV>{

E^nU;chiOH6jOUwO;Y&GUaZ5?S3j&lcba zw`ri3ZvVdv5rXR$ zfWwb-?R3246pWs0eESYR;Q#+Ge3Jtj1Oh++2mk>f00e*l5C8%|00;m9AaLOXVEupL z$e}?X00e*l5C8%|00;m9AOHk_01yBI7ft|v|L?+)LxVs72mk>f00e*l5C8%|00;m9 pAOHj|oB*8vUpR7T5C{MPAOHk_01yBIKmZ5;0U!VbfWU + class ArchiverTemplate { + public: + struct HasFunc { + typedef ArchiverTemplate& ArchRef; + template struct Write : FalseType {}; + template struct Write()().archiveWrite(DeclareValue()()))>> : TrueType {}; + + template struct Read : FalseType {}; + template struct Read()().archiveRead(DeclareValue()()))>> : TrueType {}; + + template struct Archive : FalseType {}; + template struct Archive()().archive(DeclareValue()()))>> : TrueType {}; + + template + struct AssertCombinations { + static constexpr auto assert() { + if (HasFunc::template Read::value != HasFunc::template Write::value) return false; + if (HasFunc::template Read::value) { + if (HasFunc::template Read::value == HasFunc::template Archive::value) return false; + } + return true; + } + }; + }; + + public: + virtual void writeBytes(const int1* val, ualni size) = 0; + virtual void readBytes(int1* val, ualni size) = 0; + + public: + ArchiverTemplate() = default; + + // check if type has explicit write method. if not write as bytes + template + void operator <<(const Type& val) { + static_assert(!tRead); + static_assert(HasFunc::template AssertCombinations::assert()); + if constexpr (HasFunc::template Write::value) { + val.archiveWrite(*this); + } else { + writeBytes((const int1*) &val, sizeof(val)); + } + } + + // check if type has explicit read method. if not read as bytes + template + void operator >>(Type& val) { + static_assert(tRead); + static_assert(HasFunc::template AssertCombinations::assert()); + if constexpr (HasFunc::template Read::value) { + val.archiveRead(*this); + } else { + readBytes((int1*) &val, sizeof(val)); + } + } + + // check if type has explicit archive method. if not read/write as bytes + template + void operator %(Type& val) { + static_assert(HasFunc::template AssertCombinations::assert()); + if constexpr (HasFunc::template Archive::value) { + val.archive(*this); + } else { + if constexpr (tRead) { + operator>>(val); + } else { + operator<<(val); + } + } + } + + template + void operator %(const Type& val) { + static_assert(HasFunc::template AssertCombinations::assert()); + if constexpr (HasFunc::template Archive::value) { + ((Type&)val).archive(*this); + } else { + if constexpr (tRead) { + operator>>(val); + } else { + operator<<(val); + } + } + } + }; + + template + class ArchiverExample : public ArchiverTemplate { + public: + ArchiverExample() = default; + + protected: + void incrementAddresses(ualni size) { + if (mFirstNotWritten == mAddress) mFirstNotWritten += size; + mAddress += size; + } + + void writeBytes(const int1* val, ualni size) override { + for (auto i = 0; i < size; i++) mBuff[mAddress + i] = val[i]; + incrementAddresses(size); + } + + void readBytes(int1* val, ualni size) override { + for (auto i = 0; i < size; i++) val[i] = mBuff[mAddress + i]; + incrementAddresses(size); + } + + template + static void test() { + const tType val; + tType res; + + res.change(); + + ArchiverExample write; + ArchiverExample read; + + write % val; + + for (auto i = 0; i < tMaxMemory; i++) read.mBuff[i] = write.mBuff[i]; + + read % res; + + if (val != res) { + throw "test failed"; + } + } + + public: + int1 mBuff[tMaxMemory]{}; + + private: + ualni mAddress = 0; + ualni mFirstNotWritten = 0; + }; +} \ No newline at end of file diff --git a/Modules/public/Common.hpp b/Modules/public/Common.hpp index 047f964..06c94ca 100644 --- a/Modules/public/Common.hpp +++ b/Modules/public/Common.hpp @@ -4,15 +4,40 @@ #include "Environment.hpp" #include "TypeInfo.hpp" #include +#include +#include namespace tp { template - using init_list = std::initializer_list; + using InitialierList = std::initializer_list; // Selects whether to pass by constant reference or by value template - using SelCopyArg = typename TypeSelect<(sizeof(tType) > sizeof(tp::alni)), const tType&, tType>::Result; + using SelectValueOrReference = typename TypeSelect<(sizeof(tType) > sizeof(tp::alni)), const tType&, tType>::Result; + template + using VoidType = void; + + template + struct Reference { + tType& operator()() {} + }; + + template + struct DeclareValue { + tType operator()() {} + }; + + struct TrueType { + static constexpr auto value = true; + }; + + struct FalseType { + static constexpr auto value = false; + }; +} + +namespace tp { ualni next2pow(ualni v); uhalni next2pow(uhalni v); ufalni next2pow(ufalni v); diff --git a/Modules/public/SizeCounter.hpp b/Modules/public/SizeCounter.hpp new file mode 100644 index 0000000..a44895d --- /dev/null +++ b/Modules/public/SizeCounter.hpp @@ -0,0 +1,50 @@ +#pragma once + +#include "Common.hpp" + +namespace tp { + + // TODO + template + class SizeCounter { + typedef SizeCounter& ArchRef; + template struct HasSizeCounter : FalseType {}; + template struct HasSizeCounter()().count(DeclareValue()()))>> : TrueType {}; + + public: + SizeCounter() = default; + + template + void count(const Type& val) { + if constexpr (HasSizeCounter::value) { + val.count(*this); + } else { + if constexpr (tRecursive) { + count(sizeof(val)); + } else { + mSize += sizeof(Type); + } + } + } + + void countSizeUnused(ualni size) { + mSizeUnused += size; + } + + void countSize(ualni size) { + mSize += size; + } + + [[nodiscard]] ualni getSizeUnused() const { + return mSizeUnused; + } + + [[nodiscard]] ualni getSize() const { + return mSize; + } + + private: + ualni mSize = 0; + ualni mSizeUnused = 0; + }; +} \ No newline at end of file diff --git a/Modules/tests/TestArchiver.cpp b/Modules/tests/TestArchiver.cpp new file mode 100644 index 0000000..3c9ee68 --- /dev/null +++ b/Modules/tests/TestArchiver.cpp @@ -0,0 +1,202 @@ + +#include "Archiver.hpp" +#include +#include + +using namespace tp; + +bool sFailed = false; + +struct Undef { + char character1 = 'a'; + bool boolean = true; + ualni integer = 321; + char character2 = 'a'; + + void change() { + character1++; + character2--; + integer++; + boolean = !boolean; + } + + [[nodiscard]] bool operator!=(const Undef& in) const { + if (character1 != in.character1) return true; + if (character2 != in.character2) return true; + if (integer != in.integer) return true; + if (boolean != in.boolean) return true; + return false; + } +}; + + +struct SimpleArchive { + char character = 'a'; + ualni integer = 123; + bool boolean = true; + Undef undef; + + template + void archive(tArchiver& ar) { + ar % character; + ar % integer; + ar % boolean; + ar % undef; + } + + void change() { + character++; + integer++; + boolean = !boolean; + undef.change(); + } + + [[nodiscard]] bool operator!=(const SimpleArchive& in) const { + if (character != in.character) return true; + if (integer != in.integer) return true; + if (boolean != in.boolean) return true; + if (undef != in.undef) return true; + return false; + } +}; + +struct Simple { + char character = 'a'; + ualni integer = 123; + bool boolean = true; + Undef undef; + + template + void archiveRead(tArchiver& ar) { + ar >> character; + ar >> integer; + ar >> boolean; + ar >> undef; + } + + template + void archiveWrite(tArchiver& ar) const { + ar << character; + ar << integer; + ar << boolean; + ar << undef; + } + + void change() { + character++; + integer++; + boolean = !boolean; + undef.change(); + } + + [[nodiscard]] bool operator!=(const Simple& in) const { + if (character != in.character) return true; + if (integer != in.integer) return true; + if (boolean != in.boolean) return true; + if (undef != in.undef) return true; + return false; + } +}; + +template +struct Set { + tType buff[10]; + ualni len = 5; + + template + void archiveRead(tArchiver& ar) { + ar >> len; + for (auto i = 0; i < len; i++) { + ar >> buff[i]; + } + } + + template + void archiveWrite(tArchiver& ar) const { + ar << len; + for (auto i = 0; i < len; i++) { + ar << buff[i]; + } + } + + void change() { + len = 2; + for (auto i = 0; i < len; i++) { + buff[i].change(); + } + } + + [[nodiscard]] bool operator!=(const Set& in) const { + for (auto i = 0; i < len; i++) { + if (buff[i] != in.buff[i]) return true; + } + + if (len != in.len) return true; + return false; + } +}; + + +template +struct Complex { + Undef undef; + Set set; + + template + void archive(tArchiver& ar) { + ar % set; + ar % undef; + } + + void change() { + undef.change(); + set.change(); + } + + [[nodiscard]] bool operator!=(const Complex& in) const { + return undef != in.undef || set != in.set; + } +}; + +template +void test() { + constexpr auto size = 1024; + + const tType val; + tType res; + + res.change(); + + ArchiverExample write; + ArchiverExample read; + + write % val; + + for (auto i = 0; i < size; i++) read.mBuff[i] = write.mBuff[i]; + + read % res; + + if (val != res) { + printf("Test %s Failed\n", typeid(tType).name()); + sFailed = true; + } +} + +template struct HasArchive : FalseType {}; +template struct HasArchive()().archive(DeclareValue()()))>> : TrueType {}; + +void testArchiver() { + + printf("has archive method - %i\n", HasArchive>::value); + printf("has archive method - %i\n", ArchiverExample<10, false>::HasFunc::Archive::value); + printf("has archive method - %i\n", ArchiverExample<10, false>::HasFunc::Archive::value); + + test(); + test(); + test>(); + test>(); + + if (sFailed) { + exit(1); + } +} diff --git a/Modules/tests/Tests.cpp b/Modules/tests/Tests.cpp index 5910731..474d6d7 100644 --- a/Modules/tests/Tests.cpp +++ b/Modules/tests/Tests.cpp @@ -1,7 +1,7 @@ #include "Module.hpp" -#include "Common.hpp" +void testArchiver(); int main() { tp::ModuleManifest* ModuleDependencies[] = { &tp::gModuleBase, nullptr }; @@ -13,10 +13,12 @@ int main() { ASSERT(tp::gEnvironment.mWidth == tp::Environment::ArchWidth::X64); - ASSERT(tp::max(2, 1) == 2); - ASSERT(tp::max(-1, 0) == 0); - ASSERT(tp::max(0, -1) == 0); - ASSERT(tp::min(0, -1) == -1); + ASSERT(tp::max(2, 1) == 2) + ASSERT(tp::max(-1, 0) == 0) + ASSERT(tp::max(0, -1) == 0) + ASSERT(tp::min(0, -1) == -1) + + testArchiver(); TestModule.deinitialize(); } \ No newline at end of file diff --git a/TODO b/TODO index c37edc4..47a4ae9 100644 --- a/TODO +++ b/TODO @@ -1,7 +1,7 @@ -Testing ! -Serialization ! +Testing !! Objects: + Testing Serialization Alloc Size queries Compile !! @@ -16,25 +16,29 @@ Objects: rename classes move functions around -Storage: - Finish networking - Enable preloads - Graphics: Window event system Graphics API FPS count & Performance control -Utils: - Stack trace fixes - Containers: + Test generatePermutations Add variant size buffer Buffer add resize function Buffer improvements + Buff serialization + Tree serialization + Write Traverse Avl test Add mem leakage tests Add check copy times AVL dont copy data just swap +Storage: + Finish networking + Enable preloads + +Utils: + Stack trace fixes + TextEditor: Implement diff --git a/Tokenizer/public/RegularExpression.h b/Tokenizer/public/RegularExpression.h index 07f7bbc..322cd6a 100644 --- a/Tokenizer/public/RegularExpression.h +++ b/Tokenizer/public/RegularExpression.h @@ -342,7 +342,7 @@ namespace tp::RegEx { return compileUtil(regex, state); } - Node compile(Graph& aGraph, init_list> aRules) { + Node compile(Graph& aGraph, InitialierList> aRules) { mGraph = &aGraph; auto left = mGraph->addVertex(); @@ -513,7 +513,7 @@ namespace tp::RegEx { } template - CompileError compile(NFA& out, const init_list>& rules) { + CompileError compile(NFA& out, const InitialierList>& rules) { Compiler compiler; compiler.compile(out, rules); return compiler.mError; diff --git a/Tokenizer/public/Tokenizer.hpp b/Tokenizer/public/Tokenizer.hpp index cdee460..bf32739 100644 --- a/Tokenizer/public/Tokenizer.hpp +++ b/Tokenizer/public/Tokenizer.hpp @@ -25,7 +25,7 @@ namespace tp { MODULE_SANITY_CHECK(gModuleTokenizer) } - void build(const init_list>& rules) { + void build(const InitialierList>& rules) { NFA nfa; mError = RegEx::compile(nfa, rules); @@ -89,7 +89,7 @@ namespace tp { SimpleTokenizer() = default; - void build(const init_list>& rules) { + void build(const InitialierList>& rules) { mTokenizer.build(rules); } diff --git a/Utils/tests/Tests.cpp b/Utils/tests/Tests.cpp index 79c6c18..d3e1a49 100644 --- a/Utils/tests/Tests.cpp +++ b/Utils/tests/Tests.cpp @@ -17,7 +17,7 @@ void printSnapshot(const tp::CallStackCapture::CallStack* snapshot) { } void common() { - gCSCapture->getSnapshot(); + auto tmp = gCSCapture->getSnapshot(); } void first() {