File indexing completed on 2024-05-05 04:44:41

0001 /*  This file is part of the KDE project
0002     Copyright (C) 2007-2008 Matthias Kretz <kretz@kde.org>
0003 
0004     This library is free software; you can redistribute it and/or
0005     modify it under the terms of the GNU Library General Public
0006     License version 2 as published by the Free Software Foundation.
0007 
0008     This library is distributed in the hope that it will be useful,
0009     but WITHOUT ANY WARRANTY; without even the implied warranty of
0010     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0011     Library General Public License for more details.
0012 
0013     You should have received a copy of the GNU Library General Public License
0014     along with this library; see the file COPYING.LIB.  If not, write to
0015     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0016     Boston, MA 02110-1301, USA.
0017 
0018 */
0019 
0020 #include "packet.h"
0021 #include "packet_p.h"
0022 #include "packetpool.h"
0023 #include "packetpool_p.h"
0024 
0025 namespace Phonon
0026 {
0027 
0028 Q_GLOBAL_STATIC(PacketPrivate, shared_null_packet)
0029 
0030 Packet::Packet() : d_ptr(shared_null_packet()) { d_ptr->ref.ref(); }
0031 Packet::Packet(PacketPool &pool) : d_ptr(pool.d_ptr->acquirePacket().d_ptr) { d_ptr->ref.ref(); }
0032 Packet::Packet(const Packet &rhs) : d_ptr(rhs.d_ptr) { d_ptr->ref.ref(); }
0033 Packet::Packet(PacketPrivate &dd) : d_ptr(&dd) { d_ptr->ref.ref(); }
0034 Packet &Packet::operator=(const Packet &rhs)
0035 {
0036     if (!d_ptr->ref.deref()) {
0037         Q_ASSERT(d_ptr->m_pool);
0038         d_ptr->m_pool->releasePacket(*this);
0039     }
0040     d_ptr = rhs.d_ptr;
0041     d_ptr->ref.ref();
0042     return *this;
0043 }
0044 Packet::~Packet()
0045 {
0046     if (!d_ptr->ref.deref()) {
0047         Q_ASSERT(d_ptr->m_pool);
0048         d_ptr->m_pool->releasePacket(*this);
0049     }
0050 }
0051 bool Packet::operator==(const Packet &rhs) const { return d_ptr == rhs.d_ptr; }
0052 bool Packet::operator!=(const Packet &rhs) const { return d_ptr != rhs.d_ptr; }
0053 bool Packet::isNull() const { return d_ptr->m_pool == nullptr; }
0054 const char *Packet::data() const { return d_ptr->m_data; }
0055 char *Packet::data() { return d_ptr->m_data; }
0056 int Packet::size() const { return d_ptr->m_size; }
0057 void Packet::setSize(int size) { d_ptr->m_size = size; }
0058 int Packet::capacity() const { return d_ptr->m_pool ? d_ptr->m_pool->packetSize : 0; }
0059 
0060 } // namespace Phonon