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@4:cd0a1f4c623f, 2013-06-26 (annotated)
- Committer:
- sam_grove
- Date:
- Wed Jun 26 15:24:27 2013 +0000
- Revision:
- 4:cd0a1f4c623f
- Parent:
- 2:d13a72146516
- Parent:
- 3:c2de0ddfe65b
- Child:
- 5:7b754354b99c
merged
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> |
sam_grove | 0:5e4bca1bd5f7 | 45 | void Buffer<T>::clear(void) |
sam_grove | 0:5e4bca1bd5f7 | 46 | { |
sam_grove | 0:5e4bca1bd5f7 | 47 | _wloc = 0; |
sam_grove | 0:5e4bca1bd5f7 | 48 | _rloc = 0; |
sam_grove | 1:490224f41c09 | 49 | memset(_buf, 0, _size); |
sam_grove | 0:5e4bca1bd5f7 | 50 | |
sam_grove | 0:5e4bca1bd5f7 | 51 | return; |
sam_grove | 0:5e4bca1bd5f7 | 52 | } |
sam_grove | 0:5e4bca1bd5f7 | 53 | |
sam_grove | 0:5e4bca1bd5f7 | 54 | template <class T> |
sam_grove | 2:d13a72146516 | 55 | uint32_t Buffer<T>::peek(char c) |
sam_grove | 2:d13a72146516 | 56 | { |
sam_grove | 2:d13a72146516 | 57 | return 1; |
sam_grove | 2:d13a72146516 | 58 | } |
sam_grove | 2:d13a72146516 | 59 | |
sam_grove | 1:490224f41c09 | 60 | // make the linker aware of some possible types |
sam_grove | 0:5e4bca1bd5f7 | 61 | template class Buffer<uint8_t>; |
sam_grove | 0:5e4bca1bd5f7 | 62 | template class Buffer<int8_t>; |
sam_grove | 0:5e4bca1bd5f7 | 63 | template class Buffer<uint16_t>; |
sam_grove | 0:5e4bca1bd5f7 | 64 | template class Buffer<int16_t>; |
sam_grove | 0:5e4bca1bd5f7 | 65 | template class Buffer<uint32_t>; |
sam_grove | 0:5e4bca1bd5f7 | 66 | template class Buffer<int32_t>; |
sam_grove | 0:5e4bca1bd5f7 | 67 | template class Buffer<uint64_t>; |
sam_grove | 0:5e4bca1bd5f7 | 68 | template class Buffer<int64_t>; |
sam_grove | 0:5e4bca1bd5f7 | 69 | template class Buffer<char>; |
sam_grove | 0:5e4bca1bd5f7 | 70 | template class Buffer<wchar_t>; |
sam_grove | 0:5e4bca1bd5f7 | 71 |