BALL  1.4.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
binaryFileAdaptor.h
Go to the documentation of this file.
1 #ifndef BALL_SYSTEM_BINARYFILEADAPTOR_H
2 #define BALL_SYSTEM_BINARYFILEADAPTOR_H
3 
4 #include <iostream>
5 #include <algorithm>
6 
7 #ifndef BALL_COMMON_LOGSTREAM_H
8 # include <BALL/COMMON/logStream.h>
9 #endif
10 
11 namespace BALL
12 {
16  template <typename T>
17  void swapBytes(T& t)
18  {
19  if (sizeof(T) % 2 != 0)
20  {
21  Log.error() << "Cannot swap types of uneven size." << std::endl;
22  return;
23  }
24 
25  char* tmp = reinterpret_cast<char*>(&t);
26  std::reverse(tmp, tmp + sizeof(T));
27  }
28 
29  //In the following some specialisations of swapBytes are provided for efficiency reasons
30  //These should also cover BALL types like Size, Position and Index
31  template<> BALL_EXPORT void swapBytes(unsigned short&);
32  template<> BALL_EXPORT void swapBytes(short&);
33  template<> BALL_EXPORT void swapBytes(unsigned int&);
34  template<> BALL_EXPORT void swapBytes(int&);
35  template<> BALL_EXPORT void swapBytes(unsigned long&);
36  template<> BALL_EXPORT void swapBytes(long&);
37  template<> BALL_EXPORT void swapBytes(float&);
38  template<> BALL_EXPORT void swapBytes(double&);
39 
52  template <typename T>
54  {
55 
56  public:
57 
59 
60 
63 
65  BinaryFileAdaptor(const T& data, bool swap_endian = false);
66 
68 
69 
70 
72  void setSwapEndian(bool swap_endian);
73 
75  bool getSwapEndian() const;
76 
80  void setData(const T& data);
81 
84  const T& getData() const;
85 
88  T& getData();
89 
91 
92  protected:
93 
94  //_ The member data.
95  T data_;
96 
97  //_ A flag indicating whether we should swap all reads and writes
99  };
100 
101  template <typename T>
103  : data_(),
104  swap_endian_(false)
105  {
106  }
107 
108  template <typename T>
109  BinaryFileAdaptor<T>::BinaryFileAdaptor(const T& data, bool swap_endian)
110  : data_(data),
111  swap_endian_(swap_endian)
112  {
113  }
114 
115  template <typename T>
116  void BinaryFileAdaptor<T>::setSwapEndian(bool swap_endian)
117  {
118  swap_endian_ = swap_endian;
119  }
120 
121  template <typename T>
123  {
124  return swap_endian_;
125  }
126 
127  template <typename T>
128  void BinaryFileAdaptor<T>::setData(const T& data)
129  {
130  data_ = data;
131  }
132 
133  template <typename T>
135  {
136  return data_;
137  }
138 
139  template <typename T>
141  {
142  return data_;
143  }
144 
146  template <typename T>
147  std::ostream& operator << (std::ostream& os, const BinaryFileAdaptor<T>& data)
148  {
149  // do we need to swap endianness?
150  if (!data.getSwapEndian())
151  {
152  os.write(reinterpret_cast<const char*>(&data.getData()), sizeof(T));
153  }
154  else
155  {
156  T swapped_data = data.getData();
157  swapBytes(swapped_data);
158  os.write(reinterpret_cast<const char*>(&swapped_data), sizeof(T));
159  }
160  return os;
161  }
162 
164  template <typename T>
165  std::istream& operator >> (std::istream& is, BinaryFileAdaptor<T>& data)
166  {
167  // do we need to swap endianness?
168  if (!data.getSwapEndian())
169  {
170  is.read(reinterpret_cast<char*>(&data.getData()), sizeof(T));
171  }
172  else
173  {
174  T swapped_data;
175  is.read(reinterpret_cast<char*>(&swapped_data), sizeof(T));
176  swapBytes(swapped_data);
177  data.setData(swapped_data);
178  }
179  return is;
180  }
181 
182 } //namespace BALL
183 
184 #ifndef BALL_NO_INLINE_FUNCTIONS
185  #include <BALL/SYSTEM/binaryFileAdaptor.iC>
186 #endif
187 
188 #endif //BALL_SYSTEM_BINARYFILEADAPTOR_H