In C++ it's common to stream data. You write to a file using the << operator.
example:
cout << "hello, world! << endl;
In this blog I'm making my own minimal output stream class. Just enough code to show that it works. This could become an object that allows you to write to a UART, SPI, CAN, ... . In this post, I called my class uartostream, although it doesn't do anything UART. Just to give it an embedded sounding name, and to show how this could look like in firmware.
I put embedded friendly in the title because this is a resource-friendly exercise. Can be used on the smallest controllers.
output stream class
You can stream c-type strings to this class, and std::string objects.
class uartostream {
public:
uartostream() {}
uartostream& operator << (const char* msg) {
// write to UART
return *this;
}
uartostream& operator << (const std::string& msg) {
// write to UART
return *this;
}
};
Notice that the class doesn't do anything with them. In this first post I want to check if the code compiles. And I use a debugger to see if the strings arrive in their handlers. If I put a breakpoint in the handler methods, and the msg parameter contains the string we've streamed to it, the mechanism works.
using the class
In the example code, I'll stream a std::string and a classic c-style char string.
#include <string>
class uartostream{ // see previous section for class definition
};
uartostream u;
int main() {
u << std::string("5") << "5" ;
}
test
I put a breakpoint in both operator << members. The code should first halt in the one that takes a std::string reference. Then the one that takes the c string.

The screen capture above shows the state while streaming the first part. If you want to see what happens when the "5" is streamed: try it in your favourite C++ IDE.
Thank you for reading. To be continued.
-
DAB
-
Cancel
-
Vote Up
0
Vote Down
-
-
Sign in to reply
-
More
-
Cancel
Comment-
DAB
-
Cancel
-
Vote Up
0
Vote Down
-
-
Sign in to reply
-
More
-
Cancel
Children