Line data Source code
1 : /** 2 : Copyright (c) 2016-2022 Roman Katuntsev <sbkarr@stappler.org> 3 : Copyright (c) 2023 Stappler LLC <admin@stappler.dev> 4 : 5 : Permission is hereby granted, free of charge, to any person obtaining a copy 6 : of this software and associated documentation files (the "Software"), to deal 7 : in the Software without restriction, including without limitation the rights 8 : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 : copies of the Software, and to permit persons to whom the Software is 10 : furnished to do so, subject to the following conditions: 11 : 12 : The above copyright notice and this permission notice shall be included in 13 : all copies or substantial portions of the Software. 14 : 15 : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 : THE SOFTWARE. 22 : **/ 23 : 24 : #ifndef STAPPLER_CORE_IO_SPIOBUFFER_H_ 25 : #define STAPPLER_CORE_IO_SPIOBUFFER_H_ 26 : 27 : #include "SPIOCommon.h" 28 : 29 : namespace STAPPLER_VERSIONIZED stappler::io { 30 : 31 : struct Buffer { 32 : template <typename T, typename Traits = BufferTraits<T>> Buffer(T &t); 33 : 34 : uint8_t * prepare(size_t & size) const; 35 : void save(uint8_t *, size_t source, size_t nbytes) const; 36 : 37 : size_t capacity() const; 38 : size_t size() const; 39 : uint8_t *data() const; 40 : void clear() const; 41 : 42 : void *ptr = nullptr; 43 : prepare_fn prepare_ptr = nullptr; 44 : save_fn save_ptr = nullptr; 45 : size_fn size_ptr = nullptr; 46 : size_fn capacity_ptr = nullptr; 47 : data_fn data_ptr = nullptr; 48 : clear_fn clear_ptr = nullptr; 49 : }; 50 : 51 : template <typename T, typename Traits> 52 780244 : inline Buffer::Buffer(T &t) 53 780244 : : ptr((void *)&t) 54 780244 : , prepare_ptr(&Traits::PrepareFn) 55 780244 : , save_ptr(&Traits::SaveFn) 56 780244 : , size_ptr(&Traits::SizeFn) 57 780244 : , capacity_ptr(&Traits::CapacityFn) 58 780244 : , data_ptr(&Traits::DataFn) 59 780244 : , clear_ptr(&Traits::ClearFn) { } 60 : 61 : // reserve memory block in buffer 62 642569 : inline uint8_t * Buffer::prepare(size_t & size) const { return prepare_ptr(ptr, size); } 63 642569 : inline void Buffer::save(uint8_t *buf, size_t source, size_t nbytes) const { save_ptr(ptr, buf, source, nbytes); } 64 125 : inline size_t Buffer::capacity() const { return capacity_ptr(ptr); } 65 424775 : inline size_t Buffer::size() const { return size_ptr(ptr); } 66 424775 : inline uint8_t *Buffer::data() const { return data_ptr(ptr); } 67 25 : inline void Buffer::clear() const { clear_ptr(ptr); } 68 : 69 : } 70 : 71 : #endif /* STAPPLER_CORE_IO_SPIOBUFFER_H_ */