]> git.sesse.net Git - casparcg/blob - common/endian.h
[streaming_consumer] Added support for sending recording activity via OSC like in...
[casparcg] / common / endian.h
1 /*
2 * Copyright 2013 Sveriges Television AB http://casparcg.com/
3 *
4 * This file is part of CasparCG (www.casparcg.com).
5 *
6 * CasparCG is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * CasparCG 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 * You should have received a copy of the GNU General Public License
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * Author: Helge Norberg, helge.norberg@svt.se
20 * Author: Robert Nagy, ronag89@gmail.com
21 */
22
23 #pragma once
24
25 #include <type_traits>
26 #include <cstdint>
27
28 #ifdef _MSC_VER
29 #include <intrin.h>
30 #endif
31
32 namespace caspar {
33
34 template<typename T>
35 typename std::enable_if<sizeof(T) == sizeof(std::uint8_t), T>::type swap_byte_order(
36                 const T& value)
37 {
38         return value;
39 }
40
41 template<typename T>
42 typename std::enable_if<sizeof(T) == sizeof(std::uint16_t), T>::type swap_byte_order(
43                 const T& value)
44 {
45 #ifdef _MSC_VER
46         auto swapped = _byteswap_ushort(reinterpret_cast<const unsigned short&>(value));
47 #elif __GNUC__
48         auto swapped = __builtin_bswap16(value);
49 #endif
50
51         return reinterpret_cast<const T&>(swapped);
52 }
53
54 template<typename T>
55 typename std::enable_if<sizeof(T) == sizeof(std::uint32_t), T>::type swap_byte_order(
56                 const T& value)
57 {
58 #ifdef _MSC_VER
59         auto swapped = _byteswap_ulong(reinterpret_cast<const unsigned long&>(value));
60 #elif __GNUC__
61         auto swapped = __builtin_bswap32(value);
62 #endif
63
64         return reinterpret_cast<const T&>(swapped);
65 }
66
67 template<typename T>
68 typename std::enable_if<sizeof(T) == sizeof(std::uint64_t), T>::type swap_byte_order(
69                 const T& value)
70 {
71 #ifdef _MSC_VER
72         auto swapped = _byteswap_uint64(reinterpret_cast<const unsigned long long&>(value));
73 #elif __GNUC__
74         auto swapped = __builtin_bswap64(value);
75 #endif
76
77         return reinterpret_cast<const T&>(swapped);
78 }
79
80 }