boostwrap.cc

Go to the documentation of this file.
00001 ///
00002 /// \file       boostwrap.cc
00003 ///             Wrap Boost serialization into its own lib, so each usage
00004 ///             in the tools doesn't cause a compile speed meltdown.
00005 ///
00006 
00007 /*
00008     Copyright (C) 2012, Net Direct Inc. (http://www.netdirect.ca/)
00009 
00010     This program is free software; you can redistribute it and/or modify
00011     it under the terms of the GNU General Public License as published by
00012     the Free Software Foundation; either version 2 of the License, or
00013     (at your option) any later version.
00014 
00015     This program is distributed in the hope that it will be useful,
00016     but WITHOUT ANY WARRANTY; without even the implied warranty of
00017     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00018 
00019     See the GNU General Public License in the COPYING file at the
00020     root directory of this project for more details.
00021 */
00022 
00023 #include "boostwrap.h"
00024 
00025 using namespace std;
00026 using namespace Barry;
00027 
00028 #ifdef __BARRY_BOOST_MODE__
00029 
00030 template <class RecordT>
00031 bool DoLoadBoostFile(const std::string &filename,
00032         std::vector<RecordT> &container,
00033         std::string &dbName,
00034         std::string &errmsg)
00035 {
00036         try {
00037                 std::ifstream ifs(filename.c_str());
00038                 std::getline(ifs, dbName);
00039                 boost::archive::text_iarchive ia(ifs);
00040                 ia >> container;
00041                 return true;
00042         }
00043         catch( boost::archive::archive_exception &ae ) {
00044                 errmsg = "Archive exception in DoLoadBoostFile(): ";
00045                 errmsg += ae.what();
00046                 return false;
00047         }
00048 }
00049 
00050 template <class RecordT>
00051 bool DoSaveBoostFile(const std::string &filename,
00052         const std::vector<RecordT> &container,
00053         std::string &errmsg)
00054 {
00055         try {
00056                 std::ofstream ofs(filename.c_str());
00057                 ofs << RecordT::GetDBName() << endl;
00058                 boost::archive::text_oarchive oa(ofs);
00059                 oa << container;
00060                 return true;
00061         }
00062         catch( boost::archive::archive_exception &ae ) {
00063                 errmsg = "Archive exception in DoSaveBoostFile(): ";
00064                 errmsg += ae.what();
00065                 return false;
00066         }
00067 }
00068 
00069 #undef HANDLE_PARSER
00070 #define HANDLE_PARSER(tname) \
00071 bool LoadBoostFile(const std::string &filename, \
00072         std::vector<tname> &container, \
00073         std::string &dbName, \
00074         std::string &errmsg) \
00075 { \
00076         return DoLoadBoostFile<tname>(filename, container, dbName, errmsg); \
00077 } \
00078 bool SaveBoostFile(const std::string &filename, \
00079         const std::vector<tname> &container, \
00080         std::string &errmsg) \
00081 { \
00082         return DoSaveBoostFile<tname>(filename, container, errmsg); \
00083 }
00084 ALL_KNOWN_PARSER_TYPES
00085 
00086 #endif
00087