Reputation: 315
What i'm trying to do is to insert into a stream ( std::ostream ) a buffer .
this buffer is an attribute of my class "AcquiSample"
I made this example very simple for you to understand my problem.
here the definition of the class :
class AcquiSample
{
public:
IQSample * buffer;
}
here's the definition of the struct IQSample :
struct IQSample {
struct {
short val;
}I;
struct {
short val;
}Q;
}
what I want to do is put this buffer with the known size into a stream like that:
std::ostream stream;
AcquiSample obj;
stream << obj.buffer->I.val << obj.buffer->I.val;
I think it's not working because it's not copying the data but the pointer adress, what i want is to put the entire buffer into this stream because i'm sending this stream on another computer.
If anyone know how to do this i'd be very thankful.
Upvotes: 0
Views: 1252
Reputation: 1840
Perhaps something like this is what you are looking for?
std::ostream& operator<<(std::ostream& os, const AcquiSample & sample)
{
//Deserialize your object here, or create methods in AcquiSample for serialization and deserialization.
os << sample.buffer->I.val;
os << sample.buffer->Q.val;
return os;
}
int main(int argc, char **argv) {
std::ostream stream; // Don't forget to initialize the stream.
AcquiSample obj; // Don't forget to set buffer to something!
stream << obj;
}
Upvotes: 1
Reputation: 60989
The most idiomatic way to achieve this is to overload the insertion operator:
struct IQSample {
struct {
short val;
}I;
struct {
short val;
}Q;
};
std::ostream& operator<<( std::ostream& os, IQSample iqs )
{
return os << iqs.I.val << ' ' << iqs.Q.val; // Don't forget to put white spaces between the numbers!
}
std::istream& operator>>( std::istream& os, IQSample& iqs )
{
return is >> iqs.I.val >> iqs.Q.val;
}
And now
std::ostream stream; // some stream bound to a streambuf who transfers over network
AcquiSample obj;
stream << *obj.buffer;
Upvotes: 2