PLCnext API Documentation 24.0.0.71
ByteConverter.hpp
1
2//
3// Copyright PHOENIX CONTACT Electronics GmbH
4//
6#pragma once
8#include <algorithm>
9namespace Arp
10{
11
14{
15private: // construction/destruction
16 ByteConverter(void) = delete;
17 ByteConverter(const ByteConverter& arg) = delete;
18 ByteConverter& operator=(const ByteConverter& arg) = delete;
19 ~ByteConverter(void) = delete;
20
21public: // static operations
25 template<class T>
26 static void Swap(T& value);
27
31 static void Swap(byte* pBuffer, size_t size);
32
33
36 static void Swap(int8& value);
37
41 static void Swap(byte& value);
42
45 static void Swap(int16& value);
46
49 static void Swap(uint16& value);
50
54 template<class T>
55 static void ReverseCopy(const T& source, T& target);
56
61 static void ReverseCopy(const byte* pSource, byte* pTarget, size_t size);
62};
63
65// inline methods of class ByteConverter
66template<class T>
67inline void ByteConverter::Swap(T& value)
68{
69 Swap((byte*)&value, sizeof(T));
70}
71
72inline void ByteConverter::Swap(byte* pBuffer, size_t size)
73{
74 std::reverse(pBuffer, pBuffer + size);
75}
76
77inline void ByteConverter::Swap(byte& /*value*/)
78{
79 // overloaded for optimization: nothing to do
80}
81
82inline void ByteConverter::Swap(int8& /*value*/)
83{
84 // overload for optimization: nothing to do
85}
86
87// optimization to avoid call of algorithm for types with sizeof(T) <= 2
88inline void ByteConverter::Swap(int16& value)
89{
90 std::swap(*reinterpret_cast<byte*>(&value), *(reinterpret_cast<byte*>(&value) + 1));
91}
92
93// optimization to avoid call of algorithm for types with sizeof(T) <= 2
94inline void ByteConverter::Swap(uint16& value)
95{
96 std::swap(*reinterpret_cast<byte*>(&value), *(reinterpret_cast<byte*>(&value) + 1));
97}
98
99inline void ByteConverter::ReverseCopy(const byte* pSource, byte* pTarget, size_t size)
100{
101 std::reverse_copy(pSource, pSource + size, pTarget);
102}
103
104template<class T>
105inline void ByteConverter::ReverseCopy(const T& source, T& target)
106{
107 const byte* pSource = reinterpret_cast<const byte*>(&source);
108 byte* pTarget = reinterpret_cast<byte*>(&target);
109 std::reverse_copy(pSource, pSource + sizeof(T), pTarget);
110}
111
112} // end of namespace Arp
This pure static class provides operation sfor byte converting from little to big endian and vice ver...
Definition: ByteConverter.hpp:14
static void Swap(T &value)
Swaps the bytes of the as argument passed value of any primitive type.
Definition: ByteConverter.hpp:67
static void ReverseCopy(const T &source, T &target)
Copies the reverted bytes of the source argument to the target argument.
Definition: ByteConverter.hpp:105
void swap(BasicString< CharType, Alloc > &left, BasicString< CharType, Alloc > &right) noexcept
Swaps the content of the left string with the content of the right string.
Definition: BasicString.hxx:1730
std::int8_t int8
The Arp integer type of 1 byte size.
Definition: PrimitiveTypes.hpp:30
std::uint16_t uint16
The Arp unsigned integer type of 2 byte size.
Definition: PrimitiveTypes.hpp:32
std::int16_t int16
The Arp integer type of 2 byte size.
Definition: PrimitiveTypes.hpp:34
Root namespace for the PLCnext API