/*  This file is part of the KDE project
    Copyright (C) 2007 Matthias Kretz <kretz@kde.org>

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) version 3.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.

*/

#include "proxyobject.h"
#include "backend.h"

namespace Phonon
{
namespace Proxy
{

ProxyObject::ProxyObject()
    : m_valid(true),
    m_objectPtr(0)
{
    Backend::addProxyObject(this);
}

ProxyObject::~ProxyObject()
{
    Q_ASSERT(!m_objectPtr);
    Backend::removeProxyObject(this);
}

void ProxyObject::createProxiedObject(BackendInterface::Class c, const QVariantList &args)
{
    QVariant v;
    Backend::_createProxiedObject(c, this, args) >> v;
    m_objectPtr = qvariant_cast<void *>(v);
    if (!m_objectPtr) {
        makeInvalid();
    }
    Q_ASSERT(m_objectPtr);
}

QDataStream &ProxyObject::send(FunctionId func) const
{
    Q_ASSERT(m_objectPtr);
    return Backend::_send(func, m_objectPtr);
}

QDataStream &ProxyObject::receive(FunctionId func) const
{
    QDataStream &s = Backend::_receive(func);
    ProxyObject *o = 0;
    s >> o;
    Q_ASSERT(o == this);
    if (func == Destructor) {
        const_cast<ProxyObject *>(this)->m_objectPtr = 0;
    }
    return s;
}

} // namespace Proxy
} // namespace Phonon
// vim: sw=4 sts=4 et tw=100
