3 /// Virtual protocol packet builder 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_BUILDER_H__
23 #define __BARRY_BUILDER_H__
29 // forward declarations
35 /// Base class for the builder functor hierarchy.
37 /// This defines the API used by the Controller and Packet classes
38 /// for building a raw device record to write to the device.
40 class BXEXPORT Builder
46 /// Called first in the sequence, to allow the application to
47 /// load the needed data from memory, disk, etc. If successful,
48 /// return true. If at the end of the series, return false.
49 virtual bool Retrieve(unsigned int databaseId
) = 0;
51 /// Called to retrive the unique ID for this record.
52 virtual uint8_t GetRecType() const = 0;
53 virtual uint32_t GetUniqueId() const = 0;
55 /// Called before BuildFields() in order to build the header
56 /// for this record. Store the raw data in data, at the
57 /// offset given in offset. When finished, update offset to
58 /// point to the next spot to put new data.
59 virtual void BuildHeader(Data
&data
, size_t &offset
) = 0;
61 /// Called to build the record field data. Store the raw data
62 /// in data, using offset to know where to write. Be sure to
63 /// update offset, and be sure to adjust the size of the data
64 /// packet (possibly with Data::ReleaseBuffer()).
65 virtual void BuildFields(Data
&data
, size_t &offset
,
66 const IConverter
*ic
) = 0;
71 // RecordBuilder template class
73 /// Template class for easy creation of specific protocol packet builder
74 /// objects. This template takes the following template arguments:
76 /// - RecordT: One of the record classes in record.h
77 /// - StorageT: A custom storage functor class. An object of this type
78 /// will be called as a function with empty Record as an
79 /// argument. The storage class is expected to fill the
80 /// record object in preparation for building the packet
81 /// out of that data. These calls happen on the fly as the data
82 /// is sent to the device over USB, so it should not block forever.
84 /// Example SaveDatabase() call:
90 template <class RecordT
, class StorageT
>
91 class RecordBuilder
: public Builder
98 /// Constructor that references an externally managed storage object.
99 RecordBuilder(StorageT
&storage
)
100 : m_storage(&storage
), m_owned(false) {}
102 /// Constructor that references a locally managed storage object.
103 /// The pointer passed in will be stored, and freed when this class
104 /// is destroyed. It is safe to call this constructor with
105 /// a 'new'ly created storage object.
106 RecordBuilder(StorageT
*storage
)
107 : m_storage(storage
), m_owned(true) {}
115 virtual bool Retrieve(unsigned int databaseId
)
117 return (*m_storage
)(m_rec
, databaseId
);
120 virtual uint8_t GetRecType() const
122 return m_rec
.GetRecType();
125 virtual uint32_t GetUniqueId() const
127 return m_rec
.GetUniqueId();
130 /// Functor member called by Controller::SaveDatabase() during
132 virtual void BuildHeader(Data
&data
, size_t &offset
)
134 m_rec
.BuildHeader(data
, offset
);
137 virtual void BuildFields(Data
&data
, size_t &offset
, const IConverter
*ic
)
139 m_rec
.BuildFields(data
, offset
, ic
);
145 // RecordFetch template class
147 /// Generic record fetch class, to help with using records without
150 template <class RecordT
>
153 const RecordT
&m_rec
;
157 RecordFetch(const RecordT
&rec
) : m_rec(rec
), m_done(false) {}
158 bool operator()(RecordT
&rec
, unsigned int dbId
) const