libktorrent  2.1.1
circularbuffer.h
1 /***************************************************************************
2  * Copyright (C) 2010 by Joris Guisson *
3  * joris.guisson@gmail.com *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19  ***************************************************************************/
20 
21 #ifndef BT_CIRCULARBUFFER_H
22 #define BT_CIRCULARBUFFER_H
23 
24 #include <ktorrent_export.h>
25 #include <util/constants.h>
26 #include <utility>
27 
28 namespace bt
29 {
30 
34  class KTORRENT_EXPORT CircularBuffer
35  {
36  public:
37  CircularBuffer(bt::Uint32 cap = 64 * 1024);
38  virtual ~CircularBuffer();
39 
46  virtual bt::Uint32 read(bt::Uint8* ptr,bt::Uint32 max_len);
47 
54  virtual bt::Uint32 write(const bt::Uint8* ptr,bt::Uint32 len);
55 
57  bool empty() const {return buf_size == 0;}
58 
60  bool full() const {return buf_size == buf_capacity;}
61 
63  bt::Uint32 size() const {return buf_size;}
64 
66  bt::Uint32 capacity() const {return buf_capacity;}
67 
69  bt::Uint32 available() const {return buf_capacity - buf_size;}
70 
71  private:
72  typedef std::pair<bt::Uint8*,bt::Uint32> Range;
73 
75  Range firstRange();
76  Range secondRange();
77 
78  private:
79  bt::Uint8* data;
80  bt::Uint32 buf_capacity;
81  bt::Uint32 start;
82  bt::Uint32 buf_size;
83  };
84 
85 }
86 
87 #endif // BT_CIRCULARBUFFER_H