Commit 19e01596 authored by Marco Bubke's avatar Marco Bubke

Utils: Improve string vector for string view usage

Some times it makes sense to have a Utils::StringViewVector instead
of a Utils::SmallStringVector because you don't want to hold the ownership.

Change-Id: I5ae39efe21aebbf82d4b3effa9e67eae6727ba8b
Reviewed-by: Tim Jenssen's avatarTim Jenssen <tim.jenssen@qt.io>
parent 3b46c095
......@@ -113,8 +113,8 @@ void PrintTo(const BasicSmallString<Size> &string, ::std::ostream *os)
*os<< "'";
}
template <uint Size>
QDataStream &operator<<(QDataStream &out, const BasicSmallStringVector<Size> &stringVector)
template <typename String>
QDataStream &operator<<(QDataStream &out, const BasicSmallStringVector<String> &stringVector)
{
out << quint64(stringVector.size());
......@@ -124,8 +124,8 @@ QDataStream &operator<<(QDataStream &out, const BasicSmallStringVector<Size> &s
return out;
}
template <uint Size>
QDataStream &operator>>(QDataStream &in, BasicSmallStringVector<Size> &stringVector)
template <typename String>
QDataStream &operator>>(QDataStream &in, BasicSmallStringVector<String> &stringVector)
{
stringVector.clear();
......@@ -136,7 +136,7 @@ QDataStream &operator>>(QDataStream &in, BasicSmallStringVector<Size> &stringVe
stringVector.reserve(size);
for (quint64 i = 0; i < size; ++i) {
BasicSmallString<Size> string;
String string;
in >> string;
......@@ -146,18 +146,18 @@ QDataStream &operator>>(QDataStream &in, BasicSmallStringVector<Size> &stringVe
return in;
}
template <uint Size>
QDebug operator<<(QDebug debug, const BasicSmallStringVector<Size> &stringVector)
template <typename String>
QDebug operator<<(QDebug debug, const BasicSmallStringVector<String> &stringVector)
{
debug << "StringVector(" << stringVector.join(BasicSmallString<Size>(", ")).constData() << ")";
debug << "StringVector(" << stringVector.join(", ").constData() << ")";
return debug;
}
template <uint Size>
void PrintTo(const BasicSmallStringVector<Size> &textVector, ::std::ostream* os)
template <typename String>
void PrintTo(const BasicSmallStringVector<String> &textVector, ::std::ostream* os)
{
*os << "[" << textVector.join(BasicSmallString<Size>(", ")).constData() << "]";
*os << "[" << textVector.join(", ").constData() << "]";
}
} // namespace Utils
......
......@@ -35,25 +35,27 @@
namespace Utils {
template<uint SmallStringSize>
class BasicSmallStringVector : public std::vector<BasicSmallString<SmallStringSize>>
template<typename String>
class BasicSmallStringVector : public std::vector<String>
{
using SmallString = BasicSmallString<SmallStringSize>;
using Base = std::vector<SmallString>;
using Base = std::vector<String>;
public:
BasicSmallStringVector() = default;
using Base::Base;
explicit BasicSmallStringVector(const Base &stringVector)
: Base(stringVector.begin(), stringVector.end())
{
}
BasicSmallStringVector(std::initializer_list<SmallString> list)
BasicSmallStringVector(std::initializer_list<String> list)
{
Base::reserve(list.size());
for (auto &&entry : list)
Base::push_back(entry.clone());
Base::push_back(std::move(entry));
}
explicit BasicSmallStringVector(const QStringList &stringList)
......@@ -79,7 +81,7 @@ public:
BasicSmallStringVector &operator=(BasicSmallStringVector &&)
noexcept(std::is_nothrow_move_assignable<Base>::value) = default;
SmallString join(SmallString &&separator) const
SmallString join(SmallStringView separator) const
{
SmallString joinedString;
......@@ -110,7 +112,7 @@ public:
return hasEntry;
}
void append(SmallString &&string)
void append(String &&string)
{
push_back(std::move(string));
}
......@@ -153,6 +155,9 @@ private:
}
};
using SmallStringVector = BasicSmallStringVector<31>;
using PathStringVector = BasicSmallStringVector<190>;
using SmallStringVector = BasicSmallStringVector<BasicSmallString<31>>;
using PathStringVector = BasicSmallStringVector<BasicSmallString<190>>;
using StringViewVector = BasicSmallStringVector<SmallStringView>;
} // namespace Utils;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment