JayBeams  0.1
Another project to have fun coding.
char_list_field.hpp
Go to the documentation of this file.
1 #ifndef jb_itch5_char_list_field_hpp
2 #define jb_itch5_char_list_field_hpp
3 
6 
7 #include <boost/operators.hpp>
8 
9 #include <cctype>
10 #include <iostream>
11 
12 namespace jb {
13 namespace itch5 {
14 
15 /**
16  * A helper type to define char fields with a limited set of values.
17  *
18  * Many ITCH-5.0 fields are represented by a single byte on the wire,
19  * and are only supposed to take a limited set of values.
20  */
21 template <int... V>
23  : public boost::less_than_comparable<char_list_field<V...>>,
24  public boost::less_than_comparable<char_list_field<V...>, int>,
25  public boost::equality_comparable<char_list_field<V...>>,
26  public boost::equality_comparable<char_list_field<V...>, int> {
27 public:
28  /// Default constructor
30  : value_() {
31  }
32 
33  /**
34  * Constructor from an integer value.
35  *
36  * @throws std::runtime_exception if the value is not in the
37  * template parameter list
38  */
39  explicit char_list_field(int x)
40  : value_(x) {
41  char_list_validator<true, V...> validator;
42  validator(value_);
43  }
44 
45  /// Return the integer value
46  int as_int() const {
47  return value_;
48  }
49 
50  //@{
51  /**
52  * @name Comparison operators
53  */
54  bool operator==(char_list_field const& rhs) const {
55  return value_ == rhs.value_;
56  }
57  bool operator==(int rhs) const {
58  return value_ == rhs;
59  }
60  bool operator<(char_list_field const& rhs) const {
61  return value_ < rhs.value_;
62  }
63  bool operator<(int rhs) const {
64  return value_ < rhs;
65  }
66  //@}
67 
68 private:
69  friend struct decoder<true, char_list_field>;
70  friend struct decoder<false, char_list_field>;
71 
72  /// In-memory representation of the field (int). Typically ints are
73  /// more efficient (in CPU time) than 8-bits octet.
74  int value_;
75 };
76 
77 /// Specialize decoder<bool,T> for char_list_field.
78 template <bool validate, int... V>
79 struct decoder<validate, char_list_field<V...>> {
80  /// Please see the generic documentation for jb::itch5::decoder<>::r()
81  static char_list_field<V...>
82  r(std::size_t size, void const* buf, std::size_t offset) {
83  char_list_field<V...> tmp;
84  tmp.value_ = decoder<false, std::uint8_t>::r(size, buf, offset);
85 
86  char_list_validator<validate, V...> validator;
87  validator(tmp.as_int());
88  return tmp;
89  }
90 };
91 
92 /// Streaming operator for jb::itch5::char_list_field<>
93 template <int... V>
94 std::ostream& operator<<(std::ostream& os, char_list_field<V...> const& x) {
95  if (std::isprint(x.as_int())) {
96  return os << static_cast<char>(x.as_int());
97  }
98  return os << ".(" << x.as_int() << ")";
99 }
100 
101 } // namespace itch5
102 } // namespace jb
103 
104 #endif // jb_itch5_char_list_field_hpp
static T r(std::size_t size, void const *msg, std::size_t offset)
Read a single message or field.
static char_list_field< V... > r(std::size_t size, void const *buf, std::size_t offset)
Please see the generic documentation for jb::itch5::decoder<>::r()
bool operator<(int rhs) const
int value_
In-memory representation of the field (int).
A helper type to define char fields with a limited set of values.
bool operator==(int rhs) const
char_list_field(int x)
Constructor from an integer value.
Define the interface to decode ITCH-5.0 messages and message fields.
Definition: decoder.hpp:24
Define a functor to validate character fields with limited values.
std::ostream & operator<<(std::ostream &os, add_order_message const &x)
Streaming operator for jb::itch5::add_order_message.
bool operator<(char_list_field const &rhs) const
bool operator==(char_list_field const &rhs) const
char_list_field()
Default constructor.
The top-level namespace for the JayBeams library.
Definition: as_hhmmss.hpp:7
int as_int() const
Return the integer value.