ArrayPrinter.hpp
Go to the documentation of this file.
1 /****
2  * ArrayPrinter.hpp - Print support for arrays
3  *
4  * Copyright 2019 mikee47 <mike@sillyhouse.net>
5  *
6  * This file is part of the FlashString Library
7  *
8  * This library is free software: you can redistribute it and/or modify it under the terms of the
9  * GNU General Public License as published by the Free Software Foundation, version 3 or later.
10  *
11  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  * See the GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along with this library.
16  * If not, see <https://www.gnu.org/licenses/>.
17  *
18  ****/
19 
20 #pragma once
21 
22 #include "Print.hpp"
23 #include <stringutil.h>
24 
25 namespace FSTR
26 {
27 template <typename T>
28 typename std::enable_if<!std::is_same<T, char>::value, size_t>::type printElement(Print& p, const T& e)
29 {
30  return print(p, e);
31 }
32 
33 size_t printElement(Print& p, char c);
34 
39 template <class ArrayType> class ArrayPrinter
40 {
41 public:
42  ArrayPrinter(const ArrayType& array) : array(array)
43  {
44  }
45 
46  size_t printTo(Print& p) const
47  {
48  size_t count = 0;
49 
50  count += p.print('{');
51  for(unsigned i = 0; i < array.length(); ++i) {
52  if(i > 0) {
53  count += p.print(", ");
54  }
55  count += printElement(p, array[i]);
56  }
57  count += p.print('}');
58 
59  return count;
60  }
61 
62 private:
63  const ArrayType& array;
64 };
65 
66 } // namespace FSTR
Class template to provide a simple way to print the contents of an array.
Definition: ArrayPrinter.hpp:40
size_t printTo(Print &p) const
Definition: ArrayPrinter.hpp:46
ArrayPrinter(const ArrayType &array)
Definition: ArrayPrinter.hpp:42
Provides formatted output to stream.
Definition: Print.h:37
size_t print(char c)
Prints a single character to output stream.
Definition: Print.h:97
Definition: Array.hpp:108
std::enable_if<!std::is_same< T, char >::value, size_t >::type printElement(Print &p, const T &e)
Definition: ArrayPrinter.hpp:28
std::enable_if< std::is_class< ObjectType >::value, size_t >::type print(Print &p, const ObjectType &object)
Print an object.
Definition: Print.hpp:40