Open Lighting Architecture
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
IOQueue.h
1 /*
2  * This library is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU Lesser General Public
4  * License as published by the Free Software Foundation; either
5  * version 2.1 of the License, or (at your option) any later version.
6  *
7  * This library is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10  * Lesser General Public License for more details.
11  *
12  * You should have received a copy of the GNU Lesser General Public
13  * License along with this library; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15  *
16  * IOQueue.h
17  * A non-contigous memory buffer that operates as a queue (FIFO).
18  * Copyright (C) 2012 Simon Newton
19  */
20 
21 #ifndef INCLUDE_OLA_IO_IOQUEUE_H_
22 #define INCLUDE_OLA_IO_IOQUEUE_H_
23 
24 #include <ola/io/IOVecInterface.h>
25 #include <ola/io/InputBuffer.h>
26 #include <ola/io/OutputBuffer.h>
27 #include <stdint.h>
28 #include <sys/uio.h>
29 #include <deque>
30 #include <iostream>
31 #include <queue>
32 #include <string>
33 
34 namespace ola {
35 namespace io {
36 
41  public OutputBufferInterface,
42  public IOVecInterface {
43  public:
44  IOQueue();
45  explicit IOQueue(class MemoryBlockPool *block_pool);
46 
47  ~IOQueue();
48 
49  unsigned int Size() const;
50 
51  bool Empty() const {
52  return m_blocks.empty();
53  }
54 
55  // From OutputBuffer
56  void Write(const uint8_t *data, unsigned int length);
57 
58  // From InputBuffer, these reads consume data from the buffer.
59  unsigned int Read(uint8_t *data, unsigned int length);
60  unsigned int Read(std::string *output, unsigned int length);
61 
62  unsigned int Peek(uint8_t *data, unsigned int length) const;
63 
64  // From IOVecInterface
65  const struct iovec *AsIOVec(int *io_count) const;
66  void Pop(unsigned int n);
67 
68  // Append a MemoryBlock to this IOQueue. Ownership of the block is taken.
69  void AppendBlock(class MemoryBlock *block);
70 
71  void Clear();
72 
73  // purge the underlying memory pool
74  void Purge();
75 
76  void Dump(std::ostream *output);
77 
78  private:
79  typedef std::deque<class MemoryBlock*> BlockVector;
80 
81  class MemoryBlockPool* m_pool;
82  bool m_delete_pool;
83 
84  BlockVector m_blocks;
85 
86  void AppendBlock();
87 
88  // no copying / assignment for now
89  IOQueue(const IOQueue&);
90  IOQueue& operator=(const IOQueue&);
91 };
92 } // namespace io
93 } // namespace ola
94 #endif // INCLUDE_OLA_IO_IOQUEUE_H_