xorp

buffer.hh

00001 // -*- c-basic-offset: 4; tab-width: 8; indent-tabs-mode: t -*-
00002 // vim:set sts=4 ts=8:
00003 
00004 // Copyright (c) 2001-2011 XORP, Inc and Others
00005 //
00006 // This program is free software; you can redistribute it and/or modify
00007 // it under the terms of the GNU Lesser General Public License, Version
00008 // 2.1, June 1999 as published by the Free Software Foundation.
00009 // Redistribution and/or modification of this program under the terms of
00010 // any other version of the GNU Lesser General Public License is not
00011 // permitted.
00012 // 
00013 // This program is distributed in the hope that it will be useful, but
00014 // WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For more details,
00016 // see the GNU Lesser General Public License, Version 2.1, a copy of
00017 // which can be found in the XORP LICENSE.lgpl file.
00018 // 
00019 // XORP, Inc, 2953 Bunker Hill Lane, Suite 204, Santa Clara, CA 95054, USA;
00020 // http://xorp.net
00021 
00022 #ifndef __LIBXORP_BUFFER_HH__
00023 #define __LIBXORP_BUFFER_HH__
00024 
00025 
00026 #include <string.h>
00027 
00028 #include "xorp.h"
00029 #include "exceptions.hh"
00030 
00031 
00039 class Buffer {
00040 public:
00047     explicit Buffer(size_t init_max_size) : _max_size(init_max_size) {
00048     _data = new uint8_t[_max_size];
00049     reset();
00050     }
00051     
00055     ~Buffer() { delete[] _data; }
00056     
00060     void reset() { _cur_size = 0; memset(_data, 0, _max_size); }
00061     
00065     size_t max_size() const { return (_max_size); }
00066     
00070     size_t data_size() const { return (_cur_size); }
00071     
00078     uint8_t data(size_t offset) const throw (InvalidBufferOffset) {
00079     if (offset >= _max_size)
00080         xorp_throw(InvalidBufferOffset, "invalid get data() offset");
00081     return (_data[offset]);
00082     }
00083     
00087     uint8_t *data() { return (_data); }
00088     
00095     int add_data(uint8_t value) {
00096     if (is_full())
00097         return (XORP_ERROR);
00098     _data[_cur_size++] = value;
00099         return (XORP_OK);
00100     }
00101     
00107     bool is_full() const { return (_cur_size >= _max_size); }
00108     
00109 private:
00110     size_t _max_size;       // The maximum amount of data to store
00111     size_t _cur_size;       // The current amount of stored data
00112     uint8_t *_data;     // Pointer to the buffer with the data
00113 };
00114 
00115 
00116 #endif // __LIBXORP_BUFFER_HH__
 All Classes Namespaces Functions Variables Typedefs Enumerations