- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 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
struct read_access_tag {};
struct write_access_tag {};
struct read_write_access_tag : read_access_tag, write_access_tag {};
template <
typename ByteOrder,
typename ValueType
>
std::size_t get_from(const uint8_t *src, ValueType &dst, const read_access_tag&) {
ByteOrder::decode(src, dst);
return sizeof(dst);
}
template <
typename ByteOrder,
typename ValueType
>
std::size_t put_into(ValueType src, uint8_t *dst, const write_access_tag&) {
ByteOrder::encode(src, dst);
return sizeof(src);
}
// ...
template <
typename ByteOrder = default_byte_order,
typename AccessTag = read_write_access_tag
>
class basic_buffer {
public:
typedef ByteOrder byte_order;
typedef AccessTag access_tag;
typedef typename access_traits<access_tag>::value_type value_type;
typedef typename access_traits<access_tag>::strict_type strict_type;
typedef typename access_traits<access_tag>::iterator iterator;
typedef typename access_traits<access_tag>::const_iterator const_iterator;
basic_buffer(iterator begin, iterator end)
: begin_(begin)
, end_(end)
, pos_(begin)
{}
// ...
template <typename T>
basic_buffer & put(T value) {
if (bytes_left() < sizeof(value)) throw Overflow;
pos_ += put_into<byte_order>(value, pos_, access_tag());
return *this;
}
template <typename T>
basic_buffer & get(T &value) {
if (bytes_left() < sizeof(value)) throw Overflow;
pos_ += get_from<byte_order>(pos_, value, access_tag());
return *this;
}
}