From faf78dda7fad89e058fd67643b6f717629881d81 Mon Sep 17 00:00:00 2001 From: Chris Frey Date: Fri, 3 Feb 2012 21:37:37 -0500 Subject: [PATCH] tools: added libboostwrap.a to put boost related code into one spot Hopefully this will speed up compiles when Boost is enabled. --- .gitignore | 1 + tools/Makefile.am | 15 +++++++-- tools/boostwrap.cc | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++ tools/boostwrap.h | 40 +++++++++++++++++++++++ tools/bs11nread.cc | 25 +++++++++------ tools/btool.cc | 93 +++++++++++++++++++++++++++--------------------------- 6 files changed, 202 insertions(+), 59 deletions(-) create mode 100644 tools/boostwrap.cc create mode 100644 tools/boostwrap.h diff --git a/.gitignore b/.gitignore index 6316c042..78de145a 100644 --- a/.gitignore +++ b/.gitignore @@ -79,6 +79,7 @@ tools/bwatch tools/pppob tools/upldif tools/libutil.a +tools/libboostwrap.a gui/src/barrybackup diff --git a/tools/Makefile.am b/tools/Makefile.am index 60a58fb3..8eacfbae 100644 --- a/tools/Makefile.am +++ b/tools/Makefile.am @@ -16,6 +16,7 @@ noinst_HEADERS = \ mimedump.h \ brecsum.h \ util.h \ + boostwrap.h \ barrygetopt.h \ platform.h @@ -25,7 +26,8 @@ noinst_PROGRAMS = \ brimtrans noinst_LIBRARIES = \ - libutil.a + libutil.a \ + libboostwrap.a bin_PROGRAMS = \ btool \ @@ -69,8 +71,14 @@ if WITH_SYNC libutil_a_CXXFLAGS += -D__BARRY_SYNC_MODE__ $(GLIB2_CFLAGS) endif +libboostwrap_a_SOURCES = boostwrap.cc +libboostwrap_a_CXXFLAGS = $(AM_CXXFLAGS) +if WITH_BOOST +libboostwrap_a_CXXFLAGS += -D__BARRY_BOOST_MODE__ -D_REENTRANT @BOOST_INC_PATH@ +endif + btool_SOURCES = btool.cc -btool_LDADD = ../src/libbarry.la libutil.a $(USB_LIBRARY_LIBS) @BOOST_LDADD@ $(LTLIBINTL) $(LTLIBICONV) +btool_LDADD = ../src/libbarry.la libutil.a libboostwrap.a $(USB_LIBRARY_LIBS) @BOOST_LDADD@ $(LTLIBINTL) $(LTLIBICONV) if WITH_SYNC btool_LDADD += ../src/libbarrysync.la $(GLIB2_LIBS) endif @@ -91,7 +99,7 @@ btool_LDFLAGS = @BOOST_LIB_PATH@ if WITH_BOOST bs11nread_SOURCES = bs11nread.cc -bs11nread_LDADD = ../src/libbarry.la libutil.a $(USB_LIBRARY_LIBS) @BOOST_LDADD@ $(LTLIBINTL) +bs11nread_LDADD = ../src/libbarry.la libutil.a libboostwrap.a $(USB_LIBRARY_LIBS) @BOOST_LDADD@ $(LTLIBINTL) bs11nread_CXXFLAGS = -D_REENTRANT @BOOST_INC_PATH@ $(AM_CXXFLAGS) bs11nread_LDFLAGS = @BOOST_LIB_PATH@ endif @@ -206,6 +214,7 @@ bio_LDADD = ../src/libbarry.la \ ../src/libbarrysync.la $(GLIB2_LIBS) \ ../src/libbarrybackup.la \ libutil.a \ + libboostwrap.a \ $(USB_LIBRARY_LIBS) @BOOST_LDADD@ \ $(LTLIBINTL) $(LTLIBICONV) bio_LDFLAGS = @BOOST_LIB_PATH@ diff --git a/tools/boostwrap.cc b/tools/boostwrap.cc new file mode 100644 index 00000000..81cf6c03 --- /dev/null +++ b/tools/boostwrap.cc @@ -0,0 +1,87 @@ +/// +/// \file boostwrap.cc +/// Wrap Boost serialization into its own lib, so each usage +/// in the tools doesn't cause a compile speed meltdown. +/// + +/* + Copyright (C) 2012, Net Direct Inc. (http://www.netdirect.ca/) + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + + See the GNU General Public License in the COPYING file at the + root directory of this project for more details. +*/ + +#include "boostwrap.h" + +using namespace std; +using namespace Barry; + +#ifdef __BARRY_BOOST_MODE__ + +template +bool DoLoadBoostFile(const std::string &filename, + std::vector &container, + std::string &dbName, + std::string &errmsg) +{ + try { + std::ifstream ifs(filename.c_str()); + std::getline(ifs, dbName); + boost::archive::text_iarchive ia(ifs); + ia >> container; + return true; + } + catch( boost::archive::archive_exception &ae ) { + errmsg = "Archive exception in DoLoadBoostFile(): "; + errmsg += ae.what(); + return false; + } +} + +template +bool DoSaveBoostFile(const std::string &filename, + const std::vector &container, + std::string &errmsg) +{ + try { + std::ofstream ofs(filename.c_str()); + ofs << RecordT::GetDBName() << endl; + boost::archive::text_oarchive oa(ofs); + oa << container; + return true; + } + catch( boost::archive::archive_exception &ae ) { + errmsg = "Archive exception in DoSaveBoostFile(): "; + errmsg += ae.what(); + return false; + } +} + +#undef HANDLE_PARSER +#define HANDLE_PARSER(tname) \ +bool LoadBoostFile(const std::string &filename, \ + std::vector &container, \ + std::string &dbName, \ + std::string &errmsg) \ +{ \ + return DoLoadBoostFile(filename, container, dbName, errmsg); \ +} \ +bool SaveBoostFile(const std::string &filename, \ + const std::vector &container, \ + std::string &errmsg) \ +{ \ + return DoSaveBoostFile(filename, container, errmsg); \ +} +ALL_KNOWN_PARSER_TYPES + +#endif + diff --git a/tools/boostwrap.h b/tools/boostwrap.h new file mode 100644 index 00000000..b2b0cd49 --- /dev/null +++ b/tools/boostwrap.h @@ -0,0 +1,40 @@ +/// +/// \file boostwrap.h +/// Wrap Boost serialization into its own lib, so each usage +/// in the tools doesn't cause a compile speed meltdown. +/// + +/* + Copyright (C) 2012, Net Direct Inc. (http://www.netdirect.ca/) + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + + See the GNU General Public License in the COPYING file at the + root directory of this project for more details. +*/ + +#ifndef __BARRY_TOOLS_BOOSTWRAP_H__ +#define __BARRY_TOOLS_BOOSTWRAP_H__ + +#include + +#undef HANDLE_PARSER +#define HANDLE_PARSER(tname) \ +bool LoadBoostFile(const std::string &filename, \ + std::vector &container, \ + std::string &dbName, \ + std::string &errmsg); \ +bool SaveBoostFile(const std::string &filename, \ + const std::vector &container, \ + std::string &errmsg); +ALL_KNOWN_PARSER_TYPES + +#endif + diff --git a/tools/bs11nread.cc b/tools/bs11nread.cc index 2cffacae..31bbc2fd 100644 --- a/tools/bs11nread.cc +++ b/tools/bs11nread.cc @@ -19,7 +19,10 @@ root directory of this project for more details. */ -#define __BARRY_BOOST_MODE__ // this program always requires BOOST +// Boost is required, but since the real Boost serialization code is in +// util.cc, we don't need it here. +#undef __BARRY_BOOST_MODE__ + #include #include #include @@ -32,6 +35,7 @@ #include "barrygetopt.h" #include "util.h" +#include "boostwrap.h" using namespace std; using namespace Barry; @@ -54,14 +58,19 @@ void Usage() } template -bool Dump(const std::string &dbName, ifstream &ifs) +bool Dump(const std::string &dbName, const std::string &filename) { if( dbName != Record::GetDBName() ) return false; std::vector records; - boost::archive::text_iarchive ia(ifs); - ia >> records; + std::string junk, errmsg; + + if( !LoadBoostFile(filename, records, junk, errmsg) ) { + cerr << errmsg << endl; + return false; + } + cout << records.size() << " records loaded" << endl; sort(records.begin(), records.end()); @@ -81,10 +90,11 @@ void DumpDB(const string &filename) ifstream ifs(filename.c_str()); std::string dbName; getline(ifs, dbName); + ifs.close(); // check for recognized database names #undef HANDLE_PARSER -#define HANDLE_PARSER(tname) Dump(dbName, ifs) || +#define HANDLE_PARSER(tname) Dump(dbName, filename) || ALL_KNOWN_PARSER_TYPES cerr << "Unknown database name: " << dbName << endl; } @@ -131,11 +141,6 @@ int main(int argc, char *argv[]) DumpDB(filename); } - catch( boost::archive::archive_exception &ae ) { - cerr << "Archive exception: " - << ae.what() << endl; - return 1; - } catch( Usb::Error &ue) { std::cerr << "Usb::Error caught: " << ue.what() << endl; return 1; diff --git a/tools/btool.cc b/tools/btool.cc index f015cbd5..0f7941d0 100644 --- a/tools/btool.cc +++ b/tools/btool.cc @@ -19,6 +19,22 @@ root directory of this project for more details. */ +// +// This define is used in barry/barry.h to signal inclusion of Boost +// serialization headers. It is intended to be used by applications, +// so we shouldn't mess with it. +// +// But all actual Boost related code is now stuffed into util.cc, safely +// locked away from other code. So we don't need the Boost headers, we +// just need a flag for our own functionality. So translate this define +// into our own, and undef to skip the Boost headers, and the compile speed +// slowdown that it creates. +// +#ifdef __BARRY_BOOST_MODE__ +#define __BTOOL_BOOST_MODE__ +#endif +#undef __BARRY_BOOST_MODE__ + #include #ifdef __BARRY_SYNC_MODE__ #include @@ -39,6 +55,7 @@ #include #include "i18n.h" #include "util.h" +#include "boostwrap.h" #include "barrygetopt.h" @@ -58,7 +75,7 @@ void Usage() << " Copyright 2005-2012, Net Direct Inc. (http://www.netdirect.ca/)\n" << " Using: " << Version << "\n" << " Compiled " -#ifdef __BARRY_BOOST_MODE__ +#ifdef __BTOOL_BOOST_MODE__ << "with" #else << "without" @@ -83,7 +100,7 @@ void Usage() << " endpoint pair. Example: -e 83,5\n" << " Note: Endpoints are specified in hex.\n" << " You should never need to use this option.\n" -#ifdef __BARRY_BOOST_MODE__ +#ifdef __BTOOL_BOOST_MODE__ << " -f file Filename to save or load handheld data to/from\n" #endif << " -F sort Field name by which to sort the output. Note that the\n" @@ -165,33 +182,25 @@ struct Store from_device_count(0), to_device_count(0) { -#ifdef __BARRY_BOOST_MODE__ - try { - - if( load && filename.size() ) { - // filename is available, attempt to load - cout << "Loading: " << filename << endl; - ifstream ifs(filename.c_str()); - std::string dbName; - getline(ifs, dbName); - boost::archive::text_iarchive ia(ifs); - ia >> records; - cout << records.size() - << " records loaded from '" - << filename << "'" << endl; - sort(records.begin(), records.end()); - rec_it = records.begin(); - - // debugging aid - typename std::vector::const_iterator beg = records.begin(), end = records.end(); - for( ; beg != end; beg++ ) { - cout << (*beg) << endl; - } +#ifdef __BTOOL_BOOST_MODE__ + if( load && filename.size() ) { + // filename is available, attempt to load + cout << "Loading: " << filename << endl; + string errmsg, dbName; + if( !LoadBoostFile(filename, records, dbName, errmsg) ) { + cerr << errmsg << endl; + } + cout << records.size() + << " records loaded from '" + << filename << "'" << endl; + sort(records.begin(), records.end()); + rec_it = records.begin(); + + // debugging aid + typename std::vector::const_iterator beg = records.begin(), end = records.end(); + for( ; beg != end; beg++ ) { + cout << (*beg) << endl; } - - } catch( boost::archive::archive_exception &ae ) { - cerr << "Archive exception in ~Store(): " - << ae.what() << endl; } #endif } @@ -211,24 +220,16 @@ struct Store } cout << "Store counted " << dec << from_device_count << " records read from device, and " << dec << to_device_count << " records written to device." << endl; -#ifdef __BARRY_BOOST_MODE__ - try { - - if( !load && filename.size() ) { - // filename is available, attempt to save - cout << "Saving: " << filename << endl; - const std::vector &r = records; - ofstream ofs(filename.c_str()); - ofs << Record::GetDBName() << endl; - boost::archive::text_oarchive oa(ofs); - oa << r; - cout << dec << r.size() << " records saved to '" - << filename << "'" << endl; +#ifdef __BTOOL_BOOST_MODE__ + if( !load && filename.size() ) { + // filename is available, attempt to save + cout << "Saving: " << filename << endl; + string errmsg; + if( !SaveBoostFile(filename, records, errmsg) ) { + cerr << errmsg << endl; } - - } catch( boost::archive::archive_exception &ae ) { - cerr << "Archive exception in ~Store(): " - << ae.what() << endl; + cout << dec << records.size() << " records saved to '" + << filename << "'" << endl; } #endif } @@ -607,7 +608,7 @@ int main(int argc, char *argv[]) break; case 'f': // filename -#ifdef __BARRY_BOOST_MODE__ +#ifdef __BTOOL_BOOST_MODE__ if( !bbackup_mode && filename.size() == 0 ) { filename = optarg; } -- 2.11.4.GIT