TMsbWord.h
Go to the documentation of this file.
1 #ifndef ibase_TMsbWord_included
2 #define ibase_TMsbWord_included
3 
4 
5 // ACF includes
6 #include <iser/IArchive.h>
7 
8 
9 namespace ibase
10 {
11 
12 
13 #pragma pack(push, 1)
14 
15 
19 template <int Size>
20 class TMsbWord
21 {
22 public:
23  TMsbWord();
24  TMsbWord(quint32 inputValue);
25  TMsbWord(const TMsbWord& inputValue);
26 
30  operator quint32() const;
31 
35  quint32 GetLsb() const;
36 
37  // pseudo-reimplemented (iser::ISerializable)
38  bool Serialize(iser::IArchive& archive);
39 
40 private:
41  quint8 m_bytes[Size];
42 };
43 
44 
45 #pragma pack(pop)
46 
47 
48 // inline methods
49 
50 template <int Size>
52 {
53  Q_ASSERT(Size > 0);
54  Q_ASSERT(Size <= 4);
55 }
56 
57 
58 template <int Size>
59 inline TMsbWord<Size>::TMsbWord(quint32 inputValue)
60 {
61  Q_ASSERT(Size > 0);
62  Q_ASSERT(Size <= 4);
63 
64  for (int i = 0; i < Size; i++){
65  m_bytes[i] = quint8((inputValue >> (i * 8)) & 0xff);
66  }
67 }
68 
69 
70 template <int Size>
71 inline TMsbWord<Size>::TMsbWord(const TMsbWord& inputValue)
72 {
73  Q_ASSERT(Size > 0);
74  Q_ASSERT(Size <= 4);
75 
76  std::memcpy(m_bytes, &inputValue.m_bytes, sizeof(m_bytes));
77 }
78 
79 
80 template <int Size>
81 inline TMsbWord<Size>::operator quint32() const
82 {
83  quint32 retVal = 0;
84 
85  for (int i = 0; i < Size; i++){
86  retVal += quint32(m_bytes[i]) << (i * 8);
87  }
88 
89  return retVal;
90 }
91 
92 
93 template <int Size>
94 inline quint32 TMsbWord<Size>::GetLsb() const
95 {
96  quint32 retVal = 0;
97 
98  for (int i = 0; i < Size; i++){
99  retVal += quint32(m_bytes[Size - i - 1]) << (i * 8);
100  }
101 
102  return retVal;
103 }
104 
105 
106 // pseudo-reimplemented (iser::ISerializable)
107 
108 template <int Size>
110 {
111  return archive.ProcessData(m_bytes, int(sizeof(m_bytes)));
112 }
113 
114 
117 
118 
119 } // namespace ibase
120 
121 
122 #endif // !ibase_TMsbWord_included
123 
124 
This class represents double word type with network byte order (big endian, MSB first).
Definition: TMsbWord.h:20
quint32 GetLsb() const
Returns the value as LSB.
Definition: TMsbWord.h:94
Represent input/output persistence archive.
Definition: IArchive.h:30
bool Serialize(iser::IArchive &archive)
Definition: TMsbWord.h:109
TMsbWord< 2 > CMsbWord
Definition: TMsbWord.h:116
virtual bool ProcessData(void *dataPtr, int size)=0
Process binary data block.
TMsbWord< 4 > CMsbDWord
Definition: TMsbWord.h:115

© 2007-2017 Witold Gantzke and Kirill Lepskiy