diff options
author | Allan Sandfeld Jensen <allan.jensen@digia.com> | 2014-06-19 11:01:33 +0200 |
---|---|---|
committer | Thiago Macieira <thiago.macieira@intel.com> | 2014-06-25 16:51:53 +0200 |
commit | a1fc11ca655850a47701f3715f1408c336ddb6c7 (patch) | |
tree | 39f897d1674a11368ef98a840edea178be48ee4e /src/corelib/tools/qbytearray.h | |
parent | 273ed8e0fa527f4c4382d45eaced54314318173f (diff) |
Introduce std::string conversion to QByteArray
Add conversion methods similar to those in QString to QByteArray. This
is often more useful than the QString version since std::string like
QByteArray are byte arrays.
[ChangeLog][QtCore][QByteArray] Added convenience methods to convert
directly to and from std::string.
Change-Id: I92c29d4bb1d9e06a667dd9cdd936970e2d272006
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Olivier Goffart <ogoffart@woboq.com>
Diffstat (limited to 'src/corelib/tools/qbytearray.h')
-rw-r--r-- | src/corelib/tools/qbytearray.h | 10 |
1 files changed, 10 insertions, 0 deletions
diff --git a/src/corelib/tools/qbytearray.h b/src/corelib/tools/qbytearray.h index 9155ddc977..b5af305233 100644 --- a/src/corelib/tools/qbytearray.h +++ b/src/corelib/tools/qbytearray.h @@ -50,6 +50,8 @@ #include <string.h> #include <stdarg.h> +#include <string> + #ifdef truncate #error qbytearray.h must be included before any header file that defines truncate #endif @@ -397,6 +399,9 @@ public: void push_front(const char *c); void push_front(const QByteArray &a); + static inline QByteArray fromStdString(const std::string &s); + inline std::string toStdString() const; + inline int count() const { return d->size; } int length() const { return d->size; } bool isNull() const; @@ -620,6 +625,11 @@ inline QByteArray &QByteArray::setNum(uint n, int base) inline QByteArray &QByteArray::setNum(float n, char f, int prec) { return setNum(double(n),f,prec); } +inline std::string QByteArray::toStdString() const +{ return std::string(constData(), length()); } + +inline QByteArray QByteArray::fromStdString(const std::string &s) +{ return QByteArray(s.data(), int(s.size())); } #if !defined(QT_NO_DATASTREAM) || (defined(QT_BOOTSTRAPPED) && !defined(QT_BUILD_QMAKE)) Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QByteArray &); |