QxOrm 1.4.9
C++ Object Relational Mapping library
Loading...
Searching...
No Matches
archive_wide_traits.h
Go to the documentation of this file.
1/****************************************************************************
2**
3** https://www.qxorm.com/
4** Copyright (C) 2013 Lionel Marty (contact@qxorm.com)
5**
6** This file is part of the QxOrm library
7**
8** This software is provided 'as-is', without any express or implied
9** warranty. In no event will the authors be held liable for any
10** damages arising from the use of this software
11**
12** Commercial Usage
13** Licensees holding valid commercial QxOrm licenses may use this file in
14** accordance with the commercial license agreement provided with the
15** Software or, alternatively, in accordance with the terms contained in
16** a written agreement between you and Lionel Marty
17**
18** GNU General Public License Usage
19** Alternatively, this file may be used under the terms of the GNU
20** General Public License version 3.0 as published by the Free Software
21** Foundation and appearing in the file 'license.gpl3.txt' included in the
22** packaging of this file. Please review the following information to
23** ensure the GNU General Public License version 3.0 requirements will be
24** met : http://www.gnu.org/copyleft/gpl.html
25**
26** If you are unsure which license is appropriate for your use, or
27** if you have questions regarding the use of this file, please contact :
28** contact@qxorm.com
29**
30****************************************************************************/
31
32#ifndef _QX_ARCHIVE_WIDE_TRAITS_H_
33#define _QX_ARCHIVE_WIDE_TRAITS_H_
34
35#ifdef _MSC_VER
36#pragma once
37#endif
38
46#include <string>
47#include <iostream>
48#include <sstream>
49#include <fstream>
50
51#include <QtCore/qstring.h>
52
54
55namespace qx {
56namespace trait {
57
62template <typename T> struct is_archive_wide { enum { value = false }; };
63
64#if _QX_SERIALIZE_WIDE_BINARY
65template <> struct is_archive_wide<boost::archive::binary_wiarchive> { enum { value = true }; };
66template <> struct is_archive_wide<boost::archive::binary_woarchive> { enum { value = true }; };
67#endif // _QX_SERIALIZE_WIDE_BINARY
68
69#if _QX_SERIALIZE_WIDE_TEXT
70template <> struct is_archive_wide<boost::archive::text_wiarchive> { enum { value = true }; };
71template <> struct is_archive_wide<boost::archive::text_woarchive> { enum { value = true }; };
72#endif // _QX_SERIALIZE_WIDE_TEXT
73
74#if _QX_SERIALIZE_WIDE_XML
75template <> struct is_archive_wide<boost::archive::xml_wiarchive> { enum { value = true }; };
76template <> struct is_archive_wide<boost::archive::xml_woarchive> { enum { value = true }; };
77#endif // _QX_SERIALIZE_WIDE_XML
78
79template <typename T>
81{
82
83public:
84
86
87 typedef typename std::conditional<is_wide, wchar_t, char>::type type_char;
88 typedef typename std::conditional<is_wide, std::wstring, std::string>::type type_string;
89
90 typedef typename std::conditional<is_wide, std::wistream, std::istream>::type type_istream;
91 typedef typename std::conditional<is_wide, std::wostream, std::ostream>::type type_ostream;
92
93 typedef typename std::conditional<is_wide, std::wstringstream, std::stringstream>::type type_stringstream;
94 typedef typename std::conditional<is_wide, std::wistringstream, std::istringstream>::type type_istringstream;
95 typedef typename std::conditional<is_wide, std::wostringstream, std::ostringstream>::type type_ostringstream;
96
97 typedef typename std::conditional<is_wide, std::wfstream, std::fstream>::type type_fstream;
98 typedef typename std::conditional<is_wide, std::wifstream, std::ifstream>::type type_ifstream;
99 typedef typename std::conditional<is_wide, std::wofstream, std::ofstream>::type type_ofstream;
100
101 static inline QString toQString(const type_string & str) { return cvtQString<is_wide, 0>::toQString(str); }
102 static inline void fromQString(const QString & str, type_string & result) { cvtQString<is_wide, 0>::fromQString(str, result); }
103
104 static inline QByteArray toQByteArray(const type_string & str, type_string * owner) { return cvtQByteArray<is_wide, 0>::toQByteArray(str, owner); }
105 static inline void fromQByteArray(const QByteArray & data, type_string & result) { cvtQByteArray<is_wide, 0>::fromQByteArray(data, result); }
106
107private:
108
109 template <bool isWide /* = false */, int dummy>
111 {
112#ifndef QT_NO_STL
113 static inline QString toQString(const std::string & str) { return QString::fromStdString(str); }
114 static inline void fromQString(const QString & str, std::string & result) { result = str.toStdString(); }
115#else // QT_NO_STL
116 static inline QString toQString(const std::string & str) { return QString::fromLatin1(str.data(), int(str.size())); }
117 static inline void fromQString(const QString & str, std::string & result) { result = str.toLatin1().constData(); }
118#endif // QT_NO_STL
119 };
120
121 template <int dummy>
122 struct cvtQString<true, dummy>
123 {
124#if ((! defined(QT_NO_STL)) && (! defined(QT_NO_STL_WCHAR)))
125 static inline QString toQString(const std::wstring & str) { return QString::fromStdWString(str); }
126 static inline void fromQString(const QString & str, std::wstring & result) { result = str.toStdWString(); }
127#else // ((! defined(QT_NO_STL)) && (! defined(QT_NO_STL_WCHAR)))
128 static inline QString toQString(const std::wstring & str) { Q_UNUSED(str); qAssert(false); /* Need STL compatibility ! */ return QString(); }
129 static inline void fromQString(const QString & str, std::wstring & result) { Q_UNUSED(str); Q_UNUSED(result); qAssert(false); /* Need STL compatibility ! */ }
130#endif // ((! defined(QT_NO_STL)) && (! defined(QT_NO_STL_WCHAR)))
131 };
132
133 template <bool isWide /* = false */, int dummy>
135 {
136 static inline QByteArray toQByteArray(const std::string & str, std::string * owner)
137 { if (owner) { (* owner) = str; }; return (owner ? QByteArray::fromRawData(owner->data(), owner->size()) : QByteArray(str.data(), str.size())); }
138 static inline void fromQByteArray(const QByteArray & data, std::string & result)
139 { result.clear(); result.append(data.constData(), data.size()); }
140 };
141
142 template <int dummy>
143 struct cvtQByteArray<true, dummy>
144 {
145 static inline QByteArray toQByteArray(const std::wstring & str, std::wstring * owner)
146#if ((! defined(QT_NO_STL)) && (! defined(QT_NO_STL_WCHAR)))
147 { Q_UNUSED(owner); return QString::fromStdWString(str).toUtf8(); }
148#else // ((! defined(QT_NO_STL)) && (! defined(QT_NO_STL_WCHAR)))
149 { Q_UNUSED(owner); Q_UNUSED(str); qAssert(false); /* Need STL compatibility ! */ return QByteArray(); }
150#endif // ((! defined(QT_NO_STL)) && (! defined(QT_NO_STL_WCHAR)))
151
152 static inline void fromQByteArray(const QByteArray & data, std::wstring & result)
153#if ((! defined(QT_NO_STL)) && (! defined(QT_NO_STL_WCHAR)))
154 { result = QString::fromUtf8(data.constData(), data.size()).toStdWString(); }
155#else // ((! defined(QT_NO_STL)) && (! defined(QT_NO_STL_WCHAR)))
156 { Q_UNUSED(data); Q_UNUSED(result); qAssert(false); /* Need STL compatibility ! */ }
157#endif // ((! defined(QT_NO_STL)) && (! defined(QT_NO_STL_WCHAR)))
158 };
159
160};
161
162} // namespace trait
163} // namespace qx
164
165#endif // _QX_ARCHIVE_WIDE_TRAITS_H_
#define qAssert(x)
Definition QxMacro.h:52
std::conditional< is_wide, std::wostringstream, std::ostringstream >::type type_ostringstream
std::conditional< is_wide, std::wfstream, std::fstream >::type type_fstream
std::conditional< is_wide, std::wofstream, std::ofstream >::type type_ofstream
static void fromQByteArray(const QByteArray &data, type_string &result)
static QString toQString(const type_string &str)
std::conditional< is_wide, std::wostream, std::ostream >::type type_ostream
std::conditional< is_wide, std::wstring, std::string >::type type_string
std::conditional< is_wide, std::wistream, std::istream >::type type_istream
static QByteArray toQByteArray(const type_string &str, type_string *owner)
std::conditional< is_wide, std::wstringstream, std::stringstream >::type type_stringstream
std::conditional< is_wide, std::wistringstream, std::istringstream >::type type_istringstream
std::conditional< is_wide, wchar_t, char >::type type_char
std::conditional< is_wide, std::wifstream, std::ifstream >::type type_ifstream
static void fromQString(const QString &str, type_string &result)
Root namespace for all QxOrm library features.
static void fromQByteArray(const QByteArray &data, std::wstring &result)
static QByteArray toQByteArray(const std::wstring &str, std::wstring *owner)
static QByteArray toQByteArray(const std::string &str, std::string *owner)
static void fromQByteArray(const QByteArray &data, std::string &result)
static void fromQString(const QString &str, std::wstring &result)
static QString toQString(const std::string &str)
static void fromQString(const QString &str, std::string &result)
qx::trait::is_archive_wide<T>::value : define if a boost::archive type uses wide string character and...