CuteHMI - Modbus (CuteHMI.Modbus.2)
functions.hpp
1 #ifndef H_EXTENSIONS_CUTEHMI_MODBUS_2_INCLUDE_CUTEHMI_MODBUS_INTERNAL_FUNCTIONS_HPP
2 #define H_EXTENSIONS_CUTEHMI_MODBUS_2_INCLUDE_CUTEHMI_MODBUS_INTERNAL_FUNCTIONS_HPP
3 
4 #include "common.hpp"
5 
6 #include <QtEndian>
7 
8 namespace cutehmi {
9 namespace modbus {
10 namespace internal {
11 
12 template <typename T>
13 T toBigEndian(T src)
14 {
15  // Will use Qt endian functions, but need to assert that uchar is 8 bit wide. Qt implementation seems to be unaware that
16  // sizeof(char) == 1 byte == not necessarily 8 bits.
17  static_assert(std::numeric_limits<uchar>::digits == 8, "uchar must be 8 bit wide");
18 
19  return qToBigEndian(src);
20 }
21 
22 template <typename T>
23 T fromBigEndian(T src)
24 {
25  // Will use Qt endian functions, but need to assert that uchar is 8 bit wide. Qt implementation seems to be unaware that
26  // sizeof(char) == 1 byte == not necessarily 8 bits.
27  static_assert(std::numeric_limits<uchar>::digits == 8, "uchar must be 8 bit wide");
28 
29  return qFromBigEndian(src);
30 }
31 
32 template <typename T>
34 {
35  // Will use Qt endian functions, but need to assert that uchar is 8 bit wide. Qt implementation seems to be unaware that
36  // sizeof(char) == 1 byte == not necessarily 8 bits.
37  static_assert(std::numeric_limits<uchar>::digits == 8, "uchar must be 8 bit wide");
38 
39  return qToLittleEndian(src);
40 }
41 
42 template <typename T>
44 {
45  // Will use Qt endian functions, but need to assert that uchar is 8 bit wide. Qt implementation seems to be unaware that
46  // sizeof(char) == 1 byte == not necessarily 8 bits.
47  static_assert(std::numeric_limits<uchar>::digits == 8, "uchar must be 8 bit wide");
48 
49  return qFromLittleEndian(src);
50 }
51 
61 quint16 CUTEHMI_MODBUS_PRIVATE intToUint16(int value);
62 
70 quint16 CUTEHMI_MODBUS_PRIVATE int16ToUint16(qint16 value);
71 
79 qint16 CUTEHMI_MODBUS_PRIVATE int16FromUint16(quint16 value);
80 
81 }
82 }
83 }
84 
85 #endif
86 
87 //(c)C: Copyright © 2019-2020, Michał Policht <michal@policht.pl>, Yuri Chornoivan <yurchor@ukr.net>. All rights reserved.
88 //(c)C: This file is a part of CuteHMI.
89 //(c)C: CuteHMI is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
90 //(c)C: CuteHMI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
91 //(c)C: You should have received a copy of the GNU Lesser General Public License along with CuteHMI. If not, see <https://www.gnu.org/licenses/>.
cutehmi::modbus::internal::toBigEndian
T toBigEndian(T src)
Definition: functions.hpp:13
cutehmi::modbus::internal::toLittleEndian
T toLittleEndian(T src)
Definition: functions.hpp:33
cutehmi::modbus::internal::int16ToUint16
quint16 CUTEHMI_MODBUS_PRIVATE int16ToUint16(qint16 value)
Store 16 bit integer as 16 bit unsigned integer.
Definition: functions.cpp:22
cutehmi::modbus::internal::fromLittleEndian
T fromLittleEndian(T src)
Definition: functions.hpp:43
cutehmi
cutehmi::modbus::internal::fromBigEndian
T fromBigEndian(T src)
Definition: functions.hpp:23
cutehmi::modbus::internal::intToUint16
quint16 CUTEHMI_MODBUS_PRIVATE intToUint16(int value)
Store integer as 16 bit unsigned integer.
Definition: functions.cpp:11
std::internal
T internal(T... args)
std::numeric_limits
cutehmi::modbus::internal::int16FromUint16
qint16 CUTEHMI_MODBUS_PRIVATE int16FromUint16(quint16 value)
Restore 16 bit integer from 16 bit unsigned integer.
Definition: functions.cpp:38