Fawkes API Fawkes Development Version

dynamic_buffer.h

00001 
00002 /***************************************************************************
00003  *  dynamic_buffer.h - A dynamic buffer
00004  *
00005  *  Created: Fri Jun 01 13:28:02 2007
00006  *  Copyright  2007-2008  Tim Niemueller [www.niemueller.de]
00007  *             2007       Daniel Beck
00008  *
00009  ****************************************************************************/
00010 
00011 /*  This program is free software; you can redistribute it and/or modify
00012  *  it under the terms of the GNU General Public License as published by
00013  *  the Free Software Foundation; either version 2 of the License, or
00014  *  (at your option) any later version. A runtime exception applies to
00015  *  this software (see LICENSE.GPL_WRE file mentioned below for details).
00016  *
00017  *  This program is distributed in the hope that it will be useful,
00018  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00019  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020  *  GNU Library General Public License for more details.
00021  *
00022  *  Read the full text in the LICENSE.GPL_WRE file in the doc directory.
00023  */
00024 
00025 #ifndef __NETCOMM_UTILS_DYNAMIC_BUFFER_H_
00026 #define __NETCOMM_UTILS_DYNAMIC_BUFFER_H_
00027 
00028 #include <sys/types.h>
00029 #include <stdint.h>
00030 
00031 namespace fawkes {
00032 
00033 #pragma pack(push,4)
00034 
00035 /** Dynamic list type.
00036  * Use this element in your message struct if you want to add a dynamic list.
00037  * This is meant to be used in conjunction with DynamicBuffer.
00038  */
00039 typedef struct {
00040   uint32_t  size;               /**< total size of list buffer */
00041   uint32_t  num_elements;       /**< number of elements in list */
00042 } dynamic_list_t;
00043 
00044 #pragma pack(pop)
00045 
00046 class DynamicBuffer
00047 {
00048  public:
00049   DynamicBuffer(dynamic_list_t *db, size_t initial_buffer_size = 1024);
00050   DynamicBuffer(dynamic_list_t *db, void *buf, size_t size);
00051   virtual ~DynamicBuffer();
00052 
00053   void   append(const void *data, size_t data_size);
00054   void * buffer();
00055   size_t buffer_size();
00056   unsigned int num_elements();
00057 
00058   size_t real_buffer_size();
00059 
00060   bool   has_next();
00061   void * next(size_t *size);
00062   void   reset_iterator();
00063 
00064  private:
00065 
00066   typedef uint16_t element_header_t;
00067 
00068   void   increase();
00069 
00070   bool               _read_only;
00071 
00072   dynamic_list_t    *_db;
00073   void              *_buffer;
00074   size_t             _buffer_size;
00075   element_header_t  *_curhead;
00076   void              *_curdata;
00077 
00078   uint16_t           _it_curel;
00079   element_header_t  *_it_curhead;
00080   void              *_it_curdata;
00081 
00082 };
00083 
00084 } // end namespace fawkes
00085 
00086 #endif
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends