Open Lighting Architecture
 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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  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 UIDFieldDescriptor*);
54  void Visit(const StringFieldDescriptor*);
55  void Visit(const UInt8FieldDescriptor*);
56  void Visit(const UInt16FieldDescriptor*);
57  void Visit(const UInt32FieldDescriptor*);
58  void Visit(const Int8FieldDescriptor*);
59  void Visit(const Int16FieldDescriptor*);
60  void Visit(const Int32FieldDescriptor*);
61  void Visit(const FieldDescriptorGroup*);
62  void PostVisit(const FieldDescriptorGroup*);
63 
64  private:
65  bool m_include_intervals, m_include_labels;
66  std::stringstream m_str;
67  unsigned int m_indent, m_indent_size;
68 
69  void AppendHeading(const string &name, const string &type);
70 
71  template<class vector_class>
72  void MaybeAppendIntervals(const vector_class &intervals) {
73  if (!m_include_intervals)
74  return;
75  typename vector_class::const_iterator iter = intervals.begin();
76  for (; iter != intervals.end(); ++iter) {
77  if (iter->first == iter->second) {
78  m_str << (iter == intervals.begin() ? ": " : ", ") <<
79  static_cast<int64_t>(iter->first);
80  } else {
81  m_str << (iter == intervals.begin() ? ": " : ", ") << "(" <<
82  static_cast<int64_t>(iter->first) << ", " <<
83  static_cast<int64_t>(iter->second) << ")";
84  }
85  }
86  }
87 
88  template<class map_class>
89  void MaybeAppendLabels(const map_class &labels) {
90  if (!m_include_labels)
91  return;
92  typename map_class::const_iterator iter = labels.begin();
93  for (; iter != labels.end(); ++iter) {
94  m_str << std::endl << string(m_indent + m_indent_size, ' ') <<
95  iter->first << ": " << static_cast<int64_t>(iter->second);
96  }
97  }
98 
99  static const unsigned int DEFAULT_INDENT = 2;
100 };
101 } // namespace messaging
102 } // namespace ola
103 #endif // INCLUDE_OLA_MESSAGING_SCHEMAPRINTER_H_