Руслан Урядинский / libuavcan

Dependents:   UAVCAN UAVCAN_Subscriber

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ostream.hpp Source File

ostream.hpp

00001 /*
00002  * Copyright (C) 2014 Pavel Kirienko <pavel.kirienko@gmail.com>
00003  */
00004 
00005 #ifndef UAVCAN_HELPERS_OSTREAM_HPP_INCLUDED
00006 #define UAVCAN_HELPERS_OSTREAM_HPP_INCLUDED
00007 
00008 #include <uavcan/util/templates.hpp>
00009 #include <cstdio>
00010 
00011 namespace uavcan
00012 {
00013 /**
00014  * Compact replacement for std::ostream for use on embedded systems.
00015  * Can be used for printing UAVCAN messages to stdout.
00016  *
00017  * Relevant discussion: https://groups.google.com/forum/#!topic/px4users/6c1CLNutN90
00018  *
00019  * Usage:
00020  *      OStream::instance() << "Hello world!" << OStream::endl;
00021  */
00022 class UAVCAN_EXPORT OStream : uavcan::Noncopyable
00023 {
00024     OStream() { }
00025 
00026 public:
00027     static OStream& instance()
00028     {
00029         static OStream s;
00030         return s;
00031     }
00032 
00033     static OStream& endl(OStream& stream)
00034     {
00035         std::printf("\n");
00036         return stream;
00037     }
00038 };
00039 
00040 inline OStream& operator<<(OStream& s, long long x)          { std::printf("%lld", x);  return s; }
00041 inline OStream& operator<<(OStream& s, unsigned long long x) { std::printf("%llu", x); return s; }
00042 
00043 inline OStream& operator<<(OStream& s, long x)           { std::printf("%ld", x); return s; }
00044 inline OStream& operator<<(OStream& s, unsigned long x)  { std::printf("%lu", x); return s; }
00045 
00046 inline OStream& operator<<(OStream& s, int x)            { std::printf("%d", x);  return s; }
00047 inline OStream& operator<<(OStream& s, unsigned int x)   { std::printf("%u", x);  return s; }
00048 
00049 inline OStream& operator<<(OStream& s, short x)          { return operator<<(s, static_cast<int>(x)); }
00050 inline OStream& operator<<(OStream& s, unsigned short x) { return operator<<(s, static_cast<unsigned>(x)); }
00051 
00052 inline OStream& operator<<(OStream& s, long double x) { std::printf("%Lg", x); return s; }
00053 inline OStream& operator<<(OStream& s, double x)      { std::printf("%g", x);  return s; }
00054 inline OStream& operator<<(OStream& s, float x)       { return operator<<(s, static_cast<double>(x)); }
00055 
00056 inline OStream& operator<<(OStream& s, char x)        { std::printf("%c", x); return s; }
00057 inline OStream& operator<<(OStream& s, const char* x) { std::printf("%s", x); return s; }
00058 
00059 inline OStream& operator<<(OStream& s, OStream&(*manip)(OStream&)) { return manip(s); }
00060 
00061 }
00062 
00063 #endif // UAVCAN_HELPERS_OSTREAM_HPP_INCLUDED