PLCnext API Documentation 25.0.2.69
ByteConverter.hpp
1
2//
3// Copyright Phoenix Contact GmbH & Co. KG
4//
6#pragma once
7#ifndef ARP_USE_ARP_SYSTEM_CORE
8#include "Arp/Base/Core/ByteConverter.hpp"
9#else
10#include "Arp/System/Core/Arp.h"
11#include <algorithm>
12namespace Arp
13{
14
16class ByteConverter
17{
18private: // construction/destruction
19 ByteConverter(void) = delete;
20 ByteConverter(const ByteConverter& arg) = delete;
21 ByteConverter& operator=(const ByteConverter& arg) = delete;
22 ~ByteConverter(void) = delete;
23
24public: // static operations
28 template<class T>
29 static void Swap(T& value);
30
34 static void Swap(byte* pBuffer, size_t size);
35
36
39 static void Swap(int8& value);
40
44 static void Swap(byte& value);
45
48 static void Swap(int16& value);
49
52 static void Swap(uint16& value);
53
57 template<class T>
58 static void ReverseCopy(const T& source, T& target);
59
64 static void ReverseCopy(const byte* pSource, byte* pTarget, size_t size);
65};
66
68// inline methods of class ByteConverter
69template<class T>
70inline void ByteConverter::Swap(T& value)
71{
72 Swap((byte*)&value, sizeof(T));
73}
74
75inline void ByteConverter::Swap(byte* pBuffer, size_t size)
76{
77 std::reverse(pBuffer, pBuffer + size);
78}
79
80inline void ByteConverter::Swap(byte& /*value*/)
81{
82 // overloaded for optimization: nothing to do
83}
84
85inline void ByteConverter::Swap(int8& /*value*/)
86{
87 // overload for optimization: nothing to do
88}
89
90// optimization to avoid call of algorithm for types with sizeof(T) <= 2
91inline void ByteConverter::Swap(int16& value)
92{
93 std::swap(*reinterpret_cast<byte*>(&value), *(reinterpret_cast<byte*>(&value) + 1));
94}
95
96// optimization to avoid call of algorithm for types with sizeof(T) <= 2
97inline void ByteConverter::Swap(uint16& value)
98{
99 std::swap(*reinterpret_cast<byte*>(&value), *(reinterpret_cast<byte*>(&value) + 1));
100}
101
102inline void ByteConverter::ReverseCopy(const byte* pSource, byte* pTarget, size_t size)
103{
104 std::reverse_copy(pSource, pSource + size, pTarget);
105}
106
107template<class T>
108inline void ByteConverter::ReverseCopy(const T& source, T& target)
109{
110 const byte* pSource = reinterpret_cast<const byte*>(&source);
111 byte* pTarget = reinterpret_cast<byte*>(&target);
112 std::reverse_copy(pSource, pSource + sizeof(T), pTarget);
113}
114
115} // end of namespace Arp
116
117#endif // ndef ARP_USE_ARP_SYSTEM_CORE
static void ReverseCopy(const T &source, T &target)
Copies the reverted bytes of the source argument to the target argument.
Definition: ByteConverter.hpp:48
static void Swap(T &value)
Swaps the bytes of the as argument passed value of any primitive type.
Definition: ByteConverter.hpp:39
std::int16_t int16
The Arp integer type of 2 byte size.
Definition: PrimitiveTypes.hpp:31
std::uint16_t uint16
The Arp unsigned integer type of 2 byte size.
Definition: PrimitiveTypes.hpp:29
std::int8_t int8
The Arp integer type of 1 byte size.
Definition: PrimitiveTypes.hpp:27
Root namespace for the PLCnext API