⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 streambuf_operators.h

📁 网络泡泡被.net管理
💻 H
字号:
/************************************************************************

							Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  							   streambuf_operators.h

	$Header: heroes4/resource_editor/streambuf_operators.h $

	$NoKeywords: $

 ************************************************************************/
#ifndef STREAMBUF_OPERATORS_H_INCLUDED
#define STREAMBUF_OPERATORS_H_INCLUDED

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include <exception>
#include <ios>
#include <streambuf>


// -----------------------------------------------------------------
// t_streambuf_read_failure exception class
// -----------------------------------------------------------------

class t_streambuf_read_failure : public std::exception
{
public:
	// Member functions
	virtual char const * what() const throw();
};

// -----------------------------------------------------------------
// t_streambuf_write_failure exception class
// -----------------------------------------------------------------

class t_streambuf_write_failure : public std::exception
{
public:
	// Member functions
	virtual char const * what() const throw();
};

// -----------------------------------------------------------------
// functions to allow rational read / writes without extra typecasts
// -----------------------------------------------------------------

inline int read( std::streambuf& buffer, void* arg, int length )
{
	return buffer.sgetn( reinterpret_cast<char*>(arg), length );
}

inline int write( std::streambuf& buffer, void const* arg, int length )
{
	return buffer.sputn( reinterpret_cast<char const*>(arg), length );
}

// -----------------------------------------------------------------
// Convenience function templates for reading and writing simple
// types
// -----------------------------------------------------------------

template < typename t_arg >
inline void put( std::streambuf & buffer, t_arg const & arg )
{
	if ( write( buffer, &arg, sizeof( arg ) ) != sizeof( arg ) )
		throw t_streambuf_write_failure();
}

template < typename t_result >
inline t_result get( std::streambuf & buffer )
{
	t_result result;
	if ( read( buffer, &result, sizeof( result ) ) != sizeof( result ) )
		throw t_streambuf_read_failure();
	return result;
}

// -----------------------------------------------------------------
// string I/O functions
// -----------------------------------------------------------------

bool read_string16( std::streambuf& buffer, std::string& arg );

inline bool write_string16( std::streambuf& buffer, std::string const& arg )
{
	WORD size = arg.size();
	return		write( buffer, &size, sizeof( size ) ) == sizeof( size )
			&&	write( buffer, arg.data(), size ) == size;
}

// -----------------------------------------------------------------
// inlined input / output operators for basic types.
// -----------------------------------------------------------------

inline std::streambuf& operator<<(std::streambuf& buffer, bool const& arg)
{
	if ( write( buffer, &arg, sizeof( arg ) ) != sizeof( arg ) )
		throw t_streambuf_write_failure();
	return buffer;
}

inline std::streambuf& operator>>(std::streambuf& buffer, bool& arg)
{
	if ( read( buffer, &arg, sizeof( arg ) ) != sizeof( arg ) )
		throw t_streambuf_read_failure();
	return buffer;
}

inline std::streambuf& operator<<(std::streambuf& buffer, int const& arg)
{
	if ( write( buffer, &arg, sizeof( arg ) ) != sizeof( arg ) )
		throw t_streambuf_write_failure();
	return buffer;
}

inline std::streambuf& operator>>(std::streambuf& buffer, int& arg)
{
	if ( read( buffer, &arg, sizeof( arg ) ) != sizeof( arg ) )
		throw t_streambuf_read_failure();
	return buffer;
}

inline std::streambuf& operator<<(std::streambuf& buffer, unsigned int const& arg)
{
	if ( write( buffer, &arg, sizeof( arg ) ) != sizeof( arg ) )
		throw t_streambuf_write_failure();
	return buffer;
}

inline std::streambuf& operator>>(std::streambuf& buffer, unsigned int& arg)
{
	if ( read( buffer, &arg, sizeof( arg ) ) != sizeof( arg ) )
		throw t_streambuf_read_failure();
	return buffer;
}

inline std::streambuf& operator<<(std::streambuf& buffer, char const& arg)
{
	if ( write( buffer, &arg, sizeof( arg ) ) != sizeof( arg ) )
		throw t_streambuf_write_failure();
	return buffer;
}

