LeechCraft 0.6.70-16373-g319c272718
Modular cross-platform feature rich live environment.
Loading...
Searching...
No Matches
qtutil.h
Go to the documentation of this file.
1/**********************************************************************
2 * LeechCraft - modular cross-platform feature rich internet client.
3 * Copyright (C) 2006-2014 Georg Rudoy
4 *
5 * Distributed under the Boost Software License, Version 1.0.
6 * (See accompanying file LICENSE or copy at https://www.boost.org/LICENSE_1_0.txt)
7 **********************************************************************/
8
9#pragma once
10
11#include "sllconfig.h"
12#include <memory>
13#include <utility>
14#include <QLatin1String>
15#include "ctstring.h"
16
17class QObject;
18
19namespace LC::Util
20{
47 template<typename Assoc>
48 auto Stlize (Assoc&& assoc) noexcept
49 {
50 struct Range
51 {
52 Assoc Assoc_;
53
54 auto begin () const { return Assoc_.keyValueBegin (); }
55 auto end () const { return Assoc_.keyValueEnd (); }
56 };
57
58 return Range { std::forward<Assoc> (assoc) };
59 }
60
61 template<typename Assoc>
62 auto StlizeKeys (Assoc&& assoc) noexcept
63 {
64 struct Range
65 {
66 Assoc Assoc_;
67
68 auto begin () const { return Assoc_.keyBegin (); }
69 auto end () const { return Assoc_.keyEnd (); }
70 };
71
72 return Range { std::forward<Assoc> (assoc) };
73 }
74
87 inline QByteArray AsByteArray (std::string_view view) noexcept
88 {
89 return QByteArray::fromRawData (view.data (), static_cast<int> (view.size ()));
90 }
91
102 inline QByteArray ToByteArray (std::string_view view) noexcept
103 {
104 return { view.data (), static_cast<int> (view.size ()) };
105 }
106
112 inline std::string_view AsStringView (const QByteArray& arr) noexcept
113 {
114 return { arr.constData (), static_cast<size_t> (arr.size ()) };
115 }
116
117 template<typename T, typename S>
118 std::pair<T, T> BreakAt (const T& str, S c) noexcept
119 {
120 const auto pos = str.indexOf (c);
121 if (pos == -1)
122 return { str, {} };
123
124 return { str.left (pos), str.mid (pos + 1) };
125 }
126
127 template<typename T>
128 void ReleaseInto (std::unique_ptr<T>&& ptr, QObject& parent)
129 {
130 ptr.release ()->setParent (&parent);
131 }
132
133 inline QString UnsafeFromView (QStringView sv)
134 {
135 return QString::fromRawData (sv.data (), sv.size ());
136 }
137}
138
139namespace LC
140{
141 constexpr QLatin1String operator"" _ql (const char *str, std::size_t size) noexcept
142 {
143 return QLatin1String { str, static_cast<int> (size) };
144 }
145
146 constexpr QStringView operator"" _qsv (const char16_t *str, std::size_t size) noexcept
147 {
148 return QStringView { str, static_cast<qsizetype> (size) };
149 }
150
151 inline QByteArray operator"" _qba (const char *str, std::size_t size) noexcept
152 {
153 return QByteArray::fromRawData (str, static_cast<int> (size));
154 }
155
156 template<Util::CtString S>
157 QString operator""_qs ()
158 {
159 return Util::ToString<S> ();
160 }
161}
QByteArray ToByteArray()
Definition ctstring.h:115
auto Stlize(Assoc &&assoc) noexcept
Converts an Qt's associative sequence assoc to an STL-like iteratable range.
Definition qtutil.h:48
std::pair< T, T > BreakAt(const T &str, S c) noexcept
Definition qtutil.h:118
void ReleaseInto(std::unique_ptr< T > &&ptr, QObject &parent)
Definition qtutil.h:128
QString ToString()
Definition ctstring.h:130
std::string_view AsStringView(const QByteArray &arr) noexcept
Create a std::string_view referring the data within a QByteArray.
Definition qtutil.h:112
QByteArray AsByteArray(std::string_view view) noexcept
Convert the view into a QByteArray without copying.
Definition qtutil.h:87
QString UnsafeFromView(QStringView sv)
Definition qtutil.h:133
auto StlizeKeys(Assoc &&assoc) noexcept
Definition qtutil.h:62
Definition constants.h:15