3 /// Virtual parser wrapper
7 Copyright (C) 2005, 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
) { return true; }
47 virtual bool GetOperation(const Data
&data
, unsigned int &operation
);
48 virtual size_t GetHeaderSize(size_t recordsize
) const;
53 // RecordParser template class
55 /// Template class for easy creation of specific parser objects. This template
56 /// takes the following template argumens:
58 /// - Record: One of the record parser classes in record.h
59 /// - Storage: A custom storage functor class. An object of this type
60 /// will be called as a function with parsed Record as an
61 /// argument. This happens on the fly as the data is retrieved
62 /// from the device over USB, so it should not block forever.
64 /// Example LoadDatabase() call:
67 /// struct StoreContact
69 /// std::vector<Contact> &array;
70 /// StoreContact(std::vector<Contact> &a) : array(a) {}
71 /// void operator() (const Contact &c)
73 /// array.push_back(c);
77 /// Controller con(probeResult);
78 /// con.OpenMode(Controller::Desktop);
79 /// std::vector<Contact> contactList;
80 /// StoreContact storage(contactList);
81 /// RecordParser<Contact, StoreContact> parser(storage);
82 /// con.LoadDatabase(con.GetDBID("Address Book"), parser);
85 template <class Record
, class Storage
>
86 class RecordParser
: public Parser
92 /// Constructor that references an externally managed storage object.
93 RecordParser(Storage
&storage
)
94 : m_store(&storage
), m_owned(false) {}
96 /// Constructor that references a locally managed storage object.
97 /// The pointer passed in will be stored, and freed when this class
98 /// is destroyed. It is safe to call this constructor with
99 /// a 'new'ly created storage object.
100 RecordParser(Storage
*storage
)
101 : m_store(storage
), m_owned(true) {}
109 virtual bool CheckHeaderSize(const Data
&data
, unsigned int operation
)
114 case SB_DBOP_GET_RECORDS
:
115 // using the new protocol
116 recordsize
= Record::GetProtocolRecordSize();
119 case SB_DBOP_OLD_GET_RECORDS_REPLY
:
120 // using the old protocol
121 recordsize
= Record::GetOldProtocolRecordSize();
126 dout("Unknown protocol");
130 // return true if header is ok
131 return (size_t)data
.GetSize() > GetHeaderSize(recordsize
);
134 /// Functor member called by Controller::LoadDatabase() during
136 virtual bool operator()(const Data
&data
)
138 unsigned int operation
;
139 if( !GetOperation(data
, operation
) )
141 if( !CheckHeaderSize(data
, operation
) )
145 rec
.Parse(data
, operation
);