valueschangedcommand.cpp 5.23 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
2
**
3
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
hjk's avatar
hjk committed
4
** Contact: http://www.qt-project.org/legal
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
7
**
hjk's avatar
hjk committed
8 9 10 11 12 13 14
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
15 16
**
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17 18 19 20 21 22 23 24 25
** Alternatively, 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, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
26 27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
28
****************************************************************************/
29

30 31
#include "valueschangedcommand.h"

32 33
#include <QSharedMemory>
#include <QCache>
34
#include <QDebug>
35

36 37
#include <cstring>

38 39
namespace QmlDesigner {

40 41
static QCache<qint32, QSharedMemory> globalSharedMemoryCache(10000);

42
ValuesChangedCommand::ValuesChangedCommand()
43
    : m_keyNumber(0)
44 45 46 47
{
}

ValuesChangedCommand::ValuesChangedCommand(const QVector<PropertyValueContainer> &valueChangeVector)
48 49
    : m_valueChangeVector (valueChangeVector),
      m_keyNumber(0)
50 51 52 53 54 55 56 57
{
}

QVector<PropertyValueContainer> ValuesChangedCommand::valueChanges() const
{
    return m_valueChangeVector;
}

58 59 60 61 62 63 64 65 66 67 68 69 70
quint32 ValuesChangedCommand::keyNumber() const
{
    return m_keyNumber;
}

void ValuesChangedCommand::removeSharedMemorys(const QVector<qint32> &keyNumberVector)
{
    foreach (qint32 keyNumber, keyNumberVector) {
        QSharedMemory *sharedMemory = globalSharedMemoryCache.take(keyNumber);
        delete sharedMemory;
    }
}

71 72 73 74 75
void ValuesChangedCommand::sort()
{
    qSort(m_valueChangeVector);
}

76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
static const QLatin1String valueKeyTemplateString("Values-%1");

static QSharedMemory *createSharedMemory(qint32 key, int byteCount)
{
    QSharedMemory *sharedMemory = new QSharedMemory(QString(valueKeyTemplateString).arg(key));

    bool sharedMemoryIsCreated = sharedMemory->create(byteCount);
    if (!sharedMemoryIsCreated) {
        if (sharedMemory->isAttached())
            sharedMemory->attach();
        sharedMemory->detach();
        sharedMemoryIsCreated = sharedMemory->create(byteCount);
    }

    if (sharedMemoryIsCreated) {
        globalSharedMemoryCache.insert(key, sharedMemory);
        return sharedMemory;
    }

    return 0;
}

98 99
QDataStream &operator<<(QDataStream &out, const ValuesChangedCommand &command)
{
100 101 102
    static const bool dontUseSharedMemory = !qgetenv("DESIGNER_DONT_USE_SHARED_MEMORY").isEmpty();

    if (!dontUseSharedMemory && command.valueChanges().count() > 5) {
103 104 105 106 107
        static quint32 keyCounter = 0;
        ++keyCounter;
        command.m_keyNumber = keyCounter;
        QByteArray outDataStreamByteArray;
        QDataStream temporaryOutDataStream(&outDataStreamByteArray, QIODevice::WriteOnly);
108
        temporaryOutDataStream.setVersion(QDataStream::Qt_4_8);
109

Nikolai Kosjar's avatar
Nikolai Kosjar committed
110
        temporaryOutDataStream << command.valueChanges();
111 112 113 114

        QSharedMemory *sharedMemory = createSharedMemory(keyCounter, outDataStreamByteArray.size());

        if (sharedMemory) {
115
            sharedMemory->lock();
116
            std::memcpy(sharedMemory->data(), outDataStreamByteArray.constData(), sharedMemory->size());
117
            sharedMemory->unlock();
118 119 120 121 122 123
            out << command.keyNumber();
            return out;
        }
    }

    out << qint32(0);
124 125 126 127 128
    out << command.valueChanges();

    return out;
}

129 130 131 132 133 134 135
void readSharedMemory(qint32 key, QVector<PropertyValueContainer> *valueChangeVector)
{
    QSharedMemory sharedMemory(QString(valueKeyTemplateString).arg(key));
    bool canAttach = sharedMemory.attach(QSharedMemory::ReadOnly);

    if (canAttach) {
        QDataStream in(QByteArray::fromRawData(static_cast<const char*>(sharedMemory.constData()), sharedMemory.size()));
136
        in.setVersion(QDataStream::Qt_4_8);
137 138 139 140
        in >> *valueChangeVector;
    }
}

141 142
QDataStream &operator>>(QDataStream &in, ValuesChangedCommand &command)
{
143
    in >> command.m_keyNumber;
144

145 146 147 148 149
    if (command.keyNumber() > 0) {
        readSharedMemory(command.keyNumber(), &command.m_valueChangeVector);
    } else {
        in >> command.m_valueChangeVector;
    }
150 151 152
    return in;
}

153 154 155 156 157
bool operator ==(const ValuesChangedCommand &first, const ValuesChangedCommand &second)
{
    return first.m_valueChangeVector == second.m_valueChangeVector;
}

158 159 160 161 162 163 164
QDebug operator <<(QDebug debug, const ValuesChangedCommand &command)
{
    return debug.nospace() << "ValuesChangedCommand("
                    << "keyNumber: " << command.keyNumber() << ", "
                    << command.valueChanges() << ")";
}

165
} // namespace QmlDesigner