Buffer for general purpose use. Templated for most datatypes

Dependents:   BufferedSoftSerial 09_PT1000 10_PT1000 11_PT1000 ... more

Example

 #include "mbed.h"
 #include "Buffer.h"

 Buffer <char> buf;

 int main()
 {
     buf = 'a';
     buf.put('b');
     char *head = buf.head();
     puts(head);

     char whats_in_there[2] = {0};
     int pos = 0;

     while(buf.available())
     {   
         whats_in_there[pos++] = buf;
     }
     printf("%c %c\n", whats_in_there[0], whats_in_there[1]);
     buf.clear();
     error("done\n\n\n");
 }

Buffer.h

Committer:
sam_grove
Date:
2013-05-10
Revision:
0:5e4bca1bd5f7
Child:
1:490224f41c09

File content as of revision 0:5e4bca1bd5f7:


#ifndef BUFFER_H
#define BUFFER_H

#include <stdint.h>
#include <string.h>

template <typename T>
class Buffer
{
private:
    T           *_buf;
    uint32_t     _wloc;
    uint32_t     _rloc;
    uint32_t     _size;
    
    enum BUFFER_SIZE{SMALL = 0x100, MEDIUM = 0x400, LARGE = 0x1000};

public:
    Buffer(BUFFER_SIZE size = Buffer::SMALL);
    ~Buffer();
    
    void put(T data);
    T get(void);
    T *head(void);
    void clear(void);
    Buffer &operator= (T data){put(data);return *this;}
    uint32_t available(void);
    
};

#endif