libmoost
/home/mhx/git/github/libmoost/include/moost/container/memory_mapped_dataset/archive.hpp
Go to the documentation of this file.
00001 /* vim:set ts=3 sw=3 sts=3 et: */
00028 #ifndef MOOST_CONTAINER_MEMORY_MAPPED_DATASET_ARCHIVE_HPP__
00029 #define MOOST_CONTAINER_MEMORY_MAPPED_DATASET_ARCHIVE_HPP__
00030 
00031 #include <string>
00032 #include <sstream>
00033 
00034 #include <boost/archive/text_iarchive.hpp>
00035 #include <boost/archive/text_oarchive.hpp>
00036 #include <boost/archive/binary_iarchive.hpp>
00037 #include <boost/archive/binary_oarchive.hpp>
00038 #include <boost/noncopyable.hpp>
00039 
00040 #include "section_writer_base.hpp"
00041 
00042 namespace moost { namespace container {
00043 
00044 struct TextArchivePolicy
00045 {
00046    static const char *archive_type()
00047    {
00048       return "text";
00049    }
00050 
00051    typedef boost::archive::text_oarchive oarchive_t;
00052    typedef boost::archive::text_iarchive iarchive_t;
00053 };
00054 
00055 struct BinaryArchivePolicy
00056 {
00057    static const char *archive_type()
00058    {
00059       return "binary";
00060    }
00061 
00062    typedef boost::archive::binary_oarchive oarchive_t;
00063    typedef boost::archive::binary_iarchive iarchive_t;
00064 };
00065 
00073 template <class ArchivePolicy>
00074 class mmd_generic_archive : public boost::noncopyable
00075 {
00076 public:
00077    class writer : public mmd_section_writer_base
00078    {
00079    public:
00080       writer(memory_mapped_dataset::writer& wr, const std::string& name)
00081          : mmd_section_writer_base(wr, name, std::string("mmd_archive:") + ArchivePolicy::archive_type(), 1)
00082          , m_oss()
00083          , m_oa(m_oss)
00084       {
00085       }
00086 
00087       template <typename T>
00088       writer& operator<< (const T& e)
00089       {
00090          m_oa << e;
00091          return *this;
00092       }
00093 
00094    protected:
00095       void pre_commit()
00096       {
00097          write(m_oss.str());
00098          setattr("size", m_oss.str().size());
00099       }
00100 
00101    private:
00102       std::ostringstream m_oss;
00103       typename ArchivePolicy::oarchive_t m_oa;
00104    };
00105 
00106    mmd_generic_archive()
00107       : m_ia(m_iss)
00108    {
00109    }
00110 
00111    mmd_generic_archive(const memory_mapped_dataset& mmd, const std::string& name)
00112       : m_iss(archive_data(mmd, name))
00113       , m_ia(m_iss)
00114    {
00115    }
00116 
00117    void set(const memory_mapped_dataset& mmd, const std::string& name)
00118    {
00119       m_iss.str(archive_data(mmd, name));
00120    }
00121 
00122    template <typename T>
00123    mmd_generic_archive& operator>> (T& value)
00124    {
00125       m_ia >> value;
00126       return *this;
00127    }
00128 
00129 private:
00130    static std::string archive_data(const memory_mapped_dataset& mmd, const std::string& name)
00131    {
00132       const memory_mapped_dataset::section_info& info = mmd.find(name, std::string("mmd_archive:") + ArchivePolicy::archive_type());
00133       size_t size = info.getattr<size_t>("size");
00134       return std::string(mmd.data<char>(info.offset(), size), size);
00135    }
00136 
00137    std::istringstream m_iss;
00138    typename ArchivePolicy::iarchive_t m_ia;
00139 };
00140 
00141 typedef mmd_generic_archive<TextArchivePolicy> mmd_text_archive;
00142 typedef mmd_generic_archive<BinaryArchivePolicy> mmd_binary_archive;
00143 typedef mmd_text_archive mmd_archive;
00144 
00145 }}
00146 
00147 #endif