From e39a4db1ada6d0d98e8766e6e4b3c79ba9d6ea1c Mon Sep 17 00:00:00 2001 From: kostyafip <124047828+kostyafip@users.noreply.github.com> Date: Thu, 20 Jul 2023 11:44:23 +0300 Subject: [PATCH] Rest object references are moved to std::shared_ptr<> --- blobstamper/galley.cpp | 35 +++++++++++++++++----------------- blobstamper/galley.h | 22 ++++++++++----------- blobstamper/stamp_enumerator.h | 4 ++-- t/130-stamp_enumerator.cpp | 4 ++-- t/300-galley.cpp | 18 ++++++++--------- t/Makefile | 2 +- t/test-chars-stamps.h | 6 +++--- 7 files changed, 45 insertions(+), 46 deletions(-) diff --git a/blobstamper/galley.cpp b/blobstamper/galley.cpp index f0aa476..54bcc3d 100644 --- a/blobstamper/galley.cpp +++ b/blobstamper/galley.cpp @@ -29,19 +29,19 @@ int GalleyVectorBase::minSize() { - if (stamp.isFixedSize()) + if (stamp->isFixedSize()) { - return stamp.minSize(); // When size is fixed, series can have only one member with no extra data used + return stamp->minSize(); // When size is fixed, series can have only one member with no extra data used } else { - if (stamp.isUnbounded()) + if (stamp->isUnbounded()) { - return stamp.minSize() + ORACLE_SIZE * 2; // One -- count oracle, one -- size oracle + return stamp->minSize() + ORACLE_SIZE * 2; // One -- count oracle, one -- size oracle } else { - return stamp.minSize() + ORACLE_SIZE; // At leas one element with an oracle + return stamp->minSize() + ORACLE_SIZE; // At leas one element with an oracle } } } @@ -56,7 +56,8 @@ GalleyVectorStr::ExtractStrVector(std::shared_ptr blob) for(int i = 0; i(stamp)).ExtractStr(blobs[i]); // We know for sure that stamp is StampBaseStr + std::shared_ptr s = std::dynamic_pointer_cast(stamp); + res[i] = s->ExtractStr(blobs[i]); // We know for sure that stamp is StampBaseStr } return res; } @@ -69,7 +70,7 @@ GalleyVectorBin::ExtractBinVector(std::shared_ptr blob) for(int i = 0; iExtractBin(blobs[i]); } return res; } @@ -77,14 +78,14 @@ GalleyVectorBin::ExtractBinVector(std::shared_ptr blob) std::vector> GalleyVectorBase::extract_internal(std::shared_ptr blob) { - if (blob->Size()Size()minSize()) { throw OutOfData(); /* FIXME: May be later add option that allows empty lists if needed*/ } std::vector> res; - if (stamp.isFixedSize()) + if (stamp->isFixedSize()) { - int size = stamp.minSize(); + int size = stamp->minSize(); while (blob->Size() >= size) { std::shared_ptr el = blob->Chop(size); @@ -93,7 +94,7 @@ GalleyVectorBase::extract_internal(std::shared_ptr blob) } else { - if (stamp.isUnbounded()) + if (stamp->isUnbounded()) { /* The idea of this part is following: @@ -112,7 +113,7 @@ GalleyVectorBase::extract_internal(std::shared_ptr blob) */ /* Getting count oracle and normalze it to fit available size */ - size_t count_max = (blob->Size() - ORACLE_SIZE) / (stamp.minSize() + ORACLE_SIZE); //First oracle - for number of items, and second one is oracle for each item size + size_t count_max = (blob->Size() - ORACLE_SIZE) / (stamp->minSize() + ORACLE_SIZE); //First oracle - for number of items, and second one is oracle for each item size ORACLE_STAMP stamp_oracle; ORACLE_TYPE count_oracle = stamp_oracle.ExtractValue(blob); @@ -140,14 +141,14 @@ GalleyVectorBase::extract_internal(std::shared_ptr blob) /* Calculating available vairable size, that will be destributed between parts according to size oracles */ int data_size = blob->Size(); - int fixed_data_size = stamp.minSize() * count_target; + int fixed_data_size = stamp->minSize() * count_target; int var_data_size = data_size - fixed_data_size; /* normalizing oracles so they fit total variable size, chop to parts and stamp parts */ float remainder = 0; /* we do not want waste bytes because of rounding, so we keep the remainder, and reuse it. Thus we will use all bytes (alomost, may loose last one due to remainder=0.99999)*/ for(ORACLE_TYPE o : size_oracles) { - float el_size_f = stamp.minSize() + (float) o / size_oracle_total * var_data_size + remainder; + float el_size_f = stamp->minSize() + (float) o / size_oracle_total * var_data_size + remainder; int el_size = el_size_f; remainder = el_size_f - el_size; @@ -158,12 +159,12 @@ GalleyVectorBase::extract_internal(std::shared_ptr blob) else { /* Stamp is variated size */ - int fixed_size = stamp.minSize(); - int var_size = stamp.maxSize() - fixed_size; + int fixed_size = stamp->minSize(); + int var_size = stamp->maxSize() - fixed_size; ORACLE_STAMP stamp_oracle; while(1) { - if(stamp.minSize() + stamp_oracle.minSize() > blob->Size()) + if(stamp->minSize() + stamp_oracle.minSize() > blob->Size()) break; ORACLE_TYPE oracle = stamp_oracle.ExtractValue(blob); diff --git a/blobstamper/galley.h b/blobstamper/galley.h index 002ca72..c6cb5e0 100644 --- a/blobstamper/galley.h +++ b/blobstamper/galley.h @@ -43,9 +43,9 @@ class GalleyBase: public virtual StampBase class GalleyVectorBase : public GalleyBase { protected: - StampBase &stamp; + std::shared_ptr stamp; public: - GalleyVectorBase(StampBase & stamp_arg) : stamp(stamp_arg) {}; + GalleyVectorBase(std::shared_ptr stamp_arg) : stamp(stamp_arg) {}; std::vector> extract_internal(std::shared_ptr blob); int minSize() override; int maxSize() override {return -1;}; /* Sereies always takes as much data as it can take */ @@ -55,34 +55,32 @@ class GalleyVectorBase : public GalleyBase class GalleyVectorStr: public GalleyVectorBase { public: - GalleyVectorStr(StampBaseStr & stamp_arg): GalleyVectorBase(stamp_arg) {}; + GalleyVectorStr(std::shared_ptr stamp_arg): GalleyVectorBase(stamp_arg) {}; std::vector ExtractStrVector(std::shared_ptr blob); }; template class GalleyVectorStrStampBase: public GalleyVectorStr, public StampBaseStr { - protected: - T * item_stamp_p; public: - GalleyVectorStrStampBase(): GalleyVectorStr(*(item_stamp_p = new T())) {}; - ~GalleyVectorStrStampBase() {delete item_stamp_p;}; + GalleyVectorStrStampBase(): GalleyVectorStr(std::make_shared()) {}; + }; class GalleyVectorBin: public GalleyVectorBase { - StampBaseBin & b_stamp; + std::shared_ptr b_stamp; public: - GalleyVectorBin(StampBaseBin & stamp_arg): GalleyVectorBase(stamp_arg), b_stamp(stamp_arg) {}; + GalleyVectorBin(std::shared_ptr stamp_arg): GalleyVectorBase(stamp_arg), b_stamp(stamp_arg) {}; std::vector> ExtractBinVector(std::shared_ptr blob); }; template class GalleyVectorV: public GalleyVectorBase { - StampBaseV& v_stamp; + std::shared_ptr> v_stamp; public: - GalleyVectorV(StampBaseV & stamp_arg): GalleyVectorBase(stamp_arg), v_stamp(stamp_arg) {}; + GalleyVectorV(std::shared_ptr> stamp_arg): GalleyVectorBase(stamp_arg), v_stamp(stamp_arg) {}; std::vector ExtractValuesVector(std::shared_ptr blob); }; @@ -95,7 +93,7 @@ GalleyVectorV::ExtractValuesVector(std::shared_ptr blob) for(int i=0; iExtractValue(blobs[i]); } return res; } diff --git a/blobstamper/stamp_enumerator.h b/blobstamper/stamp_enumerator.h index 5ab73f1..20c5761 100644 --- a/blobstamper/stamp_enumerator.h +++ b/blobstamper/stamp_enumerator.h @@ -28,12 +28,12 @@ class StampStrEnumerator: public GalleyVectorStr, public StampBaseStr { protected: - StampBaseStr & stamp_str; + std::shared_ptr stamp_str; const std::string separator; const std::string left_bracket; const std::string right_bracket; public: - StampStrEnumerator(StampBaseStr &arg_stamp, + StampStrEnumerator(std::shared_ptr arg_stamp, const std::string arg_sep, const std::string arg_l, const std::string arg_r diff --git a/t/130-stamp_enumerator.cpp b/t/130-stamp_enumerator.cpp index 8349365..1cedc3a 100644 --- a/t/130-stamp_enumerator.cpp +++ b/t/130-stamp_enumerator.cpp @@ -37,8 +37,8 @@ main() { TEST_START(1); { /* 1..1 */ - std::shared_ptr blob = std::make_shared((char *) sample, sizeof(sample)); - StampArithm base_stamp; + std::shared_ptr blob = std::make_shared((char *) sample, sizeof(sample)); + std::shared_ptr> base_stamp = std::make_shared>(); StampStrEnumerator stamp(base_stamp, "; ", "<", ">"); std::string s = stamp.ExtractStr(blob); diff --git a/t/300-galley.cpp b/t/300-galley.cpp index ecabb4d..b10fa98 100644 --- a/t/300-galley.cpp +++ b/t/300-galley.cpp @@ -44,9 +44,9 @@ main() std::string expected2 = "34"; std::string expected3 = "56"; - StampTwoChars stamp; + std::shared_ptr stamp = std::make_shared(); GalleyVectorStr galley(stamp); - std::shared_ptr blob = std::make_shared(short_sample, strlen(short_sample)); + std::shared_ptr blob = std::make_shared(short_sample, strlen(short_sample)); std::vector res = galley.ExtractStrVector(blob); is(res[0], expected1, "GalleyVector, fixed size string stamp: First element of shifted list is ok"); @@ -64,8 +64,8 @@ main() std::string expected3 = "(zA, B%, CD, EF, GH, IJ, KL)"; std::string expected4 = "(MN, OP, QR, ST, UV, WX, YZ)"; - std::shared_ptr blob= std::make_shared(longer_sample, strlen(longer_sample)); - StampTwoCharsList stamp_charlist; + std::shared_ptr blob= std::make_shared(longer_sample, strlen(longer_sample)); + std::shared_ptr stamp_charlist = std::make_shared(); GalleyVectorStr galley(stamp_charlist); std::vector res = galley.ExtractStrVector(blob); @@ -84,9 +84,9 @@ main() unsigned short int expected2 = (unsigned char) '4' * 256 +(unsigned char) '3'; unsigned short int expected3 = (unsigned char) '6' * 256 +(unsigned char) '5'; - StampArithm stamp; + std::shared_ptr> stamp = std::make_shared>(); GalleyVectorBin galley(stamp); - std::shared_ptr blob = std::make_shared(short_sample, strlen(short_sample)); + std::shared_ptr blob = std::make_shared(short_sample, strlen(short_sample)); std::vector> res = galley.ExtractBinVector(blob); std::vector v; @@ -110,7 +110,7 @@ main() { /* 14 */ signed int sample[] = {1, -2, -30, 40, -55, 6}; - StampArithm stamp; + std::shared_ptr> stamp = std::make_shared>(); GalleyVectorV galley(stamp); std::shared_ptr blob = std::make_shared((char*)sample, sizeof(sample)); std::vector res = galley.ExtractValuesVector(blob); @@ -129,8 +129,8 @@ main() std::string expected3 = "bcde"; std::string expected4 = "ghij"; - std::shared_ptr blob = std::make_shared(sample, strlen(sample)); - StampSeveralChars stamp; + std::shared_ptr blob = std::make_shared(sample, strlen(sample)); + std::shared_ptr stamp = std::make_shared(); GalleyVectorStr galley(stamp); std::vector res = galley.ExtractStrVector(blob); diff --git a/t/Makefile b/t/Makefile index 0a8f917..7312e99 100644 --- a/t/Makefile +++ b/t/Makefile @@ -21,7 +21,7 @@ build-libtappp: $(MAKE) -C ../libtappp %.t: %.cpp $(BLOBSTAMPER_OBJ) - $(CXX) $(CXXFLAGS) -I../libtappp/include -I.. -o $@ $< $(BLOBSTAMPER_OBJ) -L../libtappp -ltap++ + $(CXX) $(CXXFLAGS) -I../libtappp/include -g -I.. -o $@ $< $(BLOBSTAMPER_OBJ) -L../libtappp -ltap++ test: all @echo run_tests.pl $(TEST_BIN) diff --git a/t/test-chars-stamps.h b/t/test-chars-stamps.h index 1144ccf..dabe3a1 100644 --- a/t/test-chars-stamps.h +++ b/t/test-chars-stamps.h @@ -77,13 +77,13 @@ StampSeveralChars::ExtractStr(std::shared_ptr blob) class StampTwoCharsList: public StampBaseStr { protected: - StampTwoChars el_stamp; + std::shared_ptr el_stamp; GalleyVectorStr galley; public: std::string ExtractStr(std::shared_ptr blob) override; - StampTwoCharsList(): el_stamp {}, galley {el_stamp} {}; + StampTwoCharsList(): el_stamp {std::make_shared()}, galley {el_stamp} {}; - virtual int minSize() override {return el_stamp.minSize();}; + virtual int minSize() override {return el_stamp->minSize();}; virtual int maxSize() override {return -1;}; }; pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy