archive_wide_traits.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. #ifndef _QX_ARCHIVE_WIDE_TRAITS_H_
  32. #define _QX_ARCHIVE_WIDE_TRAITS_H_
  33. #ifdef _MSC_VER
  34. #pragma once
  35. #endif
  36. /*!
  37. * \file archive_wide_traits.h
  38. * \author Lionel Marty
  39. * \ingroup QxTraits
  40. * \brief qx::trait::is_archive_wide<T>::value : define if a boost::archive type uses wide string character and stream (for example std::wstring) or not (for example std::string)
  41. */
  42. #include <string>
  43. #include <iostream>
  44. #include <sstream>
  45. #include <fstream>
  46. #include <QtCore/qstring.h>
  47. #include <QxSerialize/boost/QxSerializeInclude.h>
  48. namespace qx {
  49. namespace trait {
  50. /*!
  51. * \ingroup QxTraits
  52. * \brief qx::trait::is_archive_wide<T>::value : define if a boost::archive type uses wide string character and stream (for example std::wstring) or not (for example std::string)
  53. */
  54. template <typename T> struct is_archive_wide { enum { value = false }; };
  55. #if _QX_SERIALIZE_WIDE_BINARY
  56. template <> struct is_archive_wide<boost::archive::binary_wiarchive> { enum { value = true }; };
  57. template <> struct is_archive_wide<boost::archive::binary_woarchive> { enum { value = true }; };
  58. #endif // _QX_SERIALIZE_WIDE_BINARY
  59. #if _QX_SERIALIZE_WIDE_TEXT
  60. template <> struct is_archive_wide<boost::archive::text_wiarchive> { enum { value = true }; };
  61. template <> struct is_archive_wide<boost::archive::text_woarchive> { enum { value = true }; };
  62. #endif // _QX_SERIALIZE_WIDE_TEXT
  63. #if _QX_SERIALIZE_WIDE_XML
  64. template <> struct is_archive_wide<boost::archive::xml_wiarchive> { enum { value = true }; };
  65. template <> struct is_archive_wide<boost::archive::xml_woarchive> { enum { value = true }; };
  66. #endif // _QX_SERIALIZE_WIDE_XML
  67. template <typename T>
  68. class archive_wide_traits
  69. {
  70. public:
  71. enum { is_wide = qx::trait::is_archive_wide<T>::value };
  72. typedef typename std::conditional<is_wide, wchar_t, char>::type type_char;
  73. typedef typename std::conditional<is_wide, std::wstring, std::string>::type type_string;
  74. typedef typename std::conditional<is_wide, std::wistream, std::istream>::type type_istream;
  75. typedef typename std::conditional<is_wide, std::wostream, std::ostream>::type type_ostream;
  76. typedef typename std::conditional<is_wide, std::wstringstream, std::stringstream>::type type_stringstream;
  77. typedef typename std::conditional<is_wide, std::wistringstream, std::istringstream>::type type_istringstream;
  78. typedef typename std::conditional<is_wide, std::wostringstream, std::ostringstream>::type type_ostringstream;
  79. typedef typename std::conditional<is_wide, std::wfstream, std::fstream>::type type_fstream;
  80. typedef typename std::conditional<is_wide, std::wifstream, std::ifstream>::type type_ifstream;
  81. typedef typename std::conditional<is_wide, std::wofstream, std::ofstream>::type type_ofstream;
  82. static inline QString toQString(const type_string & str) { return cvtQString<is_wide, 0>::toQString(str); }
  83. static inline void fromQString(const QString & str, type_string & result) { cvtQString<is_wide, 0>::fromQString(str, result); }
  84. static inline QByteArray toQByteArray(const type_string & str, type_string * owner) { return cvtQByteArray<is_wide, 0>::toQByteArray(str, owner); }
  85. static inline void fromQByteArray(const QByteArray & data, type_string & result) { cvtQByteArray<is_wide, 0>::fromQByteArray(data, result); }
  86. private:
  87. template <bool isWide /* = false */, int dummy>
  88. struct cvtQString
  89. {
  90. #ifndef QT_NO_STL
  91. static inline QString toQString(const std::string & str) { return QString::fromStdString(str); }
  92. static inline void fromQString(const QString & str, std::string & result) { result = str.toStdString(); }
  93. #else // QT_NO_STL
  94. static inline QString toQString(const std::string & str) { return QString::fromLatin1(str.data(), int(str.size())); }
  95. static inline void fromQString(const QString & str, std::string & result) { result = str.toLatin1().constData(); }
  96. #endif // QT_NO_STL
  97. };
  98. template <int dummy>
  99. struct cvtQString<true, dummy>
  100. {
  101. #if ((! defined(QT_NO_STL)) && (! defined(QT_NO_STL_WCHAR)))
  102. static inline QString toQString(const std::wstring & str) { return QString::fromStdWString(str); }
  103. static inline void fromQString(const QString & str, std::wstring & result) { result = str.toStdWString(); }
  104. #else // ((! defined(QT_NO_STL)) && (! defined(QT_NO_STL_WCHAR)))
  105. static inline QString toQString(const std::wstring & str) { Q_UNUSED(str); qAssert(false); /* Need STL compatibility ! */ return QString(); }
  106. static inline void fromQString(const QString & str, std::wstring & result) { Q_UNUSED(str); Q_UNUSED(result); qAssert(false); /* Need STL compatibility ! */ }
  107. #endif // ((! defined(QT_NO_STL)) && (! defined(QT_NO_STL_WCHAR)))
  108. };
  109. template <bool isWide /* = false */, int dummy>
  110. struct cvtQByteArray
  111. {
  112. static inline QByteArray toQByteArray(const std::string & str, std::string * owner)
  113. { if (owner) { (* owner) = str; }; return (owner ? QByteArray::fromRawData(owner->data(), owner->size()) : QByteArray(str.data(), str.size())); }
  114. static inline void fromQByteArray(const QByteArray & data, std::string & result)
  115. { result.clear(); result.append(data.constData(), data.size()); }
  116. };
  117. template <int dummy>
  118. struct cvtQByteArray<true, dummy>
  119. {
  120. static inline QByteArray toQByteArray(const std::wstring & str, std::wstring * owner)
  121. #if ((! defined(QT_NO_STL)) && (! defined(QT_NO_STL_WCHAR)))
  122. { Q_UNUSED(owner); return QString::fromStdWString(str).toUtf8(); }
  123. #else // ((! defined(QT_NO_STL)) && (! defined(QT_NO_STL_WCHAR)))
  124. { Q_UNUSED(owner); Q_UNUSED(str); qAssert(false); /* Need STL compatibility ! */ return QByteArray(); }
  125. #endif // ((! defined(QT_NO_STL)) && (! defined(QT_NO_STL_WCHAR)))
  126. static inline void fromQByteArray(const QByteArray & data, std::wstring & result)
  127. #if ((! defined(QT_NO_STL)) && (! defined(QT_NO_STL_WCHAR)))
  128. { result = QString::fromUtf8(data.constData(), data.size()).toStdWString(); }
  129. #else // ((! defined(QT_NO_STL)) && (! defined(QT_NO_STL_WCHAR)))
  130. { Q_UNUSED(data); Q_UNUSED(result); qAssert(false); /* Need STL compatibility ! */ }
  131. #endif // ((! defined(QT_NO_STL)) && (! defined(QT_NO_STL_WCHAR)))
  132. };
  133. };
  134. } // namespace trait
  135. } // namespace qx
  136. #endif // _QX_ARCHIVE_WIDE_TRAITS_H_