3 /// Virtual protocol packet builder 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_BUILDER_H__
23 #define __BARRY_BUILDER_H__
26 //#include "protocol.h"
34 /// Base class for the builder functor hierarchy.
42 virtual bool operator()(Data
&data
, size_t offset
, unsigned int databaseId
) = 0;
47 // RecordBuilder template class
49 /// Template class for easy creation of specific protocol packet builder
50 /// objects. This template takes the following template arguments:
52 /// - Record: One of the record classes in record.h
53 /// - Storage: A custom storage functor class. An object of this type
54 /// will be called as a function with empty Record as an
55 /// argument. The storage class is expected to fill the
56 /// record object in preparation for building the packet
57 /// out of that data. These calls happen on the fly as the data
58 /// is sent to the device over USB, so it should not block forever.
60 /// Example SaveDatabase() call:
66 template <class Record
, class Storage
>
67 class RecordBuilder
: public Builder
73 /// Constructor that references an externally managed storage object.
74 RecordBuilder(Storage
&storage
)
75 : m_storage(&storage
), m_owned(false) {}
77 /// Constructor that references a locally managed storage object.
78 /// The pointer passed in will be stored, and freed when this class
79 /// is destroyed. It is safe to call this constructor with
80 /// a 'new'ly created storage object.
81 RecordBuilder(Storage
*storage
)
82 : m_storage(storage
), m_owned(true) {}
90 /// Functor member called by Controller::SaveDatabase() during
92 virtual bool operator()(Data
&data
, size_t offset
, unsigned int databaseId
)
95 if( !(*m_storage
)(rec
, databaseId
) )
97 rec
.Build(data
, offset
);