1

I want to use << operator to fill an external buffer,

I have tried to inherit from iostream, and then every time I use << to my class, I copy the content, into the current buffer location, and increment the buffer location...

I had no luck, I looked into some online examples or tutorials, but I am kinda lost. how can I use a class, and overload << operator, to copy variables of type T into the buffer??

Any direction?

An overview of the class I had:

class CBufferedMem
{
public:
    CBufferedMem(unsigned char* buffer, unsigned int size);
    char ReadFromBuffer(void* dst, unsigned char length);
    char writeToBuffer(void src, unsigned char length);
private:
    unsigned char* buffer;
    unsigned short buffSize;
    unsigned char* currentTmpLocation;
    unsigned char* endOfBuffer;
};
0decimal0
  • 3,884
  • 2
  • 24
  • 39
aah134
  • 860
  • 12
  • 25

2 Answers2

2

The simplest solution is to plainly use a special buffering class and overload the output operators needed:

struct buffer_class
{
    // The data needed...
};

inline buffer_class& operator<<(buffer_class& buffer, const std::string& s)
{
    // Code to add the string to the buffer
    return buffer;
}

inline buffer_class& operator<<(buffer_class& buffer, const uint8_t ub)
{
    // Code to add the value to the buffer
    return buffer;
}

inline buffer_class& operator<<(buffer_class& buffer, const int8_t sb)
{
    // Code to add the value to the buffer
    return buffer;
}

Add more operator overloads for the all the data needed.


For example, it could be like this:

struct buffer_class
{
    std::vector<int8_t> data;
};

inline buffer_class& operator<<(buffer_class& buffer, const std::string& s)
{
    for (const auto& ch : s)
        buffer.data.push_back(static_cast<unt8_t>(ch));
    return buffer;
}

inline buffer_class& operator<<(buffer_class& buffer, const uint8_t ub)
{
    buffer.data.push_back(static_cast<int8_t>(ub));
    return buffer;
}

inline buffer_class& operator<<(buffer_class& buffer, const int8_t sb)
{
    buffer.data.push_back(sb);
    return buffer;
}

Then you could use it like:

buffer_class my_buffer;

buffer << std::string("Hello") << 123:

// The raw data can now be accessed by `my_buffer.data.data()`
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
1

You don't want to inherit from an iostream to do this.

What you want to do is write your own stream buffer class that sends its data to the socket or message queue.

There are various socket stream classes around that demonstrate how to do this. Many (most?) of them are fairly old, as the stream model doesn't really work very well with sockets, but if you want to try it, the code's out there (including source code, so it shows how to do the same yourself -- which actually isn't terribly difficult).

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • I like this one, can you overload the << or >> operators? – aah134 Jun 17 '13 at 03:40
  • @abdul: When you do this, you mate the stream buffer with an iostream, so the existing `>>` and `<<` operators for the iostream work with your buffer. – Jerry Coffin Jun 17 '13 at 04:55
  • what do you mea, by "mate the stream buffer with an iostream" – aah134 Jun 17 '13 at 14:14
  • @abdul No, as it stores data binary. If you want to store the and use the data as a string, you don't need a special buffer class, then you have [`std::ostringstream`](http://en.cppreference.com/w/cpp/io/basic_ostringstream). – Some programmer dude Jul 05 '13 at 18:40