首页 > 其他分享 >big endian (comes from RocketMQ)

big endian (comes from RocketMQ)

时间:2022-08-16 19:44:05浏览次数:58  
标签:return big value len bool RocketMQ ptr comes size

#ifndef BASE_BIG_ENDIAN_H_ #define BASE_BIG_ENDIAN_H_   template <typename T> inline void ReadBigEndian(const char buf[], T* out) {   *out = buf[0];   for (size_t i = 1; i < sizeof(T); ++i) {     *out <<= 8;     // Must cast to uint8_t to avoid clobbering by sign extension.     *out |= static_cast<uint8_t>(buf[i]);    } }   template <typename T> inline void WriteBigEndian(char buf[], T val) {   for (size_t i = 0; i < sizeof(T); ++i) {     buf[sizeof(T) - i - 1] = static_cast<char>(val & 0xFF);     val >>= 8;   } }   class BigEndianReader { public: BigEndianReader(const char* buf, size_t len);
const char* ptr() const { return ptr_; } int remaining() const { return end_ - ptr_; }
bool Skip(size_t len); bool ReadBytes(void* out, size_t len); bool ReadU8(uint8_t* value); bool ReadU16(uint16_t* value); bool ReadU32(uint32_t* value); bool ReadU64(uint64_t* value);
private: // Hidden to promote type safety. template <typename T> bool Read(T* v);
const char* ptr_; const char* end_; };   class BigEndianWriter { public: BigEndianWriter(char* buf, size_t len);
char* ptr() const { return ptr_; } int remaining() const { return end_ - ptr_; }
bool Skip(size_t len); bool WriteBytes(const void* buf, size_t len); bool WriteU8(uint8_t value); bool WriteU16(uint16_t value); bool WriteU32(uint32_t value); bool WriteU64(uint64_t value);
private: // Hidden to promote type safety. template <typename T> bool Write(T v);
char* ptr_; char* end_; }; #endif // BASE_BIG_ENDIAN_H_   ==================================================================== #include "big_endian.h" #include <cstdlib> #include <cstring>

BigEndianReader::BigEndianReader(const char* buf, size_t len) : ptr_(buf), end_(ptr_ + len) {}
bool BigEndianReader::Skip(size_t len) { if (ptr_ + len > end_) return false; ptr_ += len; return true; }
bool BigEndianReader::ReadBytes(void* out, size_t len) { if (ptr_ + len > end_) return false; memcpy(out, ptr_, len); ptr_ += len; return true; }
template <typename T> bool BigEndianReader::Read(T* value) { if (ptr_ + sizeof(T) > end_) return false; ReadBigEndian<T>(ptr_, value); ptr_ += sizeof(T); return true; }
bool BigEndianReader::ReadU8(uint8_t* value) { return Read(value); }
bool BigEndianReader::ReadU16(uint16_t* value) { return Read(value); }
bool BigEndianReader::ReadU32(uint32_t* value) { return Read(value); }
bool BigEndianReader::ReadU64(uint64_t* value) { return Read(value); }
BigEndianWriter::BigEndianWriter(char* buf, size_t len) : ptr_(buf), end_(ptr_ + len) {}
bool BigEndianWriter::Skip(size_t len) { if (ptr_ + len > end_) return false; ptr_ += len; return true; }
bool BigEndianWriter::WriteBytes(const void* buf, size_t len) { if (ptr_ + len > end_) return false; memcpy(ptr_, buf, len); ptr_ += len; return true; }
template <typename T> bool BigEndianWriter::Write(T value) { if (ptr_ + sizeof(T) > end_) return false; WriteBigEndian<T>(ptr_, value); ptr_ += sizeof(T); return true; }
bool BigEndianWriter::WriteU8(uint8_t value) { return Write(value); }
bool BigEndianWriter::WriteU16(uint16_t value) { return Write(value); }
bool BigEndianWriter::WriteU32(uint32_t value) { return Write(value); }
bool BigEndianWriter::WriteU64(uint64_t value) { return Write(value); }


 

标签:return,big,value,len,bool,RocketMQ,ptr,comes,size
From: https://www.cnblogs.com/rohens-hbg/p/16592754.html

相关文章

  • RocketMQ保姆级教程
    大家好,我是三友~~上周花了一点时间从头到尾、从无到有地搭建了一套RocketMQ的环境,觉得还挺easy的,所以就写篇文章分享给大家。整篇文章可以大致分为三个部分,第一部分属于......
  • Ambiguous handler methods mapped for'xxx'报错的解决办法
    这个报错的原因是我们的Controller中,有两个模棱两可的处理方法,这两个方法有歧义,无法分清谁是谁.因为Spring无法根据传参的类型自动匹配到可以处理的方法。比如下面这里,......
  • RocketMQ 两种消费模式
    一、概述RocketMQ主要提供了两种消费模式:集群消费以及广播消费。我们只需要在定义消费者的时候通过setMessageModel(MessageModel.XXX)方法就可以指定是集群还是广播......