From: Gerd von Egidy Date: Wed, 10 Mar 2010 16:33:02 +0000 (+0100) Subject: - use explicit instantiation for the most-used version of tmpfstream X-Git-Tag: v2.6~112^2~11 X-Git-Url: http://developer.intra2net.com/git/?a=commitdiff_plain;h=82b461e4b24418e235b203b6cf2863c3675c060f;p=libi2ncommon - use explicit instantiation for the most-used version of tmpfstream - split the implementation into a separate header only needed for unusual versions - most used versions get the typedefs tmpofstream and tmpfstream - rename the template to tmpfstreamTempl --- diff --git a/src/Makefile.am b/src/Makefile.am index a0c54a7..c3a14ed 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -4,11 +4,11 @@ INCLUDES = -I$(top_srcdir)/src -I$(top_srcdir)/utils @LIBGETTEXT_CFLAGS@ @LIBICO # the library search path. lib_LTLIBRARIES = libi2ncommon.la -include_HEADERS = week.hpp cron.hpp daemonfunc.hpp filefunc.hxx \ +include_HEADERS = cron.hpp daemonfunc.hpp exception.hxx filefunc.hxx \ i2n_configdata.hpp i2n_configfile.hpp insocketstream.hxx ip_type.hxx ipfunc.hxx \ log_macros.hpp logfunc.hpp logread.hxx oftmpstream.hxx pidfile.hpp pipestream.hxx \ - pointer_func.hpp source_track_basics.hpp stringfunc.hxx timefunc.hxx \ - tracefunc.hpp userfunc.hpp exception.hxx + pointer_func.hpp source_track_basics.hpp stringfunc.hxx timefunc.hxx tmpfstream.hpp \ + tmpfstream_impl.hpp tracefunc.hpp userfunc.hpp week.hpp libi2ncommon_la_SOURCES = cron.cpp daemonfunc.cpp filefunc.cpp \ i2n_configfile.cpp ipfunc.cpp logfunc.cpp logread.cpp oftmpstream.cpp pidfile.cpp \ diff --git a/src/tmpfstream.cpp b/src/tmpfstream.cpp index e4dc459..9e2a464 100644 --- a/src/tmpfstream.cpp +++ b/src/tmpfstream.cpp @@ -15,65 +15,19 @@ #include #include -#include -#include -#include -#include - #include "tmpfstream.hpp" -#include "filefunc.hxx" +#include "tmpfstream_impl.hpp" using namespace std; namespace I2n { -/* -template< typename Device, typename Tr, typename Alloc > -bool tmpfstream::open(const std::string& tmpnametemplate, - std::ios_base::open_mode mode, - int buffer_size, int pback_size) -{ - if (tmpfstream::is_open()) - tmpfstream::close(); - - char* chbuf=new char[tmpnametemplate.size()+1]; - tmpnametemplate.copy(chbuf,tmpnametemplate.size()+1); - chbuf[tmpnametemplate.size()]=0; - - // TODO: flags handling - - int fd=mkstemp(chbuf); - filename=chbuf; - delete[] chbuf; - - if (fd==-1) - return false; - - boost::iostreams::stream::open(Device(fd,true)); - - return tmpfstream::is_open(); -} - -template< typename Device, typename Tr, typename Alloc > -bool tmpfstream::set_file_mode(int mode) -{ -} - -template< typename Device, typename Tr, typename Alloc > -bool tmpfstream::unlink() -{ - if (!get_tmp_filename().empty()) - unlink(get_tmp_filename()); - else - return false; -} -template< typename Device, typename Tr, typename Alloc > -bool tmpfstream::move_to(const std::string& targetpath, bool overwrite) -{ - -} -*/ +// explicitly instantiate the most used versions. +// means they get compiled into the lib and you don't need tmpfstream_impl.hpp +// if you want to use anything else, include tmpfstream_impl.hpp. +template class tmpfstreamTempl; +template class tmpfstreamTempl; } diff --git a/src/tmpfstream.hpp b/src/tmpfstream.hpp index 961e4ae..40b5804 100644 --- a/src/tmpfstream.hpp +++ b/src/tmpfstream.hpp @@ -20,80 +20,56 @@ #include #include -#include "filefunc.hxx" - namespace I2n { +namespace bio = boost::iostreams; + template< typename Device, typename Tr = BOOST_IOSTREAMS_CHAR_TRAITS( - BOOST_DEDUCED_TYPENAME boost::iostreams::char_type_of::type + BOOST_DEDUCED_TYPENAME bio::char_type_of::type ), typename Alloc = std::allocator< - BOOST_DEDUCED_TYPENAME boost::iostreams::char_type_of::type + BOOST_DEDUCED_TYPENAME bio::char_type_of::type > > -class tmpfstream : public boost::iostreams::stream +class tmpfstreamTempl : public bio::stream { private: std::string filename; public: - tmpfstream() - : boost::iostreams::stream() + tmpfstreamTempl() + : bio::stream() { } - tmpfstream(const std::string& tmpnametemplate, + tmpfstreamTempl(const std::string& tmpnametemplate, std::ios_base::open_mode mode = std::ios_base::out, int buffer_size = -1 , int pback_size = -1) - : boost::iostreams::stream() + : bio::stream() { open(tmpnametemplate,mode,buffer_size,pback_size); } bool open(const std::string& tmpnametemplate, std::ios_base::open_mode mode = std::ios_base::out, - int buffer_size = -1 , int pback_size = -1) - { - if (tmpfstream::is_open()) - tmpfstream::close(); - - char* chbuf=new char[tmpnametemplate.size()+1]; - tmpnametemplate.copy(chbuf,tmpnametemplate.size()+1); - chbuf[tmpnametemplate.size()]=0; - - // TODO: flags handling - - int fd=mkstemp(chbuf); - filename=chbuf; - delete[] chbuf; - - if (fd==-1) - return false; - - boost::iostreams::stream::open(Device(fd,true)); - - return tmpfstream::is_open(); - } + int buffer_size = -1 , int pback_size = -1); std::string get_tmp_filename() { return filename; } bool set_file_mode(int mode); - bool unlink() - { - if (!get_tmp_filename().empty()) - return I2n::unlink(get_tmp_filename()); - else - return false; - } + bool unlink(); bool move_to(const std::string& targetpath, bool overwrite); }; +typedef tmpfstreamTempl tmpofstream; +typedef tmpfstreamTempl tmpfstream; + } #endif diff --git a/src/tmpfstream_impl.hpp b/src/tmpfstream_impl.hpp new file mode 100644 index 0000000..83a7d33 --- /dev/null +++ b/src/tmpfstream_impl.hpp @@ -0,0 +1,83 @@ +/** @file + * @brief fstream which creates files with mkstemp. + * + * @note This file contains the template implementation. You only need + * to include it if you use something else than the explicitly + * instantiated and typedefed versions + * + * @author Gerd v. Egidy + * + * @copyright © Copyright 2010 by Intra2net AG + * @license commercial + * + * info@intra2net.com + * + */ + +#ifndef __I2N_TMPFSTREAM_IMPL_HPP__ +#define __I2N_TMPFSTREAM_IMPL_HPP__ + +#include +#include +#include +#include +#include + +#include +#include + + +namespace I2n +{ + +template< typename Device, typename Tr, typename Alloc > +bool tmpfstreamTempl::open(const std::string& tmpnametemplate, + std::ios_base::open_mode mode, + int buffer_size, int pback_size) +{ + if (tmpfstreamTempl::is_open()) + tmpfstreamTempl::close(); + + char* chbuf=new char[tmpnametemplate.size()+1]; + tmpnametemplate.copy(chbuf,tmpnametemplate.size()+1); + chbuf[tmpnametemplate.size()]=0; + + // TODO: flags handling + + int fd=mkstemp(chbuf); + filename=chbuf; + delete[] chbuf; + + if (fd==-1) + return false; + + boost::iostreams::stream::open(Device(fd,true)); + + return tmpfstreamTempl::is_open(); +} + +template< typename Device, typename Tr, typename Alloc > +bool tmpfstreamTempl::set_file_mode(int mode) +{ + +} + +template< typename Device, typename Tr, typename Alloc > +bool tmpfstreamTempl::unlink() +{ + if (!get_tmp_filename().empty()) + return I2n::unlink(get_tmp_filename()); + else + return false; +} + +template< typename Device, typename Tr, typename Alloc > +bool tmpfstreamTempl::move_to(const std::string& targetpath, + bool overwrite) +{ + +} + +} + +#endif diff --git a/test/test_tmpfstream.cpp b/test/test_tmpfstream.cpp index b29b422..d7e9e21 100644 --- a/test/test_tmpfstream.cpp +++ b/test/test_tmpfstream.cpp @@ -31,7 +31,7 @@ BOOST_AUTO_TEST_SUITE(TestTmpfstream) BOOST_AUTO_TEST_CASE(Tmpfstream) { - tmpfstream tmpf("./tmp.XXXXXX"); + tmpofstream tmpf("./tmp.XXXXXX"); BOOST_CHECK_EQUAL( true, tmpf.is_open() ); BOOST_CHECK_EQUAL( false, tmpf.get_tmp_filename().empty() );