inline std::streambuf& operator>>(std::streambuf& buffer, char& arg)
{
	if ( read( buffer, &arg, sizeof( arg ) ) != sizeof( arg ) )
		throw t_streambuf_read_failure();
	return buffer;
}

inline std::streambuf& operator<<(std::streambuf& buffer, BYTE const& arg)
{
	if ( write( buffer, &arg, sizeof( arg ) ) != sizeof( arg ) )
		throw t_streambuf_write_failure();
	return buffer;
}

inline std::streambuf& operator>>(std::streambuf& buffer, BYTE& arg)
{
	if ( read( buffer, &arg, sizeof( arg ) ) != sizeof( arg ) )
		throw t_streambuf_read_failure();
	return buffer;
}

inline std::streambuf& operator<<(std::streambuf& buffer, short const& arg)
{
	if ( write( buffer, &arg, sizeof( arg ) ) != sizeof( arg ) )
		throw t_streambuf_write_failure();
	return buffer;
}

inline std::streambuf& operator>>(std::streambuf& buffer, short& arg)
{
	if ( read( buffer, &arg, sizeof( arg ) ) != sizeof( arg ) )
		throw t_streambuf_read_failure();
	return buffer;
}

inline std::streambuf& operator<<(std::streambuf& buffer, WORD const& arg)
{
	if ( write( buffer, &arg, sizeof( arg ) ) != sizeof( arg ) )
		throw t_streambuf_write_failure();
	return buffer;
}

inline std::streambuf& operator>>(std::streambuf& buffer, WORD& arg)
{
	if ( read( buffer, &arg, sizeof( arg ) ) != sizeof( arg ) )
		throw t_streambuf_read_failure();
	return buffer;
}

inline std::streambuf& operator<<(std::streambuf& buffer, long const& arg)
{
	if ( write( buffer, &arg, sizeof( arg ) ) != sizeof( arg ) )
		throw t_streambuf_write_failure();
	return buffer;
}

inline std::streambuf& operator>>(std::streambuf& buffer, long& arg)
{
	if ( read( buffer, &arg, sizeof( arg ) ) != sizeof( arg ) )
		throw t_streambuf_read_failure();
	return buffer;
}

inline std::streambuf& operator<<(std::streambuf& buffer, DWORD const& arg)
{
	if ( write( buffer, &arg, sizeof( arg ) ) != sizeof( arg ) )
		throw t_streambuf_write_failure();
	return buffer;
}

inline std::streambuf& operator>>(std::streambuf& buffer, DWORD& arg)
{
	if ( read( buffer, &arg, sizeof( arg ) ) != sizeof( arg ) )
		throw t_streambuf_read_failure();
	return buffer;
}

inline std::streambuf & operator<<( std::streambuf & buffer, std::string const & arg )
{
	if ( !write_string16( buffer, arg ) )
		throw t_streambuf_write_failure();
	return buffer;
}

inline std::streambuf & operator>>( std::streambuf & buffer, std::string & arg )
{
	if ( !read_string16( buffer, arg ) )
		throw t_streambuf_read_failure();
	return buffer;
}


// the following is required because pubseekoff returns a non-int type, and 
// frequent typecasts are required if it is used in expressions.
inline int tell( std::streambuf& buffer )
{
	return buffer.pubseekoff( 0, std::ios::cur );
}







// -----------------------------------------------------------------
// functions to allow rational read / writes without extra typecasts
// -----------------------------------------------------------------

inline void read( fstream& buffer, void* arg, int length )
{
	buffer.read( reinterpret_cast<char*>(arg), length );
}

inline void write( fstream& buffer, void const* arg, int length )
{
	buffer.write( reinterpret_cast<char const*>(arg), length );
}

// -----------------------------------------------------------------
// Convenience function templates for reading and writing simple
// types
// -----------------------------------------------------------------

template < typename t_arg >
inline void put( fstream & buffer, t_arg const & arg )
{
	write( buffer, &arg, sizeof( arg ) );
}

template < typename t_result >
inline t_result get( fstream & buffer )
{
	t_result result;
	read( buffer, &result, sizeof( result ) );
	return result;
}

