3 /// Virtual parser wrapper
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__
28 #include <stdint.h> // for uint32_t
32 // forward declarations
56 /// Base class for the parser hierarchy.
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.
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
73 /// Called to parse sub fields in the raw data packet.
74 virtual void ParseRecord(const DBData
&data
, const IConverter
*ic
) = 0;
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.
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
90 class BXEXPORT NullParser
: public Parser
94 virtual ~NullParser() {}
96 virtual void ParseRecord(const DBData
&data
, const IConverter
*ic
) {}
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
109 class BXEXPORT HexDumpParser
: public Parser
114 explicit HexDumpParser(std::ostream
&os
);
116 virtual void ParseRecord(const Barry::DBData
&data
,
117 const IConverter
*ic
);
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
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.
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
>
160 void operator() (const RecordT
&r
)
168 /// A Storage class for RecordParser<> that dumps the parsed record data
169 /// to the given stream.
171 template <class RecordT
>
177 explicit DumpStore(std::ostream
&os
)
182 void operator() (const RecordT
&r
)
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:
203 /// struct StoreContact
205 /// std::vector<Contact> &array;
206 /// StoreContact(std::vector<Contact> &a) : array(a) {}
207 /// void operator() (const Contact &c)
209 /// array.push_back(c);
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);
221 template <class RecordT
, class StorageT
>
222 class RecordParser
: public RecordParserBase
230 /// Constructor that references an externally managed storage object.
231 RecordParser(StorageT
&storage
)
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)
245 , m_record_valid(false)
255 virtual StorageT
* GetStore()
260 virtual const StorageT
* GetStore() const
265 virtual void ParseRecord(const DBData
&data
, const IConverter
*ic
)
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;
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
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
338 class BXEXPORT MultiRecordParser
: public Parser
340 typedef std::map
<std::string
, Parser
*> map_type
;
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
);
363 virtual void ParseRecord(const DBData
&data
, const IConverter
*ic
);
369 /// Base class with overloaded functor behaviour for all available
370 /// record classes. To be used with AllRecordParser.
372 class BXEXPORT 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
405 explicit AllRecordDumpStore(std::ostream
&os
)
410 virtual void operator() (const Barry::Contact
&);
411 virtual void operator() (const Barry::Message
&);
412 virtual void operator() (const Barry::Calendar
&);
413 virtual void operator() (const Barry::CalendarAll
&);
414 virtual void operator() (const Barry::CallLog
&);
415 virtual void operator() (const Barry::Bookmark
&);
416 virtual void operator() (const Barry::ServiceBook
&);
417 virtual void operator() (const Barry::Memo
&);
418 virtual void operator() (const Barry::Task
&);
419 virtual void operator() (const Barry::PINMessage
&);
420 virtual void operator() (const Barry::SavedMessage
&);
421 virtual void operator() (const Barry::Sms
&);
422 virtual void operator() (const Barry::Folder
&);
423 virtual void operator() (const Barry::Timezone
&);
429 /// Convenience parser that creates a MultiRecordParser with all known
430 /// record parsers added. If an AllRecordStore pointer is passed in,
431 /// this class takes ownership of it, and uses it as the store object
432 /// for all the RecordParser<> objects it creates. If not, then
433 /// a custom DumpStore<> object is created with the given stream
434 /// for each RecordParser<> added.
436 /// The default parser object behaves just like MultiRecordParser
438 /// This class takes ownership of all pointers passed in.
440 class BXEXPORT AllRecordParser
: public MultiRecordParser
442 AllRecordStore
*m_store
;
445 // takes ownership of default_parser and store!
446 explicit AllRecordParser(std::ostream
&os
,
447 Parser
*default_parser
= 0,
448 AllRecordStore
*store
= 0);
455 /// Sends incoming DBData objects to all the parsers in its list.
456 /// This parser container does NOT own the parsers added.
458 class BXEXPORT TeeParser
: public Parser
460 typedef std::vector
<Parser
*> parser_list_type
;
462 parser_list_type m_external_parsers
, m_owned_parsers
;
468 /// Adds parser to internal list, and takes ownership of the
472 /// Adds parser to internal list. Does NOT own the parser reference.
475 void ParseRecord(const DBData
&data
, const IConverter
*ic
);