3 /// Virtual protocol packet builder wrapper
7 Copyright (C) 2005-2007, 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.
36 /// This defines the API used by the Controller and Packet classes
37 /// for building a raw device record to write to the device.
45 /// Called first in the sequence, to allow the application to
46 /// load the needed data from memory, disk, etc. If successful,
47 /// return true. If at the end of the series, return false.
48 virtual bool Retrieve(unsigned int databaseId
) = 0;
50 /// Called to retrive the unique ID for this record.
51 virtual uint8_t GetRecType() const = 0;
52 virtual uint32_t GetUniqueId() const = 0;
54 /// Called before BuildFields() in order to build the header
55 /// for this record. Store the raw data in data, at the
56 /// offset given in offset. When finished, update offset to
57 /// point to the next spot to put new data.
58 virtual void BuildHeader(Data
&data
, size_t &offset
) = 0;
60 /// Called to build the record field data. Store the raw data
61 /// in data, using offset to know where to write. Be sure to
62 /// update offset, and be sure to adjust the size of the data
63 /// packet (possibly with Data::ReleaseBuffer()).
64 virtual void BuildFields(Data
&data
, size_t &offset
) = 0;
69 // RecordBuilder template class
71 /// Template class for easy creation of specific protocol packet builder
72 /// objects. This template takes the following template arguments:
74 /// - RecordT: One of the record classes in record.h
75 /// - StorageT: A custom storage functor class. An object of this type
76 /// will be called as a function with empty Record as an
77 /// argument. The storage class is expected to fill the
78 /// record object in preparation for building the packet
79 /// out of that data. These calls happen on the fly as the data
80 /// is sent to the device over USB, so it should not block forever.
82 /// Example SaveDatabase() call:
88 template <class RecordT
, class StorageT
>
89 class RecordBuilder
: public Builder
96 /// Constructor that references an externally managed storage object.
97 RecordBuilder(StorageT
&storage
)
98 : m_storage(&storage
), m_owned(false) {}
100 /// Constructor that references a locally managed storage object.
101 /// The pointer passed in will be stored, and freed when this class
102 /// is destroyed. It is safe to call this constructor with
103 /// a 'new'ly created storage object.
104 RecordBuilder(StorageT
*storage
)
105 : m_storage(storage
), m_owned(true) {}
113 virtual bool Retrieve(unsigned int databaseId
)
115 return (*m_storage
)(m_rec
, databaseId
);
118 virtual uint8_t GetRecType() const
120 return m_rec
.GetRecType();
123 virtual uint32_t GetUniqueId() const
125 return m_rec
.GetUniqueId();
128 /// Functor member called by Controller::SaveDatabase() during
130 virtual void BuildHeader(Data
&data
, size_t &offset
)
132 m_rec
.BuildHeader(data
, offset
);
135 virtual void BuildFields(Data
&data
, size_t &offset
)
137 m_rec
.BuildFields(data
, offset
);
143 // RecordFetch template class
145 /// Generic record fetch class, to help with using records without
148 template <class RecordT
>
151 const RecordT
&m_rec
;
155 RecordFetch(const RecordT
&rec
) : m_rec(rec
), m_done(false) {}
156 bool operator()(RecordT
&rec
, unsigned int dbId
) const