Synergia AVR 0.1

lib/usart/buffer.cpp

Go to the documentation of this file.
00001 /*
00002  Copyright (c) 2010 Tymon Tobolski
00003  
00004  Permission is hereby granted, free of charge, to any person obtaining
00005  a copy of this software and associated documentation files (the
00006  "Software"), to deal in the Software without restriction, including
00007  without limitation the rights to use, copy, modify, merge, publish,
00008  distribute, sublicense, and/or sell copies of the Software, and to
00009  permit persons to whom the Software is furnished to do so, subject to
00010  the following conditions:
00011  
00012  The above copyright notice and this permission notice shall be
00013  included in all copies or substantial portions of the Software.
00014  
00015  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00016  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00017  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00018  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00019  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00020  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00021  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00022  */
00023 
00024 #include "buffer.h"
00025 #include <stdlib.h>
00026 
00027 Buffer::Buffer(){
00028         _head = _tail = NULL;
00029         _size = 0;
00030 }
00031 
00032 void Buffer::clear(){
00033         while(_head != NULL) pop();
00034 }
00035 
00036 bool Buffer::empty(){
00037         return (_head == NULL);
00038 }
00039 
00040 void Buffer::push(char c){
00041         BufferItem *curr = (BufferItem*) malloc(sizeof(BufferItem));
00042         if(_head == NULL) _head = curr;
00043         else _tail->next = curr;
00044         curr->character = c;
00045         curr->next = NULL;
00046         _tail = curr;
00047         _size++;
00048 }
00049 
00050 void Buffer::pop(){
00051         if(_head){
00052                 BufferItem *tmp = _head->next;
00053                 free(_head);
00054                 _head = tmp;
00055                 _size--;
00056         }
00057 }
00058 
00059 char * Buffer::front(){
00060         return (_head ? &(_head->character) : NULL);
00061 }
00062 
00063 int Buffer::size(){
00064         return _size;
00065 }
00066 
00067 unsigned char Buffer::read(){
00068         unsigned char n = *front();
00069         pop();
00070         return n;
00071 }
 All Classes Files Functions Variables Defines