Warning, file /sdk/dferry/serialization/basictypeio.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001 /* 0002 Copyright (C) 2013 Andreas Hartmetz <ahartmetz@gmail.com> 0003 0004 This library is free software; you can redistribute it and/or 0005 modify it under the terms of the GNU Library General Public 0006 License as published by the Free Software Foundation; either 0007 version 2 of the License, or (at your option) any later version. 0008 0009 This library is distributed in the hope that it will be useful, 0010 but WITHOUT ANY WARRANTY; without even the implied warranty of 0011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0012 Library General Public License for more details. 0013 0014 You should have received a copy of the GNU Library General Public License 0015 along with this library; see the file COPYING.LGPL. If not, write to 0016 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 0017 Boston, MA 02110-1301, USA. 0018 0019 Alternatively, this file is available under the Mozilla Public License 0020 Version 1.1. You may obtain a copy of the License at 0021 http://www.mozilla.org/MPL/ 0022 */ 0023 0024 #ifndef BASICTYPEIO_H 0025 #define BASICTYPEIO_H 0026 0027 #include "types.h" 0028 0029 #include <algorithm> // for std::min on Windows... 0030 0031 static inline uint32 align(uint32 index, uint32 alignment) 0032 { 0033 const uint32 maxStepUp = alignment - 1; 0034 return (index + maxStepUp) & ~maxStepUp; 0035 } 0036 0037 static inline bool isPaddingZero(const chunk &buffer, uint32 padStart, uint32 padEnd) 0038 { 0039 padEnd = std::min(padEnd, buffer.length); 0040 for (; padStart < padEnd; padStart++) { 0041 if (unlikely(buffer.ptr[padStart] != '\0')) { 0042 return false; 0043 } 0044 } 0045 return true; 0046 } 0047 0048 static inline void zeroPad(byte *buffer, uint32 alignment, uint32 *bufferPos) 0049 { 0050 uint32 i = *bufferPos; 0051 const uint32 padEnd = align(i, alignment); 0052 for (; i < padEnd; i++) { 0053 buffer[i] = '\0'; 0054 } 0055 *bufferPos = padEnd; 0056 } 0057 0058 // ### this is the dumb version for now (unrolled for possible performance gain) 0059 0060 // note that there are no byte-swapping writeFoo() methods; 0061 // we just expect the receiver to deal with our byte order. 0062 0063 // we could add alignment assertions here to make extra sure that it's correct 0064 0065 namespace basic 0066 { 0067 0068 inline int16 readInt16(const byte *raw, bool swap) 0069 { 0070 byte buf[2]; 0071 if (unlikely(swap)) { 0072 buf[0] = raw[1]; 0073 buf[1] = raw[0]; 0074 raw = buf; 0075 } 0076 return *reinterpret_cast<const int16 *>(raw); 0077 } 0078 0079 inline uint16 readUint16(const byte *raw, bool swap) 0080 { 0081 byte buf[2]; 0082 if (unlikely(swap)) { 0083 buf[0] = raw[1]; 0084 buf[1] = raw[0]; 0085 raw = buf; 0086 } 0087 return *reinterpret_cast<const uint16 *>(raw); 0088 } 0089 0090 inline int32 readInt32(const byte *raw, bool swap) 0091 { 0092 byte buf[4]; 0093 if (unlikely(swap)) { 0094 buf[0] = raw[3]; 0095 buf[1] = raw[2]; 0096 buf[2] = raw[1]; 0097 buf[3] = raw[0]; 0098 raw = buf; 0099 } 0100 return *reinterpret_cast<const int32 *>(raw); 0101 } 0102 0103 inline uint32 readUint32(const byte *raw, bool swap) 0104 { 0105 byte buf[4]; 0106 if (unlikely(swap)) { 0107 buf[0] = raw[3]; 0108 buf[1] = raw[2]; 0109 buf[2] = raw[1]; 0110 buf[3] = raw[0]; 0111 raw = buf; 0112 } 0113 return *reinterpret_cast<const uint32 *>(raw); 0114 } 0115 0116 inline int64 readInt64(const byte *raw, bool swap) 0117 { 0118 byte buf[8]; 0119 if (unlikely(swap)) { 0120 buf[0] = raw[7]; 0121 buf[1] = raw[6]; 0122 buf[2] = raw[5]; 0123 buf[3] = raw[4]; 0124 buf[4] = raw[3]; 0125 buf[5] = raw[2]; 0126 buf[6] = raw[1]; 0127 buf[7] = raw[0]; 0128 raw = buf; 0129 } 0130 return *reinterpret_cast<const int64 *>(raw); 0131 } 0132 0133 inline uint64 readUint64(const byte *raw, bool swap) 0134 { 0135 byte buf[8]; 0136 if (unlikely(swap)) { 0137 buf[0] = raw[7]; 0138 buf[1] = raw[6]; 0139 buf[2] = raw[5]; 0140 buf[3] = raw[4]; 0141 buf[4] = raw[3]; 0142 buf[5] = raw[2]; 0143 buf[6] = raw[1]; 0144 buf[7] = raw[0]; 0145 raw = buf; 0146 } 0147 return *reinterpret_cast<const uint64 *>(raw); 0148 } 0149 0150 inline double readDouble(const byte *raw, bool swap) 0151 { 0152 byte buf[8]; 0153 if (unlikely(swap)) { 0154 buf[0] = raw[7]; 0155 buf[1] = raw[6]; 0156 buf[2] = raw[5]; 0157 buf[3] = raw[4]; 0158 buf[4] = raw[3]; 0159 buf[5] = raw[2]; 0160 buf[6] = raw[1]; 0161 buf[7] = raw[0]; 0162 raw = buf; 0163 } 0164 return *reinterpret_cast<const double *>(raw); 0165 } 0166 0167 inline void writeInt16(byte *raw, int16 i) 0168 { 0169 *reinterpret_cast<int16 *>(raw) = i; 0170 } 0171 0172 inline void writeUint16(byte *raw, uint16 i) 0173 { 0174 *reinterpret_cast<uint16 *>(raw) = i; 0175 } 0176 0177 inline void writeInt32(byte *raw, int32 i) 0178 { 0179 *reinterpret_cast<int32 *>(raw) = i; 0180 } 0181 0182 inline void writeUint32(byte *raw, uint32 i) 0183 { 0184 *reinterpret_cast<uint32 *>(raw) = i; 0185 } 0186 0187 inline void writeInt64(byte *raw, int64 i) 0188 { 0189 *reinterpret_cast<int64 *>(raw) = i; 0190 } 0191 0192 inline void writeUint64(byte *raw, uint64 i) 0193 { 0194 *reinterpret_cast<uint64 *>(raw) = i; 0195 } 0196 0197 inline void writeDouble(byte *raw, double d) 0198 { 0199 *reinterpret_cast<double *>(raw) = d; 0200 } 0201 0202 } // namespace basic 0203 0204 #endif // BASICTYPEIO_H