menu: added new Keywords tag to .desktop files
[barry.git] / src / m_desktoptmpl.h
blobebc291ebabc1aa710e04820332caac177f30424e
1 ///
2 /// \file m_desktoptmpl.h
3 /// Ease of use templates for the Desktop mode class
4 ///
6 /*
7 Copyright (C) 2005-2013, 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_M_DESKTOPTMPL_H__
23 #define __BARRY_M_DESKTOPTMPL_H__
25 #include "dll.h"
26 #include "m_desktop.h"
27 #include "parser.h"
28 #include "builder.h"
30 namespace Barry { namespace Mode {
32 BXEXPORT void LoadDatabase(unsigned int dbId, Parser &parser);
33 BXEXPORT void SaveDatabase(unsigned int dbId, Builder &builder);
35 template <class RecordT, class StorageT>
36 void Desktop::LoadDatabaseByType(StorageT &store)
38 unsigned int dbId = this->GetDBID( RecordT::GetDBName() );
39 Barry::RecordParser<RecordT, StorageT> parser(store);
40 this->LoadDatabase(dbId, parser);
43 template <class RecordT, class StorageT>
44 void Desktop::SaveDatabaseByType(StorageT &store)
46 unsigned int dbId = this->GetDBID( RecordT::GetDBName() );
47 Barry::RecordBuilder<RecordT, StorageT> build(store);
48 SaveDatabase(dbId, build);
51 template <class StorageT>
52 void Desktop::LoadDatabaseByName(const std::string &name, StorageT &store)
54 #undef HANDLE_PARSER
55 #define HANDLE_PARSER(rtype) \
56 else if( name == Barry::rtype::GetDBName() ) \
57 LoadDatabaseByType<rtype>(store);
59 if( !name.size() )
60 throw Error("Empty database name in LoadDatabaseByName");
61 ALL_KNOWN_PARSER_TYPES
62 else
63 throw Error("Unknown database name in LoadDatabaseByName: " + name);
66 template <class StorageT>
67 void Desktop::SaveDatabaseByName(const std::string &name, StorageT &store)
69 #undef HANDLE_BUILDER
70 #define HANDLE_BUILDER(rtype) \
71 else if( name == Barry::rtype::GetDBName() ) \
72 SaveDatabaseByType<rtype>(store);
74 if( !name.size() )
75 throw Error("Empty database name in SaveDatabaseByName");
76 ALL_KNOWN_BUILDER_TYPES
77 else
78 throw Error("Unknown database name in SaveDatabaseByName: " + name);
81 template <class RecordT>
82 void Desktop::AddRecordByType(uint32_t recordId, const RecordT &rec)
84 unsigned int dbId = this->GetDBID( RecordT::GetDBName() );
85 // FIXME - I know this is a convenience template, but it still
86 // hurts making a temporary copy just to set the record ID...
87 // create a better API for this.
88 RecordT r = rec;
89 r.SetIds(RecordT::GetDefaultRecType(), recordId);
90 Barry::RecordFetch<RecordT> fetch(r);
91 Barry::RecordBuilder<RecordT, Barry::RecordFetch<RecordT> > build(fetch);
92 this->AddRecord(dbId, build);
96 }} // namespace Barry::Mode
98 #endif