- added boost::serialization to build
[barry.git] / src / builder.h
blob9d1462a1cdf617807dd5e5ff9f8aba7cd0b58ad2
1 ///
2 /// \file builder.h
3 /// Virtual protocol packet builder wrapper
4 ///
6 /*
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_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, 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. This happens on the fly as the data is sent
56 /// to the device over USB, so it should not block forever.
57 ///
58 /// Example SaveDatabase() call:
59 ///
60 /// <pre>
61 fixme - document this
62 /// struct StoreContact
63 /// {
64 /// const std::vector<Contact> &amp;array;
65 /// std::vector<Contact>::const_iterator ci;
66 /// StoreContact(const std::vector<Contact> &amp;a)
67 /// : array(a), ci(array.begin()) {}
68 /// bool operator() (Contact &amp;c)
69 /// {
70 /// if( ci != array.end() ) {
71 /// c = *ci;
72 /// ci++;
73 /// return true;
74 /// }
75 /// return false;
76 /// }
77 /// };
78 ///
79 /// Controller con(probeResult);
80 /// con.OpenMode(Controller::Desktop);
81 /// std::vector<Contact> contactList;
82 /// StoreContact storage(contactList);
83 /// RecordBuilder<Contact, StoreContact> builder(storage);
84 /// con.SaveDatabase(con.GetDBID("Address Book"), builder);
85 /// </pre>
86 ///
87 template <class Record, class Storage>
88 class RecordBuilder : public Builder
90 Storage *m_storage;
91 bool m_owned;
93 public:
94 /// Constructor that references an externally managed storage object.
95 RecordBuilder(Storage &storage)
96 : m_store(&storage), m_owned(false) {}
98 /// Constructor that references a locally managed storage object.
99 /// The pointer passed in will be stored, and freed when this class
100 /// is destroyed. It is safe to call this constructor with
101 /// a 'new'ly created storage object.
102 RecordBuilder(Storage *storage)
103 : m_store(storage), m_owned(true) {}
105 ~RecordBuilder()
107 if( this->m_owned )
108 delete m_store;
111 /// Functor member called by Controller::SaveDatabase() during
112 /// processing.
113 virtual bool operator()(Data &data, unsigned int databaseId)
115 Record rec;
116 if( !(*m_storage)(rec) )
117 return false;
118 rec.Build(data, databaseId);
119 return true;
123 } // namespace Barry
125 #endif