Removed unnecessary utf8 codes from some doc files
[barry.git] / src / parser.h
blobe032ce1aa9a2a3def9d722063763546d75061a8f
1 ///
2 /// \file parser.h
3 /// Virtual parser wrapper
4 ///
6 /*
7 Copyright (C) 2005-2009, Net Direct Inc. (http://www.netdirect.ca/)
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 See the GNU General Public License in the COPYING file at the
19 root directory of this project for more details.
22 #ifndef __BARRY_PARSER_H__
23 #define __BARRY_PARSER_H__
25 #include "dll.h"
26 #include "data.h"
27 #include "protocol.h"
28 #include <stdint.h> // for uint32_t
30 // forward declarations
31 namespace Barry {
32 class Data;
33 class IConverter;
36 namespace Barry {
39 // Parser class
41 /// Base class for the parser hierarchy.
42 ///
43 /// This class provides the interface that the Controller class uses
44 /// to pass raw data it reads from the device. The Controller, along
45 /// with the Packet class, calls each of the virtual functions below
46 /// in the same order.
47 ///
48 /// This class is kept as a pure abstract class, in order to make sure
49 /// that the compiler will catch any API changes, for code derived
50 /// from it.
51 ///
52 class BXEXPORT Parser
54 public:
55 Parser() {}
56 virtual ~Parser() {}
58 /// Reset and prepare for a new raw data packet
59 virtual void Clear() = 0;
61 /// Stores the IDs
62 virtual void SetIds(uint8_t RecType, uint32_t UniqueId) = 0;
64 /// Called to parse the header portion of the raw data packet.
65 /// data contains the entire packet, and offset contains the
66 /// location at which to start parsing.
67 virtual void ParseHeader(const Data &data, size_t &offset) = 0;
69 /// Called to parse sub fields in the raw data packet.
70 /// The same data is passed as was passed in ParseHeader,
71 /// only the offset will be updated if it was advanced during
72 /// the header parsing.
73 virtual void ParseFields(const Data &data, size_t &offset,
74 const IConverter *ic) = 0;
76 /// Called at the very end of record parsing, and used to
77 /// store the final packet somewhere, either in memory, disk, etc.
78 virtual void Store() = 0;
83 // NullParser class
85 /// If in debug mode, this class can be used as a null parser.
86 /// Call Init() and the protocol will be dumped to stdout and
87 /// no parsing will be done.
88 ///
89 /// Do NOT derive your own personal parser classes from this,
90 /// unless you are perfectly confident that you will catch
91 /// future API changes on the devel tree without the compiler's
92 /// help.
93 ///
94 class BXEXPORT NullParser : public Parser
96 public:
97 NullParser() {}
98 virtual ~NullParser() {}
100 /// Reset and prepare for a new raw data packet
101 virtual void Clear() {}
103 /// Stores the IDs
104 virtual void SetIds(uint8_t RecType, uint32_t UniqueId) {}
106 /// Called to parse the header portion of the raw data packet.
107 /// data contains the entire packet, and offset contains the
108 /// location at which to start parsing.
109 virtual void ParseHeader(const Data &data, size_t &offset) {}
111 /// Called to parse sub fields in the raw data packet.
112 /// The same data is passed as was passed in ParseHeader,
113 /// only the offset will be updated if it was advanced during
114 /// the header parsing.
115 virtual void ParseFields(const Data &data, size_t &offset,
116 const IConverter *ic) {}
118 /// Called at the very end of record parsing, and used to
119 /// store the final packet somewhere, either in memory, disk, etc.
120 virtual void Store() {}
125 // RecordParser template class
127 /// Template class for easy creation of specific parser objects. This template
128 /// takes the following template arguments:
130 /// - RecordT: One of the record parser classes in record.h
131 /// - StorageT: A custom storage functor class. An object of this type
132 /// will be called as a function with parsed Record as an
133 /// argument. This happens on the fly as the data is retrieved
134 /// from the device over USB, so it should not block forever.
136 /// Example LoadDatabase() call:
138 /// <pre>
139 /// struct StoreContact
140 /// {
141 /// std::vector<Contact> &amp;array;
142 /// StoreContact(std::vector<Contact> &amp;a) : array(a) {}
143 /// void operator() (const Contact &amp;c)
144 /// {
145 /// array.push_back(c);
146 /// }
147 /// };
149 /// Controller con(probeResult);
150 /// con.OpenMode(Controller::Desktop);
151 /// std::vector<Contact> contactList;
152 /// StoreContact storage(contactList);
153 /// RecordParser<Contact, StoreContact> parser(storage);
154 /// con.LoadDatabase(con.GetDBID("Address Book"), parser);
155 /// </pre>
157 template <class RecordT, class StorageT>
158 class RecordParser : public Parser
160 StorageT *m_store;
161 bool m_owned;
162 RecordT m_rec;
164 public:
165 /// Constructor that references an externally managed storage object.
166 RecordParser(StorageT &storage)
167 : m_store(&storage), m_owned(false) {}
169 /// Constructor that references a locally managed storage object.
170 /// The pointer passed in will be stored, and freed when this class
171 /// is destroyed. It is safe to call this constructor with
172 /// a 'new'ly created storage object.
173 RecordParser(StorageT *storage)
174 : m_store(storage), m_owned(true) {}
176 ~RecordParser()
178 if( this->m_owned )
179 delete m_store;
182 virtual void Clear()
184 m_rec = RecordT();
187 virtual void SetIds(uint8_t RecType, uint32_t UniqueId)
189 m_rec.SetIds(RecType, UniqueId);
192 virtual void ParseHeader(const Data &data, size_t &offset)
194 m_rec.ParseHeader(data, offset);
197 virtual void ParseFields(const Data &data, size_t &offset,
198 const IConverter *ic)
200 m_rec.ParseFields(data, offset, ic);
203 virtual void Store()
205 (*m_store)(m_rec);
209 } // namespace Barry
211 #endif