autobuffer.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // Tencent is pleased to support the open source community by making Mars available.
  2. // Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.
  3. // Licensed under the MIT License (the "License"); you may not use this file except in
  4. // compliance with the License. You may obtain a copy of the License at
  5. // http://opensource.org/licenses/MIT
  6. // Unless required by applicable law or agreed to in writing, software distributed under the License is
  7. // distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
  8. // either express or implied. See the License for the specific language governing permissions and
  9. // limitations under the License.
  10. #ifndef COMM_AUTOBUFFER_H_
  11. #define COMM_AUTOBUFFER_H_
  12. #include <sys/types.h>
  13. #include <string.h>
  14. class AutoBuffer {
  15. public:
  16. enum TSeek {
  17. ESeekStart,
  18. ESeekCur,
  19. ESeekEnd,
  20. };
  21. public:
  22. explicit AutoBuffer(size_t _size = 128);
  23. explicit AutoBuffer(void* _pbuffer, size_t _len, size_t _size = 128);
  24. explicit AutoBuffer(const void* _pbuffer, size_t _len, size_t _size = 128);
  25. ~AutoBuffer();
  26. void AllocWrite(size_t _readytowrite, bool _changelength = true);
  27. void AddCapacity(size_t _len);
  28. template<class T> void Write(const T& _val)
  29. { Write(&_val, sizeof(_val));}
  30. template<class T> void Write(off_t& _pos, const T& _val)
  31. { Write(_pos, &_val, sizeof(_val));}
  32. template<class T> void Write(const off_t& _pos, const T& _val)
  33. { Write(_pos, &_val, sizeof(_val));}
  34. void Write(const char* const _val)
  35. { Write(_val, strlen(_val));}
  36. void Write(off_t& _pos, const char* const _val)
  37. { Write(_pos, _val, strlen(_val));}
  38. void Write(const off_t& _pos, const char* const _val)
  39. { Write(_pos, _val, strlen(_val));}
  40. void Write(const AutoBuffer& _buffer);
  41. void Write(const void* _pbuffer, size_t _len);
  42. void Write(off_t& _pos, const AutoBuffer& _buffer);
  43. void Write(off_t& _pos, const void* _pbuffer, size_t _len);
  44. void Write(const off_t& _pos, const AutoBuffer& _buffer);
  45. void Write(const off_t& _pos, const void* _pbuffer, size_t _len);
  46. void Write(TSeek _seek, const void* _pbuffer, size_t _len);
  47. template<class T> size_t Read(T& _val)
  48. { return Read(&_val, sizeof(_val)); }
  49. template<class T> size_t Read(off_t& _pos, T& _val) const
  50. { return Read(_pos, &_val, sizeof(_val)); }
  51. template<class T> size_t Read(const off_t& _pos, T& _val) const
  52. { return Read(_pos, &_val, sizeof(_val)); }
  53. size_t Read(void* _pbuffer, size_t _len);
  54. size_t Read(AutoBuffer& _rhs , size_t _len);
  55. size_t Read(off_t& _pos, void* _pbuffer, size_t _len) const;
  56. size_t Read(off_t& _pos, AutoBuffer& _rhs, size_t _len) const;
  57. size_t Read(const off_t& _pos, void* _pbuffer, size_t _len) const;
  58. size_t Read(const off_t& _pos, AutoBuffer& _rhs, size_t _len) const;
  59. off_t Move(off_t _move_len);
  60. void Seek(off_t _offset, TSeek _eorigin);
  61. void Length(off_t _pos, size_t _lenght);
  62. void* Ptr(off_t _offset=0);
  63. void* PosPtr();
  64. const void* Ptr(off_t _offset=0) const;
  65. const void* PosPtr() const;
  66. off_t Pos() const;
  67. size_t PosLength() const;
  68. size_t Length() const;
  69. size_t Capacity() const;
  70. void Attach(void* _pbuffer, size_t _len);
  71. void Attach(AutoBuffer& _rhs);
  72. void* Detach(size_t* _plen = NULL);
  73. void Reset();
  74. private:
  75. void __FitSize(size_t _len);
  76. private:
  77. AutoBuffer(const AutoBuffer& _rhs);
  78. AutoBuffer& operator = (const AutoBuffer& _rhs);
  79. private:
  80. unsigned char* parray_;
  81. off_t pos_;
  82. size_t length_;
  83. size_t capacity_;
  84. size_t malloc_unitsize_;
  85. };
  86. extern const AutoBuffer KNullAtuoBuffer;
  87. template <class S> class copy_wrapper_helper;
  88. template <>
  89. class copy_wrapper_helper<AutoBuffer> {
  90. public:
  91. static void copy_constructor(AutoBuffer& _lhs, AutoBuffer& _rhs)
  92. { _lhs.Attach(_rhs); }
  93. static void copy_constructor(AutoBuffer& _lhs, const AutoBuffer& _rhs)
  94. { _lhs.Attach(const_cast<AutoBuffer&>(_rhs)); }
  95. static void destructor(AutoBuffer& _delobj) {}
  96. };
  97. #endif //COMM_AUTOBUFFER_H_