@@ -255,10 +255,15 @@ BOOST_AUTO_TEST_CASE(class_methods)
255
255
}
256
256
}
257
257
258
- enum class BaseFormat {
259
- RAW,
260
- HEX,
258
+ struct BaseFormat {
259
+ const enum {
260
+ RAW,
261
+ HEX,
262
+ } m_base_format;
263
+ SER_PARAMS_OPFUNC
261
264
};
265
+ constexpr BaseFormat RAW{BaseFormat::RAW};
266
+ constexpr BaseFormat HEX{BaseFormat::HEX};
262
267
263
268
// / (Un)serialize a number as raw byte or 2 hexadecimal chars.
264
269
class Base
@@ -272,7 +277,7 @@ class Base
272
277
template <typename Stream>
273
278
void Serialize (Stream& s) const
274
279
{
275
- if (s.GetParams () == BaseFormat::RAW) {
280
+ if (s.GetParams (). m_base_format == BaseFormat::RAW) {
276
281
s << m_base_data;
277
282
} else {
278
283
s << Span{HexStr (Span{&m_base_data, 1 })};
@@ -282,7 +287,7 @@ class Base
282
287
template <typename Stream>
283
288
void Unserialize (Stream& s)
284
289
{
285
- if (s.GetParams () == BaseFormat::RAW) {
290
+ if (s.GetParams (). m_base_format == BaseFormat::RAW) {
286
291
s >> m_base_data;
287
292
} else {
288
293
std::string hex{" aa" };
@@ -301,6 +306,8 @@ class DerivedAndBaseFormat
301
306
LOWER,
302
307
UPPER,
303
308
} m_derived_format;
309
+
310
+ SER_PARAMS_OPFUNC
304
311
};
305
312
306
313
class Derived : public Base
@@ -310,7 +317,7 @@ class Derived : public Base
310
317
311
318
SERIALIZE_METHODS_PARAMS (Derived, obj, DerivedAndBaseFormat, fmt)
312
319
{
313
- READWRITE (WithParams ( fmt.m_base_format , AsBase<Base>(obj)));
320
+ READWRITE (fmt.m_base_format ( AsBase<Base>(obj)));
314
321
315
322
if (ser_action.ForRead ()) {
316
323
std::string str;
@@ -330,20 +337,20 @@ BOOST_AUTO_TEST_CASE(with_params_base)
330
337
331
338
DataStream stream;
332
339
333
- stream << WithParams (BaseFormat:: RAW, b);
340
+ stream << RAW ( b);
334
341
BOOST_CHECK_EQUAL (stream.str (), " \x0F " );
335
342
336
343
b.m_base_data = 0 ;
337
- stream >> WithParams (BaseFormat:: RAW, b);
344
+ stream >> RAW ( b);
338
345
BOOST_CHECK_EQUAL (b.m_base_data , 0x0F );
339
346
340
347
stream.clear ();
341
348
342
- stream << WithParams (BaseFormat:: HEX, b);
349
+ stream << HEX ( b);
343
350
BOOST_CHECK_EQUAL (stream.str (), " 0f" );
344
351
345
352
b.m_base_data = 0 ;
346
- stream >> WithParams (BaseFormat:: HEX, b);
353
+ stream >> HEX ( b);
347
354
BOOST_CHECK_EQUAL (b.m_base_data , 0x0F );
348
355
}
349
356
@@ -353,45 +360,42 @@ BOOST_AUTO_TEST_CASE(with_params_vector_of_base)
353
360
354
361
DataStream stream;
355
362
356
- stream << WithParams (BaseFormat:: RAW, v);
363
+ stream << RAW ( v);
357
364
BOOST_CHECK_EQUAL (stream.str (), " \x02\x0F\xFF " );
358
365
359
366
v[0 ].m_base_data = 0 ;
360
367
v[1 ].m_base_data = 0 ;
361
- stream >> WithParams (BaseFormat:: RAW, v);
368
+ stream >> RAW ( v);
362
369
BOOST_CHECK_EQUAL (v[0 ].m_base_data , 0x0F );
363
370
BOOST_CHECK_EQUAL (v[1 ].m_base_data , 0xFF );
364
371
365
372
stream.clear ();
366
373
367
- stream << WithParams (BaseFormat:: HEX, v);
374
+ stream << HEX ( v);
368
375
BOOST_CHECK_EQUAL (stream.str (), " \x02 "
369
376
" 0fff" );
370
377
371
378
v[0 ].m_base_data = 0 ;
372
379
v[1 ].m_base_data = 0 ;
373
- stream >> WithParams (BaseFormat:: HEX, v);
380
+ stream >> HEX ( v);
374
381
BOOST_CHECK_EQUAL (v[0 ].m_base_data , 0x0F );
375
382
BOOST_CHECK_EQUAL (v[1 ].m_base_data , 0xFF );
376
383
}
377
384
385
+ constexpr DerivedAndBaseFormat RAW_LOWER{{BaseFormat::RAW}, DerivedAndBaseFormat::DerivedFormat::LOWER};
386
+ constexpr DerivedAndBaseFormat HEX_UPPER{{BaseFormat::HEX}, DerivedAndBaseFormat::DerivedFormat::UPPER};
387
+
378
388
BOOST_AUTO_TEST_CASE (with_params_derived)
379
389
{
380
390
Derived d;
381
391
d.m_base_data = 0x0F ;
382
392
d.m_derived_data = " xY" ;
383
393
384
- DerivedAndBaseFormat fmt;
385
-
386
394
DataStream stream;
387
395
388
- fmt.m_base_format = BaseFormat::RAW;
389
- fmt.m_derived_format = DerivedAndBaseFormat::DerivedFormat::LOWER;
390
- stream << WithParams (fmt, d);
396
+ stream << RAW_LOWER (d);
391
397
392
- fmt.m_base_format = BaseFormat::HEX;
393
- fmt.m_derived_format = DerivedAndBaseFormat::DerivedFormat::UPPER;
394
- stream << WithParams (fmt, d);
398
+ stream << HEX_UPPER (d);
395
399
396
400
BOOST_CHECK_EQUAL (stream.str (), " \x0F\x02 xy"
397
401
" 0f\x02 XY" );
0 commit comments