Loading...
Searching...
No Matches
binaryFileAdaptor.h
Go to the documentation of this file.
#ifndef BALL_SYSTEM_BINARYFILEADAPTOR_H
#define BALL_SYSTEM_BINARYFILEADAPTOR_H
#include <iostream>
#include <algorithm>
#ifndef BALL_COMMON_LOGSTREAM_H
# include <BALL/COMMON/logStream.h>
#endif
namespace BALL
{
template <typename T>
void swapBytes(T& t)
{
if (sizeof(T) % 2 != 0)
{
Log.error() << "Cannot swap types of uneven size." << std::endl;
return;
}
char* tmp = reinterpret_cast<char*>(&t);
std::reverse(tmp, tmp + sizeof(T));
}
//In the following some specialisations of swapBytes are provided for efficiency reasons
//These should also cover BALL types like Size, Position and Index
template<> BALL_EXPORT void swapBytes(unsigned short&);
template<> BALL_EXPORT void swapBytes(short&);
template<> BALL_EXPORT void swapBytes(unsigned int&);
template<> BALL_EXPORT void swapBytes(int&);
template<> BALL_EXPORT void swapBytes(unsigned long&);
template<> BALL_EXPORT void swapBytes(long&);
template<> BALL_EXPORT void swapBytes(float&);
template<> BALL_EXPORT void swapBytes(double&);
template <typename T>
class BinaryFileAdaptor
{
public:
BinaryFileAdaptor();
BinaryFileAdaptor(const T& data, bool swap_endian = false);
void setSwapEndian(bool swap_endian);
bool getSwapEndian() const;
void setData(const T& data);
const T& getData() const;
T& getData();
protected:
//_ The member data.
T data_;
//_ A flag indicating whether we should swap all reads and writes
bool swap_endian_;
};
template <typename T>
BinaryFileAdaptor<T>::BinaryFileAdaptor()
: data_(),
swap_endian_(false)
{
}
template <typename T>
BinaryFileAdaptor<T>::BinaryFileAdaptor(const T& data, bool swap_endian)
: data_(data),
swap_endian_(swap_endian)
{
}
template <typename T>
void BinaryFileAdaptor<T>::setSwapEndian(bool swap_endian)
{
swap_endian_ = swap_endian;
}
template <typename T>
bool BinaryFileAdaptor<T>::getSwapEndian() const
{
return swap_endian_;
}
template <typename T>
void BinaryFileAdaptor<T>::setData(const T& data)
{
data_ = data;
}
template <typename T>
const T& BinaryFileAdaptor<T>::getData() const
{
return data_;
}
template <typename T>
T& BinaryFileAdaptor<T>::getData()
{
return data_;
}
template <typename T>
std::ostream& operator << (std::ostream& os, const BinaryFileAdaptor<T>& data)
{
// do we need to swap endianness?
if (!data.getSwapEndian())
{
os.write(reinterpret_cast<const char*>(&data.getData()), sizeof(T));
}
else
{
T swapped_data = data.getData();
swapBytes(swapped_data);
os.write(reinterpret_cast<const char*>(&swapped_data), sizeof(T));
}
return os;
}
template <typename T>
std::istream& operator >> (std::istream& is, BinaryFileAdaptor<T>& data)
{
// do we need to swap endianness?
if (!data.getSwapEndian())
{
is.read(reinterpret_cast<char*>(&data.getData()), sizeof(T));
}
else
{
T swapped_data;
is.read(reinterpret_cast<char*>(&swapped_data), sizeof(T));
swapBytes(swapped_data);
data.setData(swapped_data);
}
return is;
}
} //namespace BALL
#ifndef BALL_NO_INLINE_FUNCTIONS
#include <BALL/SYSTEM/binaryFileAdaptor.iC>
#endif
#endif //BALL_SYSTEM_BINARYFILEADAPTOR_H
BALL_EXPORT
#define BALL_EXPORT
Definition COMMON/global.h:50
BALL::BinaryFileAdaptor
Definition binaryFileAdaptor.h:54
BALL::BinaryFileAdaptor::getSwapEndian
bool getSwapEndian() const
return the swap_endian flag
Definition binaryFileAdaptor.h:122
BALL::BinaryFileAdaptor::getData
const T & getData() const
Definition binaryFileAdaptor.h:134
BALL::BinaryFileAdaptor::data_
T data_
Definition binaryFileAdaptor.h:95
BALL::BinaryFileAdaptor::swap_endian_
bool swap_endian_
Definition binaryFileAdaptor.h:98
BALL::BinaryFileAdaptor::BinaryFileAdaptor
BinaryFileAdaptor()
Default constructor.
Definition binaryFileAdaptor.h:102
BALL::BinaryFileAdaptor::setSwapEndian
void setSwapEndian(bool swap_endian)
Set the swap_endian flag.
Definition binaryFileAdaptor.h:116
BALL::BinaryFileAdaptor::setData
void setData(const T &data)
Definition binaryFileAdaptor.h:128
BALL::operator<<
BALL_EXPORT std::ostream & operator<<(std::ostream &os, const Exception::GeneralException &e)
logStream.h
BALL
Definition constants.h:13
BALL::Log
BALL_EXPORT LogStream Log
BALL::swapBytes
void swapBytes(T &t)
Definition binaryFileAdaptor.h:17
BALL::operator>>
std::istream & operator>>(std::istream &is, TRegularData1D< ValueType > &grid)
Input operator.
Definition regularData1D.h:802