Open Lighting Architecture  0.9.4
 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 <deque>
29 #include <iostream>
30 #include <queue>
31 #include <string>
32 
33 namespace ola {
34 namespace io {
35 
39 class IOStack: public IOVecInterface,
40  public InputBufferInterface,
41  public OutputBufferInterface {
42  public:
43  IOStack();
44  explicit IOStack(class MemoryBlockPool *block_pool);
45 
46  ~IOStack();
47 
48  unsigned int Size() const;
49 
50  bool Empty() const {
51  // guard against the case of Empty blocks.
52  return m_blocks.empty() || Size() == 0;
53  }
54 
55  // From OutputBufferInterface
56  void Write(const uint8_t *data, unsigned int length);
57 
58  // From InputBufferInterface, 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  // From IOVecInterface
63  const struct IOVec *AsIOVec(int *io_count) const;
64  void Pop(unsigned int n);
65 
66  // 0-copy append to an IOQueue
67  void MoveToIOQueue(class IOQueue *queue);
68 
69  // purge the underlying memory pool
70  void Purge();
71 
72  void Dump(std::ostream *output);
73 
74  private:
75  typedef std::deque<class MemoryBlock*> BlockVector;
76 
77  class MemoryBlockPool* m_pool;
78  bool m_delete_pool;
79 
80  BlockVector m_blocks;
81 
82  void PrependBlock();
83 
84  // no copying / assignment for now
85  IOStack(const IOStack&);
86  IOStack& operator=(const IOStack&);
87 };
88 } // namespace io
89 } // namespace ola
90 #endif // INCLUDE_OLA_IO_IOSTACK_H_