• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdesdk API Reference
  • KDE Home
  • Contact Us
 

okteta

  • sources
  • kde-4.12
  • kdesdk
  • okteta
  • kasten
  • controllers
  • view
  • structures
allprimitivetypes.h
Go to the documentation of this file.
1 /*
2  * This file is part of the Okteta Kasten Framework, made within the KDE community.
3  *
4  * Copyright 2009, 2010, 2011 Alex Richardson <alex.richardson@gmx.de>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) version 3, or any
10  * later version accepted by the membership of KDE e.V. (or its
11  * successor approved by the membership of KDE e.V.), which shall
12  * act as a proxy defined in Section 6 of version 3 of the license.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
21  */
22 #ifndef ALLPRIMITIVETYPES_H_
23 #define ALLPRIMITIVETYPES_H_
24 
25 #include <QSysInfo>
26 #include <QtEndian>
27 
28 #include <size.h>
29 #include <address.h>
30 #include <abstractbytearraymodel.h>
31 
32 #include "datatypes/datainformationbase.h"
33 
34 namespace Okteta
35 {
36 class AbstractByteArrayModel;
37 }
38 
39 #ifdef Q_CC_GNU
40 #define PACKED_STRUCT __attribute__((packed, aligned(8)))
41 #else
42 #define PACKED_STRUCT
43 #endif
44 
49 template<typename T, int padCount>
50 struct EndianIndependentBase {
51 #if Q_BYTE_ORDER == Q_BIG_ENDIAN
52  char padding[padCount];
53 #endif
54  T value;
55 #if Q_BYTE_ORDER == Q_LITTLE_ENDIAN
56  char padding[padCount];
57 #endif
58 } PACKED_STRUCT;
59 template<typename T>
60 struct EndianIndependentBase<T, 0> {
61  T value;
62 };
63 
64 template<typename T>
65 struct EndianIndependent : public EndianIndependentBase<T, 8 - sizeof(T)> {
66 };
67 
70 union AllPrimitiveTypes
71 {
72 private:
73  EndianIndependent<qint8> _byte;
74  EndianIndependent<quint8> _ubyte;
75  EndianIndependent<qint16> _short;
76  EndianIndependent<quint16> _ushort;
77  EndianIndependent<qint32> _int;
78  EndianIndependent<quint32> _uint;
79  EndianIndependent<qint64> _long;
80  EndianIndependent<quint64> _ulong;
81  EndianIndependent<float> _float;
82  EndianIndependent<double> _double;
83 public:
84  qint8 allBytes[8];
85  inline AllPrimitiveTypes() { _ulong.value = 0; }
86  inline AllPrimitiveTypes(const AllPrimitiveTypes& a) { _ulong.value = a._ulong.value; }
87  inline AllPrimitiveTypes(quint64 val) { _ulong.value = val;}
88  inline AllPrimitiveTypes(qint64 val) { _long.value = val;}
89  //set all to zero first with smaller data types
90  inline AllPrimitiveTypes(qint32 val) { _long.value = (val < 0 ? -1 : 0); _int.value = val; }
91  inline AllPrimitiveTypes(quint32 val) { _ulong.value = 0; _uint.value = val; }
92  inline AllPrimitiveTypes(qint16 val) { _long.value = (val < 0 ? -1 : 0); _short.value = val; }
93  inline AllPrimitiveTypes(quint16 val) { _ulong.value = 0; _ushort.value = val; }
94  inline AllPrimitiveTypes(qint8 val) { _long.value = (val < 0 ? -1 : 0); _byte.value = val; }
95  inline AllPrimitiveTypes(quint8 val) { _ulong.value = 0; _ubyte.value = val; }
96  inline AllPrimitiveTypes(float val) { _ulong.value = 0; _float.value = val; }
97  inline AllPrimitiveTypes(double val) { _double.value = val; }
98 
99  inline bool operator!=(AllPrimitiveTypes other) const
100  {
101  return _ulong.value != other._ulong.value;
102  }
103  inline bool operator==(AllPrimitiveTypes other) const
104  {
105  return _ulong.value == other._ulong.value;
106  }
108  inline bool operator<(AllPrimitiveTypes other) const
109  {
110  return _ulong.value < other._ulong.value;
111  }
129  bool writeBits(quint8 bitCount, AllPrimitiveTypes newValue,
130  Okteta::AbstractByteArrayModel* out, QSysInfo::Endian byteOrder,
131  Okteta::Address address, BitCount64 bitsRemaining, quint8* const bitOffset);
148  bool readBits(quint8 bitCount, const Okteta::AbstractByteArrayModel* input,
149  QSysInfo::Endian byteOrder, Okteta::Address address, BitCount64 bitsRemaining,
150  quint8* const bitOffset);
151 
152  template<typename T> T value() const;
161  //TODO bool* ok parameter for when reading from model can cause errors (or exceptions sometime?)
162  template<typename T> static T readValue(const Okteta::AbstractByteArrayModel* input, Okteta::Address address,
163  QSysInfo::Endian endianess, quint8 bitOffset);
164  //TODO add writeValue
165 
166 private:
167  template<int size> static typename QIntegerForSize<size>::Unsigned readValuePrivate(
168  const Okteta::AbstractByteArrayModel* input, Okteta::Address address,
169  QSysInfo::Endian endianess, quint8 bitOffset);
170  template<int size> static typename QIntegerForSize<size>::Unsigned readRawBytes(
171  const Okteta::AbstractByteArrayModel* input, Okteta::Address address);
172 
173  void readDataLittleEndian(quint8 bitCount, const Okteta::AbstractByteArrayModel* input,
174  Okteta::Address address, quint8 bo);
175  void writeDataLittleEndian(quint8 bitCount, AllPrimitiveTypes newValue,
176  Okteta::AbstractByteArrayModel *out, Okteta::Address address, quint8 bo) const;
177 
178  void readDataBigEndian(quint8 bitCount, const Okteta::AbstractByteArrayModel* input,
179  Okteta::Address address, quint8 bo);
180  void writeDataBigEndian(quint8 bitCount, AllPrimitiveTypes newValue,
181  Okteta::AbstractByteArrayModel *out, Okteta::Address address, quint8 bo) const;
182 
183  //optimised methods for reading/writing full bytes
184  void readFullBytes(quint8 byteCount, const Okteta::AbstractByteArrayModel* input,
185  QSysInfo::Endian byteOrder, Okteta::Address address);
186  void writeFullBytes(quint8 byteCount, AllPrimitiveTypes newValue,
187  Okteta::AbstractByteArrayModel* out, QSysInfo::Endian byteOrder,
188  Okteta::Address address);
189 };
190 
191 template<> inline quint8 AllPrimitiveTypes::value<quint8>() const { return _ubyte.value; }
192 template<> inline quint16 AllPrimitiveTypes::value<quint16>() const { return _ushort.value; }
193 template<> inline quint32 AllPrimitiveTypes::value<quint32>() const { return _uint.value; }
194 template<> inline quint64 AllPrimitiveTypes::value<quint64>() const { return _ulong.value; }
195 template<> inline qint8 AllPrimitiveTypes::value<qint8>() const { return _byte.value; }
196 template<> inline qint16 AllPrimitiveTypes::value<qint16>() const { return _short.value; }
197 template<> inline qint32 AllPrimitiveTypes::value<qint32>() const { return _int.value; }
198 template<> inline qint64 AllPrimitiveTypes::value<qint64>() const { return _long.value; }
199 template<> inline float AllPrimitiveTypes::value<float>() const { return _float.value; }
200 template<> inline double AllPrimitiveTypes::value<double>() const { return _double.value; }
201 
202 
203 template<typename T>
204 inline T AllPrimitiveTypes::readValue(const Okteta::AbstractByteArrayModel* input,
205  Okteta::Address address, QSysInfo::Endian endianess, quint8 bitOffset)
206 {
207  //check for out of bounds
208  Q_ASSERT(BitCount64(input->size() - address) * 8 - bitOffset >= sizeof(T) * 8);
209  Q_ASSERT(bitOffset < 8);
210  //this union exists to force unsigned shifts
211  union {
212  T value;
213  typename QIntegerForSizeof<T>::Unsigned unsignedValue;
214  } u;
215  u.unsignedValue = readValuePrivate<sizeof(T)>(input, address, endianess, bitOffset);
216  return u.value;
217 }
218 
219 template<int size>
220 inline typename QIntegerForSize<size>::Unsigned AllPrimitiveTypes::readValuePrivate(
221  const Okteta::AbstractByteArrayModel* input, Okteta::Address address,
222  QSysInfo::Endian endianess, quint8 bitOffset)
223 {
224  typename QIntegerForSize<size>::Unsigned unsignedValue = readRawBytes<size>(input, address);
225  if (endianess != QSysInfo::ByteOrder)
226  {
227  //swap the byte order if machine endianess does not match requested endianess
228  unsignedValue = qbswap(unsignedValue);
229  }
230  if (Q_UNLIKELY(bitOffset != 0))
231  {
232  quint8 lastByte = input->byte(address + size);
233  //handle the remaining bits
234  if (endianess == QSysInfo::BigEndian)
235  {
236  //the coming bits are the least significant, and range from bit (8-bitOffset)..7
237  unsignedValue <<= bitOffset;
238  lastByte >>= 8 - bitOffset; //unsigned shift
239  Q_ASSERT((unsignedValue & lastByte) == 0); //must not overlap
240  unsignedValue |= lastByte;
241  }
242  else
243  {
244  //the coming bits are the most significant bits and range from 0..bitOffset
245  unsignedValue >>= bitOffset;
246  //promote lastByte to unsigned T and mask off the interesting bits
247  typename QIntegerForSize<size>::Unsigned tmp = lastByte & ((1u << bitOffset) - 1);
248  tmp <<= (size * 8) - bitOffset;
249  unsignedValue |= tmp;
250  }
251  }
252  return unsignedValue;
253 }
254 
255 template<int size>
256 inline typename QIntegerForSize<size>::Unsigned AllPrimitiveTypes::readRawBytes(
257  const Okteta::AbstractByteArrayModel* input, Okteta::Address address)
258 {
259  union {
260  typename QIntegerForSize<size>::Unsigned value;
261  Okteta::Byte bytes[size];
262  } buf;
263  Okteta::Size read = input->copyTo(buf.bytes, address, size);
264  Q_ASSERT(read == size);
265  Q_UNUSED(read)
266  return buf.value;
267 }
268 
269 //specialize it for the case where we only need to read one byte
270 template<>
271 inline quint8 AllPrimitiveTypes::readRawBytes<1>(
272  const Okteta::AbstractByteArrayModel* input, Okteta::Address address)
273 {
274  return input->byte(address);
275 }
276 
277 #endif /* ALLPRIMITIVETYPES_H_ */
Okteta::Address
qint32 Address
Definition: address.h:34
Okteta::AbstractByteArrayModel
could it be useful to hide the data access behind an iterator? * class KDataBufferIterator { public: ...
Definition: abstractbytearraymodel.h:79
abstractbytearraymodel.h
PACKED_STRUCT
#define PACKED_STRUCT
Definition: allprimitivetypes.h:42
AllPrimitiveTypes::readBits
bool readBits(quint8 bitCount, const Okteta::AbstractByteArrayModel *input, QSysInfo::Endian byteOrder, Okteta::Address address, BitCount64 bitsRemaining, quint8 *const bitOffset)
Reads given number of bits from input and sets value of this union to the new value.
Definition: allprimitivetypes.cpp:79
AllPrimitiveTypes::AllPrimitiveTypes
AllPrimitiveTypes(quint8 val)
Definition: allprimitivetypes.h:95
BitCount64
quint64 BitCount64
Definition: datainformationbase.h:42
EndianIndependent
Definition: allprimitivetypes.h:65
EndianIndependentBase::padding
char padding[padCount]
Definition: allprimitivetypes.h:52
EndianIndependentBase::value
T value
Definition: allprimitivetypes.h:54
AllPrimitiveTypes::operator<
bool operator<(AllPrimitiveTypes other) const
Not useful, but needed so we can store this in a QMap.
Definition: allprimitivetypes.h:108
Okteta::Byte
unsigned char Byte
Definition: byte.h:29
AllPrimitiveTypes::AllPrimitiveTypes
AllPrimitiveTypes(qint32 val)
Definition: allprimitivetypes.h:90
AllPrimitiveTypes::AllPrimitiveTypes
AllPrimitiveTypes(double val)
Definition: allprimitivetypes.h:97
AllPrimitiveTypes::AllPrimitiveTypes
AllPrimitiveTypes(quint64 val)
Definition: allprimitivetypes.h:87
EndianIndependentBase< T, 0 >::value
T value
Definition: allprimitivetypes.h:61
BigEndian
Definition: endianness.h:33
Okteta::AbstractByteArrayModel::copyTo
virtual Size copyTo(Byte *dest, const AddressRange &copyRange) const
copies the data of the section into a given array Dest.
Definition: abstractbytearraymodel.cpp:60
AllPrimitiveTypes::allBytes
qint8 allBytes[8]
Definition: allprimitivetypes.h:84
Okteta::AbstractByteArrayModel::size
virtual Size size() const =0
Okteta::ByteOrder
ByteOrder
Definition: oktetacore.h:111
AllPrimitiveTypes::AllPrimitiveTypes
AllPrimitiveTypes()
Definition: allprimitivetypes.h:85
address.h
AllPrimitiveTypes::AllPrimitiveTypes
AllPrimitiveTypes(qint64 val)
Definition: allprimitivetypes.h:88
AllPrimitiveTypes::readValue
static T readValue(const Okteta::AbstractByteArrayModel *input, Okteta::Address address, QSysInfo::Endian endianess, quint8 bitOffset)
Read data of type T from the model.
Definition: allprimitivetypes.h:204
AllPrimitiveTypes::AllPrimitiveTypes
AllPrimitiveTypes(qint16 val)
Definition: allprimitivetypes.h:92
datainformationbase.h
AllPrimitiveTypes::AllPrimitiveTypes
AllPrimitiveTypes(quint32 val)
Definition: allprimitivetypes.h:91
size.h
AllPrimitiveTypes::value
T value() const
AllPrimitiveTypes::operator!=
bool operator!=(AllPrimitiveTypes other) const
Definition: allprimitivetypes.h:99
AllPrimitiveTypes::AllPrimitiveTypes
AllPrimitiveTypes(quint16 val)
Definition: allprimitivetypes.h:93
AllPrimitiveTypes::AllPrimitiveTypes
AllPrimitiveTypes(qint8 val)
Definition: allprimitivetypes.h:94
AllPrimitiveTypes::AllPrimitiveTypes
AllPrimitiveTypes(const AllPrimitiveTypes &a)
Definition: allprimitivetypes.h:86
Okteta::Size
qint32 Size
Definition: size.h:33
AllPrimitiveTypes
This union holds the value of one primitive datatype.
Definition: allprimitivetypes.h:70
AllPrimitiveTypes::operator==
bool operator==(AllPrimitiveTypes other) const
Definition: allprimitivetypes.h:103
EndianIndependentBase
Ensures that when used in a union the uint8 value will be equal to the lowest bits of the uint32 valu...
Definition: allprimitivetypes.h:50
AllPrimitiveTypes::writeBits
bool writeBits(quint8 bitCount, AllPrimitiveTypes newValue, Okteta::AbstractByteArrayModel *out, QSysInfo::Endian byteOrder, Okteta::Address address, BitCount64 bitsRemaining, quint8 *const bitOffset)
Writes given number of bits to out.
Definition: allprimitivetypes.cpp:38
AllPrimitiveTypes::AllPrimitiveTypes
AllPrimitiveTypes(float val)
Definition: allprimitivetypes.h:96
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:04:06 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

okteta

Skip menu "okteta"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdesdk API Reference

Skip menu "kdesdk API Reference"
  • kapptemplate
  • kcachegrind
  • kompare
  • lokalize
  • okteta
  • umbrello
  •   umbrello

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal