Branch data Line data Source code
1 : : /* 2 : : ** Copyright (C) 2023 Sylvain Fargier 3 : : ** 4 : : ** This software is provided 'as-is', without any express or implied 5 : : ** warranty. In no event will the authors be held liable for any damages 6 : : ** arising from the use of this software. 7 : : ** 8 : : ** Permission is granted to anyone to use this software for any purpose, 9 : : ** including commercial applications, and to alter it and redistribute it 10 : : ** freely, subject to the following restrictions: 11 : : ** 12 : : ** 1. The origin of this software must not be misrepresented; you must not 13 : : ** claim that you wrote the original software. If you use this software 14 : : ** in a product, an acknowledgment in the product documentation would be 15 : : ** appreciated but is not required. 16 : : ** 2. Altered source versions must be plainly marked as such, and must not be 17 : : ** misrepresented as being the original software. 18 : : ** 3. This notice may not be removed or altered from any source distribution. 19 : : ** 20 : : ** Created on: 2023-12-16T11:12:51 21 : : ** Author: Sylvain Fargier <fargier.sylvain@gmail.com> 22 : : */ 23 : : 24 : : #ifndef BUFFER_HXX__ 25 : : #define BUFFER_HXX__ 26 : : 27 : : #include "Buffer.hpp" 28 : : 29 : : namespace ccut { 30 : : 31 : : template<typename T> 32 : 126 : Buffer &Buffer::write(size_t offset, const T &value, bool be) 33 : : { 34 : 126 : T *ptr = reinterpret_cast<T *>(data() + offset); 35 : 126 : if (be) 36 : 31 : *ptr = htobe(value); 37 : : else 38 : 95 : *ptr = htole(value); 39 : 126 : return *this; 40 : : } 41 : : 42 : : template<typename T> 43 : 88 : T Buffer::read(size_t offset, bool be) const 44 : : { 45 : 88 : const T *ptr = reinterpret_cast<const T *>(data() + offset); 46 : 88 : if (be) 47 : 29 : return betoh(*ptr); 48 : : else 49 : 59 : return letoh(*ptr); 50 : : } 51 : : 52 : : // } // namespace ccut 53 : : 54 : : template<typename T> 55 : : typename std::enable_if< 56 : : std::is_same<ccut::buffer_t, typename std::decay<T>::type>::value, 57 : : const logger::Logger &>::type 58 : 4 : operator<<(const logger::Logger &logger, const T &data) 59 : : { 60 : 4 : if (!logger.isEnabled()) 61 : 0 : return logger; 62 : : 63 : 4 : logger << "{"; 64 : 28 : for (size_t i = 0; i < data.size(); ++i) 65 : : { 66 : 24 : if (i) 67 : 21 : logger << " "; 68 : 24 : logger << logger::hex(data[i]); 69 : 24 : if (i > 20) 70 : : { 71 : 0 : logger << "..."; 72 : 0 : break; 73 : : } 74 : : } 75 : 4 : return logger << "}"; 76 : : } 77 : : 78 : : template<typename T> 79 : : typename std::enable_if< 80 : : std::is_same<ccut::buffer_view_t<typename T::shared_buffer_type>, 81 : : typename std::decay<T>::type>::value, 82 : : const logger::Logger &>::type 83 : 4 : operator<<(const logger::Logger &logger, const T &data) 84 : : { 85 : 4 : if (!logger.isEnabled()) 86 : 0 : return logger; 87 : : 88 : 4 : if (!data) 89 : 2 : return logger << "{invalid}"; 90 : : 91 : 2 : logger << "{"; 92 : 44 : for (size_t i = 0; i < data.size(); ++i) 93 : : { 94 : 44 : if (i) 95 : 42 : logger << " "; 96 : 44 : logger << logger::hex(data[i]); 97 : 44 : if (i > 20) 98 : : { 99 : 2 : logger << "..."; 100 : 2 : break; 101 : : } 102 : : } 103 : 2 : return logger << "}"; 104 : : } 105 : : 106 : : } // namespace ccut 107 : : 108 : : namespace logger { 109 : : 110 : : template<typename T> 111 : : struct exclude_default<ccut::buffer_view_t<T>> : std::true_type 112 : : {}; 113 : : 114 : : template<> 115 : : struct exclude_default<ccut::buffer_t> : std::true_type 116 : : {}; 117 : : 118 : : } // namespace logger 119 : : 120 : : #endif