m_desktoptmpl.h

Go to the documentation of this file.
00001 ///
00002 /// \file       m_desktoptmpl.h
00003 ///             Ease of use templates for the Desktop mode class
00004 ///
00005 
00006 /*
00007     Copyright (C) 2005-2012, Net Direct Inc. (http://www.netdirect.ca/)
00008 
00009     This program is free software; you can redistribute it and/or modify
00010     it under the terms of the GNU General Public License as published by
00011     the Free Software Foundation; either version 2 of the License, or
00012     (at your option) any later version.
00013 
00014     This program is distributed in the hope that it will be useful,
00015     but WITHOUT ANY WARRANTY; without even the implied warranty of
00016     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00017 
00018     See the GNU General Public License in the COPYING file at the
00019     root directory of this project for more details.
00020 */
00021 
00022 #ifndef __BARRY_M_DESKTOPTMPL_H__
00023 #define __BARRY_M_DESKTOPTMPL_H__
00024 
00025 #include "dll.h"
00026 #include "m_desktop.h"
00027 #include "parser.h"
00028 #include "builder.h"
00029 
00030 namespace Barry { namespace Mode {
00031 
00032 BXEXPORT void LoadDatabase(unsigned int dbId, Parser &parser);
00033 BXEXPORT void SaveDatabase(unsigned int dbId, Builder &builder);
00034 
00035 template <class RecordT, class StorageT>
00036 void Desktop::LoadDatabaseByType(StorageT &store)
00037 {
00038         unsigned int dbId = this->GetDBID( RecordT::GetDBName() );
00039         Barry::RecordParser<RecordT, StorageT> parser(store);
00040         this->LoadDatabase(dbId, parser);
00041 }
00042 
00043 template <class RecordT, class StorageT>
00044 void Desktop::SaveDatabaseByType(StorageT &store)
00045 {
00046         unsigned int dbId = this->GetDBID( RecordT::GetDBName() );
00047         Barry::RecordBuilder<RecordT, StorageT> build(store);
00048         SaveDatabase(dbId, build);
00049 }
00050 
00051 template <class StorageT>
00052 void Desktop::LoadDatabaseByName(const std::string &name, StorageT &store)
00053 {
00054 #undef HANDLE_PARSER
00055 #define HANDLE_PARSER(rtype) \
00056         else if( name == Barry::rtype::GetDBName() ) \
00057                 LoadDatabaseByType<rtype>(store);
00058 
00059         if( !name.size() )
00060                 throw Error("Empty database name in LoadDatabaseByName");
00061         ALL_KNOWN_PARSER_TYPES
00062         else
00063                 throw Error("Unknown database name in LoadDatabaseByName: " + name);
00064 }
00065 
00066 template <class StorageT>
00067 void Desktop::SaveDatabaseByName(const std::string &name, StorageT &store)
00068 {
00069 #undef HANDLE_BUILDER
00070 #define HANDLE_BUILDER(rtype) \
00071         else if( name == Barry::rtype::GetDBName() ) \
00072                 SaveDatabaseByType<rtype>(store);
00073 
00074         if( !name.size() )
00075                 throw Error("Empty database name in SaveDatabaseByName");
00076         ALL_KNOWN_BUILDER_TYPES
00077         else
00078                 throw Error("Unknown database name in SaveDatabaseByName: " + name);
00079 }
00080 
00081 template <class RecordT>
00082 void Desktop::AddRecordByType(uint32_t recordId, const RecordT &rec)
00083 {
00084         unsigned int dbId = this->GetDBID( RecordT::GetDBName() );
00085         // FIXME - I know this is a convenience template, but it still
00086         // hurts making a temporary copy just to set the record ID...
00087         // create a better API for this.
00088         RecordT r = rec;
00089         r.SetIds(RecordT::GetDefaultRecType(), recordId);
00090         Barry::RecordFetch<RecordT> fetch(r);
00091         Barry::RecordBuilder<RecordT, Barry::RecordFetch<RecordT> > build(fetch);
00092         this->AddRecord(dbId, build);
00093 }
00094 
00095 
00096 }} // namespace Barry::Mode
00097 
00098 #endif
00099