MapPrinter.hpp
Go to the documentation of this file.
1 /****
2  * MapPrinter.cpp - Print support for maps
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 FlashString.
16  * If not, see <https://www.gnu.org/licenses/>.
17  *
18  ****/
19 
20 #pragma once
21 
22 #include "Print.hpp"
23 
24 namespace FSTR
25 {
30 template <class MapType> class MapPrinter : public Printable
31 {
32 public:
33  MapPrinter(const MapType& map) : map(map)
34  {
35  }
36 
37  size_t printTo(Print& p) const override
38  {
39  size_t count = 0;
40 
41  count += p.println("{");
42  for(unsigned i = 0; i < map.length(); ++i) {
43  count += p.print(" ");
44  count += map.valueAt(i).printTo(p);
45  count += p.println();
46  }
47  count += p.print("}");
48 
49  return count;
50  }
51 
52 private:
53  const MapType& map;
54 };
55 
56 } // namespace FSTR
MapPrinter(const MapType &map)
Definition: MapPrinter.hpp:33
size_t print(char c)
Prints a single character to output stream.
Definition: Print.h:93
size_t printTo(Print &p) const override
Definition: MapPrinter.hpp:37
size_t println()
Prints a newline to output stream.
Definition: Print.h:179
Provides formatted output to stream.
Definition: Print.h:36
Definition: Printable.h:42
Class template to provide a simple way to print the contents of a Map.
Definition: MapPrinter.hpp:30
Definition: Array.hpp:107