bit_stream.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2014 Pavel Kirienko <pavel.kirienko@gmail.com>
3  */
4 
5 #ifndef UAVCAN_MARSHAL_BIT_STREAM_HPP_INCLUDED
6 #define UAVCAN_MARSHAL_BIT_STREAM_HPP_INCLUDED
7 
8 #include <cassert>
9 #include <uavcan/std.hpp>
12 #include <uavcan/build_config.hpp>
13 
14 namespace uavcan
15 {
24 void bitarrayCopy(const unsigned char* src_org, std::size_t src_offset, std::size_t src_len,
25  unsigned char* dst_org, std::size_t dst_offset);
26 
32 {
33  static const unsigned MaxBytesPerRW = 16;
34 
36  unsigned bit_offset_;
38 
39  static inline unsigned bitlenToBytelen(unsigned bits) { return (bits + 7) / 8; }
40 
41  static inline void copyBitArrayAlignedToUnaligned(const uint8_t* src_org, unsigned src_len,
42  uint8_t* dst_org, unsigned dst_offset)
43  {
44  bitarrayCopy(reinterpret_cast<const unsigned char*>(src_org), 0, src_len,
45  reinterpret_cast<unsigned char*>(dst_org), dst_offset);
46  }
47 
48  static inline void copyBitArrayUnalignedToAligned(const uint8_t* src_org, unsigned src_offset, unsigned src_len,
49  uint8_t* dst_org)
50  {
51  bitarrayCopy(reinterpret_cast<const unsigned char*>(src_org), src_offset, src_len,
52  reinterpret_cast<unsigned char*>(dst_org), 0);
53  }
54 
55 public:
56  static const unsigned MaxBitsPerRW = MaxBytesPerRW * 8;
57 
58  enum
59  {
60  ResultOutOfBuffer = 0,
61  ResultOk = 1
62  };
63 
64  explicit BitStream(ITransferBuffer& buf)
65  : buf_(buf)
66  , bit_offset_(0)
67  , byte_cache_(0)
68  {
69  StaticAssert<sizeof(uint8_t) == 1>::check();
70  }
71 
83  int write(const uint8_t* bytes, const unsigned bitlen);
84  int read(uint8_t* bytes, const unsigned bitlen);
85 
86 #if UAVCAN_TOSTRING
87  std::string toString() const;
88 #endif
89 };
90 
91 }
92 
93 #endif // UAVCAN_MARSHAL_BIT_STREAM_HPP_INCLUDED
std::uint8_t uint8_t
Definition: std.hpp:24
static unsigned bitlenToBytelen(unsigned bits)
Definition: bit_stream.hpp:39
unsigned long size_t
static std::string toString(long x)
Definition: multiset.cpp:16
static void copyBitArrayAlignedToUnaligned(const uint8_t *src_org, unsigned src_len, uint8_t *dst_org, unsigned dst_offset)
Definition: bit_stream.hpp:41
unsigned bit_offset_
Definition: bit_stream.hpp:36
void bitarrayCopy(const unsigned char *src_org, std::size_t src_offset, std::size_t src_len, unsigned char *dst_org, std::size_t dst_offset)
static void copyBitArrayUnalignedToAligned(const uint8_t *src_org, unsigned src_offset, unsigned src_len, uint8_t *dst_org)
Definition: bit_stream.hpp:48
ITransferBuffer & buf_
Definition: bit_stream.hpp:35
BitStream(ITransferBuffer &buf)
Definition: bit_stream.hpp:64


uavcan_communicator
Author(s):
autogenerated on Wed Jan 11 2023 03:59:39