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