Added documentation comment for LoadALXFile's enable flag
[barry.git] / src / parser.h
blobca8150bb26d5ef9c403f39bd7207ea80b170933b
1 ///
2 /// \file parser.h
3 /// Virtual parser wrapper
4 ///
6 /*
7 Copyright (C) 2005-2010, 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
29 #include <iosfwd>
30 #include <map>
32 // forward declarations
33 namespace Barry {
34 class IConverter;
35 class Contact;
36 class Message;
37 class Calendar;
38 class CalendarAll;
39 class CallLog;
40 class Bookmark;
41 class ServiceBook;
42 class Memo;
43 class Task;
44 class PINMessage;
45 class SavedMessage;
46 class Sms;
47 class Folder;
48 class Timezone;
51 namespace Barry {
54 // Parser class
56 /// Base class for the parser hierarchy.
57 ///
58 /// This class provides the interface that the Controller class uses
59 /// to pass raw data it reads from the device. The Controller, along
60 /// with the Packet class, calls each of the virtual functions below
61 /// in the same order.
62 ///
63 /// This class is kept as a pure abstract class, in order to make sure
64 /// that the compiler will catch any API changes, for code derived
65 /// from it.
66 ///
67 class BXEXPORT Parser
69 public:
70 Parser() {}
71 virtual ~Parser() {}
73 /// Called to parse sub fields in the raw data packet.
74 virtual void ParseRecord(const DBData &data, const IConverter *ic) = 0;
79 // NullParser class
81 /// If in debug mode, this class can be used as a null parser.
82 /// Call Init() and the protocol will be dumped to stdout and
83 /// no parsing will be done.
84 ///
85 /// Do NOT derive your own personal parser classes from this,
86 /// unless you are perfectly confident that you will catch
87 /// future API changes on the devel tree without the compiler's
88 /// help.
89 ///
90 class BXEXPORT NullParser : public Parser
92 public:
93 NullParser() {}
94 virtual ~NullParser() {}
96 virtual void ParseRecord(const DBData &data, const IConverter *ic) {}
100 // HexDumpParser
102 /// Dumps raw hex of the given DBData to the given stream.
104 /// Do NOT derive your own personal parser classes from this,
105 /// unless you are perfectly confident that you will catch
106 /// future API changes on the devel tree without the compiler's
107 /// help.
109 class BXEXPORT HexDumpParser : public Parser
111 std::ostream &m_os;
113 public:
114 explicit HexDumpParser(std::ostream &os);
116 virtual void ParseRecord(const Barry::DBData &data,
117 const IConverter *ic);
121 // RecordParserBase
123 /// Abstract base class for the following RecordParser template, that exposes
124 /// some information on the specifics that the record parser can handle.
125 /// Specifically, it exposes the database name it is able to parse
127 class BXEXPORT RecordParserBase : public Parser
129 public:
130 // These functions are always valid, regardless of the
131 // state of the parser.
132 virtual const char * GetDBName() const = 0;
133 virtual uint8_t GetDefaultRecType() const = 0;
135 // These functions depend on the parser having just parsed
136 // a record successfully.
137 virtual bool IsRecordValid() const = 0;
138 virtual uint8_t GetRecType() const = 0;
139 virtual uint32_t GetUniqueId() const = 0;
140 virtual void Dump(std::ostream &os) const = 0;
145 // Note: Store classes take parsed Record objects as a functor.
146 // Parser classes deal with raw data, while Store classes deal with
147 // parsed Record objects.
151 // NullStore
153 /// A Storage class for RecordParser<> that does nothing, for the cases
154 /// where you only want to dump parsed record data to a stream.
156 template <class RecordT>
157 class NullStore
159 public:
160 void operator() (const RecordT &r)
166 // DumpStore
168 /// A Storage class for RecordParser<> that dumps the parsed record data
169 /// to the given stream.
171 template <class RecordT>
172 class DumpStore
174 std::ostream &m_os;
176 public:
177 explicit DumpStore(std::ostream &os)
178 : m_os(os)
182 void operator() (const RecordT &r)
184 r.Dump(m_os);
189 // RecordParser template class
191 /// Template class for easy creation of specific parser objects. This template
192 /// takes the following template arguments:
194 /// - RecordT: One of the record parser classes in record.h
195 /// - StorageT: A custom storage functor class. An object of this type
196 /// will be called as a function with parsed Record as an
197 /// argument. This happens on the fly as the data is retrieved
198 /// from the device over USB, so it should not block forever.
200 /// Example LoadDatabase() call:
202 /// <pre>
203 /// struct StoreContact
204 /// {
205 /// std::vector<Contact> &amp;array;
206 /// StoreContact(std::vector<Contact> &amp;a) : array(a) {}
207 /// void operator() (const Contact &amp;c)
208 /// {
209 /// array.push_back(c);
210 /// }
211 /// };
213 /// Controller con(probeResult);
214 /// con.OpenMode(Controller::Desktop);
215 /// std::vector<Contact> contactList;
216 /// StoreContact storage(contactList);
217 /// RecordParser<Contact, StoreContact> parser(storage);
218 /// con.LoadDatabase(con.GetDBID("Address Book"), parser);
219 /// </pre>
221 template <class RecordT, class StorageT>
222 class RecordParser : public RecordParserBase
224 StorageT *m_store;
225 bool m_owned;
226 RecordT m_rec;
227 bool m_record_valid;
229 public:
230 /// Constructor that references an externally managed storage object.
231 RecordParser(StorageT &storage)
232 : m_store(&storage)
233 , m_owned(false)
234 , m_record_valid(false)
238 /// Constructor that references a locally managed storage object.
239 /// The pointer passed in will be stored, and freed when this class
240 /// is destroyed. It is safe to call this constructor with
241 /// a 'new'ly created storage object.
242 RecordParser(StorageT *storage = 0)
243 : m_store(storage)
244 , m_owned(true)
245 , m_record_valid(false)
249 ~RecordParser()
251 if( this->m_owned )
252 delete m_store;
255 virtual StorageT* GetStore()
257 return m_store;
260 virtual const StorageT* GetStore() const
262 return m_store;
265 virtual void ParseRecord(const DBData &data, const IConverter *ic)
267 m_rec = RecordT();
268 m_record_valid = false;
270 m_rec.SetIds(data.GetRecType(), data.GetUniqueId());
271 size_t offset = data.GetOffset();
272 m_rec.ParseHeader(data.GetData(), offset);
273 m_rec.ParseFields(data.GetData(), offset, ic);
274 m_record_valid = true;
276 if( m_store )
277 (*m_store)(m_rec);
281 // RecordParserBase overrides
284 // These functions are always valid, regardless of the
285 // state of the parser.
286 virtual const char * GetDBName() const
288 return RecordT::GetDBName();
291 virtual uint8_t GetDefaultRecType() const
293 return RecordT::GetDefaultRecType();
296 // These functions depend on the parser having just parsed
297 // a record successfully.
298 virtual bool IsRecordValid() const
300 return m_record_valid;
303 virtual uint8_t GetRecType() const
305 return m_rec.GetRecType();
308 virtual uint32_t GetUniqueId() const
310 return m_rec.GetUniqueId();
313 virtual void Dump(std::ostream &os) const
315 m_rec.Dump(os);
320 // MultiRecordParser
322 /// Container parser class that accepts multiple Parser objects
323 /// (often RecordParser<> objects but they don't have to be) and
324 /// automatically routes incoming records to the appropriate parser.
325 /// Note that this container owns *all* Parser objects, and will
326 /// free them upon destruction.
328 /// Incoming records that have no matching parser are passed to the
329 /// default parser object, if one exists, otherwise they are dropped
330 /// silently. The default parser object is also owned by the container,
331 /// and will be freed on destruction.
333 /// Do NOT derive your own personal parser classes from this,
334 /// unless you are perfectly confident that you will catch
335 /// future API changes on the devel tree without the compiler's
336 /// help.
338 class BXEXPORT MultiRecordParser : public Parser
340 typedef std::map<std::string, Parser*> map_type;
342 Parser *m_default;
343 map_type m_parsers;
345 public:
346 // takes ownership of default_parser!
347 explicit MultiRecordParser(Parser *default_parser = 0);
348 ~MultiRecordParser();
350 /// Adds given parser to list and takes ownership of it
351 void Add(const std::string &dbname, Parser *parser);
353 /// Adds given parser to list and takes ownership of it
354 void Add(RecordParserBase *parser);
356 /// Creates a RecordParser<> object for the given database name,
357 /// using DumpStore<> with the given stream for the output,
358 /// and adds it to list.
359 /// Returns false if there is no known Record class for dbname.
360 bool Add(const std::string &dbname, std::ostream &os);
362 // Parser overrides
363 virtual void ParseRecord(const DBData &data, const IConverter *ic);
367 // AllRecordStore
369 /// Base class with overloaded functor behaviour for all available
370 /// record classes. To be used with AllRecordParser.
372 class BXEXPORT AllRecordStore
374 public:
375 AllRecordStore() {}
376 virtual ~AllRecordStore() {}
378 virtual void operator() (const Barry::Contact &) = 0;
379 virtual void operator() (const Barry::Message &) = 0;
380 virtual void operator() (const Barry::Calendar &) = 0;
381 virtual void operator() (const Barry::CalendarAll &) = 0;
382 virtual void operator() (const Barry::CallLog &) = 0;
383 virtual void operator() (const Barry::Bookmark &) = 0;
384 virtual void operator() (const Barry::ServiceBook &) = 0;
385 virtual void operator() (const Barry::Memo &) = 0;
386 virtual void operator() (const Barry::Task &) = 0;
387 virtual void operator() (const Barry::PINMessage &) = 0;
388 virtual void operator() (const Barry::SavedMessage &) = 0;
389 virtual void operator() (const Barry::Sms &) = 0;
390 virtual void operator() (const Barry::Folder &) = 0;
391 virtual void operator() (const Barry::Timezone &) = 0;
395 // AllRecordDumpStore
397 /// Derived from AllRecordStore, which just calls each record's
398 /// Dump() member with the given stream.
400 class BXEXPORT AllRecordDumpStore : public AllRecordStore
402 protected:
403 std::ostream &m_os;
405 public:
406 explicit AllRecordDumpStore(std::ostream &os)
407 : m_os(os)
411 virtual void operator() (const Barry::Contact &);
412 virtual void operator() (const Barry::Message &);
413 virtual void operator() (const Barry::Calendar &);
414 virtual void operator() (const Barry::CalendarAll &);
415 virtual void operator() (const Barry::CallLog &);
416 virtual void operator() (const Barry::Bookmark &);
417 virtual void operator() (const Barry::ServiceBook &);
418 virtual void operator() (const Barry::Memo &);
419 virtual void operator() (const Barry::Task &);
420 virtual void operator() (const Barry::PINMessage &);
421 virtual void operator() (const Barry::SavedMessage &);
422 virtual void operator() (const Barry::Sms &);
423 virtual void operator() (const Barry::Folder &);
424 virtual void operator() (const Barry::Timezone &);
428 // AllRecordParser
430 /// Convenience parser that creates a MultiRecordParser with all known
431 /// record parsers added. If an AllRecordStore pointer is passed in,
432 /// this class takes ownership of it, and uses it as the store object
433 /// for all the RecordParser<> objects it creates. If not, then
434 /// a custom DumpStore<> object is created with the given stream
435 /// for each RecordParser<> added.
437 /// The default parser object behaves just like MultiRecordParser
439 /// This class takes ownership of all pointers passed in.
441 class BXEXPORT AllRecordParser : public MultiRecordParser
443 AllRecordStore *m_store;
445 public:
446 // takes ownership of default_parser and store!
447 explicit AllRecordParser(std::ostream &os,
448 Parser *default_parser = 0,
449 AllRecordStore *store = 0);
450 ~AllRecordParser();
454 // TeeParser
456 /// Sends incoming DBData objects to all the parsers in its list.
457 /// This parser container does NOT own the parsers added.
459 class BXEXPORT TeeParser : public Parser
461 typedef std::vector<Parser*> parser_list_type;
463 parser_list_type m_external_parsers, m_owned_parsers;
465 public:
466 TeeParser();
467 ~TeeParser();
469 /// Adds parser to internal list, and takes ownership of the
470 /// pointer.
471 void Add(Parser *p);
473 /// Adds parser to internal list. Does NOT own the parser reference.
474 void Add(Parser &p);
476 void ParseRecord(const DBData &data, const IConverter *ic);
479 } // namespace Barry
481 #endif