Release tarball for barry-0.9
[barry.git] / src / controllertmpl.h
blobc38639cf9c34b78af641f35718bc8370a622d7a3
1 ///
2 /// \file controllertmpl.h
3 /// Ease of use templates for the controller class
4 ///
6 /*
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_CONTROLLERTMPL_H__
23 #define __BARRY_CONTROLLERTMPL_H__
25 #include "controller.h"
26 #include "parser.h"
27 #include "builder.h"
29 namespace Barry {
31 void LoadDatabase(unsigned int dbId, Parser &parser);
32 void SaveDatabase(unsigned int dbId, Builder &builder);
34 template <class RecordT, class StorageT>
35 void Controller::LoadDatabaseByType(StorageT &store)
37 unsigned int dbId = this->GetDBID( RecordT::GetDBName() );
38 Barry::RecordParser<RecordT, StorageT> parser(store);
39 this->LoadDatabase(dbId, parser);
42 template <class RecordT, class StorageT>
43 void Controller::SaveDatabaseByType(StorageT &store)
45 unsigned int dbId = this->GetDBID( RecordT::GetDBName() );
46 Barry::RecordBuilder<RecordT, StorageT> build(store);
47 SaveDatabase(dbId, build);
50 template <class StorageT>
51 void Controller::LoadDatabaseByName(const std::string &name, StorageT &store)
53 if( name == Contact::GetDBName() )
54 LoadDatabaseByType<Contact>(store);
55 else if( name == Message::GetDBName() )
56 LoadDatabaseByType<Message>(store);
57 else if( name == Calendar::GetDBName() )
58 LoadDatabaseByType<Calendar>(store);
59 else
60 throw Error("Unknown database name in LoadDatabaseByName: " + name);
63 template <class StorageT>
64 void Controller::SaveDatabaseByName(const std::string &name, StorageT &store)
66 if( name == Contact::GetDBName() )
67 SaveDatabaseByType<Contact>(store);
68 else if( name == Message::GetDBName() )
69 SaveDatabaseByType<Message>(store);
70 else if( name == Calendar::GetDBName() )
71 SaveDatabaseByType<Calendar>(store);
72 else
73 throw Error("Unknown database name in SaveDatabaseByName: " + name);
76 template <class RecordT>
77 void Controller::AddRecordByType(uint32_t recordId, const RecordT &rec)
79 unsigned int dbId = this->GetDBID( RecordT::GetDBName() );
80 // FIXME - I know this is a convenience template, but it still
81 // hurts making a temporary copy just to set the record ID...
82 // create a better API for this.
83 RecordT r = rec;
84 r.SetIds(RecordT::GetDefaultRecType(), recordId);
85 Barry::RecordFetch<RecordT> fetch(r);
86 Barry::RecordBuilder<RecordT, Barry::RecordFetch<RecordT> > build(fetch);
87 this->AddRecord(dbId, build);
91 } // namespace Barry
93 #endif