AgentXcpp  Version:0.3
Internals Documentation
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Friends Pages
util.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2011-2016 Tanjeff-Nicolai Moos <tanjeff@cccmz.de>
3  *
4  * This file is part of the agentXcpp library.
5  *
6  * AgentXcpp is free software: you can redistribute it and/or modify
7  * it under the terms of the AgentXcpp library license, version 1, which
8  * consists of the GNU General Public License and some additional
9  * permissions.
10  *
11  * AgentXcpp is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * See the AgentXcpp library license in the LICENSE file of this package
17  * for more details.
18  */
19 
20 #ifndef _HELPER_H_
21 #define _HELPER_H_
22 
23 #include <QtGlobal>
24 
25 #include "binary.hpp"
26 
27 
28 namespace agentxcpp
29 {
30 
31  inline quint64 read64(binary::const_iterator& pos, bool big_endian)
32  {
33  quint64 value;
34  if( big_endian )
35  {
36  value = static_cast<quint64>(*pos++) << 56;
37  value |= static_cast<quint64>(*pos++) << 48;
38  value |= static_cast<quint64>(*pos++) << 40;
39  value |= static_cast<quint64>(*pos++) << 32;
40  value |= static_cast<quint64>(*pos++) << 24;
41  value |= static_cast<quint64>(*pos++) << 16;
42  value |= static_cast<quint64>(*pos++) << 8;
43  value |= static_cast<quint64>(*pos++) << 0;
44  }
45  else
46  {
47  value = static_cast<quint64>(*pos++) << 0;
48  value |= static_cast<quint64>(*pos++) << 8;
49  value |= static_cast<quint64>(*pos++) << 16;
50  value |= static_cast<quint64>(*pos++) << 24;
51  value |= static_cast<quint64>(*pos++) << 32;
52  value |= static_cast<quint64>(*pos++) << 40;
53  value |= static_cast<quint64>(*pos++) << 48;
54  value |= static_cast<quint64>(*pos++) << 56;
55  }
56  return value;
57  }
58 
59 
60  /**
61  * \brief Write a 64-bit value into a string
62  *
63  * \param serialized The string to which the value is appended.
64  *
65  * \param value The value which is appended to the string.
66  */
67  inline void write64(binary& serialized, quint64 value)
68  {
69  // always big endian
70  serialized.push_back(value >> 56 & 0xff);
71  serialized.push_back(value >> 48 & 0xff);
72  serialized.push_back(value >> 40 & 0xff);
73  serialized.push_back(value >> 32 & 0xff);
74  serialized.push_back(value >> 24 & 0xff);
75  serialized.push_back(value >> 16 & 0xff);
76  serialized.push_back(value >> 8 & 0xff);
77  serialized.push_back(value >> 0 & 0xff);
78  }
79 
80 
81  inline quint32 read32(binary::const_iterator& pos, bool big_endian)
82  {
83  quint32 value;
84  if( big_endian )
85  {
86  value = *pos++ << 24;
87  value |= *pos++ << 16;
88  value |= *pos++ << 8;
89  value |= *pos++ << 0;
90  }
91  else
92  {
93  value = *pos++ << 0;
94  value |= *pos++ << 8;
95  value |= *pos++ << 16;
96  value |= *pos++ << 24;
97  }
98 
99  return value;
100  }
101 
102  /**
103  * \brief Write a 32-bit value into a string
104  *
105  * \param serialized The string to which the value is appended.
106  *
107  * \param value The value which is appended to the string.
108  */
109  inline void write32(binary& serialized, quint32 value)
110  {
111  // always big endian
112  serialized.push_back(value >> 24 & 0xff);
113  serialized.push_back(value >> 16 & 0xff);
114  serialized.push_back(value >> 8 & 0xff);
115  serialized.push_back(value >> 0 & 0xff);
116  }
117 
118  inline quint16 read16(binary::const_iterator& pos, bool big_endian)
119  {
120  quint16 value = 0;
121  if( big_endian )
122  {
123  value |= *pos++ << 8;
124  value |= *pos++ << 0;
125  }
126  else
127  {
128  value = *pos++ << 0;
129  value |= *pos++ << 8;
130  }
131 
132  return value;
133  }
134 
135 
136  /**
137  * \brief Write a 16-bit value into a string
138  *
139  * \param serialized The string to which the value is appended.
140  *
141  * \param value The value which is appended to the string.
142  */
143  inline void write16(binary& serialized, quint16 value)
144  {
145  // always big endian
146  serialized.push_back(value >> 8 & 0xff);
147  serialized.push_back(value >> 0 & 0xff);
148  }
149 }
150 
151 #endif
void write16(binary &serialized, quint16 value)
Write a 16-bit value into a string.
Definition: util.hpp:143
quint32 read32(binary::const_iterator &pos, bool big_endian)
Definition: util.hpp:81
quint64 read64(binary::const_iterator &pos, bool big_endian)
Definition: util.hpp:31
A type representing a contigous byte stream.
Definition: binary.hpp:35
void write64(binary &serialized, quint64 value)
Write a 64-bit value into a string.
Definition: util.hpp:67
void write32(binary &serialized, quint32 value)
Write a 32-bit value into a string.
Definition: util.hpp:109
quint16 read16(binary::const_iterator &pos, bool big_endian)
Definition: util.hpp:118