File indexing completed on 2024-05-12 04:44:37

0001 /*
0002     Copyright (C) 2012 Harald Sitter <sitter@kde.org>
0003 
0004     This library is free software; you can redistribute it and/or
0005     modify it under the terms of the GNU Lesser General Public
0006     License as published by the Free Software Foundation; either
0007     version 2.1 of the License, or (at your option) any later version.
0008 
0009     This library is distributed in the hope that it will be useful,
0010     but WITHOUT ANY WARRANTY; without even the implied warranty of
0011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012     Lesser General Public License for more details.
0013 
0014     You should have received a copy of the GNU Lesser General Public
0015     License along with this library.  If not, see <http://www.gnu.org/licenses/>.
0016 */
0017 
0018 #ifndef PHONON_VLC_VSTRING_H
0019 #define PHONON_VLC_VSTRING_H
0020 
0021 #include <QtCore/QString>
0022 #include <vlc/libvlc.h>
0023 #include <vlc/libvlc_version.h>
0024 
0025 /**
0026  * @brief The VString class wraps around a char* returned from libvlc functions.
0027  * Directly from libvlc functions returned cstrings are unique in two ways.
0028  * For one they are to be freed by the caller, and for another they are always
0029  * UTF8 (as VLC internally only uses UTF8).
0030  *
0031  * Particularly the first point is where VString comes in, it will on destruction
0032  * call libvlc_free to free the string, thus avoiding memleaks.
0033  * Additionally it conveniently converts to QString using either toQString
0034  * or implicit cast to QString which makes it completely transparent to other
0035  * functions. It also prevents you from carrying the cstring out of scope and
0036  * render implicit copies of it invalid once free is called somewhere.
0037  * Both functions use QString::fromUtf8 and are therefore the best way to
0038  * process the string.
0039  */
0040 class VString
0041 {
0042 public:
0043     explicit VString(char *vlcString) : m_vlcString(vlcString) {}
0044     ~VString()
0045     {
0046         libvlc_free(m_vlcString);
0047     }
0048 
0049     // VLC internally only uses UTF8!
0050     QString toQString() { return QString::fromUtf8(m_vlcString); }
0051     operator QString() { return toQString(); }
0052 
0053 private:
0054     VString() {}
0055 
0056     char *m_vlcString;
0057 };
0058 
0059 #endif // PHONON_VLC_VSTRING_H