Open Lighting Architecture  0.9.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
IOStack.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  * IOStack.h
17  * A non-contigous memory buffer that operates as a stack (LIFO).
18  * Copyright (C) 2013 Simon Newton
19  */
20 
21 #ifndef INCLUDE_OLA_IO_IOSTACK_H_
22 #define INCLUDE_OLA_IO_IOSTACK_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 
40 class IOStack: public IOVecInterface,
41  public InputBufferInterface,
42  public OutputBufferInterface {
43  public:
44  IOStack();
45  explicit IOStack(class MemoryBlockPool *block_pool);
46 
47  ~IOStack();
48 
49  unsigned int Size() const;
50 
51  bool Empty() const {
52  // guard against the case of Empty blocks.
53  return m_blocks.empty() || Size() == 0;
54  }
55 
56  // From OutputBufferInterface
57  void Write(const uint8_t *data, unsigned int length);
58 
59  // From InputBufferInterface, these reads consume data from the buffer.
60  unsigned int Read(uint8_t *data, unsigned int length);
61  unsigned int Read(std::string *output, unsigned int length);
62 
63  // From IOVecInterface
64  const struct iovec *AsIOVec(int *io_count) const;
65  void Pop(unsigned int n);
66 
67  // 0-copy append to an IOQueue
68  void MoveToIOQueue(class IOQueue *queue);
69 
70  // purge the underlying memory pool
71  void Purge();
72 
73  void Dump(std::ostream *output);
74 
75  private:
76  typedef std::deque<class MemoryBlock*> BlockVector;
77 
78  class MemoryBlockPool* m_pool;
79  bool m_delete_pool;
80 
81  BlockVector m_blocks;
82 
83  void PrependBlock();
84 
85  // no copying / assignment for now
86  IOStack(const IOStack&);
87  IOStack& operator=(const IOStack&);
88 };
89 } // namespace io
90 } // namespace ola
91 #endif // INCLUDE_OLA_IO_IOSTACK_H_