Sign in to follow this  

endian stream?

This topic is 3340 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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 );
}

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

This topic is 3340 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this