-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarty_enum.h
722 lines (560 loc) · 31.3 KB
/
marty_enum.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
/*! \file
\brief Helpers for enum
*/
#pragma once
#include "marty_cpp.h"
#include <algorithm>
#include <cctype>
#include <cstring>
#include <cwctype>
#include <iterator>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
//----------------------------------------------------------------------------
#if !defined(MARTY_CPP_NO_MARTY_CPP_EXCEPTIONS)
#include "marty_cpp_exceptions.h"
#endif
#if !defined(THROW_MARTY_CPP_MARTY_ENUM_DESERIALIZE_ERROR)
#if defined(MARTY_CPP_NO_MARTY_CPP_EXCEPTIONS)
#define THROW_MARTY_CPP_MARTY_ENUM_DESERIALIZE_ERROR( typeName, str ) \
throw std::runtime_error( typeName " - failed to deserialize value")
#else
#define THROW_MARTY_CPP_MARTY_ENUM_DESERIALIZE_ERROR( tymeName, str ) \
throw marty_cpp::enum_deserialization_error( tymeName, str )
#endif
#endif
//----------------------------------------------------------------------------
#ifndef MARTY_ARG_USED
//! Подавление варнинга о неиспользованном аргументе
#define MARTY_ARG_USED(x) (void)(x)
#endif
//----------------------------------------------------------------------------
// template<typename EnumType> inline
// EnumType enum_deserialize_unsafe(const std::string &str)
// {
// throw std::runtime_error( "enum_deserialize_unsafe not specialized for this type")
// }
//----------------------------------------------------------------------------
// Requires
// <marty_cpp/marty_cpp.h>
// <map> or <unordered_map>
// <exception>
// <stdexcept>
// to be included
#if defined(STM32) || defined(TARGET_STM32)
#define MARTY_CPP_MAKE_ENUM_IS_FLAGS_FOR_NON_FLAGS_ENUM(enumTypeName)
#define MARTY_CPP_ENUM_SERIALIZE_BEGIN( enumTypeName, mapType, doLower )
#define MARTY_CPP_ENUM_SERIALIZE_ITEM( val, valStr )
#define MARTY_CPP_ENUM_SERIALIZE_END( enumTypeName, mapType, doLower )
#define MARTY_CPP_ENUM_CLASS_SERIALIZE_BEGIN( enumTypeName, mapType, doLower )
#define MARTY_CPP_ENUM_CLASS_SERIALIZE_ITEM( val, valStr )
#define MARTY_CPP_ENUM_CLASS_SERIALIZE_END( enumTypeName, mapType, doLower )
#define MARTY_CPP_ENUM_SERIALIZE_SET(enumTypeName, setType)
#define MARTY_CPP_ENUM_CLASS_SERIALIZE_SET(enumTypeName, setType)
#define MARTY_CPP_ENUM_DESERIALIZE_BEGIN( enumTypeName, mapType, doLower )
#define MARTY_CPP_ENUM_DESERIALIZE_ITEM( val, valStr )
#define MARTY_CPP_ENUM_DESERIALIZE_END( enumTypeName, mapType, doLower )
#define MARTY_CPP_ENUM_CLASS_DESERIALIZE_BEGIN( enumTypeName, mapType, doLower )
#define MARTY_CPP_ENUM_CLASS_DESERIALIZE_ITEM( val, valStr )
#define MARTY_CPP_ENUM_CLASS_DESERIALIZE_END( enumTypeName, mapType, doLower )
#define MARTY_CPP_ENUM_DESERIALIZE_SET(enumTypeName, setType)
#define MARTY_CPP_ENUM_CLASS_DESERIALIZE_SET(enumTypeName, setType)
#else
//----------------------------------------------------------------------------
#define MARTY_CPP_MAKE_ENUM_IS_FLAGS_FOR_NON_FLAGS_ENUM(enumTypeName) \
inline \
bool enum_is_flags(enumTypeName) \
{ \
return false; \
}
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
#define MARTY_CPP_ENUM_SERIALIZE_BEGIN( enumTypeName, mapType, doLower ) \
inline \
mapType< enumTypeName, std::string > enum_serialize_make_map_##enumTypeName()\
{ \
/* static */ mapType< enumTypeName, std::string > _m; \
if (_m.empty()) \
{
//------------------------------
#define MARTY_CPP_ENUM_SERIALIZE_ITEM( val, valStr ) \
_m[val] = valStr
//------------------------------
#define MARTY_CPP_ENUM_SERIALIZE_END( enumTypeName, mapType, doLower ) \
} \
\
return _m; \
} \
\
inline \
const mapType< enumTypeName, std::string >& enum_serialize_get_map_##enumTypeName() \
{ \
static mapType< enumTypeName, std::string > m = enum_serialize_make_map_##enumTypeName();\
return m; \
} \
\
\
inline \
std::string enum_serialize_##enumTypeName( enumTypeName v ) \
{ \
const auto &m = enum_serialize_get_map_##enumTypeName(); \
mapType< enumTypeName, std::string >::const_iterator it = m.find(v); \
if (it==m.end()) \
return std::string(); \
\
return it->second; \
}
//------------------------------
#define MARTY_CPP_ENUM_CLASS_SERIALIZE_BEGIN( enumTypeName, mapType, doLower )\
MARTY_CPP_ENUM_SERIALIZE_BEGIN( enumTypeName, mapType, doLower )
#define MARTY_CPP_ENUM_CLASS_SERIALIZE_ITEM( val, valStr ) \
MARTY_CPP_ENUM_SERIALIZE_ITEM( val, valStr )
#define MARTY_CPP_ENUM_CLASS_SERIALIZE_END( enumTypeName, mapType, doLower ) \
MARTY_CPP_ENUM_SERIALIZE_END( enumTypeName, mapType, doLower ) \
inline \
std::string enum_serialize( enumTypeName v ) \
{ \
return enum_serialize_##enumTypeName(v); \
}
//------------------------------
#define MARTY_CPP_ENUM_SERIALIZE_SET(enumTypeName, setType) \
\
inline \
std::string enum_serialize_set_##enumTypeName( const setType<enumTypeName> &s, const char *seps = ",|+", char quotChar = '\'' ) \
{ \
return marty_cpp::serializeEnumSetImpl(s, enum_serialize_##enumTypeName, seps, quotChar); \
} \
\
inline \
std::string enum_serialize_set_##enumTypeName( const setType<enumTypeName> &s, const std::string &seps, char quotChar = '\'' ) \
{ \
return marty_cpp::serializeEnumSetImpl(s, enum_serialize_##enumTypeName, seps, quotChar); \
}
//------------------------------
#define MARTY_CPP_ENUM_CLASS_SERIALIZE_SET(enumTypeName, setType) \
MARTY_CPP_ENUM_SERIALIZE_SET(enumTypeName, setType) \
\
inline \
std::string enum_serialize_set( const setType<enumTypeName> &s, const char *seps = ",|+", char quotChar = '\'' ) \
{ \
return marty_cpp::serializeEnumSetImpl(s, enum_serialize_##enumTypeName, seps, quotChar); \
} \
\
inline \
std::string enum_serialize_set( const setType<enumTypeName> &s, const std::string &seps, char quotChar = '\'' ) \
{ \
return marty_cpp::serializeEnumSetImpl(s, enum_serialize_##enumTypeName, seps, quotChar); \
}
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
#define MARTY_CPP_ENUM_DESERIALIZE_BEGIN( enumTypeName, mapType, doLower ) \
inline \
mapType< std::string, enumTypeName > enum_deserialize_make_map_##enumTypeName( )\
{ \
static bool lowerCaseConvert = doLower ? true : false; \
/* static */ mapType< std::string, enumTypeName > _m; \
if (_m.empty()) \
{
//------------------------------
#define MARTY_CPP_ENUM_DESERIALIZE_ITEM( val, valStr ) \
_m[lowerCaseConvert ? marty_cpp::toLower(std::string(valStr)) : std::string(valStr)] = val
//------------------------------
#define MARTY_CPP_ENUM_DESERIALIZE_END( enumTypeName, mapType, doLower ) \
} \
return _m; \
} \
\
inline \
const mapType< std::string, enumTypeName >& enum_deserialize_get_map_##enumTypeName( ) \
{ \
static mapType< std::string, enumTypeName > _m = enum_deserialize_make_map_##enumTypeName(); \
return _m; \
} \
\
\
inline \
mapType< std::string, enumTypeName >::const_iterator enum_deserialize_impl_find_##enumTypeName( const mapType< std::string, enumTypeName > &_m, const std::string &str ) \
{ \
static bool lowerCaseConvert = doLower ? true : false; \
mapType< std::string, enumTypeName >::const_iterator it = _m.find(lowerCaseConvert ? marty_cpp::toLower(str) : str); \
return it; \
} \
\
inline \
enumTypeName enum_deserialize_##enumTypeName( const std::string &str ) \
{ \
const auto & _m = enum_deserialize_get_map_##enumTypeName( ); \
\
auto it = enum_deserialize_impl_find_##enumTypeName(_m, str); \
if (it==_m.end()) \
THROW_MARTY_CPP_MARTY_ENUM_DESERIALIZE_ERROR( #enumTypeName, str ); \
\
return it->second; \
} \
\
inline \
enumTypeName enum_deserialize_##enumTypeName( const std::string &str, enumTypeName defVal ) \
{ \
const auto & _m = enum_deserialize_get_map_##enumTypeName( ); \
\
auto it = enum_deserialize_impl_find_##enumTypeName(_m, str); \
if (it==_m.end()) \
return defVal; \
\
return it->second; \
}
// Начал пилить прикольно, но потом понял, что оно не сработает
#if 0
template<> inline
enumTypeName enum_deserialize_unsafe<enumTypeName>(const std::string &str)
{
return enum_deserialize_##enumTypeName( str )
}
#endif
//------------------------------
#define MARTY_CPP_ENUM_CLASS_DESERIALIZE_BEGIN( enumTypeName, mapType, doLower )\
MARTY_CPP_ENUM_DESERIALIZE_BEGIN( enumTypeName, mapType, doLower )
#define MARTY_CPP_ENUM_CLASS_DESERIALIZE_ITEM( val, valStr ) \
MARTY_CPP_ENUM_DESERIALIZE_ITEM( val, valStr )
#define MARTY_CPP_ENUM_CLASS_DESERIALIZE_END( enumTypeName, mapType, doLower ) \
MARTY_CPP_ENUM_DESERIALIZE_END( enumTypeName, mapType, doLower ) \
inline \
void enum_deserialize( enumTypeName &deserializeTo, const std::string &str ) \
{ \
deserializeTo = enum_deserialize_##enumTypeName(str); \
} \
\
inline \
enumTypeName enum_deserialize( const std::string &str, enumTypeName defVal ) \
{ \
return enum_deserialize_##enumTypeName(str, defVal); \
}
//------------------------------
#define MARTY_CPP_ENUM_DESERIALIZE_SET(enumTypeName, setType) \
\
inline \
setType<enumTypeName> enum_deserialize_set_##enumTypeName( const std::string &str, const char *seps = ",|+", char quotChar = '\'' ) \
{ \
auto deser = [](const std::string &str) \
{ return enum_deserialize_##enumTypeName(str); }; \
setType<enumTypeName> s; \
marty_cpp::deserializeEnumSetImpl(s, str, deser, seps, quotChar); \
return s; \
} \
\
inline \
setType<enumTypeName> enum_deserialize_set_##enumTypeName( const std::string &str, const std::string &seps, char quotChar = '\'' ) \
{ \
auto deser = [](const std::string &str) \
{ return enum_deserialize_##enumTypeName(str); }; \
setType<enumTypeName> s; \
marty_cpp::deserializeEnumSetImpl(s, str, deser, seps, quotChar); \
return s; \
}
//------------------------------
#define MARTY_CPP_ENUM_CLASS_DESERIALIZE_SET(enumTypeName, setType) \
MARTY_CPP_ENUM_DESERIALIZE_SET(enumTypeName, setType) \
\
inline \
void enum_deserialize_set( setType<enumTypeName> &s, const std::string &str, const char *seps = ",|+", char quotChar = '\'' ) \
{ \
s = enum_deserialize_set_##enumTypeName(str, seps, quotChar); \
} \
\
inline \
void enum_deserialize_set( setType<enumTypeName> &s, const std::string &str, const std::string &seps, char quotChar = '\'' ) \
{ \
s = enum_deserialize_set_##enumTypeName(str, seps, quotChar); \
}
//----------------------------------------------------------------------------
#endif // !STM32
//----------------------------------------------------------------------------
namespace marty_cpp
{
//-----------------------------------------------------------------------------
#ifndef MARTY_CPP_SIMPLE_STRING_SPLIT_DECLARED
#define MARTY_CPP_SIMPLE_STRING_SPLIT_DECLARED
//-----------------------------------------------------------------------------
template<typename StringType> inline
std::vector<StringType> simple_string_split(const StringType &str, const StringType &delim, typename StringType::size_type nSplits = -1)
{
// std::string s = "scott>=tiger>=mushroom";
// std::string delimiter = ">=";
// txt = "apple#banana#cherry#orange"
// # setting the maxsplit parameter to 1, will return a list with 2 elements!
// x = txt.split("#", 1)
std::vector<StringType> res;
typename StringType::size_type curPos = 0;
typename StringType::size_type prevPos = 0;
//StringType token;
while (res.size()!=nSplits && (curPos = str.find(delim, prevPos)) != StringType::npos)
{
res.emplace_back(str, prevPos, curPos-prevPos);
prevPos = curPos+delim.size();
}
res.emplace_back(str, prevPos);
return res;
}
//-----------------------------------------------------------------------------
template<typename StringType> inline
std::vector<StringType> simple_string_split(const StringType &str, const typename StringType::value_type *delim, typename StringType::size_type nSplits = -1)
{
return simple_string_split( str, StringType(delim), nSplits);
}
//-----------------------------------------------------------------------------
template<typename StringType> inline
std::vector<StringType> simple_string_split(const StringType &str, const typename StringType::value_type delim, typename StringType::size_type nSplits = -1)
{
typename StringType::value_type tmpDelimStr[2] = { delim, 0 };
return simple_string_split( str, tmpDelimStr, nSplits);
}
//-----------------------------------------------------------------------------
#endif // MARTY_CPP_SIMPLE_STRING_SPLIT_DECLARED
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
#ifndef MARTY_CPP_SIMPLE_STRING_REPLACE_DECLARED
#define MARTY_CPP_SIMPLE_STRING_REPLACE_DECLARED
//-----------------------------------------------------------------------------
template<typename StringType> inline
StringType simple_string_replace(const StringType &str, const StringType &searchFor, const StringType &replaceWith, typename StringType::size_type nSplits = -1)
{
typename StringType::size_type replaceCounter = 0;
typename StringType::size_type curPos = 0;
typename StringType::size_type prevPos = 0;
StringType res; res.reserve(str.size());
for(; replaceCounter!=nSplits && (curPos = str.find(searchFor, prevPos)) != StringType::npos; ++replaceCounter)
{
res.append(str, prevPos, curPos-prevPos);
prevPos = curPos+searchFor.size();
res.append(replaceWith);
}
res.append(str, prevPos);
return res;
}
//-----------------------------------------------------------------------------
#endif // MARTY_CPP_SIMPLE_STRING_REPLACE_DECLARED
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
#ifndef MARTY_CPP_SIMPLE_SEQ_FILTER_DECLARED
#define MARTY_CPP_SIMPLE_SEQ_FILTER_DECLARED
//-----------------------------------------------------------------------------
template<typename InputIterType, typename OutputIterType, typename FilterAllowPred> inline
void simple_seq_filter(InputIterType b, InputIterType e, OutputIterType o, const FilterAllowPred &allowPred)
{
for(; b!=e; ++b)
{
if (allowPred(*b))
*o++ = *b;
}
}
//-----------------------------------------------------------------------------
template<typename ItemType, typename FilterAllowPred> inline
std::vector<ItemType> simple_seq_filter( const std::vector<ItemType> &v, const FilterAllowPred &allowPred )
{
std::vector<ItemType> res;
simple_seq_filter(v.cbegin(), v.cend(), std::back_inserter(res), allowPred);
return res;
}
//-----------------------------------------------------------------------------
#endif // MARTY_CPP_SIMPLE_SEQ_FILTER_DECLARED
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
#ifndef MARTY_CPP_SIMPLE_TRIM_DECLARED
#define MARTY_CPP_SIMPLE_TRIM_DECLARED
//-----------------------------------------------------------------------------
template<typename IterType, typename ConditionType> inline
IterType trim_iter_impl( IterType b, IterType e, const ConditionType &trimCondition )
{
for(; b!=e && trimCondition(*b); ++b) {}
return b;
}
//-----------------------------------------------------------------------------
template<typename StringType, typename ConditionType> inline
StringType simple_trim(const StringType &str, const ConditionType &trimCondition)
{
// if (str.empty())
// return str;
auto e = trim_iter_impl(str.crbegin(), str.crend(), trimCondition).base();
//auto b = trim_iter_impl(str.cbegin(), er.base(), trimCondition);
auto b = trim_iter_impl(str.cbegin(), e, trimCondition);
//return StringType(b,er.base());
return StringType(b,e);
}
template<typename StringType, typename ConditionType> inline
StringType simple_ltrim(const StringType &str, const ConditionType &trimCondition)
{
auto b = trim_iter_impl(str.cbegin(), str.cend(), trimCondition);
return StringType(b,str.cend());
}
template<typename StringType, typename ConditionType> inline
StringType simple_rtrim(const StringType &str, const ConditionType &trimCondition)
{
// auto er = trim_iter_impl(str.crbegin(), str.crend(), trimCondition).base();
// return StringType(str.cbegin(),er.base());
auto e = trim_iter_impl(str.crbegin(), str.crend(), trimCondition).base();
return StringType(str.cbegin(),e);
}
//-----------------------------------------------------------------------------
#endif // MARTY_CPP_SIMPLE_TRIM_DECLARED
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
#ifndef MARTY_CPP_TO_UPPER_TO_LOWER_DECLARED
#define MARTY_CPP_TO_UPPER_TO_LOWER_DECLARED
inline bool isLower( char ch ) { return (ch>='a' && ch<='z'); }
inline bool isUpper( char ch ) { return (ch>='A' && ch<='Z'); }
inline bool isLower( wchar_t ch ) { return (ch>=L'a' && ch<=L'z'); }
inline bool isUpper( wchar_t ch ) { return (ch>=L'A' && ch<=L'Z'); }
inline char toLower( char ch ) { return isUpper(ch) ? ch-'A'+'a' : ch; }
inline char toUpper( char ch ) { return isLower(ch) ? ch-'a'+'A' : ch; }
inline wchar_t toLower( wchar_t ch ) { return isUpper(ch) ? ch-L'A'+L'a' : ch; }
inline wchar_t toUpper( wchar_t ch ) { return isLower(ch) ? ch-L'a'+L'A' : ch; }
template< class CharT, class Traits = std::char_traits<CharT>, class Allocator = std::allocator<CharT> >
inline std::basic_string< CharT, Traits, Allocator >
toLower( const std::basic_string< CharT, Traits, Allocator > &str )
{
std::basic_string< CharT, Traits, Allocator > resStr; resStr.reserve(str.size());
for( auto it = str.begin(); it != str.end(); ++it )
{
resStr.append( 1, toLower(*it) );
}
return resStr;
}
template< class CharT, class Traits = std::char_traits<CharT>, class Allocator = std::allocator<CharT> >
inline std::basic_string< CharT, Traits, Allocator >
toUpper( const std::basic_string< CharT, Traits, Allocator > &str )
{
std::basic_string< CharT, Traits, Allocator > resStr; resStr.reserve(str.size());
for( auto it = str.begin(); it != str.end(); ++it )
{
resStr.append( 1, toUpper(*it) );
}
return resStr;
}
#endif // MARTY_CPP_TO_UPPER_TO_LOWER_DECLARED
//-----------------------------------------------------------------------------
// Прямо сейчас мне десериализация enum'ов не нужна. Допишу потом.
#if 0
template<typename StringType, typename HandlerType> inline
void splitEnumSetStringHelper( const StringType &str, const StringType &seps, typename StringType::value_type quotChar, HandlerType handler )
{
typedef typename StringType::value_type CharType;
typedef typename StringType::size_type PosType ;
if (seps.empty()) // разделители не заданы вообще - строку не надо/не можем разделить
{
handler(str);
return;
}
auto isPosValid = [&](auto pos)
{
return pos!=str.npos && pos!=str.size();
};
const CharType spaces[] = { (CharType)' ', (CharType)'\t', 0 };
PosType startPos = str.find_first_not_of(spaces, 0);
while(isPosValid(startPos))
{
bool hasStartQuot = false;
if (str[startPos]==quotChar)
{
}
}
}
#endif
//-----------------------------------------------------------------------------
template< typename EnumSetType
, typename EnumTypeSerializer
> inline
std::string serializeEnumSetImpl( const EnumSetType &enumValsSet
, EnumTypeSerializer serializer
, const char *seps // используем только первый элемент
, char quotChar = '\''
)
{
const char sepChar = (seps && seps[0]) ? '+' : seps[0];
std::string res;
for( auto enumVal : enumValsSet )
{
auto enumValStr = serializer(enumVal);
if (enumValStr.empty()) // нужно ли сериализовать значения, которые сериализовались в пустую строку? Бросить исключение? Или добавить в кавычках?
continue;
if (!res.empty())
res.append(1,sepChar);
bool quoteVal = (enumValStr.find_first_of(' ')!=enumValStr.npos) ? true : false;
if (!quoteVal && enumValStr.size()==1 && enumValStr[0]==sepChar)
quoteVal = true;
if (quotChar==0)
quoteVal = false;
if (quoteVal)
res.append(1,quotChar);
res.append(enumValStr);
if (quoteVal)
res.append(1,quotChar);
}
return res;
}
//-----------------------------------------------------------------------------
template< typename EnumSetType
, typename EnumTypeSerializer
> inline
std::string serializeEnumSetImpl( const EnumSetType &enumValsSet
, EnumTypeSerializer serializer
, const std::string &seps // используем только первый элемент
, char quotChar = '\''
)
{
return serializeEnumSetImpl(enumValsSet, serializer, seps.c_str(), quotChar);
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
template< typename EnumSetType
, typename EnumTypeDeserializer
> inline
void deserializeEnumSetImpl( EnumSetType &enumValsSet
, std::string str
, EnumTypeDeserializer deserializer
, const char *seps
, char quotChar = '\''
)
{
MARTY_ARG_USED(quotChar);
char sep = ',';
if (seps && *seps)
{
sep = *seps++;
while(*seps)
{
str = simple_string_replace<std::string>(str, std::string(1,*seps++), std::string(1,sep));
}
}
auto isSpaceChar = [](char ch)
{
typedef char CharType;
return ch==(CharType)' ' || ch==(CharType)'\t' || ch==(CharType)'\r' || ch==(CharType)'\n';
};
auto items = simple_seq_filter(simple_string_split(str, std::string(1,sep)), [&](auto s) { return !simple_trim(s, isSpaceChar).empty(); } );
enumValsSet.clear();
for(const auto &i : items)
{
enumValsSet.insert(deserializer(i));
}
}
//-----------------------------------------------------------------------------
template< typename EnumSetType
, typename EnumTypeDeserializer
> inline
void deserializeEnumSetImpl( EnumSetType &enumValsSet
, const std::string &str
, EnumTypeDeserializer deserializer
, const std::string &seps
, char quotChar = '\''
)
{
deserializeEnumSetImpl(enumValsSet, str, deserializer, seps.c_str(), quotChar);
}
//-----------------------------------------------------------------------------
} // namespace marty_cpp