Braced list initialization for StringList and ByteVectorList (#1154)

Additionally, provide operator==() and operator!=() for Map and
operator<<() for ByteVectorList to facilitate the writing of tests.
This commit is contained in:
Urs Fleisch 2023-10-07 08:46:59 +02:00
parent 21b08c0dcb
commit 41077aa57e
7 changed files with 79 additions and 1 deletions

View File

@ -72,6 +72,11 @@ ByteVectorList::ByteVectorList(const ByteVectorList &l) :
{
}
ByteVectorList::ByteVectorList(std::initializer_list<ByteVector> init) :
List<ByteVector>(init)
{
}
ByteVectorList &ByteVectorList::operator=(const ByteVectorList &l)
{
if(this == &l)
@ -81,6 +86,12 @@ ByteVectorList &ByteVectorList::operator=(const ByteVectorList &l)
return *this;
}
ByteVectorList &ByteVectorList::operator=(std::initializer_list<ByteVector> init)
{
List<ByteVector>::operator=(init);
return *this;
}
ByteVector ByteVectorList::toByteVector(const ByteVector &separator) const
{
ByteVector v;
@ -93,3 +104,18 @@ ByteVector ByteVectorList::toByteVector(const ByteVector &separator) const
return v;
}
////////////////////////////////////////////////////////////////////////////////
// related functions
////////////////////////////////////////////////////////////////////////////////
std::ostream &operator<<(std::ostream &s, const ByteVectorList &l)
{
for(auto it = l.begin(); it != l.end(); ++it) {
if(it != l.begin()) {
s << ' ';
}
s << *it;
}
return s;
}

View File

@ -59,7 +59,13 @@ namespace TagLib {
*/
ByteVectorList(const ByteVectorList &l);
/*!
* Construct a ByteVectorList with the contents of the braced initializer list.
*/
ByteVectorList(std::initializer_list<ByteVector> init);
ByteVectorList &operator=(const ByteVectorList &);
ByteVectorList &operator=(std::initializer_list<ByteVector> init);
/*!
* Convert the ByteVectorList to a ByteVector separated by \a separator. By
@ -83,4 +89,10 @@ namespace TagLib {
} // namespace TagLib
/*!
* \related TagLib::ByteVectorList
* Send the ByteVectorList to an output stream.
*/
std::ostream TAGLIB_EXPORT &operator<<(std::ostream &s, const TagLib::ByteVectorList &l);
#endif

View File

@ -73,7 +73,7 @@ namespace TagLib {
List(const List<T> &l);
/*!
* Construct a List with the contents of the braced initiliazer list
* Construct a List with the contents of the braced initializer list.
*/
List(std::initializer_list<T> init);

View File

@ -212,6 +212,17 @@ namespace TagLib {
*/
void swap(Map<Key, T> &m);
/*!
* Compares this map with \a m and returns true if all of the elements are
* the same.
*/
bool operator==(const Map<Key, T> &m) const;
/*!
* Compares this map with \a m and returns true if the maps differ.
*/
bool operator!=(const Map<Key, T> &m) const;
protected:
/*
* If this List is being shared via implicit sharing, do a deep copy of the

View File

@ -205,6 +205,18 @@ void Map<Key, T>::swap(Map<Key, T> &m)
swap(d, m.d);
}
template <class Key, class T>
bool Map<Key, T>::operator==(const Map<Key, T> &m) const
{
return d->map == m.d->map;
}
template <class Key, class T>
bool Map<Key, T>::operator!=(const Map<Key, T> &m) const
{
return d->map != m.d->map;
}
////////////////////////////////////////////////////////////////////////////////
// protected members
////////////////////////////////////////////////////////////////////////////////

View File

@ -61,6 +61,11 @@ StringList::StringList(const StringList &l) :
{
}
StringList::StringList(std::initializer_list<String> init) :
List<String>(init)
{
}
StringList &StringList::operator=(const StringList &l)
{
if(this == &l)
@ -70,6 +75,12 @@ StringList &StringList::operator=(const StringList &l)
return *this;
}
StringList &StringList::operator=(std::initializer_list<String> init)
{
List<String>::operator=(init);
return *this;
}
StringList::StringList(const String &s)
{
append(s);

View File

@ -58,7 +58,13 @@ namespace TagLib {
*/
StringList(const StringList &l);
/*!
* Construct a StringList with the contents of the braced initializer list.
*/
StringList(std::initializer_list<String> init);
StringList &operator=(const StringList &);
StringList &operator=(std::initializer_list<String> init);
/*!
* Constructs a StringList with \a s as a member.