You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
2.2KB

  1. // Copyright 2009 Olivier Gillet.
  2. //
  3. // Author: Olivier Gillet (ol.gillet@gmail.com)
  4. //
  5. // This program is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. //
  16. // -----------------------------------------------------------------------------
  17. //
  18. // Output stream. Wraps any module implementing the basic Output interface
  19. // (in fact, just a Write method), and provide string and integer formatting
  20. // using the << stream operator.
  21. #ifndef AVRLIB_OUTPUT_STREAM_H_
  22. #define AVRLIB_OUTPUT_STREAM_H_
  23. #include "avrlib/string.h"
  24. using avrlib::Itoa;
  25. using avrlib::TypeInfo;
  26. namespace avrlib {
  27. enum EndOfLine {
  28. endl = 0
  29. };
  30. template<typename Output>
  31. struct OutputStream {
  32. static void Print(const char* string) {
  33. while (*string) {
  34. Output::Write(*string++);
  35. }
  36. }
  37. static void Print(uint8_t byte) {
  38. Output::Write(byte);
  39. }
  40. static void Print(char byte) {
  41. Output::Write(byte);
  42. }
  43. static void Print(uint16_t value) {
  44. char buffer[TypeInfo<uint16_t>::max_size + 1];
  45. Itoa(value, TypeInfo<uint16_t>::max_size + 1, buffer);
  46. Print(buffer);
  47. }
  48. static void Print(int16_t value) {
  49. char buffer[TypeInfo<int16_t>::max_size + 1];
  50. Itoa(value, TypeInfo<int16_t>::max_size + 1, buffer);
  51. Print(buffer);
  52. }
  53. static void Print(uint32_t value) {
  54. char buffer[TypeInfo<uint32_t>::max_size + 1];
  55. Itoa(value, TypeInfo<uint32_t>::max_size + 1, buffer);
  56. Print(buffer);
  57. }
  58. static void Print(EndOfLine e) {
  59. Print('\r');
  60. Print('\n');
  61. }
  62. template<class T>
  63. inline OutputStream<Output>& operator<<(const T value) {
  64. Print(value);
  65. return *this;
  66. }
  67. };
  68. } // namespace avrlib
  69. #endif // AVRLIB_OUTPUT_STREAM_H_