3 /// Virtual parser wrapper
7 Copyright (C) 2005-2012, 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__
28 #include <stdint.h> // for uint32_t
32 // forward declarations
54 // This macro can be used to automatically generate code for all known
55 // record types. Just #undef HANDLE_PARSER, then #define it to whatever
56 // you need, then use ALL_KNOWN_PARSER_TYPES. See parser.cc for
59 // These are sorted so their GetDBName()'s will display in alphabetical order.
61 #define ALL_KNOWN_PARSER_TYPES \
62 HANDLE_PARSER(Contact) \
63 HANDLE_PARSER(Bookmark) \
64 HANDLE_PARSER(Calendar) \
65 HANDLE_PARSER(CalendarAll) \
66 HANDLE_PARSER(ContentStore) \
67 HANDLE_PARSER(Folder) \
68 HANDLE_PARSER(HandheldAgent) \
70 HANDLE_PARSER(Message) \
71 HANDLE_PARSER(CallLog) \
72 HANDLE_PARSER(PINMessage) \
73 HANDLE_PARSER(SavedMessage) \
74 HANDLE_PARSER(ServiceBook) \
77 HANDLE_PARSER(TimeZone)
84 /// Base class for the parser hierarchy.
86 /// This class provides the interface that the Controller class uses
87 /// to pass raw data it reads from the device. The Controller, along
88 /// with the Packet class, calls each of the virtual functions below
89 /// in the same order.
91 /// This class is kept as a pure abstract class, in order to make sure
92 /// that the compiler will catch any API changes, for code derived
101 /// Called to parse sub fields in the raw data packet.
102 virtual void ParseRecord(const DBData
&data
, const IConverter
*ic
) = 0;
109 /// If in debug mode, this class can be used as a null parser.
110 /// Call Init() and the protocol will be dumped to stdout and
111 /// no parsing will be done.
113 /// Do NOT derive your own personal parser classes from this,
114 /// unless you are perfectly confident that you will catch
115 /// future API changes on the devel tree without the compiler's
118 class BXEXPORT NullParser
: public Parser
122 virtual ~NullParser() {}
124 virtual void ParseRecord(const DBData
&data
, const IConverter
*ic
) {}
130 /// Dumps raw hex of the given DBData to the given stream.
132 /// Do NOT derive your own personal parser classes from this,
133 /// unless you are perfectly confident that you will catch
134 /// future API changes on the devel tree without the compiler's
137 class BXEXPORT HexDumpParser
: public Parser
140 std::string m_last_dbname
;
143 explicit HexDumpParser(std::ostream
&os
);
145 virtual void ParseRecord(const Barry::DBData
&data
,
146 const IConverter
*ic
);
152 /// Abstract base class for the following RecordParser template, that exposes
153 /// some information on the specifics that the record parser can handle.
154 /// Specifically, it exposes the database name it is able to parse
156 class BXEXPORT RecordParserBase
: public Parser
159 // These functions are always valid, regardless of the
160 // state of the parser.
161 virtual const char * GetDBName() const = 0;
162 virtual uint8_t GetDefaultRecType() const = 0;
164 // These functions depend on the parser having just parsed
165 // a record successfully.
166 virtual bool IsRecordValid() const = 0;
167 virtual uint8_t GetRecType() const = 0;
168 virtual uint32_t GetUniqueId() const = 0;
169 virtual void Dump(std::ostream
&os
) const = 0;
174 // Note: Store classes take parsed Record objects as a functor.
175 // Parser classes deal with raw data, while Store classes deal with
176 // parsed Record objects.
182 /// A Storage class for RecordParser<> that does nothing, for the cases
183 /// where you only want to dump parsed record data to a stream.
185 template <class RecordT
>
189 void operator() (const RecordT
&r
)
197 /// A Storage class for RecordParser<> that dumps the parsed record data
198 /// to the given stream.
200 template <class RecordT
>
206 explicit DumpStore(std::ostream
&os
)
211 void operator() (const RecordT
&r
)
220 /// A Storage class for RecordParser that stores a copy of the parsed record.
222 template <class RecordT
>
228 void operator() (const RecordT
&r
)
237 /// Contains the proper way to convert a DBData object into a record.
239 template <class RecordT
>
240 void ParseDBData(const DBData
&data
, RecordT
&rec
, const IConverter
*ic
)
246 rec
.SetIds(data
.GetRecType(), data
.GetUniqueId());
247 size_t offset
= data
.GetOffset();
248 rec
.ParseHeader(data
.GetData(), offset
);
249 rec
.ParseFields(data
.GetData(), offset
, ic
);
253 // RecordParser template class
255 /// Template class for easy creation of specific parser objects. This template
256 /// takes the following template arguments:
258 /// - RecordT: One of the record parser classes in record.h
259 /// - StorageT: A custom storage functor class. An object of this type
260 /// will be called as a function with parsed Record as an
261 /// argument. This happens on the fly as the data is retrieved
262 /// from the device over USB, so it should not block forever.
264 /// Example LoadDatabase() call:
267 /// struct StoreContact
269 /// std::vector<Contact> &array;
270 /// StoreContact(std::vector<Contact> &a) : array(a) {}
271 /// void operator() (const Contact &c)
273 /// array.push_back(c);
277 /// Controller con(probeResult);
278 /// con.OpenMode(Controller::Desktop);
279 /// std::vector<Contact> contactList;
280 /// StoreContact storage(contactList);
281 /// RecordParser<Contact, StoreContact> parser(storage);
282 /// con.LoadDatabase(con.GetDBID("Address Book"), parser);
285 template <class RecordT
, class StorageT
>
286 class RecordParser
: public RecordParserBase
294 /// Constructor that references an externally managed storage object.
295 RecordParser(StorageT
&storage
)
298 , m_record_valid(false)
302 /// Constructor that references a locally managed storage object.
303 /// The pointer passed in will be stored, and freed when this class
304 /// is destroyed. It is safe to call this constructor with
305 /// a 'new'ly created storage object.
306 RecordParser(StorageT
*storage
= 0)
309 , m_record_valid(false)
319 virtual StorageT
* GetStore()
324 virtual const StorageT
* GetStore() const
329 virtual void ParseRecord(const DBData
&data
, const IConverter
*ic
)
331 m_record_valid
= false;
332 ParseDBData(data
, m_rec
, ic
);
333 m_record_valid
= true;
340 // RecordParserBase overrides
343 // These functions are always valid, regardless of the
344 // state of the parser.
345 virtual const char * GetDBName() const
347 return RecordT::GetDBName();
350 virtual uint8_t GetDefaultRecType() const
352 return RecordT::GetDefaultRecType();
355 // These functions depend on the parser having just parsed
356 // a record successfully.
357 virtual bool IsRecordValid() const
359 return m_record_valid
;
362 virtual const RecordT
& GetRecord() const
367 virtual uint8_t GetRecType() const
369 return m_rec
.GetRecType();
372 virtual uint32_t GetUniqueId() const
374 return m_rec
.GetUniqueId();
377 virtual void Dump(std::ostream
&os
) const
386 /// Base class with overloaded functor behaviour for all available
387 /// record classes. To be used with AllRecordParser.
389 class BXEXPORT AllRecordStore
393 virtual ~AllRecordStore() {}
396 #define HANDLE_PARSER(tname) \
397 virtual void operator() (const Barry::tname &) = 0;
399 ALL_KNOWN_PARSER_TYPES
405 /// Container parser class that accepts multiple Parser objects
406 /// (often RecordParser<> objects but they don't have to be) and
407 /// automatically routes incoming records to the appropriate parser.
408 /// Note that this container owns *all* Parser objects, and will
409 /// free them upon destruction.
411 /// Incoming records that have no matching parser are passed to the
412 /// default parser object, if one exists, otherwise they are dropped
413 /// silently. The default parser object is also owned by the container,
414 /// and will be freed on destruction.
416 /// Do NOT derive your own personal parser classes from this,
417 /// unless you are perfectly confident that you will catch
418 /// future API changes on the devel tree without the compiler's
421 class BXEXPORT MultiRecordParser
: public Parser
423 typedef std::map
<std::string
, Parser
*> map_type
;
425 Parser
*m_delete_default
; // if set, will be freed
426 Parser
*m_default
; // used by all code for actual work
427 // and may or may not be "owned" by us
431 // takes ownership of default_parser!
432 explicit MultiRecordParser(Parser
*default_parser
= 0);
434 // does not take ownership of the default_parser
435 explicit MultiRecordParser(Parser
&default_parser
);
437 ~MultiRecordParser();
439 /// Adds given parser to list and takes ownership of it
440 void Add(const std::string
&dbname
, Parser
*parser
);
442 /// Adds given parser to list and takes ownership of it
443 void Add(RecordParserBase
*parser
);
445 /// Creates a RecordParser<> object using the given record
446 /// type and AllRecordStore. Does NOT take ownership of the
447 /// store object, since it can be used multiple times for
448 /// multiple records.
449 template <class RecordT
>
450 void Add(AllRecordStore
&store
)
452 Add( RecordT::GetDBName(),
453 new RecordParser
<RecordT
, AllRecordStore
>(store
) );
456 /// Two helper template functions that create the RecordParser<>
457 /// automatically based on the function call. Both pointer and
458 /// reference versions.
459 template <class RecordT
, class StorageT
>
460 void Add(StorageT
*store
)
462 Add( RecordT::GetDBName(),
463 new RecordParser
<RecordT
, StorageT
>(store
) );
466 template <class RecordT
, class StorageT
>
467 void Add(StorageT
&store
)
469 Add( RecordT::GetDBName(),
470 new RecordParser
<RecordT
, StorageT
>(store
) );
473 /// Creates a RecordParser<> object for the given database name,
474 /// using DumpStore<> with the given stream for the output,
475 /// and adds it to list.
476 /// Returns false if there is no known Record class for dbname.
477 bool Add(const std::string
&dbname
, std::ostream
&os
);
479 /// Creates a RecordParser<> object for the given database name,
480 /// using the given store object.
481 /// Returns false if there is no known Record class for dbname.
482 bool Add(const std::string
&dbname
, AllRecordStore
&store
);
485 virtual void ParseRecord(const DBData
&data
, const IConverter
*ic
);
489 // AllRecordDumpStore
491 /// Derived from AllRecordStore, which just calls each record's
492 /// Dump() member with the given stream.
494 class BXEXPORT AllRecordDumpStore
: public AllRecordStore
500 explicit AllRecordDumpStore(std::ostream
&os
)
506 #define HANDLE_PARSER(tname) \
507 virtual void operator() (const Barry::tname &);
509 ALL_KNOWN_PARSER_TYPES
515 /// Convenience parser that creates a MultiRecordParser with all known
516 /// record parsers added. If an AllRecordStore pointer is passed in,
517 /// this class takes ownership of it, and uses it as the store object
518 /// for all the RecordParser<> objects it creates. If not, then
519 /// a custom DumpStore<> object is created with the given stream
520 /// for each RecordParser<> added.
522 /// The default parser object behaves just like MultiRecordParser
524 /// This class takes ownership of all pointers passed in.
526 class BXEXPORT AllRecordParser
: public MultiRecordParser
528 AllRecordStore
*m_store
;
531 // does not take ownership of store, by itself,
532 // but the constructor that calls it might
533 void AddRecords(std::ostream
*os
, AllRecordStore
*store
);
536 // takes ownership of default_parser and store!
537 explicit AllRecordParser(std::ostream
&os
,
538 Parser
*default_parser
= 0,
539 AllRecordStore
*store
= 0);
541 // does not take ownership of default_parser or store
542 AllRecordParser(Parser
&default_parser
, AllRecordStore
&store
);
550 /// Sends incoming DBData objects to all the parsers in its list.
551 /// This parser container does NOT own the parsers added.
553 class BXEXPORT TeeParser
: public Parser
555 typedef std::vector
<Parser
*> parser_list_type
;
557 parser_list_type m_external_parsers
, m_owned_parsers
;
563 /// Adds parser to internal list, and takes ownership of the
567 /// Adds parser to internal list. Does NOT own the parser reference.
570 void ParseRecord(const DBData
&data
, const IConverter
*ic
);