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 31ab348..13b04be 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,29 @@ 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) @@ -318,4 +374,36 @@ namespace tp { mSize = newSize; } }; + + + template + void generatePermutations(const Buffer>& in, Buffer>& out) { + typedef long long Idx; + + // sanity check + for (const auto & vec : in) { + if (!vec.size()) return; + } + + out.resize(in.size()); + auto len = Idx(1); + for (const auto & vec : in) len *= (Idx) vec.size(); + for (auto i = 0; i < in.size(); i++) out[i].resize(len); + + auto dub = Idx(1); + for (auto power = (Idx) in.size() - 1; power >= 0; power--) { + auto& vec = in[power]; + long long i = 0; + while (i < len) { + for (auto val : vec) { + for (auto j = 0; j < dub; j++) { + out[power][i] = val; + i++; + } + } + } + dub *= (Idx) vec.size(); + } + } + } \ No newline at end of file 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 0000000..7b621f2 Binary files /dev/null and b/Modules/.vs/slnx.sqlite differ diff --git a/Modules/CMakeLists.txt b/Modules/CMakeLists.txt index c3c6221..519f53f 100644 --- a/Modules/CMakeLists.txt +++ b/Modules/CMakeLists.txt @@ -12,7 +12,8 @@ target_include_directories(${PROJECT_NAME} PUBLIC ./public/) ### -------------------------- Tests -------------------------- ### enable_testing() -add_executable(${PROJECT_NAME}Tests ${CMAKE_CURRENT_SOURCE_DIR}/tests/Tests.cpp) +file(GLOB SOURCES_TEST "./tests/*.cpp") +add_executable(${PROJECT_NAME}Tests ${SOURCES_TEST}) target_link_libraries(${PROJECT_NAME}Tests ${PROJECT_NAME}) add_test(NAME ${PROJECT_NAME}Tests COMMAND ${PROJECT_NAME}Tests) diff --git a/Modules/public/Archiver.hpp b/Modules/public/Archiver.hpp new file mode 100644 index 0000000..825bb1b --- /dev/null +++ b/Modules/public/Archiver.hpp @@ -0,0 +1,146 @@ +#pragma once + +#include "Common.hpp" + +namespace tp { + + // Used to transfer data to or from some sort of archive + // Abstracts interface so that actual archive must only overload readBytes and writeBytes + // The main idea is that it transfers data as bytes only if this type of data does not have special API functions + template + 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/Assert.hpp b/Modules/public/Assert.hpp index c85ac87..f80ad71 100644 --- a/Modules/public/Assert.hpp +++ b/Modules/public/Assert.hpp @@ -18,8 +18,6 @@ namespace tp { #define DEBUG_ASSERT(exp) {} #endif - - #if defined(ENV_OS_WINDOWS) #define DEBUG_BREAK(expr) if (expr) { __debugbreak(); } #elif defined(ENV_OS_ANDROID) 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 cf4062f..47a4ae9 100644 --- a/TODO +++ b/TODO @@ -1,7 +1,7 @@ -Testing ! -Serialization ! +Testing !! Objects: + Testing Serialization Alloc Size queries Compile !! @@ -16,24 +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() {