- use explicit instantiation for the most-used version of tmpfstream
authorGerd von Egidy <gerd.von.egidy@intra2net.com>
Wed, 10 Mar 2010 16:33:02 +0000 (17:33 +0100)
committerGerd von Egidy <gerd.von.egidy@intra2net.com>
Wed, 10 Mar 2010 16:34:58 +0000 (17:34 +0100)
- 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

src/Makefile.am
src/tmpfstream.cpp
src/tmpfstream.hpp
src/tmpfstream_impl.hpp [new file with mode: 0644]
test/test_tmpfstream.cpp

index a0c54a7..c3a14ed 100644 (file)
@@ -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 \
index e4dc459..9e2a464 100644 (file)
 #include <sstream>
 #include <iostream>
 
-#include <stdio.h>
-#include <unistd.h>
-#include <errno.h>
-#include <stdlib.h>
-
 #include "tmpfstream.hpp"
-#include "filefunc.hxx"
+#include "tmpfstream_impl.hpp"
 
 using namespace std;
 
 namespace I2n
 {
-/*
-template< typename Device, typename Tr, typename Alloc >
-bool tmpfstream<Device,Tr,Alloc>::open(const std::string& tmpnametemplate, 
-        std::ios_base::open_mode mode,
-        int buffer_size, int pback_size)
-{
-    if (tmpfstream<Device,Tr,Alloc>::is_open())
-        tmpfstream<Device,Tr,Alloc>::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<Device,Tr,Alloc>::open(Device(fd,true));
-
-    return tmpfstream<Device,Tr,Alloc>::is_open();
-}
-
-template< typename Device, typename Tr, typename Alloc >
-bool tmpfstream<Device,Tr,Alloc>::set_file_mode(int mode)
-{
 
-}
-
-template< typename Device, typename Tr, typename Alloc >
-bool tmpfstream<Device,Tr,Alloc>::unlink()
-{
-    if (!get_tmp_filename().empty())
-        unlink(get_tmp_filename());
-    else
-        return false;
-}
-template< typename Device, typename Tr, typename Alloc >
-bool tmpfstream<Device,Tr,Alloc>::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<bio::file_descriptor_sink>;
+template class tmpfstreamTempl<bio::file_descriptor>;
 
 
 }
index 961e4ae..40b5804 100644 (file)
 #include <boost/iostreams/stream.hpp>
 #include <boost/iostreams/device/file_descriptor.hpp>
 
-#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<Device>::type
+                  BOOST_DEDUCED_TYPENAME  bio::char_type_of<Device>::type
               ),
           typename Alloc =
               std::allocator<
-                  BOOST_DEDUCED_TYPENAME  boost::iostreams::char_type_of<Device>::type
+                  BOOST_DEDUCED_TYPENAME  bio::char_type_of<Device>::type
               > >
-class tmpfstream : public boost::iostreams::stream<Device, Tr, Alloc>
+class tmpfstreamTempl : public bio::stream<Device, Tr, Alloc>
 {
 private:
     std::string filename;
 
 public:
-    tmpfstream()
-        : boost::iostreams::stream<Device, Tr, Alloc>()
+    tmpfstreamTempl()
+        : bio::stream<Device, Tr, Alloc>()
         { }
 
-    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<Device, Tr, Alloc>()
+        : bio::stream<Device, Tr, Alloc>()
     {
         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<Device,Tr,Alloc>::is_open())
-            tmpfstream<Device,Tr,Alloc>::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<Device,Tr,Alloc>::open(Device(fd,true));
-
-        return tmpfstream<Device,Tr,Alloc>::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<bio::file_descriptor_sink> tmpofstream;
+typedef tmpfstreamTempl<bio::file_descriptor> tmpfstream;
+
 }
 
 #endif
diff --git a/src/tmpfstream_impl.hpp b/src/tmpfstream_impl.hpp
new file mode 100644 (file)
index 0000000..83a7d33
--- /dev/null
@@ -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 &copy; Copyright 2010 by Intra2net AG
+ * @license commercial
+ *
+ * info@intra2net.com
+ *
+ */
+
+#ifndef __I2N_TMPFSTREAM_IMPL_HPP__
+#define __I2N_TMPFSTREAM_IMPL_HPP__
+
+#include <string>
+#include <stdio.h>
+#include <unistd.h>
+#include <errno.h>
+#include <stdlib.h>
+
+#include <tmpfstream.hpp>
+#include <filefunc.hxx>
+
+
+namespace I2n
+{
+
+template< typename Device, typename Tr, typename Alloc >
+bool tmpfstreamTempl<Device,Tr,Alloc>::open(const std::string& tmpnametemplate, 
+               std::ios_base::open_mode mode,
+               int buffer_size, int pback_size)
+{
+    if (tmpfstreamTempl<Device,Tr,Alloc>::is_open())
+        tmpfstreamTempl<Device,Tr,Alloc>::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<Device,Tr,Alloc>::open(Device(fd,true));
+
+    return tmpfstreamTempl<Device,Tr,Alloc>::is_open();
+}
+
+template< typename Device, typename Tr, typename Alloc >
+bool tmpfstreamTempl<Device,Tr,Alloc>::set_file_mode(int mode)
+{
+
+}
+
+template< typename Device, typename Tr, typename Alloc >
+bool tmpfstreamTempl<Device,Tr,Alloc>::unlink()
+{
+    if (!get_tmp_filename().empty())
+        return I2n::unlink(get_tmp_filename());
+    else
+        return false;
+}
+
+template< typename Device, typename Tr, typename Alloc >
+bool tmpfstreamTempl<Device,Tr,Alloc>::move_to(const std::string& targetpath, 
+                                          bool overwrite)
+{
+
+}
+
+}
+
+#endif
index b29b422..d7e9e21 100644 (file)
@@ -31,7 +31,7 @@ BOOST_AUTO_TEST_SUITE(TestTmpfstream)
 
 BOOST_AUTO_TEST_CASE(Tmpfstream)
 {
-    tmpfstream<bio::file_descriptor_sink> tmpf("./tmp.XXXXXX");
+    tmpofstream tmpf("./tmp.XXXXXX");
 
     BOOST_CHECK_EQUAL( true, tmpf.is_open() );
     BOOST_CHECK_EQUAL( false, tmpf.get_tmp_filename().empty() );