JayBeams  0.1
Another project to have fun coding.
char_list_validator.hpp
Go to the documentation of this file.
1 #ifndef jb_itch5_char_list_validator_hpp
2 #define jb_itch5_char_list_validator_hpp
3 
4 namespace jb {
5 namespace itch5 {
6 
7 /**
8  * Define a functor to validate character fields with limited values.
9  *
10  * Many ITCH-5.0 fields are single character wide (on the wire) and
11  * are only supposed to take a limited set of values. We want a
12  * functor to validate these fields against their list of values, but
13  * also to disable the validation if not needed.
14  *
15  * Typically this validators would be used as:
16  * typedef char_list_validator<true,u'A',u'B',u'C'> validator;
17  * or
18  * typedef char_list_validator<false,u'A',u'B',u'C'> validator;
19  *
20  * The first template parameter defines if the validator actually
21  * performs any checking at all, in production one probably wants its
22  * value to be 'false'. The remaining template parameters define the
23  * list of possible values to be accepted.
24  */
25 template <bool validate, int... V>
27  void operator()(int x) const {
28  }
29 };
30 
31 /// Helper function, raises an exception describing a mismatched value.
32 [[noreturn]] void char_list_validation_failed(int x);
33 
34 /**
35  * Specialize for the empty list.
36  *
37  * All values are rejected in this case.
38  */
39 template <>
40 struct char_list_validator<true> {
41  void operator()(int x) const {
43  }
44 };
45 
46 /**
47  * Recursively define the validator for the enabled case.
48  */
49 template <int a, int... V>
50 struct char_list_validator<true, a, V...> {
51  void operator()(int x) const {
52  if (a == x) {
53  return;
54  }
55  char_list_validator<true, V...> f;
56  f(x);
57  }
58 };
59 
60 } // namespace itch5
61 } // namespace jb
62 
63 #endif // jb_itch5_char_list_validator_hpp
Define a functor to validate character fields with limited values.
void char_list_validation_failed(int x)
Helper function, raises an exception describing a mismatched value.
The top-level namespace for the JayBeams library.
Definition: as_hhmmss.hpp:7