]> git.sesse.net Git - casparcg/blob - common/endian.h
Misc modifications to fix problems found by static code analysis and some simplificat...
[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
27 #include <intrin.h>
28
29 namespace caspar {
30
31 template<typename T>
32 typename std::enable_if<sizeof(T) == sizeof(unsigned char), T>::type swap_byte_order(
33                 const T& value)
34 {
35         return value;
36 }
37
38 template<typename T>
39 typename std::enable_if<sizeof(T) == sizeof(unsigned short), T>::type swap_byte_order(
40                 const T& value)
41 {
42         auto swapped = _byteswap_ushort(reinterpret_cast<const unsigned short&>(value));
43         return reinterpret_cast<const T&>(swapped);
44 }
45
46 template<typename T>
47 typename std::enable_if<sizeof(T) == sizeof(unsigned long), T>::type swap_byte_order(
48                 const T& value)
49 {
50         auto swapped = _byteswap_ulong(reinterpret_cast<const unsigned long&>(value));
51     return reinterpret_cast<const T&>(swapped);
52 }
53
54 template<typename T>
55 typename std::enable_if<sizeof(T) == sizeof(unsigned long long), T>::type swap_byte_order(
56                 const T& value)
57 {
58         auto swapped = _byteswap_uint64(reinterpret_cast<const unsigned long long&>(value));
59     return reinterpret_cast<const T&>(swapped);
60 }
61
62 }