// -----------------------------------------------------------------
// string I/O functions
// -----------------------------------------------------------------
/*
bool read_string16( fstream& buffer, std::string& arg );

inline void write_string16( fstream& buffer, std::string const& arg )
{
	WORD size = arg.size();
	write( buffer, &size, sizeof( size ) );// == sizeof( size )
	write( buffer, arg.data(), size );// == size;
}
*/
// -----------------------------------------------------------------
// inlined input / output operators for basic types.
// -----------------------------------------------------------------

inline fstream& operator<<(fstream& buffer, bool const& arg)
{
	write( buffer, &arg, sizeof( arg ) );// != sizeof( arg ) )
	return buffer;
}

inline fstream& operator>>(fstream& buffer, bool& arg)
{
	read( buffer, &arg, sizeof( arg ) );
	return buffer;
}

inline fstream& operator<<(fstream& buffer, int const& arg)
{
	write( buffer, &arg, sizeof( arg ) );// != sizeof( arg ) )
	return buffer;
}

inline fstream& operator>>(fstream& buffer, int& arg)
{
	read( buffer, &arg, sizeof( arg ) );// != sizeof( arg ) )
	return buffer;
}

inline fstream& operator<<(fstream& buffer, unsigned int const& arg)
{
	write( buffer, &arg, sizeof( arg ) );// != sizeof( arg ) )
	return buffer;
}

inline fstream& operator>>(fstream& buffer, unsigned int& arg)
{
	read( buffer, &arg, sizeof( arg ) );// != sizeof( arg ) )
	return buffer;
}

inline fstream& operator<<(fstream& buffer, char const& arg)
{
	write( buffer, &arg, sizeof( arg ) );// != sizeof( arg ) )
	return buffer;
}

inline fstream& operator>>(fstream& buffer, char& arg)
{
	read( buffer, &arg, sizeof( arg ) );// != sizeof( arg ) )
	return buffer;
}

inline fstream& operator<<(fstream& buffer, BYTE const& arg)
{
	write( buffer, &arg, sizeof( arg ) );// != sizeof( arg ) )
	return buffer;
}

inline fstream& operator>>(fstream& buffer, BYTE& arg)
{
	read( buffer, &arg, sizeof( arg ) );// != sizeof( arg ) )
	return buffer;
}

inline fstream& operator<<(fstream& buffer, short const& arg)
{
	write( buffer, &arg, sizeof( arg ) );// != sizeof( arg ) )
	return buffer;
}

inline fstream& operator>>(fstream& buffer, short& arg)
{
	read( buffer, &arg, sizeof( arg ) );
	return buffer;
}

inline fstream& operator<<(fstream& buffer, WORD const& arg)
{
	write( buffer, &arg, sizeof( arg ) );
	return buffer;
}

inline fstream& operator>>(fstream& buffer, WORD& arg)
{
	read( buffer, &arg, sizeof( arg ) );
	return buffer;
}

inline fstream& operator<<(fstream& buffer, long const& arg)
{
	write( buffer, &arg, sizeof( arg ) );
	return buffer;
}

inline fstream& operator>>(fstream& buffer, long& arg)
{
	read( buffer, &arg, sizeof( arg ) );
	return buffer;
}

inline fstream& operator<<(fstream& buffer, DWORD const& arg)
{
	write( buffer, &arg, sizeof( arg ) );
	return buffer;
}

inline fstream& operator>>(fstream& buffer, DWORD& arg)
{
	read( buffer, &arg, sizeof( arg ) );
	return buffer;
}
/*
inline fstream & operator<<( fstream & buffer, std::string const & arg )
{
	return write_string16( buffer, arg );
}

inline fstream & operator>>( fstream & buffer, std::string & arg )
{
	return read_string16( buffer, arg );
}
*/

// the following is required because pubseekoff returns a non-int type, and 
// frequent typecasts are required if it is used in expressions.
inline int tell( fstream& buffer )
{
	return buffer.tellg();
}




#endif // ifndef STREAMBUF_OPERATORS_H_INCLUDED

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -