siostream.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #include <siostream.hpp>
  2. #include <cassert>
  3. #include <algorithm>
  4. using std::size_t;
  5. socket_ostream::socket_ostream() : std::ostream(), buffer(1024){}
  6. circular_buffer::circular_buffer(size_t size) : m_data(size), m_begin(this, m_data.data(), size, 0), m_end(this, m_data.data(), size, 0){
  7. }
  8. circular_buffer::iterator::iterator(const circular_buffer* p, char* _source, size_t _period, size_t _offset):parent(p), source(_source),period(_period), offset(_offset){
  9. }
  10. circular_buffer::const_iterator::const_iterator(const circular_buffer* p, const char* _source, size_t _period, size_t _offset):parent(p), source(_source),period(_period), offset(_offset){
  11. }
  12. void circular_buffer::resize(size_t size){
  13. std::vector<char> newdata(size);
  14. size_t os = m_end - m_begin;
  15. if(size >= (m_begin - m_end))
  16. std::copy(m_begin, m_begin, newdata.begin());
  17. else
  18. std::copy(m_begin, m_begin + size, newdata.begin());
  19. std::swap(m_data, newdata);
  20. m_begin = iterator(this, m_data.data(),size,0);
  21. m_end = iterator(this, m_data.data(),size,os);
  22. }
  23. char& circular_buffer::iterator::operator*()const{
  24. return *(source + offset);
  25. }
  26. const char& circular_buffer::const_iterator::operator*()const {
  27. return *(source + offset);
  28. }
  29. circular_buffer::iterator& circular_buffer::iterator::operator++(){
  30. ++offset;
  31. if(offset == period)offset = 0;
  32. return *this;
  33. }
  34. circular_buffer::const_iterator& circular_buffer::const_iterator::operator++(){
  35. ++offset;
  36. if(offset == period)offset = 0;
  37. return *this;
  38. }
  39. circular_buffer::iterator circular_buffer::iterator::operator+(size_t leap)const{
  40. return circular_buffer::iterator(parent, source, period, (offset + leap) % period);
  41. }
  42. circular_buffer::const_iterator circular_buffer::const_iterator::operator+(size_t leap)const{
  43. return circular_buffer::const_iterator(parent, source, period, (offset + leap) % period);
  44. }
  45. std::ptrdiff_t circular_buffer::iterator::operator-(const iterator& o)const{
  46. using ui = std::ptrdiff_t;
  47. assert(parent == o.parent);
  48. ui so = parent->m_begin.offset;
  49. ui new_this_offset = ((ui)(this->offset) - so) % period;
  50. ui new_o_offset = ((ui)(o.offset) - so) % period;
  51. return new_this_offset - new_o_offset;
  52. }
  53. std::ptrdiff_t circular_buffer::iterator::operator-(const const_iterator& o)const{
  54. using ui = std::ptrdiff_t;
  55. assert(parent == o.parent);
  56. ui so = parent->m_begin.offset;
  57. ui new_this_offset = ((ui)(this->offset) - so) % period;
  58. ui new_o_offset = ((ui)(o.offset) - so) % period;
  59. return new_this_offset - new_o_offset;
  60. }
  61. std::ptrdiff_t circular_buffer::const_iterator::operator-(const iterator& o)const{
  62. using ui = std::ptrdiff_t;
  63. assert(parent == o.parent);
  64. ui so = parent->m_begin.offset;
  65. ui new_this_offset = ((ui)(this->offset) - so) % period;
  66. ui new_o_offset = ((ui)(o.offset) - so) % period;
  67. return new_this_offset - new_o_offset;
  68. }
  69. std::ptrdiff_t circular_buffer::const_iterator::operator-(const const_iterator& o)const{
  70. using ui = std::ptrdiff_t;
  71. assert(parent == o.parent);
  72. ui so = parent->m_begin.offset;
  73. ui new_this_offset = ((ui)(this->offset) - so) % period;
  74. ui new_o_offset = ((ui)(o.offset) - so) % period;
  75. return new_this_offset - new_o_offset;
  76. }
  77. circular_buffer::iterator& circular_buffer::iterator::operator=(const circular_buffer::iterator& o){
  78. source = o.source;
  79. period = o.period;
  80. offset = o.offset;
  81. }
  82. circular_buffer::const_iterator& circular_buffer::const_iterator::operator=(const circular_buffer::const_iterator& o){
  83. source = o.source;
  84. period = o.period;
  85. offset = o.offset;
  86. }
  87. bool circular_buffer::iterator::operator!=(const iterator& o){
  88. return source != o.source || parent != o.parent || period != o.period || offset != o.offset;
  89. }
  90. bool circular_buffer::iterator::operator!=(const const_iterator& o){
  91. return source != o.source || parent != o.parent || period != o.period || offset != o.offset;
  92. }
  93. bool circular_buffer::const_iterator::operator!=(const iterator& o){
  94. return source != o.source || parent != o.parent || period != o.period || offset != o.offset;
  95. }
  96. bool circular_buffer::const_iterator::operator!=(const const_iterator& o){
  97. return source != o.source || parent != o.parent || period != o.period || offset != o.offset;
  98. }
  99. bool circular_buffer::iterator::operator==(const iterator& o){
  100. return source == o.source && parent == o.parent && period == o.period && offset == o.offset;
  101. }
  102. bool circular_buffer::iterator::operator==(const const_iterator& o){
  103. return source == o.source && parent == o.parent && period == o.period && offset == o.offset;
  104. }
  105. bool circular_buffer::const_iterator::operator==(const iterator& o){
  106. return source == o.source && parent == o.parent && period == o.period && offset == o.offset;
  107. }
  108. bool circular_buffer::const_iterator::operator==(const const_iterator& o){
  109. return source == o.source && parent == o.parent && period == o.period && offset == o.offset;
  110. }
  111. circular_buffer::iterator& circular_buffer::iterator::operator--(){
  112. if(offset == 0)
  113. offset = period;
  114. --offset;
  115. }
  116. circular_buffer::const_iterator& circular_buffer::const_iterator::operator--(){
  117. if(offset == 0)
  118. offset = period;
  119. --offset;
  120. }
  121. circular_buffer::iterator::operator const_iterator(){
  122. return const_iterator(parent, source, period, offset);
  123. }
  124. circular_buffer::iterator circular_buffer::begin(){
  125. return m_begin;
  126. }
  127. circular_buffer::const_iterator circular_buffer::begin() const{
  128. return circular_buffer::const_iterator(m_begin.parent, m_begin.source, m_begin.period, m_begin.offset);
  129. }
  130. circular_buffer::iterator circular_buffer::end(){
  131. return m_end;
  132. }
  133. circular_buffer::const_iterator circular_buffer::end()const{
  134. return circular_buffer::const_iterator(m_end.parent, m_end.source, m_end.period, m_end.offset);
  135. }
  136. void circular_buffer::push_front(char c){
  137. --m_begin;
  138. *m_begin = c;
  139. }
  140. void circular_buffer::push_back(char c){
  141. *m_end = c;
  142. ++m_end;
  143. }
  144. void circular_buffer::pop_front(char c){
  145. ++m_begin;
  146. }
  147. void circular_buffer::pop_back(char c){
  148. --m_end;
  149. }
  150. socket_ostream& socket_ostream::put(char_type c){
  151. }
  152. socket_ostream& socket_ostream::operator<<(char*){
  153. }
  154. socket_ostream& socket_ostream::operator<<(const std::string&){
  155. }
  156. socket_ostream& socket_ostream::operator<<(int){
  157. }