JayBeams  0.1
Another project to have fun coding.
base_encoders.hpp
Go to the documentation of this file.
1 #ifndef jb_itch5_base_encoders_hpp
2 #define jb_itch5_base_encoders_hpp
3 
4 #include <jb/itch5/encoder.hpp>
5 
6 #include <cstdint>
7 
8 namespace jb {
9 namespace itch5 {
10 
11 /// Specialize jb::itch5::encoder for 1-byte integer (or character) fields.
12 template <bool validate>
13 struct encoder<validate, std::uint8_t> {
14  static void
15  w(std::size_t size, void* msg, std::size_t offset, std::uint8_t x) {
16  check_offset<validate>("encode std::uint8_t", size, offset, 1);
17  static_cast<std::uint8_t*>(msg)[offset] = x;
18  }
19 };
20 
21 /// Specialize jb::itch5::encoder for 2-byte integer fields.
22 template <bool validate>
23 struct encoder<validate, std::uint16_t> {
24  /// Please see the generic documentation for jb::itch5::encoder<>::w()
25  static void
26  w(std::size_t size, void* msg, std::size_t offset, std::uint16_t x) {
27  check_offset<validate>("encode std::uint16_t", size, offset, 2);
28  encoder<false, std::uint8_t>::w(size, msg, offset, std::uint8_t(x >> 8));
30  size, msg, offset + 1, std::uint8_t(x & 0xff));
31  }
32 };
33 
34 /// Specialize jb::itch5::encoder for 4-byte integer fields.
35 template <bool validate>
36 struct encoder<validate, std::uint32_t> {
37  /// Please see the generic documentation for jb::itch5::encoder<>::w()
38  static void
39  w(std::size_t size, void* msg, std::size_t offset, std::uint32_t x) {
40  check_offset<validate>("encode std::uint32_t", size, offset, 4);
41  encoder<false, std::uint16_t>::w(size, msg, offset, std::uint16_t(x >> 16));
43  size, msg, offset + 2, std::uint16_t(x & 0xffff));
44  }
45 };
46 
47 /// Specialize jb::itch5::encoder for 4-byte integer fields.
48 template <bool validate>
49 struct encoder<validate, std::uint64_t> {
50  /// Please see the generic documentation for jb::itch5::encoder<>::w()
51  static void
52  w(std::size_t size, void* msg, std::size_t offset, std::uint64_t x) {
53  check_offset<validate>("encode std::uint32_t", size, offset, 8);
54  encoder<false, std::uint32_t>::w(size, msg, offset, std::uint32_t(x >> 32));
56  size, msg, offset + 4, std::uint32_t(x & 0xffffffff));
57  }
58 };
59 
60 } // namespace itch5
61 } // namespace jb
62 
63 #endif // jb_itch5_base_encoders_hpp
static void w(std::size_t size, void *msg, std::size_t offset, T const &x)
Write a single message or field to a buffer.
STL namespace.
static void w(std::size_t size, void *msg, std::size_t offset, std::uint8_t x)
static void w(std::size_t size, void *msg, std::size_t offset, std::uint16_t x)
Please see the generic documentation for jb::itch5::encoder<>::w()
TODO(#19) all this code should be replaced with Boost.Endian.
Definition: encoder.hpp:13
static void w(std::size_t size, void *msg, std::size_t offset, std::uint64_t x)
Please see the generic documentation for jb::itch5::encoder<>::w()
static void w(std::size_t size, void *msg, std::size_t offset, std::uint32_t x)
Please see the generic documentation for jb::itch5::encoder<>::w()
The top-level namespace for the JayBeams library.
Definition: as_hhmmss.hpp:7