12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- #pragma once
- #include "defaults.h"
- #include "yassert.h"
- #include <string.h>
- #include <type_traits>
- template <class T>
- inline T ReadUnaligned(const void* from) noexcept {
- T ret;
- memcpy(&ret, from, sizeof(T));
- return ret;
- }
- template <class T>
- inline void WriteUnaligned(void* to, const std::remove_reference_t<T>& t) noexcept {
- memcpy(to, &t, sizeof(T));
- }
- template <class T, unsigned Align = sizeof(T)>
- class TUnalignedMemoryIterator {
- public:
- inline TUnalignedMemoryIterator(const void* buf, size_t len)
- : Current_((const unsigned char*)buf)
- , End_(Current_ + len)
- , AlignedEnd_(End_ - (len % Align))
- {
- Y_FAKE_READ(buf);
- }
- inline bool AtEnd() const noexcept {
- return Current_ == AlignedEnd_;
- }
- inline T Cur() const noexcept {
- Y_ASSERT(Current_ < AlignedEnd_ || sizeof(T) < Align);
- return ::ReadUnaligned<T>(Current_);
- }
- inline T Next() noexcept {
- T ret(Cur());
- Current_ += sizeof(T);
- return ret;
- }
- inline const unsigned char* Last() const noexcept {
- return Current_;
- }
- inline size_t Left() const noexcept {
- return End_ - Current_;
- }
- private:
- const unsigned char* Current_;
- const unsigned char* End_;
- const unsigned char* AlignedEnd_;
- };
|