QxOrm  1.4.2
C++ Object Relational Mapping library
archive_wide_traits.h
Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** http://www.qxorm.com/
00004 ** Copyright (C) 2013 Lionel Marty (contact@qxorm.com)
00005 **
00006 ** This file is part of the QxOrm library
00007 **
00008 ** This software is provided 'as-is', without any express or implied
00009 ** warranty. In no event will the authors be held liable for any
00010 ** damages arising from the use of this software
00011 **
00012 ** Commercial Usage
00013 ** Licensees holding valid commercial QxOrm licenses may use this file in
00014 ** accordance with the commercial license agreement provided with the
00015 ** Software or, alternatively, in accordance with the terms contained in
00016 ** a written agreement between you and Lionel Marty
00017 **
00018 ** GNU General Public License Usage
00019 ** Alternatively, this file may be used under the terms of the GNU
00020 ** General Public License version 3.0 as published by the Free Software
00021 ** Foundation and appearing in the file 'license.gpl3.txt' included in the
00022 ** packaging of this file. Please review the following information to
00023 ** ensure the GNU General Public License version 3.0 requirements will be
00024 ** met : http://www.gnu.org/copyleft/gpl.html
00025 **
00026 ** If you are unsure which license is appropriate for your use, or
00027 ** if you have questions regarding the use of this file, please contact :
00028 ** contact@qxorm.com
00029 **
00030 ****************************************************************************/
00031 
00032 #ifndef _QX_ARCHIVE_WIDE_TRAITS_H_
00033 #define _QX_ARCHIVE_WIDE_TRAITS_H_
00034 
00035 #ifdef _MSC_VER
00036 #pragma once
00037 #endif
00038 
00046 #include <string>
00047 #include <iostream>
00048 #include <sstream>
00049 #include <fstream>
00050 
00051 #include <boost/mpl/if.hpp>
00052 #include <boost/mpl/logical.hpp>
00053 
00054 #include <QtCore/qstring.h>
00055 
00056 #include <QxSerialize/boost/QxSerializeInclude.h>
00057 
00058 namespace qx {
00059 namespace trait {
00060 
00065 template <typename T> struct is_archive_wide { enum { value = false }; };
00066 
00067 #if _QX_SERIALIZE_WIDE_BINARY
00068 template <> struct is_archive_wide<boost::archive::binary_wiarchive> { enum { value = true }; };
00069 template <> struct is_archive_wide<boost::archive::binary_woarchive> { enum { value = true }; };
00070 #endif // _QX_SERIALIZE_WIDE_BINARY
00071 
00072 #if _QX_SERIALIZE_WIDE_TEXT
00073 template <> struct is_archive_wide<boost::archive::text_wiarchive>   { enum { value = true }; };
00074 template <> struct is_archive_wide<boost::archive::text_woarchive>   { enum { value = true }; };
00075 #endif // _QX_SERIALIZE_WIDE_TEXT
00076 
00077 #if _QX_SERIALIZE_WIDE_XML
00078 template <> struct is_archive_wide<boost::archive::xml_wiarchive>    { enum { value = true }; };
00079 template <> struct is_archive_wide<boost::archive::xml_woarchive>    { enum { value = true }; };
00080 #endif // _QX_SERIALIZE_WIDE_XML
00081 
00082 template <typename T>
00083 class archive_wide_traits
00084 {
00085 
00086 public:
00087 
00088    enum { is_wide = qx::trait::is_archive_wide<T>::value };
00089 
00090    typedef typename boost::mpl::if_c<is_wide, wchar_t, char>::type type_char;
00091    typedef typename boost::mpl::if_c<is_wide, std::wstring, std::string>::type type_string;
00092 
00093    typedef typename boost::mpl::if_c<is_wide, std::wistream, std::istream>::type type_istream;
00094    typedef typename boost::mpl::if_c<is_wide, std::wostream, std::ostream>::type type_ostream;
00095 
00096    typedef typename boost::mpl::if_c<is_wide, std::wstringstream, std::stringstream>::type type_stringstream;
00097    typedef typename boost::mpl::if_c<is_wide, std::wistringstream, std::istringstream>::type type_istringstream;
00098    typedef typename boost::mpl::if_c<is_wide, std::wostringstream, std::ostringstream>::type type_ostringstream;
00099 
00100    typedef typename boost::mpl::if_c<is_wide, std::wfstream, std::fstream>::type type_fstream;
00101    typedef typename boost::mpl::if_c<is_wide, std::wifstream, std::ifstream>::type type_ifstream;
00102    typedef typename boost::mpl::if_c<is_wide, std::wofstream, std::ofstream>::type type_ofstream;
00103 
00104    static inline QString toQString(const type_string & str)                   { return cvtQString<is_wide, 0>::toQString(str); }
00105    static inline void fromQString(const QString & str, type_string & result)  { cvtQString<is_wide, 0>::fromQString(str, result); }
00106 
00107    static inline QByteArray toQByteArray(const type_string & str, type_string * owner) { return cvtQByteArray<is_wide, 0>::toQByteArray(str, owner); }
00108    static inline void fromQByteArray(const QByteArray & data, type_string & result)    { cvtQByteArray<is_wide, 0>::fromQByteArray(data, result); }
00109 
00110 private:
00111 
00112    template <bool isWide /* = false */, int dummy>
00113    struct cvtQString
00114    {
00115 #ifndef QT_NO_STL
00116       static inline QString toQString(const std::string & str)                   { return QString::fromStdString(str); }
00117       static inline void fromQString(const QString & str, std::string & result)  { result = str.toStdString(); }
00118 #else // QT_NO_STL
00119       static inline QString toQString(const std::string & str)                   { return QString::fromLatin1(str.data(), int(str.size())); }
00120       static inline void fromQString(const QString & str, std::string & result)  { result = str.toLatin1().constData(); }
00121 #endif // QT_NO_STL
00122    };
00123 
00124    template <int dummy>
00125    struct cvtQString<true, dummy>
00126    {
00127 #if ((! defined(QT_NO_STL)) && (! defined(QT_NO_STL_WCHAR)))
00128       static inline QString toQString(const std::wstring & str)                     { return QString::fromStdWString(str); }
00129       static inline void fromQString(const QString & str, std::wstring & result)    { result = str.toStdWString(); }
00130 #else // ((! defined(QT_NO_STL)) && (! defined(QT_NO_STL_WCHAR)))
00131       static inline QString toQString(const std::wstring & str)                     { Q_UNUSED(str); qAssert(false); /* Need STL compatibility ! */ return QString(); }
00132       static inline void fromQString(const QString & str, std::wstring & result)    { Q_UNUSED(str); Q_UNUSED(result); qAssert(false); /* Need STL compatibility ! */ }
00133 #endif // ((! defined(QT_NO_STL)) && (! defined(QT_NO_STL_WCHAR)))
00134    };
00135 
00136    template <bool isWide /* = false */, int dummy>
00137    struct cvtQByteArray
00138    {
00139       static inline QByteArray toQByteArray(const std::string & str, std::string * owner)
00140       { if (owner) { (* owner) = str; }; return (owner ? QByteArray::fromRawData(owner->data(), owner->size()) : QByteArray(str.data(), str.size())); }
00141       static inline void fromQByteArray(const QByteArray & data, std::string & result)
00142       { result.clear(); result.append(data.constData(), data.size()); }
00143    };
00144 
00145    template <int dummy>
00146    struct cvtQByteArray<true, dummy>
00147    {
00148       static inline QByteArray toQByteArray(const std::wstring & str, std::wstring * owner)
00149 #if ((! defined(QT_NO_STL)) && (! defined(QT_NO_STL_WCHAR)))
00150       { Q_UNUSED(owner); return QString::fromStdWString(str).toUtf8(); }
00151 #else // ((! defined(QT_NO_STL)) && (! defined(QT_NO_STL_WCHAR)))
00152       { Q_UNUSED(owner); Q_UNUSED(str); qAssert(false); /* Need STL compatibility ! */ return QByteArray(); }
00153 #endif // ((! defined(QT_NO_STL)) && (! defined(QT_NO_STL_WCHAR)))
00154 
00155       static inline void fromQByteArray(const QByteArray & data, std::wstring & result)
00156 #if ((! defined(QT_NO_STL)) && (! defined(QT_NO_STL_WCHAR)))
00157       { result = QString::fromUtf8(data.constData(), data.size()).toStdWString(); }
00158 #else // ((! defined(QT_NO_STL)) && (! defined(QT_NO_STL_WCHAR)))
00159       { Q_UNUSED(data); Q_UNUSED(result); qAssert(false); /* Need STL compatibility ! */ }
00160 #endif // ((! defined(QT_NO_STL)) && (! defined(QT_NO_STL_WCHAR)))
00161    };
00162 
00163 };
00164 
00165 } // namespace trait
00166 } // namespace qx
00167 
00168 #endif // _QX_ARCHIVE_WIDE_TRAITS_H_