3 /// Virtual parser wrapper
7 Copyright (C) 2005-2006, 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__
31 // also acts as a null parser
35 /// Base class for the parser functor hierarchy. If in debug mode, this
36 /// class can be used as a null parser. Call Init() and the protocol
37 /// will be dumped to stdout and no parsing will be done.
45 virtual bool operator()(const Data
&data
, size_t offset
) { return true; }
47 virtual bool GetOperation(const Data
&data
, unsigned int &operation
);
52 // RecordParser template class
54 /// Template class for easy creation of specific parser objects. This template
55 /// takes the following template arguments:
57 /// - Record: One of the record parser classes in record.h
58 /// - Storage: A custom storage functor class. An object of this type
59 /// will be called as a function with parsed Record as an
60 /// argument. This happens on the fly as the data is retrieved
61 /// from the device over USB, so it should not block forever.
63 /// Example LoadDatabase() call:
66 /// struct StoreContact
68 /// std::vector<Contact> &array;
69 /// StoreContact(std::vector<Contact> &a) : array(a) {}
70 /// void operator() (const Contact &c)
72 /// array.push_back(c);
76 /// Controller con(probeResult);
77 /// con.OpenMode(Controller::Desktop);
78 /// std::vector<Contact> contactList;
79 /// StoreContact storage(contactList);
80 /// RecordParser<Contact, StoreContact> parser(storage);
81 /// con.LoadDatabase(con.GetDBID("Address Book"), parser);
84 template <class Record
, class Storage
>
85 class RecordParser
: public Parser
91 /// Constructor that references an externally managed storage object.
92 RecordParser(Storage
&storage
)
93 : m_store(&storage
), m_owned(false) {}
95 /// Constructor that references a locally managed storage object.
96 /// The pointer passed in will be stored, and freed when this class
97 /// is destroyed. It is safe to call this constructor with
98 /// a 'new'ly created storage object.
99 RecordParser(Storage
*storage
)
100 : m_store(storage
), m_owned(true) {}
108 virtual bool CheckHeaderSize(const Data
&data
, size_t offset
, unsigned int operation
)
113 case SB_DBOP_GET_RECORDS
:
114 // using the new protocol
115 recordsize
= Record::GetProtocolRecordSize();
118 case SB_DBOP_OLD_GET_RECORDS_REPLY
:
119 // using the old protocol
120 recordsize
= Record::GetOldProtocolRecordSize();
125 dout("Unknown protocol");
129 // return true if header is ok
130 return data
.GetSize() > (offset
+ recordsize
);
133 /// Functor member called by Controller::LoadDatabase() during
135 virtual bool operator()(const Data
&data
, size_t offset
)
137 unsigned int operation
;
138 if( !GetOperation(data
, operation
) )
140 if( !CheckHeaderSize(data
, offset
, operation
) )
144 rec
.Parse(data
, offset
, operation
);