Getting Started 07 Network Programming

LogStream.h

const int kSmallBuffer = 4000;
const int kLargeBuffer = 4000*1000;
temlpate <int SIZE>
class FixedBuffer{
private:
  void (*cookie_)();
  char data_[SIZE];
  char* cur_;
public:
     FixedBuffer() : cur_(data_){//构造函数
        setCookie(cookieStart);
    } 
    ~dtr() {
       setCookie(cookieEnd);
    }
    void append(const char* /*restrict*/ buf, size_t len)   //追加数据
  {
    // FIXME: append partially
    if (implicit_cast<size_t>(avail()) > len)
    {
      memcpy(cur_, buf, len);
      cur_ += len;
    }
  }
  const char* data() const { return data_; }  //返回数据头
  int length() const { return static_cast<int>(cur_ - data_); }  //返回数据长度
  char* current() { return cur_; }  //返回当前数据尾巴 
  int avail() const { return static_cast<int>(end() - cur_); }  //返回缓冲区剩余长度
  void add(size_t len) { cur_ += len; }       //数据区 边长
  const char* debugString();
  void setCookie(void (*cookie)()) { cookie_ = cookie; }  //设置cookie_ 函数指针
  // for used by unit test
  string toString() const { return string(data_, length()); }                  
  StringPiece toStringPiece() const { return StringPiece(data_, length()); }
private:
  const char* end() const { return data_ + sizeof data_; }  //返回缓冲区长度
  // Must be outline function for cookies.
  static void cookieStart(); //静态函数  
  static void cookieEnd();
}

class LogStream : noncopyable  //内部用缓冲区,实现了对移位运算符重载,实现了自己的Log流类
{
public:
 typedef LogStream self;
 typedef detail::FixedBuffer<detail::kSmallBuffer> Buffer;  //FixedBuffer座位内部成员
private:
  void staticCheck();

  template<typename T>
  void formatInteger(T); //模板函数

  Buffer buffer_; //FixedBuffer

  static const int kMaxNumericSize = 32;

 
 public:
  

  self& operator<<(bool v)  //写入bool值
  {
    buffer_.append(v ? "1" : "0", 1);
    return *this;
  }

  self& operator<<(short);
  self& operator<<(unsigned short);
  self& operator<<(int);
  self& operator<<(unsigned int);
  self& operator<<(int);
  self& operator<<(unsigned int);
  self& operator<<(long);
  self& operator<<(unsigned long);
  self& operator<<(long long);
  self& operator<<(unsigned long long);

  self& operator<<(const void*);
 self& operator<<(float v)
  {
    *this << static_cast<double>(v);
    return *this;
  }
  self& operator<<(double);
  // self& operator<<(long double);

  self& operator<<(char v)
  {
    buffer_.append(&v, 1);
    return *this;
  }
 self& operator<<(const char* str)
  {
    if (str)
    {
      buffer_.append(str, strlen(str));
    }
    else
    {
      buffer_.append("(null)", 6);
    }
    return *this;
  }

  self& operator<<(const unsigned char* str)
  {
    return operator<<(reinterpret_cast<const char*>(str));
  }

  self& operator<<(const string& v)
  {
buffer_.append(v.c_str(), v.size());
    return *this;
  }

  self& operator<<(const StringPiece& v)
  {
    buffer_.append(v.data(), v.size());
    return *this;
  }

  self& operator<<(const Buffer& v)
  {
    *this << v.toStringPiece();
    return *this;
  }
void append(const char* data, int len) { buffer_.append(data, len); }
  const Buffer& buffer() const { return buffer_; }
  void resetBuffer() { buffer_.reset(); }
}

class Fmt{     //类似于redis SDS, 
private:
  char buf_[32];
  int length_;
public:
    template<typename T>
    Fmt(const char* fmt, T val);
    const char* data() { return buf_;}
    int length() const { return length_;}
}

inline LogStream& operator<<(LogStream& s, const Fmt& fmt)  //实现了logStream对 fmt的输入重载
{
  s.append(fmt.data(), fmt.length());
  return s;
}

string formatSI(int64_t n);

string formatIEC(int64_t n);

template<typename T>
size_t convert(char buf[], T value); //把int转换为字符串,算法是去余,除10,最后翻转  123455   先得到554321 在翻转一下

*#if __WORDSIZE == 64
# ifndef __intptr_t_defined
typedef long int        intptr_t;
#  define __intptr_t_defined
# endif
typedef unsigned long int   uintptr_t;
#else
# ifndef __intptr_t_defined
typedef int         intptr_t;
#  define __intptr_t_defined
# endif
typedef unsigned int        uintptr_t;
#endif
*
size_t convertHex(char buf[], uintptr_t value) //uint z

Guess you like

Origin www.cnblogs.com/aiqingyi/p/11319726.html