Meta.h
Go to the documentation of this file.
1 /****
2  * Meta.h
3  *
4  * Copyright 2021 mikee47 <mike@sillyhouse.net>
5  *
6  * This file is part of the Sming-Graphics 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  * @author: May 2021 - mikee47 <mike@sillyhouse.net>
19  *
20  ****/
21 
22 #pragma once
23 
24 #include "Types.h"
25 #include <Data/LinkedObjectList.h>
26 #include <Data/CString.h>
28 #include <Print.h>
29 
30 namespace Graphics
31 {
32 class MetaWriter;
33 
38 class Meta
39 {
40  // String getTypeStr() const;
41  // void write(MetaWriter& meta) const;
42 };
43 
48 {
49 public:
50  MetaWriter(Print& out) : out(out)
51  {
52  }
53 
54  template <typename T>
55  typename std::enable_if<std::is_base_of<Meta, T>::value, void>::type write(const String& name, const T& value)
56  {
57  writeIndent();
58  if(name) {
59  out.print(name);
60  out.print(": ");
61  }
62  out.print(value.getTypeStr());
63  out.println(" {");
64  ++indent;
65  value.write(*this);
66  --indent;
67  println("};");
68  }
69 
70  template <typename T> typename std::enable_if<std::is_base_of<Meta, T>::value, void>::type write(const T& value)
71  {
72  write(nullptr, value);
73  }
74 
75  void write(const String& name, const CString& value)
76  {
77  writeIndent();
78  out.print(name);
79  out.print(": ");
80  out.println(value.c_str());
81  }
82 
83  void write(const String& name, IDataSourceStream& stream)
84  {
85  writeIndent();
86  out.print(name);
87  out.print(": ");
88  stream.seekFrom(0, SeekOrigin::Start);
89  char buffer[1024];
90  auto len = stream.readBytes(buffer, sizeof(buffer));
91  out.write(buffer, len);
92  out.println();
93  }
94 
95  template <typename T>
96  typename std::enable_if<std::is_arithmetic<T>::value || std::is_base_of<String, T>::value, void>::type
97  write(const String& name, const T& value)
98  {
99  writeIndent();
100  out.print(name);
101  out.print(": ");
102  out.println(value);
103  }
104 
105  template <typename T>
106  typename std::enable_if<!std::is_arithmetic<T>::value && !std::is_base_of<Meta, T>::value &&
107  !std::is_base_of<String, T>::value && !std::is_base_of<CString, T>::value &&
108  !std::is_base_of<Stream, T>::value,
109  void>::type
110  write(const String& name, const T& value)
111  {
112  writeIndent();
113  out.print(name);
114  out.print(": ");
115  out.println(::toString(value));
116  }
117 
118  void beginArray(const String& name, const String& type)
119  {
120  String s;
121  s += name;
122  s += ": ";
123  s += type;
124  s += "[] {";
125  println(s);
126  ++indent;
127  }
128 
129  void endArray()
130  {
131  --indent;
132  println("}");
133  }
134 
135  template <typename T> void writeArray(const String& name, const String& type, const T* values, unsigned count)
136  {
137  beginArray(name, type);
138  for(unsigned i = 0; i < count; ++i) {
139  println(toString(values[i]));
140  }
141  endArray();
142  }
143 
144  template <typename T>
145  void writeArray(const String& name, const String& type, const LinkedObjectListTemplate<T>& list)
146  {
147  beginArray(name, type);
148  for(auto& obj : list) {
149  write(obj);
150  }
151  endArray();
152  }
153 
154 private:
155  void writeIndent()
156  {
157  auto n = indent * 2;
158  char buf[n];
159  memset(buf, ' ', n);
160  out.write(buf, n);
161  }
162 
163  template <typename T> void println(const T& value)
164  {
165  writeIndent();
166  out.println(value);
167  }
168 
169  Print& out;
170  uint8_t indent{0};
171 };
172 
173 } // namespace Graphics
std::enable_if< std::is_integral< T >::value, String >::type toString(T value)
Definition: BitSet.h:481
@ Start
SEEK_SET: Start of file.
Class to manage a NUL-terminated C-style string When storing persistent strings in RAM the regular St...
Definition: CString.h:27
const char * c_str() const
Definition: CString.h:94
Writes object content in readable format for debugging.
Definition: Meta.h:48
void writeArray(const String &name, const String &type, const T *values, unsigned count)
Definition: Meta.h:135
void write(const String &name, IDataSourceStream &stream)
Definition: Meta.h:83
void write(const String &name, const CString &value)
Definition: Meta.h:75
void beginArray(const String &name, const String &type)
Definition: Meta.h:118
MetaWriter(Print &out)
Definition: Meta.h:50
void endArray()
Definition: Meta.h:129
void writeArray(const String &name, const String &type, const LinkedObjectListTemplate< T > &list)
Definition: Meta.h:145
std::enable_if< std::is_arithmetic< T >::value||std::is_base_of< String, T >::value, void >::type write(const String &name, const T &value)
Definition: Meta.h:97
std::enable_if< std::is_base_of< Meta, T >::value, void >::type write(const T &value)
Definition: Meta.h:70
std::enable_if< std::is_base_of< Meta, T >::value, void >::type write(const String &name, const T &value)
Definition: Meta.h:55
std::enable_if<!std::is_arithmetic< T >::value &&!std::is_base_of< Meta, T >::value &&!std::is_base_of< String, T >::value &&!std::is_base_of< CString, T >::value &&!std::is_base_of< Stream, T >::value, void >::type write(const String &name, const T &value)
Definition: Meta.h:110
Empty base class to support object enumeration Non-virtual to avoid bloat.
Definition: Meta.h:39
Base class for read-only stream.
Definition: DataSourceStream.h:46
size_t readBytes(char *buffer, size_t length) override
Read chars from stream into buffer.
virtual int seekFrom(int offset, SeekOrigin origin)
Change position in stream.
Definition: DataSourceStream.h:97
Definition: LinkedObjectList.h:90
Provides formatted output to stream.
Definition: Print.h:37
size_t println()
Prints a newline to output stream.
Definition: Print.h:227
virtual size_t write(uint8_t c)=0
Writes a single character to output stream.
size_t print(char c)
Prints a single character to output stream.
Definition: Print.h:97
The String class.
Definition: WString.h:137
Definition: Virtual.h:31