Fawkes API  Fawkes Development Version
dynamic_buffer.h
1 
2 /***************************************************************************
3  * dynamic_buffer.h - A dynamic buffer
4  *
5  * Created: Fri Jun 01 13:28:02 2007
6  * Copyright 2007-2008 Tim Niemueller [www.niemueller.de]
7  * 2007 Daniel Beck
8  *
9  ****************************************************************************/
10 
11 /* This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version. A runtime exception applies to
15  * this software (see LICENSE.GPL_WRE file mentioned below for details).
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU Library General Public License for more details.
21  *
22  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
23  */
24 
25 #ifndef _NETCOMM_UTILS_DYNAMIC_BUFFER_H_
26 #define _NETCOMM_UTILS_DYNAMIC_BUFFER_H_
27 
28 #include <sys/types.h>
29 
30 #include <stdint.h>
31 
32 namespace fawkes {
33 
34 #pragma pack(push, 4)
35 
36 /** Dynamic list type.
37  * Use this element in your message struct if you want to add a dynamic list.
38  * This is meant to be used in conjunction with DynamicBuffer.
39  */
40 typedef struct
41 {
42  uint32_t size; /**< total size of list buffer */
43  uint32_t num_elements; /**< number of elements in list */
45 
46 #pragma pack(pop)
47 
49 {
50 public:
51  DynamicBuffer(dynamic_list_t *db, size_t initial_buffer_size = 1024);
52  DynamicBuffer(dynamic_list_t *db, void *buf, size_t size);
53  virtual ~DynamicBuffer();
54 
55  void append(const void *data, size_t data_size);
56  void * buffer();
57  size_t buffer_size();
58  unsigned int num_elements();
59 
60  size_t real_buffer_size();
61 
62  bool has_next();
63  void *next(size_t *size);
64  void reset_iterator();
65 
66 private:
67  typedef uint16_t element_header_t;
68 
69  void increase();
70 
71  bool _read_only;
72 
73  dynamic_list_t * _db;
74  void * _buffer;
75  size_t _buffer_size;
76  element_header_t *_curhead;
77  void * _curdata;
78 
79  uint16_t _it_curel;
80  element_header_t *_it_curhead;
81  void * _it_curdata;
82 };
83 
84 } // end namespace fawkes
85 
86 #endif
fawkes::DynamicBuffer::append
void append(const void *data, size_t data_size)
Append data.
Definition: dynamic_buffer.cpp:120
fawkes::dynamic_list_t::num_elements
uint32_t num_elements
number of elements in list
Definition: dynamic_buffer.h:43
fawkes::DynamicBuffer::num_elements
unsigned int num_elements()
Get number of elements.
Definition: dynamic_buffer.cpp:215
fawkes::DynamicBuffer::has_next
bool has_next()
Check if another element is available.
Definition: dynamic_buffer.cpp:235
fawkes::DynamicBuffer
Dynamically growing buffer.
Definition: dynamic_buffer.h:49
fawkes::dynamic_list_t::size
uint32_t size
total size of list buffer
Definition: dynamic_buffer.h:42
fawkes::DynamicBuffer::buffer
void * buffer()
Get pointer to buffer.
Definition: dynamic_buffer.cpp:155
fawkes::DynamicBuffer::~DynamicBuffer
virtual ~DynamicBuffer()
Destructor.
Definition: dynamic_buffer.cpp:104
fawkes::DynamicBuffer::buffer_size
size_t buffer_size()
Get buffer size.
Definition: dynamic_buffer.cpp:195
fawkes
Fawkes library namespace.
fawkes::dynamic_list_t
Dynamic list type.
Definition: dynamic_buffer.h:41
fawkes::DynamicBuffer::real_buffer_size
size_t real_buffer_size()
Get real buffer size.
Definition: dynamic_buffer.cpp:206
fawkes::DynamicBuffer::reset_iterator
void reset_iterator()
Reset iterator.
Definition: dynamic_buffer.cpp:223
fawkes::DynamicBuffer::next
void * next(size_t *size)
Get next buffer.
Definition: dynamic_buffer.cpp:247
fawkes::DynamicBuffer::DynamicBuffer
DynamicBuffer(dynamic_list_t *db, size_t initial_buffer_size=1024)
Write constructor.
Definition: dynamic_buffer.cpp:63