diff --git a/Modules/public/SizeCounter.hpp b/Modules/public/SizeCounter.hpp index a44895d..e861738 100644 --- a/Modules/public/SizeCounter.hpp +++ b/Modules/public/SizeCounter.hpp @@ -1,9 +1,32 @@ #pragma once -#include "Common.hpp" +#include "Archiver.hpp" namespace tp { + class SaveSizeCounter : public ArchiverTemplate { + public: + SaveSizeCounter() = default; + + template < typename tType> + static ualni calc(const tType& val) { + SaveSizeCounter cnt; + cnt << val; + return cnt.mSize; + } + + protected: + void writeBytes(const int1* val, ualni size) override { mSize += size; } + void readBytes(int1* val, ualni size) override {} + + [[nodiscard]] ualni getSize() const { return mSize; } + + private: + ualni mSize = 0; + }; + + + // TODO template class SizeCounter { diff --git a/Objects/private/core/objectsave.cpp b/Objects/private/core/objectsave.cpp index dc67e1e..5da599f 100644 --- a/Objects/private/core/objectsave.cpp +++ b/Objects/private/core/objectsave.cpp @@ -221,7 +221,7 @@ namespace obj { ndf << tp::String(in->type->name); // allocate for object file header - ndf.setFreeAddress(ndf.getFreeAddress() + sizeof(ObjectFileHead) + tp::String::Logic::calcLength(in->type->name) + sizeof(tp::String::Logic::Index)); + ndf.setFreeAddress(ndf.getFreeAddress() + sizeof(ObjectFileHead) + tp::SaveSizeCounter::calc(tp::String(in->type->name))); // calc max size needed for saving all hierarchy of types tp::alni file_alloc_size = objsize_file_util(in, in->type); diff --git a/Objects/private/core/scriptsection.cpp b/Objects/private/core/scriptsection.cpp index 030e95d..7cf9879 100644 --- a/Objects/private/core/scriptsection.cpp +++ b/Objects/private/core/scriptsection.cpp @@ -91,8 +91,7 @@ tp::alni ScriptSection::save_script_table_to_file_size(ScriptSection* self, Arch } // instructions - ASSERT(false) - // size += iter->mBytecode.mInstructions.saveSize(); + size += tp::SaveSizeCounter::calc(iter->mBytecode.mInstructions); } size += sizeof(tp::alni); // objects mem offset @@ -125,7 +124,6 @@ void ScriptSection::save_script_table_to_file(ScriptSection* self, ArchiverOut& } // mInstructions - ASSERT(false) file << iter->mBytecode.mInstructions; } @@ -163,8 +161,7 @@ void load_constants(ScriptSection* self, ArchiverIn& file, tp::alni start_addr) } // instructions - ASSERT(false) - //file.setAddress(file.getAddress() + script->mBytecode.mInstructions.saveSize()); + file.setAddress(file.getAddress() + tp::SaveSizeCounter::calc(script->mBytecode.mInstructions)); } file.setAddress(addres); diff --git a/Objects/private/primitives/dictobject.cpp b/Objects/private/primitives/dictobject.cpp index f83aea0..3848888 100644 --- a/Objects/private/primitives/dictobject.cpp +++ b/Objects/private/primitives/dictobject.cpp @@ -44,9 +44,7 @@ alni DictObject::save_size(DictObject* self) { for (auto item : self->items) { // string length - auto size = item->key.size(); - save_size += size * sizeof(*item->key.read()); - save_size += sizeof(decltype(size)); + save_size += tp::SaveSizeCounter::calc(item->key); // object file adress save_size += sizeof(alni); diff --git a/Objects/private/primitives/stringobject.cpp b/Objects/private/primitives/stringobject.cpp index 58f9947..3c44b49 100644 --- a/Objects/private/primitives/stringobject.cpp +++ b/Objects/private/primitives/stringobject.cpp @@ -50,11 +50,7 @@ alnf StringObject::to_float(StringObject* self) { } static alni save_size(StringObject* self) { - alni save_size = 0; - auto size = self->val.size(); - save_size += (size) * sizeof(*self->val.read()); - save_size += sizeof(decltype(size)); - return save_size; + return tp::SaveSizeCounter::calc(self->val); } static void save(StringObject* self, ArchiverOut& file_self) { diff --git a/Objects/public/core/object.h b/Objects/public/core/object.h index 29ae4fa..345a9e9 100644 --- a/Objects/public/core/object.h +++ b/Objects/public/core/object.h @@ -12,6 +12,7 @@ #include "core/typemethods.h" #include "Archiver.hpp" +#include "SizeCounter.hpp" //#include "interpreter/interpreter.h"