Open Lighting Architecture  0.9.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SchemaPrinter.h
1 /*
2  * This library is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU Lesser General Public
4  * License as published by the Free Software Foundation; either
5  * version 2.1 of the License, or (at your option) any later version.
6  *
7  * This library is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10  * Lesser General Public License for more details.
11  *
12  * You should have received a copy of the GNU Lesser General Public
13  * License along with this library; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15  *
16  * SchemaPrinter.h
17  * Builds a string which contains the text representation of the schema.
18  * Copyright (C) 2011 Simon Newton
19  */
20 
21 #ifndef INCLUDE_OLA_MESSAGING_SCHEMAPRINTER_H_
22 #define INCLUDE_OLA_MESSAGING_SCHEMAPRINTER_H_
23 
24 #include <ola/messaging/DescriptorVisitor.h>
25 #include <string>
26 #include <sstream>
27 
28 namespace ola {
29 namespace messaging {
30 
31 
36  public:
37  SchemaPrinter(bool include_intervals = true,
38  bool include_labels = true,
39  unsigned int indent_size = DEFAULT_INDENT)
40  : m_include_intervals(include_intervals),
41  m_include_labels(include_labels),
42  m_indent(0),
43  m_indent_size(indent_size) {
44  }
45  ~SchemaPrinter() {}
46 
47  bool Descend() const { return true; }
48  std::string AsString() { return m_str.str(); }
49  void Reset() { m_str.str(""); }
50 
51  void Visit(const BoolFieldDescriptor*);
52  void Visit(const IPV4FieldDescriptor*);
53  void Visit(const MACFieldDescriptor*);
54  void Visit(const UIDFieldDescriptor*);
55  void Visit(const StringFieldDescriptor*);
56  void Visit(const UInt8FieldDescriptor*);
57  void Visit(const UInt16FieldDescriptor*);
58  void Visit(const UInt32FieldDescriptor*);
59  void Visit(const Int8FieldDescriptor*);
60  void Visit(const Int16FieldDescriptor*);
61  void Visit(const Int32FieldDescriptor*);
62  void Visit(const FieldDescriptorGroup*);
63  void PostVisit(const FieldDescriptorGroup*);
64 
65  private:
66  bool m_include_intervals, m_include_labels;
67  std::ostringstream m_str;
68  unsigned int m_indent, m_indent_size;
69 
70  void AppendHeading(const std::string &name, const std::string &type);
71 
72  template<class vector_class>
73  void MaybeAppendIntervals(const vector_class &intervals) {
74  if (!m_include_intervals)
75  return;
76  typename vector_class::const_iterator iter = intervals.begin();
77  for (; iter != intervals.end(); ++iter) {
78  if (iter->first == iter->second) {
79  m_str << (iter == intervals.begin() ? ": " : ", ") <<
80  static_cast<int64_t>(iter->first);
81  } else {
82  m_str << (iter == intervals.begin() ? ": " : ", ") << "(" <<
83  static_cast<int64_t>(iter->first) << ", " <<
84  static_cast<int64_t>(iter->second) << ")";
85  }
86  }
87  }
88 
89  template<class map_class>
90  void MaybeAppendLabels(const map_class &labels) {
91  if (!m_include_labels)
92  return;
93  typename map_class::const_iterator iter = labels.begin();
94  for (; iter != labels.end(); ++iter) {
95  m_str << std::endl << std::string(m_indent + m_indent_size, ' ') <<
96  iter->first << ": " << static_cast<int64_t>(iter->second);
97  }
98  }
99 
100  static const unsigned int DEFAULT_INDENT = 2;
101 };
102 } // namespace messaging
103 } // namespace ola
104 #endif // INCLUDE_OLA_MESSAGING_SCHEMAPRINTER_H_