Open Lighting Architecture
 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 UIDMessageField*) {}
44  virtual void Visit(const StringMessageField*) {}
45  virtual void Visit(const BasicMessageField<uint8_t>*) {}
46  virtual void Visit(const BasicMessageField<uint16_t>*) {}
47  virtual void Visit(const BasicMessageField<uint32_t>*) {}
48  virtual void Visit(const BasicMessageField<int8_t>*) {}
49  virtual void Visit(const BasicMessageField<int16_t>*) {}
50  virtual void Visit(const BasicMessageField<int32_t>*) {}
51  virtual void Visit(const GroupMessageField*) {}
52  virtual void PostVisit(const GroupMessageField*) {}
53 
54  protected:
55  std::stringstream& Stream() { return m_str; }
56  virtual void PostStringHook() {}
57  virtual string TransformLabel(const string &label) { return label; }
58 
59  private:
60  std::stringstream m_str;
61 };
62 
63 
68  public:
69  GenericMessagePrinter(unsigned int indent_size = DEFAULT_INDENT,
70  unsigned int initial_indent = 0)
71  : m_indent(initial_indent),
72  m_indent_size(indent_size) {
73  }
75 
76  virtual void Visit(const BoolMessageField*);
77  virtual void Visit(const IPV4MessageField*);
78  virtual void Visit(const UIDMessageField*);
79  virtual void Visit(const StringMessageField*);
80  virtual void Visit(const BasicMessageField<uint8_t>*);
81  virtual void Visit(const BasicMessageField<uint16_t>*);
82  virtual void Visit(const BasicMessageField<uint32_t>*);
83  virtual void Visit(const BasicMessageField<int8_t>*);
84  virtual void Visit(const BasicMessageField<int16_t>*);
85  virtual void Visit(const BasicMessageField<int32_t>*);
86  virtual void Visit(const GroupMessageField*);
87  virtual void PostVisit(const GroupMessageField*);
88 
89  static const unsigned int DEFAULT_INDENT = 2;
90 
91  private:
92  unsigned int m_indent, m_indent_size;
93 
94  void AppendUInt(const string &name,
95  unsigned int value,
96  const string &label,
97  int8_t multipler);
98  void AppendInt(const string &name,
99  int value,
100  const string &label,
101  int8_t multipler);
102  void AppendMultipler(int8_t multipler);
103 };
104 } // namespace messaging
105 } // namespace ola
106 #endif // INCLUDE_OLA_MESSAGING_MESSAGEPRINTER_H_