- added src/endian.h... still need to add configure support to
[barry.git] / src / builder.h
blob9bd2ae7bae9b10d6273239190cfd63b4d762d40b
1 ///
2 /// \file builder.h
3 /// Virtual protocol packet builder wrapper
4 ///
6 /*
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__
25 //#include "data.h"
26 //#include "protocol.h"
27 //#include "debug.h"
29 namespace Barry {
32 // Builder class
34 /// Base class for the builder functor hierarchy.
35 ///
36 class Builder
38 public:
39 Builder() {}
40 virtual ~Builder() {}
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:
51 ///
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.
59 ///
60 /// Example SaveDatabase() call:
61 ///
62 /// <pre>
63 /// FIXME
64 /// </pre>
65 ///
66 template <class Record, class Storage>
67 class RecordBuilder : public Builder
69 Storage *m_storage;
70 bool m_owned;
72 public:
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) {}
84 ~RecordBuilder()
86 if( this->m_owned )
87 delete m_storage;
90 /// Functor member called by Controller::SaveDatabase() during
91 /// processing.
92 virtual bool operator()(Data &data, size_t offset, unsigned int databaseId)
94 Record rec;
95 if( !(*m_storage)(rec, databaseId) )
96 return false;
97 rec.Build(data, offset);
98 return true;
102 } // namespace Barry
104 #endif