endian stream?

Started by
7 comments, last by SiCrane 15 years, 6 months ago
hey guys. I want an ostream, similar to a filestream except it does not dump to a file. I want it to dump to a vector<> and resize it if necessary. Then i want to get the ptr and use send() on it. I'll probably want an istream for recv(). Does something like this exist? if not how do i write one of these? I want to focus on ostream first.
<SkilletAudio> Your framerate proves your lack of manhood
Advertisement
I don't think this exists in the standard C++ library, but it's pretty easy to cook one up yourself.

Here's an example class. You can push any data into it, get a pointer to the data stream, get the size of the data stream and erase the data stream.
class OutStream{public:	template<class T>	void PushData( const T& data );	void PushData( char data );	const char* GetData() const;	void ClearData();private:	typedef std::vector<char> ByteVec;	ByteVec bytes;};
If you want to be able to write stream << data instead of stream.PushData( data );, then you've just got to write an << operator, like this:
template<class T>OutStream& operator<<( OutStream& lhs, const T& rhs ){	lhs.PushData( rhs );	return lhs;}
And here's how you could implement the insides of that class:
template<class T>void OutStream::PushData( const T& data ){	const char* pData = reinterpret_cast<const char*>( &data );	const char* pDataEnd = pData + sizeof(T);	std::back_insert_iterator<ByteVec> pushBack( bytes );	std::copy( pData, pDataEnd, pushBack );}inline void OutStream::PushData( char data ){	bytes.push_back( data );}inline const char* OutStream::GetData() const{	if( bytes.empty() )		return NULL;	else		return &bytes[0];}inline  unsigned int OutStream::GetDataSize() const{	return bytes.size();}inline void OutStream::ClearData(){	bytes.resize( 0 ); }
I got that part down. I dont know how to get ostream to work with it

I just tried for an hour. How do i get stream << 2568; to work? it will never call my functions! stream << "text"; calls xsputn perfectly fine but i dont know why it wont call. ostream.write(...) doesnt work either and i implemented that too. only << "text" works
<SkilletAudio> Your framerate proves your lack of manhood
when I have done this I used boost iostreams, My code which allows you to connect an adt (vector, list) etc in a sink and then use normal streaming operators to fill it.

You might want to check out boost::asio as a way to do networking side of things also - it works supports vectors as a basic buffer type. When I experimented with this, I had a system whereby the flush on the iostream that is invoked by std::endl would automatically flush the text through the socket using asio - but I eventually decided it was a bit unnatural and overengineed.
It sounds to me that your problem description does not require the solution of reinventing a formatting facility, but rather that of implementing a transport facility.

See, in the C++ standard library, ostream and istream are formatters. You would be hard pressed to come up with a reason to ever try to reinvent them. The place where you need to focus is on a streambuf. It works effectively as if you are writing bytes to a std::vector and then when it reaches a certain high-water mark, flushing it out to some data sink.

I would suggest that what you want to do is use something identical to std::ofstream (in fact, use std::ofstream!) and implement a custom streambuf by deriving from std::basic_filebuf. You would want to implement open()/close() to provide socket setup/teardown, and overflow() (for output) and underflow() (for input). Use the std::ofstream's redbuf() member function to replace its default filebuf with your own.

By doing that, you've (a) work with what C++ gives you instead of against it and (2) provided a looser [please note the correct use of that word -- ed.] coupling between you data transport and the use of that data transport.

Stephen M. Webb
Professional Free Software Developer

What functions do i overload? i am looking at streambuf on this page and i stil have trouble with "<< val;". note that i am not using ofstream

http://www.cplusplus.com/reference/iostream/streambuf/
<SkilletAudio> Your framerate proves your lack of manhood
Does Mr Edd's article help?
Quote:Original post by rip-off
Does Mr Edd's article help?


Of course it does!! ;)
Ok, why not use std::stringstream if you can't get anything else to work?

This topic is closed to new replies.

Advertisement