@@ -33,10 +33,9 @@ You can use stamp on a Blob as many times as you like until you are out of Blob
33
33
Stamp is a C++ object that "bites" chunk of binary data from Blob and converts it
34
34
into certain structured representation (text string with syntax that is provided
35
35
by stamp or C-structure)
36
-
37
36
```
38
37
char data[] ="abcdefghijk";
39
- Blob blob(data, strlen(data)); // Blob with "random" data
38
+ auto blob = std::make_shared<Blob> (data, strlen(data)); // Blob with "random" data
40
39
41
40
StampArithm<short int> stamp; // Stamp for getting short integer (both string and value representations)
42
41
@@ -110,10 +109,10 @@ Example:
110
109
111
110
```
112
111
char data[] ="abcdefghijk";
113
- Blob blob1(data, strlen(data)); // Blob with "random" data
114
- Blob blob2(data, strlen(data)); // Another Blob with same data
112
+ auto blob1 = std::make_shared<Blob> (data, strlen(data)); // Blob with "random" data
113
+ auto blob2 = std::make_shared<Blob> (data, strlen(data)); // Another Blob with same data
115
114
116
- StampArithm<short int> stamp ; // Stamp for short integer data (both string and value)
115
+ auto stamp = std::make_shared< StampArithm<short int>>() ; // Stamp for short integer data (both string and value)
117
116
118
117
GalleyVectorStr galley_s(stamp);
119
118
GalleyVectorV<short int> galley_v(stamp);
@@ -134,10 +133,10 @@ Example:
134
133
135
134
```
136
135
char data[] ="abcdefghijk";
137
- Blob blob(data, strlen(data)); // Blob with "random" data
136
+ auto blob = std::make_shared<Blob> (data, strlen(data)); // Blob with "random" data
138
137
139
- StampArithm<short int> stamp_i ; // Stamp for short integer data (both string and value)
140
- StampArithm<float> stamp_f ; // Stamp for float numeric data (both string and value)
138
+ auto stamp_i = std::make_shared< StampArithm<short int>>() ; // Stamp for short integer data (both string and value)
139
+ auto stamp_f = std::make_shared< StampArithm<float>>() ; // Stamp for float numeric data (both string and value)
141
140
142
141
GalleySetStr galley({stamp_i, stamp_f});
143
142
@@ -180,7 +179,7 @@ class ComplexIntStamp: public StampBaseStr
180
179
public:
181
180
virtual int minSize() override;
182
181
virtual int maxSize() override;
183
- virtual std::string ExtractStr(Blob & blob) override;
182
+ virtual std::string ExtractStr(std::shared_ptr< Blob> blob) override;
184
183
};
185
184
```
186
185
Actually here we can have one ` StampArithm<short int> ` stamp, and apply it two times.
@@ -205,7 +204,7 @@ Now we will implement Extract Method.
205
204
We just extract two values with ` stampA ` and ` stampB ` and combine them into string we want.
206
205
207
206
```
208
- std::string ComplexIntStamp::ExtractStr(Blob & blob)
207
+ std::string ComplexIntStamp::ExtractStr(std::shared_ptr< Blob> blob)
209
208
{
210
209
std::string A, B;
211
210
A = stampA.ExtractStr(blob);
@@ -220,7 +219,7 @@ Now you can use your stamp the way any stamp is used:
220
219
int main()
221
220
{
222
221
char data[] = "abcdef";
223
- Blob blob(data, strlen(data));
222
+ auto blob = std::make_shared<Blob> (data, strlen(data));
224
223
ComplexIntStamp stamp;
225
224
226
225
std::string s = stamp.ExtractStr(blob);
@@ -254,7 +253,7 @@ class ComplexIntStamp: public StampBaseV<complex_short>
254
253
public:
255
254
virtual int minSize() override;
256
255
virtual int maxSize() override;
257
- virtual complex_short ExtractValue(Blob & blob) override;
256
+ virtual complex_short ExtractValue(std::shared_ptr< Blob> blob) override;
258
257
};
259
258
```
260
259
@@ -277,7 +276,7 @@ In `ExtractValue` method we locally create desired structure, fill it with
277
276
values fetched from the Blob, and return the structure by value.
278
277
279
278
```
280
- complex_short ComplexIntStamp::ExtractValue(Blob & blob)
279
+ complex_short ComplexIntStamp::ExtractValue(std::shared_ptr< Blob> blob)
281
280
{
282
281
complex_short res;
283
282
res.re = stampA.ExtractValue(blob);
@@ -292,7 +291,7 @@ Then we can use stamp for extracting `complex_short` directly from the Blob:
292
291
int main()
293
292
{
294
293
char data[] = "abcdef";
295
- Blob blob(data, strlen(data));
294
+ auto blob = std::make_shared<Blob> (data, strlen(data));
296
295
ComplexIntStamp stamp;
297
296
298
297
complex_short cs = stamp.ExtractValue(blob);
@@ -310,14 +309,13 @@ To add extract method to Galley you should use multiple inheritance.
310
309
```
311
310
class ArrayOfComplexIntStamp: public GalleyVectorStr, public StampBaseStr
312
311
{
313
- protected:
314
- ComplexIntStamp * item_stamp_p;
315
312
public:
316
- ComplexIntArrayStamp(): GalleyVectorStr(*(item_stamp_p = new ComplexIntStamp())) {};
317
- ~ComplexIntArrayStamp() {delete item_stamp_p;};
313
+ ArrayOfComplexIntStamp(): GalleyVectorStr(std::dynamic_pointer_cast<StampBaseStr>(std::make_shared<ComplexIntStamp>())) {};
318
314
319
- virtual std::string ExtractStr(Blob & blob) override;
315
+ virtual std::string ExtractStr(std::shared_ptr< Blob> blob) override;
320
316
};
317
+
318
+
321
319
```
322
320
Because of initialization order issue, we have to initialize the stamp inside the call of parent class constructor via ` new ` method, and then destroy in in the destructor.
323
321
@@ -326,7 +324,7 @@ We implement only the Extract method we need.
326
324
327
325
328
326
```
329
- std::string ComplexIntArrayStamp ::ExtractStr(Blob & blob)
327
+ std::string ArrayOfComplexIntStamp ::ExtractStr(std::shared_ptr< Blob> blob)
330
328
{
331
329
std::vector<std::string> data = ExtractStrVector(blob);
332
330
std::string res = "";
@@ -403,15 +401,14 @@ If you have Stamp with Value Extract Method, and you are going to use `GalleyVec
403
401
It would be something like that:
404
402
405
403
```
406
- ComplexIntStamp stamp;
404
+ auto stamp = std::make_shared<ComplexIntStamp>() ;
407
405
GalleyVectorV<complex_short> galley(stamp);
408
406
complex_short *result;
409
- int result_size;
410
407
std::vector<complex_short> vec = galley.ExtractValuesVector(blob);
411
- result_len = vec.size();
408
+ int result_size = vec.size();
412
409
413
- result = (complex_short *) malloc(sizeof(complex_short) * result_len) );
414
- memcpy((void*) result, (void*) &vec[0], sizeof(complex_short) * result_len );
410
+ result = (complex_short *) malloc(sizeof(complex_short) * result_size );
411
+ memcpy((void*) result, (void*) &vec[0], sizeof(complex_short) * result_size );
415
412
```
416
413
417
414
## Further reading
0 commit comments