Open Lighting Architecture  0.9.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MessagePrinter.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  * MessagePrinter.h
17  * Print the contents of a message.
18  * Copyright (C) 2011 Simon Newton
19  */
20 
21 #ifndef INCLUDE_OLA_MESSAGING_MESSAGEPRINTER_H_
22 #define INCLUDE_OLA_MESSAGING_MESSAGEPRINTER_H_
23 
24 #include <ola/messaging/MessageVisitor.h>
25 #include <string>
26 #include <sstream>
27 
28 namespace ola {
29 namespace messaging {
30 
31 
36  public:
37  virtual ~MessagePrinter() {}
38 
39  std::string AsString(const class Message *message);
40 
41  virtual void Visit(const BoolMessageField*) {}
42  virtual void Visit(const IPV4MessageField*) {}
43  virtual void Visit(const MACMessageField*) {}
44  virtual void Visit(const UIDMessageField*) {}
45  virtual void Visit(const StringMessageField*) {}
46  virtual void Visit(const BasicMessageField<uint8_t>*) {}
47  virtual void Visit(const BasicMessageField<uint16_t>*) {}
48  virtual void Visit(const BasicMessageField<uint32_t>*) {}
49  virtual void Visit(const BasicMessageField<int8_t>*) {}
50  virtual void Visit(const BasicMessageField<int16_t>*) {}
51  virtual void Visit(const BasicMessageField<int32_t>*) {}
52  virtual void Visit(const GroupMessageField*) {}
53  virtual void PostVisit(const GroupMessageField*) {}
54 
55  protected:
56  std::ostringstream& Stream() { return m_str; }
57  virtual void PostStringHook() {}
58  virtual std::string TransformLabel(const std::string &label) {
59  return label;
60  }
61 
62  private:
63  std::ostringstream m_str;
64 };
65 
66 
71  public:
72  GenericMessagePrinter(unsigned int indent_size = DEFAULT_INDENT,
73  unsigned int initial_indent = 0)
74  : m_indent(initial_indent),
75  m_indent_size(indent_size) {
76  }
78 
79  virtual void Visit(const BoolMessageField*);
80  virtual void Visit(const IPV4MessageField*);
81  virtual void Visit(const MACMessageField*);
82  virtual void Visit(const UIDMessageField*);
83  virtual void Visit(const StringMessageField*);
84  virtual void Visit(const BasicMessageField<uint8_t>*);
85  virtual void Visit(const BasicMessageField<uint16_t>*);
86  virtual void Visit(const BasicMessageField<uint32_t>*);
87  virtual void Visit(const BasicMessageField<int8_t>*);
88  virtual void Visit(const BasicMessageField<int16_t>*);
89  virtual void Visit(const BasicMessageField<int32_t>*);
90  virtual void Visit(const GroupMessageField*);
91  virtual void PostVisit(const GroupMessageField*);
92 
93  static const unsigned int DEFAULT_INDENT = 2;
94 
95  private:
96  unsigned int m_indent, m_indent_size;
97 
98  void AppendUInt(const std::string &name,
99  unsigned int value,
100  const std::string &label,
101  int8_t multipler);
102  void AppendInt(const std::string &name,
103  int value,
104  const std::string &label,
105  int8_t multipler);
106  void AppendMultipler(int8_t multipler);
107 };
108 } // namespace messaging
109 } // namespace ola
110 #endif // INCLUDE_OLA_MESSAGING_MESSAGEPRINTER_H_