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.cpp@5:7b754354b99c, 2015-01-07 (annotated)
- Committer:
- ansond
- Date:
- Wed Jan 07 18:34:56 2015 +0000
- Revision:
- 5:7b754354b99c
- Parent:
- 4:cd0a1f4c623f
updates to the default buffer sizing
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sam_grove | 0:5e4bca1bd5f7 | 1 | |
sam_grove | 1:490224f41c09 | 2 | /** |
sam_grove | 1:490224f41c09 | 3 | * @file Buffer.cpp |
sam_grove | 1:490224f41c09 | 4 | * @brief Software Buffer - Templated Ring Buffer for most data types |
sam_grove | 1:490224f41c09 | 5 | * @author sam grove |
sam_grove | 1:490224f41c09 | 6 | * @version 1.0 |
sam_grove | 1:490224f41c09 | 7 | * @see |
sam_grove | 1:490224f41c09 | 8 | * |
sam_grove | 1:490224f41c09 | 9 | * Copyright (c) 2013 |
sam_grove | 1:490224f41c09 | 10 | * |
sam_grove | 1:490224f41c09 | 11 | * Licensed under the Apache License, Version 2.0 (the "License"); |
sam_grove | 1:490224f41c09 | 12 | * you may not use this file except in compliance with the License. |
sam_grove | 1:490224f41c09 | 13 | * You may obtain a copy of the License at |
sam_grove | 1:490224f41c09 | 14 | * |
sam_grove | 1:490224f41c09 | 15 | * http://www.apache.org/licenses/LICENSE-2.0 |
sam_grove | 1:490224f41c09 | 16 | * |
sam_grove | 1:490224f41c09 | 17 | * Unless required by applicable law or agreed to in writing, software |
sam_grove | 1:490224f41c09 | 18 | * distributed under the License is distributed on an "AS IS" BASIS, |
sam_grove | 1:490224f41c09 | 19 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
sam_grove | 1:490224f41c09 | 20 | * See the License for the specific language governing permissions and |
sam_grove | 1:490224f41c09 | 21 | * limitations under the License. |
sam_grove | 1:490224f41c09 | 22 | */ |
sam_grove | 1:490224f41c09 | 23 | |
sam_grove | 0:5e4bca1bd5f7 | 24 | #include "Buffer.h" |
sam_grove | 0:5e4bca1bd5f7 | 25 | |
sam_grove | 0:5e4bca1bd5f7 | 26 | template <class T> |
sam_grove | 3:c2de0ddfe65b | 27 | Buffer<T>::Buffer(uint32_t size) |
sam_grove | 0:5e4bca1bd5f7 | 28 | { |
sam_grove | 0:5e4bca1bd5f7 | 29 | _buf = new T [size]; |
sam_grove | 0:5e4bca1bd5f7 | 30 | _size = size; |
sam_grove | 0:5e4bca1bd5f7 | 31 | clear(); |
sam_grove | 0:5e4bca1bd5f7 | 32 | |
sam_grove | 0:5e4bca1bd5f7 | 33 | return; |
sam_grove | 0:5e4bca1bd5f7 | 34 | } |
sam_grove | 0:5e4bca1bd5f7 | 35 | |
sam_grove | 0:5e4bca1bd5f7 | 36 | template <class T> |
sam_grove | 0:5e4bca1bd5f7 | 37 | Buffer<T>::~Buffer() |
sam_grove | 0:5e4bca1bd5f7 | 38 | { |
sam_grove | 0:5e4bca1bd5f7 | 39 | delete [] _buf; |
sam_grove | 0:5e4bca1bd5f7 | 40 | |
sam_grove | 0:5e4bca1bd5f7 | 41 | return; |
sam_grove | 0:5e4bca1bd5f7 | 42 | } |
sam_grove | 0:5e4bca1bd5f7 | 43 | |
sam_grove | 0:5e4bca1bd5f7 | 44 | template <class T> |
ansond | 5:7b754354b99c | 45 | uint32_t Buffer<T>::getSize() |
ansond | 5:7b754354b99c | 46 | { |
ansond | 5:7b754354b99c | 47 | return this->_size; |
ansond | 5:7b754354b99c | 48 | } |
ansond | 5:7b754354b99c | 49 | |
ansond | 5:7b754354b99c | 50 | template <class T> |
sam_grove | 0:5e4bca1bd5f7 | 51 | void Buffer<T>::clear(void) |
sam_grove | 0:5e4bca1bd5f7 | 52 | { |
sam_grove | 0:5e4bca1bd5f7 | 53 | _wloc = 0; |
sam_grove | 0:5e4bca1bd5f7 | 54 | _rloc = 0; |
sam_grove | 1:490224f41c09 | 55 | memset(_buf, 0, _size); |
sam_grove | 0:5e4bca1bd5f7 | 56 | |
sam_grove | 0:5e4bca1bd5f7 | 57 | return; |
sam_grove | 0:5e4bca1bd5f7 | 58 | } |
sam_grove | 0:5e4bca1bd5f7 | 59 | |
sam_grove | 0:5e4bca1bd5f7 | 60 | template <class T> |
sam_grove | 2:d13a72146516 | 61 | uint32_t Buffer<T>::peek(char c) |
sam_grove | 2:d13a72146516 | 62 | { |
sam_grove | 2:d13a72146516 | 63 | return 1; |
sam_grove | 2:d13a72146516 | 64 | } |
sam_grove | 2:d13a72146516 | 65 | |
sam_grove | 1:490224f41c09 | 66 | // make the linker aware of some possible types |
sam_grove | 0:5e4bca1bd5f7 | 67 | template class Buffer<uint8_t>; |
sam_grove | 0:5e4bca1bd5f7 | 68 | template class Buffer<int8_t>; |
sam_grove | 0:5e4bca1bd5f7 | 69 | template class Buffer<uint16_t>; |
sam_grove | 0:5e4bca1bd5f7 | 70 | template class Buffer<int16_t>; |
sam_grove | 0:5e4bca1bd5f7 | 71 | template class Buffer<uint32_t>; |
sam_grove | 0:5e4bca1bd5f7 | 72 | template class Buffer<int32_t>; |
sam_grove | 0:5e4bca1bd5f7 | 73 | template class Buffer<uint64_t>; |
sam_grove | 0:5e4bca1bd5f7 | 74 | template class Buffer<int64_t>; |
sam_grove | 0:5e4bca1bd5f7 | 75 | template class Buffer<char>; |
sam_grove | 0:5e4bca1bd5f7 | 76 | template class Buffer<wchar_t>; |