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 #include <stdint.h>
30 
31 namespace fawkes {
32 
33 #pragma pack(push,4)
34 
35 /** Dynamic list type.
36  * Use this element in your message struct if you want to add a dynamic list.
37  * This is meant to be used in conjunction with DynamicBuffer.
38  */
39 typedef struct {
40  uint32_t size; /**< total size of list buffer */
41  uint32_t num_elements; /**< number of elements in list */
43 
44 #pragma pack(pop)
45 
47 {
48  public:
49  DynamicBuffer(dynamic_list_t *db, size_t initial_buffer_size = 1024);
50  DynamicBuffer(dynamic_list_t *db, void *buf, size_t size);
51  virtual ~DynamicBuffer();
52 
53  void append(const void *data, size_t data_size);
54  void * buffer();
55  size_t buffer_size();
56  unsigned int num_elements();
57 
58  size_t real_buffer_size();
59 
60  bool has_next();
61  void * next(size_t *size);
62  void reset_iterator();
63 
64  private:
65 
66  typedef uint16_t element_header_t;
67 
68  void increase();
69 
70  bool _read_only;
71 
72  dynamic_list_t *_db;
73  void *_buffer;
74  size_t _buffer_size;
75  element_header_t *_curhead;
76  void *_curdata;
77 
78  uint16_t _it_curel;
79  element_header_t *_it_curhead;
80  void *_it_curdata;
81 
82 };
83 
84 } // end namespace fawkes
85 
86 #endif
DynamicBuffer(dynamic_list_t *db, size_t initial_buffer_size=1024)
Write constructor.
unsigned int num_elements()
Get number of elements.
uint32_t size
total size of list buffer
uint32_t num_elements
number of elements in list
Fawkes library namespace.
void * buffer()
Get pointer to buffer.
Dynamic list type.
bool has_next()
Check if another element is available.
size_t buffer_size()
Get buffer size.
virtual ~DynamicBuffer()
Destructor.
size_t real_buffer_size()
Get real buffer size.
Dynamically growing buffer.
void reset_iterator()
Reset iterator.
void append(const void *data, size_t data_size)
Append data.
void * next(size_t *size)
Get next buffer.