M
mathieu
Hello,
I am working on a project where I need to serialize my objects to a
stream in a mixed ASCII/binary way. Using the c++ iostream was
extremely straighforward and I quickly made progress. Unfortunately I
need to handle endianess (reading big endian binary file on little
endian architecture and vice versa). In order to do that I copy paste a
hierachy-like structure for my own IOStream. Basically I have (*)
All I need to do then is to redefine the binary stream operation
(istream::read, ostream::write) to byteswap the bytes array depending
on the SwapCode value. The formatted operations remain exactly the
same. Not supporting endianness is not an option for me (this is in the
file format description).
Is there another (easier?) way to achieve this ?
Thanks for your comments,
Mathieu
(*)
class IOSBase {};
class IOS : public IOSBase
{
public:
IOS () {}
~IOS() {}
SwapCode const &GetSwapCode() const { return m_SwapCode; }
protected:
SwapCode m_SwapCode;
};
class IStream : virtual public IOS
{
....// skipped to avoid too much details
};
class OStream : virtual public IOS
{
....// skipped to avoid too much details
};
class IOStream : public IStream, public OStream{};
And then I can simply do:
class StringStream : public IOStream
{
std::stringstream Internal;
};
or
class IFStream : public IStream
{
std::ifstream Internal;
};
I am working on a project where I need to serialize my objects to a
stream in a mixed ASCII/binary way. Using the c++ iostream was
extremely straighforward and I quickly made progress. Unfortunately I
need to handle endianess (reading big endian binary file on little
endian architecture and vice versa). In order to do that I copy paste a
hierachy-like structure for my own IOStream. Basically I have (*)
All I need to do then is to redefine the binary stream operation
(istream::read, ostream::write) to byteswap the bytes array depending
on the SwapCode value. The formatted operations remain exactly the
same. Not supporting endianness is not an option for me (this is in the
file format description).
Is there another (easier?) way to achieve this ?
Thanks for your comments,
Mathieu
(*)
class IOSBase {};
class IOS : public IOSBase
{
public:
IOS () {}
~IOS() {}
SwapCode const &GetSwapCode() const { return m_SwapCode; }
protected:
SwapCode m_SwapCode;
};
class IStream : virtual public IOS
{
....// skipped to avoid too much details
};
class OStream : virtual public IOS
{
....// skipped to avoid too much details
};
class IOStream : public IStream, public OStream{};
And then I can simply do:
class StringStream : public IOStream
{
std::stringstream Internal;
};
or
class IFStream : public IStream
{
std::ifstream Internal;
};