QxOrm 1.1.9
C++ Object Relational Mapping library
archive_wide_traits.h
Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** http://www.qxorm.com/
00004 ** http://sourceforge.net/projects/qxorm/
00005 ** Original file by Lionel Marty
00006 **
00007 ** This file is part of the QxOrm library
00008 **
00009 ** This software is provided 'as-is', without any express or implied
00010 ** warranty. In no event will the authors be held liable for any
00011 ** damages arising from the use of this software.
00012 **
00013 ** GNU Lesser General Public License Usage
00014 ** This file must be used under the terms of the GNU Lesser
00015 ** General Public License version 2.1 as published by the Free Software
00016 ** Foundation and appearing in the file 'license.lgpl.txt' included in the
00017 ** packaging of this file.  Please review the following information to
00018 ** ensure the GNU Lesser General Public License version 2.1 requirements
00019 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
00020 **
00021 ** If you have questions regarding the use of this file, please contact :
00022 ** contact@qxorm.com
00023 **
00024 ****************************************************************************/
00025 
00026 #ifndef _QX_ARCHIVE_WIDE_TRAITS_H_
00027 #define _QX_ARCHIVE_WIDE_TRAITS_H_
00028 
00029 #ifdef _MSC_VER
00030 #pragma once
00031 #endif
00032 
00040 #include <string>
00041 #include <iostream>
00042 #include <sstream>
00043 #include <fstream>
00044 
00045 #include <boost/mpl/if.hpp>
00046 #include <boost/mpl/logical.hpp>
00047 
00048 #include <QtCore/qstring.h>
00049 
00050 #include <QxSerialize/boost/QxSerializeInclude.h>
00051 
00052 namespace qx {
00053 namespace trait {
00054 
00059 template <typename T> struct is_archive_wide { enum { value = false }; };
00060 
00061 #if _QX_SERIALIZE_WIDE_BINARY
00062 template <> struct is_archive_wide<boost::archive::binary_wiarchive> { enum { value = true }; };
00063 template <> struct is_archive_wide<boost::archive::binary_woarchive> { enum { value = true }; };
00064 #endif // _QX_SERIALIZE_WIDE_BINARY
00065 
00066 #if _QX_SERIALIZE_WIDE_TEXT
00067 template <> struct is_archive_wide<boost::archive::text_wiarchive>   { enum { value = true }; };
00068 template <> struct is_archive_wide<boost::archive::text_woarchive>   { enum { value = true }; };
00069 #endif // _QX_SERIALIZE_WIDE_TEXT
00070 
00071 #if _QX_SERIALIZE_WIDE_XML
00072 template <> struct is_archive_wide<boost::archive::xml_wiarchive>    { enum { value = true }; };
00073 template <> struct is_archive_wide<boost::archive::xml_woarchive>    { enum { value = true }; };
00074 #endif // _QX_SERIALIZE_WIDE_XML
00075 
00076 template <typename T>
00077 class archive_wide_traits
00078 {
00079 
00080 public:
00081 
00082    enum { is_wide = qx::trait::is_archive_wide<T>::value };
00083 
00084    typedef typename boost::mpl::if_c<is_wide, wchar_t, char>::type type_char;
00085    typedef typename boost::mpl::if_c<is_wide, std::wstring, std::string>::type type_string;
00086 
00087    typedef typename boost::mpl::if_c<is_wide, std::wistream, std::istream>::type type_istream;
00088    typedef typename boost::mpl::if_c<is_wide, std::wostream, std::ostream>::type type_ostream;
00089 
00090    typedef typename boost::mpl::if_c<is_wide, std::wstringstream, std::stringstream>::type type_stringstream;
00091    typedef typename boost::mpl::if_c<is_wide, std::wistringstream, std::istringstream>::type type_istringstream;
00092    typedef typename boost::mpl::if_c<is_wide, std::wostringstream, std::ostringstream>::type type_ostringstream;
00093 
00094    typedef typename boost::mpl::if_c<is_wide, std::wfstream, std::fstream>::type type_fstream;
00095    typedef typename boost::mpl::if_c<is_wide, std::wifstream, std::ifstream>::type type_ifstream;
00096    typedef typename boost::mpl::if_c<is_wide, std::wofstream, std::ofstream>::type type_ofstream;
00097 
00098    static inline QString toQString(const type_string & str)                   { return cvtQString<is_wide, 0>::toQString(str); }
00099    static inline void fromQString(const QString & str, type_string & result)  { cvtQString<is_wide, 0>::fromQString(str, result); }
00100 
00101    static inline QByteArray toQByteArray(const type_string & str, type_string * owner) { return cvtQByteArray<is_wide, 0>::toQByteArray(str, owner); }
00102    static inline void fromQByteArray(const QByteArray & data, type_string & result)    { cvtQByteArray<is_wide, 0>::fromQByteArray(data, result); }
00103 
00104 private:
00105 
00106    template <bool isWide /* = false */, int dummy>
00107    struct cvtQString
00108    {
00109       static inline QString toQString(const std::string & str)                   { return QString::fromStdString(str); }
00110       static inline void fromQString(const QString & str, std::string & result)  { result = str.toStdString(); }
00111    };
00112 
00113    template <int dummy>
00114    struct cvtQString<true, dummy>
00115    {
00116       static inline QString toQString(const std::wstring & str)                     { return QString::fromStdWString(str); }
00117       static inline void fromQString(const QString & str, std::wstring & result)    { result = str.toStdWString(); }
00118    };
00119 
00120    template <bool isWide /* = false */, int dummy>
00121    struct cvtQByteArray
00122    {
00123       static inline QByteArray toQByteArray(const std::string & str, std::string * owner)
00124       { if (owner) { (* owner) = str; }; return (owner ? QByteArray::fromRawData(owner->data(), owner->size()) : QByteArray(str.data(), str.size())); }
00125       static inline void fromQByteArray(const QByteArray & data, std::string & result)
00126       { result.clear(); result.append(data.constData(), data.size()); }
00127    };
00128 
00129    template <int dummy>
00130    struct cvtQByteArray<true, dummy>
00131    {
00132       static inline QByteArray toQByteArray(const std::wstring & str, std::wstring * owner)
00133       { Q_UNUSED(owner); return QString::fromStdWString(str).toUtf8(); }
00134       static inline void fromQByteArray(const QByteArray & data, std::wstring & result)
00135       { result = QString::fromUtf8(data.constData(), data.size()).toStdWString(); }
00136    };
00137 
00138 };
00139 
00140 } // namespace trait
00141 } // namespace qx
00142 
00143 #endif // _QX_ARCHIVE_WIDE_TRAITS_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines