Skip to content

Commit

Permalink
Move binding and expression classes to separate files
Browse files Browse the repository at this point in the history
Change-Id: Ia9c6996a606e140f31681ecd26d93b1b0fdedf02
Reviewed-by: Roberto Raggi <roberto.raggi@nokia.com>
  • Loading branch information
Aaron Kennedy authored and Qt by Nokia committed Mar 13, 2012
1 parent 648c80c commit 5013d53
Show file tree
Hide file tree
Showing 15 changed files with 1,333 additions and 869 deletions.
12 changes: 10 additions & 2 deletions src/qml/qml/qml.pri
Expand Up @@ -47,7 +47,11 @@ SOURCES += \
$$PWD/qqmlimport.cpp \
$$PWD/qqmllist.cpp \
$$PWD/qqmllocale.cpp \
$$PWD/qlistmodelinterface.cpp
$$PWD/qlistmodelinterface.cpp \
$$PWD/qqmlabstractexpression.cpp \
$$PWD/qqmljavascriptexpression.cpp \
$$PWD/qqmlabstractbinding.cpp \
$$PWD/qqmlvaluetypeproxybinding.cpp \

HEADERS += \
$$PWD/qqmlglobal_p.h \
Expand Down Expand Up @@ -116,7 +120,11 @@ HEADERS += \
$$PWD/qqmlscriptstring_p.h \
$$PWD/qqmllocale_p.h \
$$PWD/qlistmodelinterface_p.h \
$$PWD/qqmlcomponentattached_p.h
$$PWD/qqmlcomponentattached_p.h \
$$PWD/qqmlabstractexpression_p.h \
$$PWD/qqmljavascriptexpression_p.h \
$$PWD/qqmlabstractbinding_p.h \
$$PWD/qqmlvaluetypeproxybinding_p.h \

include(parser/parser.pri)
include(rewriter/rewriter.pri)
Expand Down
193 changes: 193 additions & 0 deletions src/qml/qml/qqmlabstractbinding.cpp
@@ -0,0 +1,193 @@
/****************************************************************************
**
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/
**
** This file is part of the QtQml module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qqmlabstractbinding_p.h"

#include <private/qqmlbinding_p.h>
#include <private/qqmlvaluetypeproxybinding_p.h>

QT_BEGIN_NAMESPACE

QQmlAbstractBinding::QQmlAbstractBinding()
: m_prevBinding(0), m_nextBinding(0)
{
}

QQmlAbstractBinding::~QQmlAbstractBinding()
{
Q_ASSERT(m_prevBinding == 0);
Q_ASSERT(*m_mePtr == 0);
}

/*!
Destroy the binding. Use this instead of calling delete.
Bindings are free to implement their own memory management, so the delete operator is not
necessarily safe. The default implementation clears the binding, removes it from the object
and calls delete.
*/
void QQmlAbstractBinding::destroy()
{
removeFromObject();
clear();

delete this;
}

/*!
Add this binding to \a object.
This transfers ownership of the binding to the object, marks the object's property as
being bound.
However, it does not enable the binding itself or call update() on it.
*/
void QQmlAbstractBinding::addToObject()
{
Q_ASSERT(!m_prevBinding);

QObject *obj = object();
Q_ASSERT(obj);

int index = propertyIndex();

QQmlData *data = QQmlData::get(obj, true);

if (index & 0xFF000000) {
// Value type

int coreIndex = index & 0xFFFFFF;

// Find the value type proxy (if there is one)
QQmlValueTypeProxyBinding *proxy = 0;
if (data->hasBindingBit(coreIndex)) {
QQmlAbstractBinding *b = data->bindings;
while (b && b->propertyIndex() != coreIndex)
b = b->m_nextBinding;
Q_ASSERT(b && b->bindingType() == QQmlAbstractBinding::ValueTypeProxy);
proxy = static_cast<QQmlValueTypeProxyBinding *>(b);
}

if (!proxy) {
proxy = new QQmlValueTypeProxyBinding(obj, coreIndex);

Q_ASSERT(proxy->propertyIndex() == coreIndex);
Q_ASSERT(proxy->object() == obj);

proxy->addToObject();
}

m_nextBinding = proxy->m_bindings;
if (m_nextBinding) m_nextBinding->m_prevBinding = &m_nextBinding;
m_prevBinding = &proxy->m_bindings;
proxy->m_bindings = this;

} else {
m_nextBinding = data->bindings;
if (m_nextBinding) m_nextBinding->m_prevBinding = &m_nextBinding;
m_prevBinding = &data->bindings;
data->bindings = this;

data->setBindingBit(obj, index);
}
}

/*!
Remove the binding from the object.
*/
void QQmlAbstractBinding::removeFromObject()
{
if (m_prevBinding) {
int index = propertyIndex();

*m_prevBinding = m_nextBinding;
if (m_nextBinding) m_nextBinding->m_prevBinding = m_prevBinding;
m_prevBinding = 0;
m_nextBinding = 0;

if (index & 0xFF000000) {
// Value type - we don't remove the proxy from the object. It will sit their happily
// doing nothing until it is removed by a write, a binding change or it is reused
// to hold more sub-bindings.
} else if (QObject *obj = object()) {
QQmlData *data = QQmlData::get(obj, false);
if (data) data->clearBindingBit(index);
}
}
}

static void bindingDummyDeleter(QQmlAbstractBinding *)
{
}

QQmlAbstractBinding::Pointer QQmlAbstractBinding::weakPointer()
{
if (m_mePtr.value().isNull())
m_mePtr.value() = QSharedPointer<QQmlAbstractBinding>(this, bindingDummyDeleter);

return m_mePtr.value().toWeakRef();
}

void QQmlAbstractBinding::clear()
{
if (!m_mePtr.isNull()) {
**m_mePtr = 0;
m_mePtr = 0;
}
}

void QQmlAbstractBinding::retargetBinding(QObject *, int)
{
qFatal("QQmlAbstractBinding::retargetBinding() called on illegal binding.");
}

QString QQmlAbstractBinding::expression() const
{
return QLatin1String("<Unknown>");
}

void QQmlAbstractBinding::setEnabled(bool enabled, QQmlPropertyPrivate::WriteFlags flags)
{
if (enabled) update(flags);
}


QT_END_NAMESPACE
131 changes: 131 additions & 0 deletions src/qml/qml/qqmlabstractbinding_p.h
@@ -0,0 +1,131 @@
/****************************************************************************
**
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/
**
** This file is part of the QtQml module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#ifndef QQMLABSTRACTBINDING_P_H
#define QQMLABSTRACTBINDING_P_H

//
// W A R N I N G
// -------------
//
// This file is not part of the Qt API. It exists purely as an
// implementation detail. This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//

#include <QtCore/qsharedpointer.h>
#include <private/qtqmlglobal_p.h>
#include <private/qqmlproperty_p.h>
#include <private/qpointervaluepair_p.h>

QT_BEGIN_NAMESPACE

class Q_QML_PRIVATE_EXPORT QQmlAbstractBinding
{
public:
typedef QWeakPointer<QQmlAbstractBinding> Pointer;

QQmlAbstractBinding();

virtual void destroy();

virtual QString expression() const;

enum Type { PropertyBinding, ValueTypeProxy };
virtual Type bindingType() const { return PropertyBinding; }

// Should return the encoded property index for the binding. Should return this value
// even if the binding is not enabled or added to an object.
// Encoding is: coreIndex | (valueTypeIndex << 24)
virtual int propertyIndex() const = 0;
// Should return the object for the binding. Should return this object even if the
// binding is not enabled or added to the object.
virtual QObject *object() const = 0;

void setEnabled(bool e) { setEnabled(e, QQmlPropertyPrivate::DontRemoveBinding); }
virtual void setEnabled(bool, QQmlPropertyPrivate::WriteFlags) = 0;

void update() { update(QQmlPropertyPrivate::DontRemoveBinding); }
virtual void update(QQmlPropertyPrivate::WriteFlags) = 0;

void addToObject();
void removeFromObject();

static inline Pointer getPointer(QQmlAbstractBinding *p);

protected:
virtual ~QQmlAbstractBinding();
void clear();

// Called by QQmlPropertyPrivate to "move" a binding to a different property.
// This is only used for alias properties, and only used by QQmlBinding not
// V8 or V4 bindings. The default implementation qFatal()'s to ensure that the
// method is never called for V4 or V8 bindings.
virtual void retargetBinding(QObject *, int);
private:
Pointer weakPointer();

friend class QQmlData;
friend class QQmlComponentPrivate;
friend class QQmlValueTypeProxyBinding;
friend class QQmlPropertyPrivate;
friend class QQmlVME;
friend class QtSharedPointer::ExternalRefCount<QQmlAbstractBinding>;

typedef QSharedPointer<QQmlAbstractBinding> SharedPointer;
// To save memory, we also store the rarely used weakPointer() instance in here
QPointerValuePair<QQmlAbstractBinding*, SharedPointer> m_mePtr;

QQmlAbstractBinding **m_prevBinding;
QQmlAbstractBinding *m_nextBinding;
};

QQmlAbstractBinding::Pointer
QQmlAbstractBinding::getPointer(QQmlAbstractBinding *p)
{
return p ? p->weakPointer() : Pointer();
}

QT_END_NAMESPACE

#endif // QQMLABSTRACTBINDING_P_H

0 comments on commit 5013d53

Please sign in to comment.