Skip to content

Commit a96813b

Browse files
More code refactoring: change vector of Stamp references to vector of Stamp shared_ptr's in the API
1 parent 37132ff commit a96813b

File tree

6 files changed

+118
-128
lines changed

6 files changed

+118
-128
lines changed

blobstamper/galley.cpp

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -197,16 +197,16 @@ GalleySetBase::extract_internal(std::shared_ptr<Blob> blob)
197197
int variated_count = 0;
198198

199199
/* Loop throight stamps calculating total sizes and seeing what kind of stamps do we have*/
200-
for(StampBase & s : stamps)
200+
for(std::shared_ptr<StampBase> s : stamps)
201201
{
202-
fixed_total_size += s.minSize();
203-
if (s.isVariated())
202+
fixed_total_size += s->minSize();
203+
if (s->isVariated())
204204
{
205-
max_varited_total_size += s.maxSize() - s.minSize();
205+
max_varited_total_size += s->maxSize() - s->minSize();
206206
has_variated_stamps = true;
207207
variated_count++;
208208
}
209-
if (s.isUnbounded())
209+
if (s->isUnbounded())
210210
{
211211
has_unbounded_stamps = true;
212212
unbounded_count++;
@@ -254,26 +254,26 @@ GalleySetBase::extract_internal(std::shared_ptr<Blob> blob)
254254
double total_unbounded_modifiers = 0;
255255

256256
std::vector<double> size_modifiers;
257-
for(StampBase & s : stamps)
257+
for(std::shared_ptr<StampBase> s : stamps)
258258
{
259259
ORACLE_TYPE o_value = 0;
260260
double modifier = 0;
261-
if (!s.isFixedSize())
261+
if (!s->isFixedSize())
262262
{
263-
if (s.isUnbounded() && unbounded_count <=1 )
263+
if (s->isUnbounded() && unbounded_count <=1 )
264264
{
265265
modifier = 1; //Nothing to predict, it will use all space
266266
} else
267267
{
268268
o_value = oracle_stamp.ExtractValue(blob);
269269
modifier = (double) o_value / (double) ORACLE_MAX;
270270
}
271-
if (s.isUnbounded())
271+
if (s->isUnbounded())
272272
{
273273
total_unbounded_modifiers += modifier;
274274
} else
275275
{
276-
predicted_variated_total_size += (s.maxSize() - s.minSize()) * modifier;
276+
predicted_variated_total_size += (s->maxSize() - s->minSize()) * modifier;
277277
}
278278
}
279279
size_modifiers.push_back(modifier);
@@ -283,8 +283,8 @@ GalleySetBase::extract_internal(std::shared_ptr<Blob> blob)
283283
total_unbounded_modifiers = 0;
284284
for(int i=0; i<stamps.size();i++)
285285
{
286-
StampBase &s = stamps[i];
287-
if (s.isUnbounded())
286+
std::shared_ptr<StampBase> s = stamps[i];
287+
if (s->isUnbounded())
288288
{
289289
size_modifiers[i] = 1;
290290
total_unbounded_modifiers += 1;
@@ -321,26 +321,26 @@ GalleySetBase::extract_internal(std::shared_ptr<Blob> blob)
321321
/* chopping this sizes out of the blob and saving them a result vector*/
322322
for(int i=0; i<stamps.size();i++)
323323
{
324-
StampBase &s = stamps[i];
324+
std::shared_ptr<StampBase> s = stamps[i];
325325
double modifier = size_modifiers[i];
326326
int el_size;
327-
if (s.isFixedSize())
327+
if (s->isFixedSize())
328328
{
329-
el_size = s.minSize();
329+
el_size = s->minSize();
330330
}
331-
if (s.isVariated())
331+
if (s->isVariated())
332332
{
333-
double len = (s.maxSize() - s.minSize()) * modifier * k_variated + variated_remainder;
333+
double len = (s->maxSize() - s->minSize()) * modifier * k_variated + variated_remainder;
334334
el_size = round(len);
335335
variated_remainder = len - el_size;
336-
el_size += s.minSize();
336+
el_size += s->minSize();
337337
}
338-
if (s.isUnbounded())
338+
if (s->isUnbounded())
339339
{
340340
double len = modifier * k_unbounded + unbounded_remainder;
341341
el_size = round(len);
342342
unbounded_remainder = len - el_size;
343-
el_size +=s.minSize();
343+
el_size +=s->minSize();
344344
}
345345
std::shared_ptr<Blob> blob2 = blob->Chop(el_size);
346346
res.push_back(blob2);
@@ -355,8 +355,8 @@ GalleySetBase::LoadAll(std::shared_ptr<Blob> blob)
355355
for(int i=0; i<blobs.size(); i++)
356356
{
357357
std::shared_ptr<Blob> blob = blobs[i];
358-
StampBase & stamp = stamps[i];
359-
stamp.Load(blob);
358+
std::shared_ptr<StampBase> s = stamps[i];
359+
s->Load(blob);
360360
}
361361
}
362362

@@ -369,8 +369,8 @@ GalleySetStr::ExtractStrSet(std::shared_ptr<Blob> blob)
369369
for(int i=0; i<blobs.size(); i++)
370370
{
371371
std::shared_ptr<Blob> blob = blobs[i];
372-
StampBaseStr & stamp = s_stamps[i];
373-
std::string str = stamp.ExtractStr(blob);
372+
auto stamp = std::dynamic_pointer_cast<StampBaseStr>(stamps[i]);
373+
std::string str = stamp->ExtractStr(blob);
374374
res.push_back(str);
375375
}
376376
return res;
@@ -384,8 +384,8 @@ GalleySetBin::ExtractBinSet(std::shared_ptr<Blob> blob)
384384
for(int i=0; i<blobs.size(); i++)
385385
{
386386
std::shared_ptr<Blob> blob = blobs[i];
387-
StampBaseBin & stamp = b_stamps[i];
388-
std::vector<char> v = stamp.ExtractBin(blob);
387+
auto stamp = std::dynamic_pointer_cast<StampBaseBin>(stamps[i]);
388+
std::vector<char> v = stamp->ExtractBin(blob);
389389
res.push_back(v);
390390
}
391391
return res;
@@ -403,15 +403,15 @@ GalleySetBase::minSize()
403403
int res = 0;
404404

405405
/* Loop throight stamps calculating total sizes and seeing what kind of stamps do we have*/
406-
for(StampBase & s : stamps)
406+
for(std::shared_ptr<StampBase> s : stamps)
407407
{
408-
res += s.minSize();
409-
if (s.isVariated())
408+
res += s->minSize();
409+
if (s->isVariated())
410410
{
411411
has_variated_stamps = true;
412412
variated_count++;
413413
}
414-
if (s.isUnbounded())
414+
if (s->isUnbounded())
415415
{
416416
has_unbounded_stamps = true;
417417
unbounded_count++;
@@ -434,14 +434,14 @@ GalleySetBase::maxSize()
434434
int res = 0;
435435

436436
/* Loop throight stamps calculating total sizes and seeing what kind of stamps do we have*/
437-
for(StampBase & s : stamps)
437+
for(std::shared_ptr<StampBase> s : stamps)
438438
{
439-
res += s.maxSize();
440-
if (s.isVariated())
439+
res += s->maxSize();
440+
if (s->isVariated())
441441
{
442442
res += ORACLE_SIZE; // Each variated stamp needs an oracle to predict it's size. It also affects max size
443443
}
444-
if (s.isUnbounded())
444+
if (s->isUnbounded())
445445
{
446446
return -1; // Junst one unbounded stamp makes all thing unbounded
447447
}

blobstamper/galley.h

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,9 @@ GalleyVectorV<T>::ExtractValuesVector(std::shared_ptr<Blob> blob)
102102
class GalleySetBase : public GalleyBase
103103
{
104104
protected:
105-
std::vector<std::reference_wrapper<StampBase>> stamps;
105+
std::vector<std::shared_ptr<StampBase>> stamps;
106106
public:
107-
GalleySetBase(std::vector<std::reference_wrapper<StampBase>> arg) : stamps(arg) {};
107+
GalleySetBase(std::vector<std::shared_ptr<StampBase>> arg) : stamps(arg) {};
108108
std::vector<std::shared_ptr<Blob>> extract_internal(std::shared_ptr<Blob> blob);
109109
void LoadAll(std::shared_ptr<Blob> blob);
110110

@@ -114,39 +114,17 @@ class GalleySetBase : public GalleyBase
114114

115115
class GalleySetBin : public GalleySetBase
116116
{
117-
std::vector<std::reference_wrapper<StampBaseBin>> b_stamps;
118117
public:
119-
GalleySetBin(std::vector<std::reference_wrapper<StampBaseBin>> arg) : GalleySetBase(cast_arg(arg)), b_stamps(arg) {};
118+
GalleySetBin(std::vector<std::shared_ptr<StampBaseBin>> arg) : GalleySetBase(cast_shared_vector<StampBase>(arg)) {};
120119
std::vector<std::vector<char>> ExtractBinSet(std::shared_ptr<Blob> blob);
121-
122-
std::vector<std::reference_wrapper<StampBase>> cast_arg(std::vector<std::reference_wrapper<StampBaseBin>> in)
123-
{
124-
std::vector<std::reference_wrapper<StampBase>> res;
125-
for(StampBaseBin & s : in)
126-
{
127-
res.push_back(s);
128-
}
129-
return res;
130-
};
131120
};
132121

133122

134123
class GalleySetStr : public GalleySetBase
135124
{
136-
std::vector<std::reference_wrapper<StampBaseStr>> s_stamps;
137125
public:
138-
GalleySetStr(std::vector<std::reference_wrapper<StampBaseStr>> arg) : GalleySetBase(cast_arg(arg)), s_stamps(arg) {};
126+
GalleySetStr(std::vector<std::shared_ptr<StampBaseStr>> arg) : GalleySetBase(cast_shared_vector<StampBase>(arg)){};
139127
std::vector<std::string> ExtractStrSet(std::shared_ptr<Blob> blob);
140-
141-
std::vector<std::reference_wrapper<StampBase>> cast_arg(std::vector<std::reference_wrapper<StampBaseStr>> in)
142-
{
143-
std::vector<std::reference_wrapper<StampBase>> res;
144-
for(StampBaseStr & s : in)
145-
{
146-
res.push_back(s);
147-
}
148-
return res;
149-
};
150128
};
151129

152130

blobstamper/helpers.h

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#define HELPERS_H
2121

2222
#include<vector>
23+
#include<memory>
2324

2425
void hexdump(void *pAddressIn, long lSize);
2526

@@ -145,7 +146,16 @@ VLATO_ptr<T,ArrayT>::VLATO_ptr(size_t offset, size_t length)
145146
_offset = offset;
146147
}
147148

149+
template<class Tto, class Tfrom>
150+
std::vector<std::shared_ptr<Tto>> cast_shared_vector(std::vector<std::shared_ptr<Tfrom>> v)
151+
{
152+
std::vector<std::shared_ptr<Tto>> res;
153+
for(auto e : v)
154+
{
155+
auto p = std::dynamic_pointer_cast<Tto>(e);
156+
res.push_back(p);
157+
}
158+
return res;
159+
}
148160

149-
150-
151-
#endif /*HELPERS_H*/
161+
#endif /*HELPERS_H*/

blobstamper/stamp_math_op.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@
2222
std::string
2323
StampMathUnaryOp::ExtractStr(std::shared_ptr<Blob> blob)
2424
{
25-
return op_name + "(" + stamp.ExtractStr(blob) + ")";
25+
return op_name + "(" + stamp->ExtractStr(blob) + ")";
2626
}
2727

2828

2929
std::string
3030
StampMathBinaryOp::ExtractStr(std::shared_ptr<Blob> blob)
3131
{
3232
std::vector<std::shared_ptr<Blob>> blobs = extract_internal(blob);
33-
return (std::string)"(" + stamp1.ExtractStr(blobs[0]) + " "+ op_name + " " + stamp2.ExtractStr(blobs[1]) + ")";
33+
return (std::string)"(" + stamp1->ExtractStr(blobs[0]) + " "+ op_name + " " + stamp2->ExtractStr(blobs[1]) + ")";
3434
}
3535

blobstamper/stamp_math_op.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ class StampMathUnaryOp: public StampBaseStr
2828
{
2929
protected:
3030
std::string op_name;
31-
StampBaseStr &stamp;
31+
std::shared_ptr<StampBaseStr> stamp;
3232
public:
3333
virtual std::string ExtractStr(std::shared_ptr<Blob> blob) override;
34-
StampMathUnaryOp(std::string arg_op_name, StampBaseStr& arg_stamp) : op_name(arg_op_name), stamp(arg_stamp) {};
34+
StampMathUnaryOp(std::string arg_op_name, std::shared_ptr<StampBaseStr> arg_stamp) : op_name(arg_op_name), stamp(arg_stamp) {};
3535
virtual int maxSize() override {return -1;};
36-
virtual int minSize() override {return stamp.minSize();};
36+
virtual int minSize() override {return stamp->minSize();};
3737
};
3838

3939

@@ -42,11 +42,13 @@ class StampMathBinaryOp: public StampBaseStr, public GalleySetBase
4242
{
4343
protected:
4444
std::string op_name;
45-
StampBaseStr &stamp1;
46-
StampBaseStr &stamp2;
45+
std::shared_ptr<StampBaseStr> stamp1;
46+
std::shared_ptr<StampBaseStr> stamp2;
4747
public:
4848
virtual std::string ExtractStr(std::shared_ptr<Blob> blob) override;
49-
StampMathBinaryOp(std::string arg_op_name, StampBaseStr& arg_stamp1, StampBaseStr& arg_stamp2) :
49+
StampMathBinaryOp(std::string arg_op_name,
50+
std::shared_ptr<StampBaseStr> arg_stamp1,
51+
std::shared_ptr<StampBaseStr> arg_stamp2) :
5052
GalleySetBase({arg_stamp1, arg_stamp2}),
5153
op_name(arg_op_name),
5254
stamp1(arg_stamp1),

0 commit comments

Comments
 (0)