QxOrm  1.5.0
C++ Object Relational Mapping library
archive_wide_traits.h
Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** https://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 <QtCore/qstring.h>
00052 
00053 #include <QxSerialize/boost/QxSerializeInclude.h>
00054 
00055 namespace qx {
00056 namespace trait {
00057 
00062 template <typename T> struct is_archive_wide { enum { value = false }; };
00063 
00064 #if _QX_SERIALIZE_WIDE_BINARY
00065 template <> struct is_archive_wide<boost::archive::binary_wiarchive> { enum { value = true }; };
00066 template <> struct is_archive_wide<boost::archive::binary_woarchive> { enum { value = true }; };
00067 #endif // _QX_SERIALIZE_WIDE_BINARY
00068 
00069 #if _QX_SERIALIZE_WIDE_TEXT
00070 template <> struct is_archive_wide<boost::archive::text_wiarchive>   { enum { value = true }; };
00071 template <> struct is_archive_wide<boost::archive::text_woarchive>   { enum { value = true }; };
00072 #endif // _QX_SERIALIZE_WIDE_TEXT
00073 
00074 #if _QX_SERIALIZE_WIDE_XML
00075 template <> struct is_archive_wide<boost::archive::xml_wiarchive>    { enum { value = true }; };
00076 template <> struct is_archive_wide<boost::archive::xml_woarchive>    { enum { value = true }; };
00077 #endif // _QX_SERIALIZE_WIDE_XML
00078 
00079 template <typename T>
00080 class archive_wide_traits
00081 {
00082 
00083 public:
00084 
00085    enum { is_wide = qx::trait::is_archive_wide<T>::value };
00086 
00087    typedef typename std::conditional<is_wide, wchar_t, char>::type type_char;
00088    typedef typename std::conditional<is_wide, std::wstring, std::string>::type type_string;
00089 
00090    typedef typename std::conditional<is_wide, std::wistream, std::istream>::type type_istream;
00091    typedef typename std::conditional<is_wide, std::wostream, std::ostream>::type type_ostream;
00092 
00093    typedef typename std::conditional<is_wide, std::wstringstream, std::stringstream>::type type_stringstream;
00094    typedef typename std::conditional<is_wide, std::wistringstream, std::istringstream>::type type_istringstream;
00095    typedef typename std::conditional<is_wide, std::wostringstream, std::ostringstream>::type type_ostringstream;
00096 
00097    typedef typename std::conditional<is_wide, std::wfstream, std::fstream>::type type_fstream;
00098    typedef typename std::conditional<is_wide, std::wifstream, std::ifstream>::type type_ifstream;
00099    typedef typename std::conditional<is_wide, std::wofstream, std::ofstream>::type type_ofstream;
00100 
00101    static inline QString toQString(const type_string & str)                   { return cvtQString<is_wide, 0>::toQString(str); }
00102    static inline void fromQString(const QString & str, type_string & result)  { cvtQString<is_wide, 0>::fromQString(str, result); }
00103 
00104    static inline QByteArray toQByteArray(const type_string & str, type_string * owner) { return cvtQByteArray<is_wide, 0>::toQByteArray(str, owner); }
00105    static inline void fromQByteArray(const QByteArray & data, type_string & result)    { cvtQByteArray<is_wide, 0>::fromQByteArray(data, result); }
00106 
00107 private:
00108 
00109    template <bool isWide /* = false */, int dummy>
00110    struct cvtQString
00111    {
00112 #ifndef QT_NO_STL
00113       static inline QString toQString(const std::string & str)                   { return QString::fromStdString(str); }
00114       static inline void fromQString(const QString & str, std::string & result)  { result = str.toStdString(); }
00115 #else // QT_NO_STL
00116       static inline QString toQString(const std::string & str)                   { return QString::fromLatin1(str.data(), int(str.size())); }
00117       static inline void fromQString(const QString & str, std::string & result)  { result = str.toLatin1().constData(); }
00118 #endif // QT_NO_STL
00119    };
00120 
00121    template <int dummy>
00122    struct cvtQString<true, dummy>
00123    {
00124 #if ((! defined(QT_NO_STL)) && (! defined(QT_NO_STL_WCHAR)))
00125       static inline QString toQString(const std::wstring & str)                     { return QString::fromStdWString(str); }
00126       static inline void fromQString(const QString & str, std::wstring & result)    { result = str.toStdWString(); }
00127 #else // ((! defined(QT_NO_STL)) && (! defined(QT_NO_STL_WCHAR)))
00128       static inline QString toQString(const std::wstring & str)                     { Q_UNUSED(str); qAssert(false); /* Need STL compatibility ! */ return QString(); }
00129       static inline void fromQString(const QString & str, std::wstring & result)    { Q_UNUSED(str); Q_UNUSED(result); qAssert(false); /* Need STL compatibility ! */ }
00130 #endif // ((! defined(QT_NO_STL)) && (! defined(QT_NO_STL_WCHAR)))
00131    };
00132 
00133    template <bool isWide /* = false */, int dummy>
00134    struct cvtQByteArray
00135    {
00136       static inline QByteArray toQByteArray(const std::string & str, std::string * owner)
00137       { if (owner) { (* owner) = str; }; return (owner ? QByteArray::fromRawData(owner->data(), owner->size()) : QByteArray(str.data(), str.size())); }
00138       static inline void fromQByteArray(const QByteArray & data, std::string & result)
00139       { result.clear(); result.append(data.constData(), data.size()); }
00140    };
00141 
00142    template <int dummy>
00143    struct cvtQByteArray<true, dummy>
00144    {
00145       static inline QByteArray toQByteArray(const std::wstring & str, std::wstring * owner)
00146 #if ((! defined(QT_NO_STL)) && (! defined(QT_NO_STL_WCHAR)))
00147       { Q_UNUSED(owner); return QString::fromStdWString(str).toUtf8(); }
00148 #else // ((! defined(QT_NO_STL)) && (! defined(QT_NO_STL_WCHAR)))
00149       { Q_UNUSED(owner); Q_UNUSED(str); qAssert(false); /* Need STL compatibility ! */ return QByteArray(); }
00150 #endif // ((! defined(QT_NO_STL)) && (! defined(QT_NO_STL_WCHAR)))
00151 
00152       static inline void fromQByteArray(const QByteArray & data, std::wstring & result)
00153 #if ((! defined(QT_NO_STL)) && (! defined(QT_NO_STL_WCHAR)))
00154       { result = QString::fromUtf8(data.constData(), data.size()).toStdWString(); }
00155 #else // ((! defined(QT_NO_STL)) && (! defined(QT_NO_STL_WCHAR)))
00156       { Q_UNUSED(data); Q_UNUSED(result); qAssert(false); /* Need STL compatibility ! */ }
00157 #endif // ((! defined(QT_NO_STL)) && (! defined(QT_NO_STL_WCHAR)))
00158    };
00159 
00160 };
00161 
00162 } // namespace trait
00163 } // namespace qx
00164 
00165 #endif // _QX_ARCHIVE_WIDE_TRAITS_H_