www.cemf.ir
vtkByteSwapper.hpp
Go to the documentation of this file.
1 /*------------------------------- phasicFlow ---------------------------------
2  O C enter of
3  O O E ngineering and
4  O O M ultiscale modeling of
5  OOOOOOO F luid flow
6 ------------------------------------------------------------------------------
7  Copyright (C): www.cemf.ir
8  email: hamid.r.norouzi AT gmail.com
9 ------------------------------------------------------------------------------
10 Licence:
11  This file is part of phasicFlow code. It is a free software for simulating
12  granular and multiphase flows. You can redistribute it and/or modify it under
13  the terms of GNU General Public License v3 or any other later versions.
14 
15  phasicFlow is distributed to help others in their research in the field of
16  granular and multiphase flows, but WITHOUT ANY WARRANTY; without even the
17  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 
19 -----------------------------------------------------------------------------*/
20 
21 #ifndef __vtkByteSwapper_h__
22 #define __vtkByteSwapper_h__
23 
24 namespace pFlow
25 {
26 
27 /*
28 This utility is used to re-order bytes when writing data in binary format to vtk file
29 These lines of code are exactly copied from the source code of https://github.com/Kitware/VTK/
30 */
31 
32 
33 template <size_t s>
35 
37 template <>
38 struct vtkByteSwapper<1>
39 {
40  static inline void Swap(char*) {}
41 };
42 
44 template <>
45 struct vtkByteSwapper<2>
46 {
47  static inline void Swap(char* data)
48  {
49  const uint16_t& ref16 = *reinterpret_cast<uint16_t*>(data);
50  *reinterpret_cast<uint16_t*>(data) = (ref16 >> 8) | (ref16 << 8);
51  }
52 };
53 
55 template <>
56 struct vtkByteSwapper<4>
57 {
58  static inline void Swap(char* data)
59  {
60  const uint32_t& ref32 = *reinterpret_cast<uint32_t*>(data);
61  *reinterpret_cast<uint32_t*>(data) =
62  (ref32 >> 24) | (ref32 << 24) | ((ref32 & 0x00ff0000) >> 8) | ((ref32 & 0x0000ff00) << 8);
63  }
64 };
65 
67 template <>
68 struct vtkByteSwapper<8>
69 {
70  static inline void Swap(char* data)
71  {
72  const uint64_t& ref64 = *reinterpret_cast<uint64_t*>(data);
73  *reinterpret_cast<uint64_t*>(data) = (ref64 >> 56) | (ref64 << 56) |
74  ((ref64 & 0x00ff000000000000) >> 40) | ((ref64 & 0x000000000000ff00) << 40) |
75  ((ref64 & 0x0000ff0000000000) >> 24) | ((ref64 & 0x0000000000ff0000) << 24) |
76  ((ref64 & 0x000000ff00000000) >> 8) | ((ref64 & 0x00000000ff000000) << 8);
77  }
78 };
79 
80 template<typename T>
81 inline T byteSwaper(const T& p)
82 {
83  union
84  {
85  T value;
86  char data[sizeof(T)];
87  } temp = { p };
88  vtkByteSwapper<sizeof(T)>::Swap(temp.data);
89  return temp.value;
90 }
91 
92 
93 
94 } // pFlow
95 
96 #endif //__vtkByteSwapper_h__
pFlow::vtkByteSwapper< 8 >::Swap
static void Swap(char *data)
Definition: vtkByteSwapper.hpp:70
pFlow::NBSLevel0Kernels::Swap
INLINE_FUNCTION_HD void Swap(T &x, T &y)
Definition: NBSLevel0Kernels.hpp:38
pFlow
Definition: demGeometry.hpp:27
pFlow::vtkByteSwapper< 1 >::Swap
static void Swap(char *)
Definition: vtkByteSwapper.hpp:40
pFlow::vtkByteSwapper
Definition: vtkByteSwapper.hpp:34
pFlow::vtkByteSwapper< 2 >::Swap
static void Swap(char *data)
Definition: vtkByteSwapper.hpp:47
pFlow::byteSwaper
T byteSwaper(const T &p)
Definition: vtkByteSwapper.hpp:81
pFlow::vtkByteSwapper< 4 >::Swap
static void Swap(char *data)
Definition: vtkByteSwapper.hpp:58