Open Lighting Architecture  Latest Git
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/base/Macro.h>
25 #include <ola/io/IOVecInterface.h>
26 #include <ola/io/InputBuffer.h>
27 #include <ola/io/OutputBuffer.h>
28 #include <stdint.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
87 };
88 } // namespace io
89 } // namespace ola
90 #endif // INCLUDE_OLA_IO_IOSTACK_H_
void MoveToIOQueue(class IOQueue *queue)
Definition: IOStack.cpp:204
#define DISALLOW_COPY_AND_ASSIGN(TypeName)
Creates dummy copy constructor and assignment operator declarations.
Definition: Macro.h:44
Definition: OutputBuffer.h:36
void Dump(std::ostream *output)
Definition: IOStack.cpp:220
unsigned int Size() const
Definition: IOStack.cpp:71
~IOStack()
Definition: IOStack.cpp:57
void Write(const uint8_t *data, unsigned int length)
Definition: IOStack.cpp:86
Definition: IOVecInterface.h:36
Definition: IOQueue.h:40
Definition: IOVecInterface.h:53
Definition: InputBuffer.h:34
Helper macros.
unsigned int Read(uint8_t *data, unsigned int length)
Definition: IOStack.cpp:106
Definition: IOStack.h:40
The namespace containing all OLA symbols.
Definition: Credentials.cpp:44
IOStack()
Definition: IOStack.cpp:43
MemoryBlockPool. This class is not thread safe.
Definition: MemoryBlockPool.h:35
void Pop(unsigned int n)
Definition: IOStack.cpp:182
const struct IOVec * AsIOVec(int *io_count) const
Definition: IOStack.cpp:157