CiftiLib
A C++ library for CIFTI-2 and CIFTI-1 files
src
Common
ByteSwapping.h
1
#ifndef __BYTE_SWAPPING_H__
2
#define __BYTE_SWAPPING_H__
3
4
/*LICENSE_START*/
5
/*
6
* Copyright (c) 2014, Washington University School of Medicine
7
* All rights reserved.
8
*
9
* Redistribution and use in source and binary forms, with or without modification,
10
* are permitted provided that the following conditions are met:
11
*
12
* 1. Redistributions of source code must retain the above copyright notice,
13
* this list of conditions and the following disclaimer.
14
*
15
* 2. Redistributions in binary form must reproduce the above copyright notice,
16
* this list of conditions and the following disclaimer in the documentation
17
* and/or other materials provided with the distribution.
18
*
19
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
*/
30
31
#include <stdint.h>
32
33
namespace
cifti
{
34
40
class
ByteSwapping
{
41
public
:
42
template
<
typename
T>
43
static
void
swap(T& toSwap);
44
45
template
<
typename
T>
46
static
void
swapArray(T* toSwap,
const
uint64_t& count);
47
48
inline
static
bool
isBigEndian()
49
{
50
uint16_t test = 1;
51
return
(((
char
*)&test)[0] == 0);
52
}
53
54
};
55
56
template
<
typename
T>
57
void
ByteSwapping::swap(T& toSwap)
58
{
59
if
(
sizeof
(T) == 1)
return
;
//we could specialize 1-byte types, but this should optimize out
60
T temp = toSwap;
61
char
* from = (
char
*)&temp;
62
char
* to = (
char
*)&toSwap;
63
for
(
int
i = 0; i < (int)
sizeof
(T); ++i)
64
{
65
to[i] = from[
sizeof
(T) - i - 1];
66
}
67
}
68
69
template
<
typename
T>
70
void
ByteSwapping::swapArray(T* toSwap,
const
uint64_t& count)
71
{
72
if
(
sizeof
(T) == 1)
return
;
//ditto
73
for
(uint64_t i = 0; i < count; ++i)
74
{
75
swap(toSwap[i]);
76
}
77
}
78
79
}
80
81
#endif // __BYTE_SWAPPING_H__
82
cifti::ByteSwapping
Definition:
ByteSwapping.h:40
cifti
namespace for all CiftiLib functionality
Definition:
CiftiBrainModelsMap.h:42
Generated by
1.8.18