summaryrefslogtreecommitdiffstats
path: root/src/event/EventFactory.cpp
blob: 4bf776c6442133fd1dd2755e9f7a8d210fd1a5e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/****************************************************************************
**
** Copyright (C) 2016 NVIDIA Corporation.
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt 3D Studio.
**
** $QT_BEGIN_LICENSE:GPL$
** 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 The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 or (at your option) any later version
** approved by the KDE Free Qt Foundation. The licenses are as published by
** the Free Software Foundation and appearing in the file LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "EventFactory.h"
#include "foundation/Qt3DSFoundation.h"
#include "foundation/Qt3DSBroadcastingAllocator.h"
#include "foundation/StringTable.h"
#include "foundation/Utils.h"

using namespace qt3ds::evt;

typedef char TEventChar;
using qt3ds::QT3DSU32;

class CInitableRegisteredStr : public Qt3DSEventSystemRegisteredStr
{
public:
    CInitableRegisteredStr(const TEventStr inString) { m_Data = inString; }
};

CFactory &CFactory::Create(qt3ds::NVFoundationBase &inFoundation)
{
    return *QT3DS_NEW(inFoundation.getAllocator(), CFactory)(inFoundation);
}

CFactory::CFactory(qt3ds::NVFoundationBase &inFoundation)
    : m_Foundation(inFoundation)
    , m_Allocator(inFoundation.getAllocator(), "event factory allocator")
    , m_StringTable(qt3ds::foundation::IStringTable::CreateStringTable(inFoundation.getAllocator()))
{
}

CFactory::~CFactory()
{
}

Qt3DSEventSystemEvent &CFactory::CreateEvent(int inNumData)
{
    // Ensure num data is in range
    inNumData = qt3ds::NVMax(0, inNumData);
    // Ensure we can actually allocate something that big
    QT3DSU32 dataSize = static_cast<QT3DSU32>(
        qt3ds::NVMin((size_t)inNumData * sizeof(Qt3DSEventSystemEventData),
                  TEventAllocator::getSlabSize() - sizeof(Qt3DSEventSystemEvent)));
    // get the actual num data after safety checks.
    inNumData = (int)(dataSize / sizeof(Qt3DSEventSystemEventData));

    QT3DSU32 allocSize = sizeof(Qt3DSEventSystemEvent) + inNumData * sizeof(Qt3DSEventSystemEventData);

    Qt3DSEventSystemEvent *theEvent = (Qt3DSEventSystemEvent *)m_Allocator.allocate(
        allocSize, "Event allocation", __FILE__, __LINE__);
    theEvent->m_NumData = inNumData;
    theEvent->m_Data = reinterpret_cast<Qt3DSEventSystemEventData *>(((qt3ds::QT3DSU8 *)theEvent)
                                                                    + sizeof(Qt3DSEventSystemEvent));

    // Initialize the event data to zero so that a free event won't cause a calamity.
    qt3ds::intrinsics::memZero(theEvent->m_Data, dataSize);

    return *theEvent;
}

size_t CFactory::GetMaxNumEventData()
{
    return m_Allocator.getSlabSize() - sizeof(Qt3DSEventSystemEvent);
}

size_t CFactory::GetMaxStrLength()
{
    return m_Allocator.getSlabSize();
}

void CFactory::ReleaseOutstandingEvents()
{
    m_Allocator.reset();
}

Qt3DSEventSystemRegisteredStr CFactory::RegisterStr(TEventStr inSrc)
{
    return CInitableRegisteredStr(m_StringTable->RegisterStr(inSrc));
}

TEventStr CFactory::AllocateStr(TEventStr inSrc)
{
    size_t theSizeInByte = sizeof(TEventChar) * (strlen(qt3ds::foundation::nonNull(inSrc)) + 1);
    theSizeInByte = qt3ds::NVMin(theSizeInByte, TEventAllocator::getSlabSize());

    TEventChar *theNewString = (TEventChar *)m_Allocator.allocate(
        theSizeInByte, "Qt3DS::evt::CFactory::UnManagedString", __FILE__, __LINE__);

    if (theNewString)
        memcpy(theNewString, inSrc, theSizeInByte);

    theNewString[theSizeInByte - 1] = 0;

    return theNewString;
}

TEventStr CFactory::AllocateStr(int inLength)
{
    ++inLength;
    int safeLen = (int)qt3ds::NVMin((size_t)inLength, TEventAllocator::getSlabSize());
    // Either give the users what they ask for or return nothing.
    if (safeLen != inLength)
        return NULL;

    void *retval = m_Allocator.allocate(inLength * sizeof(TEventChar),
                                        "Qt3DS::evt::CFactory::UnManagedString", __FILE__, __LINE__);
    qt3ds::intrinsics::memZero(retval, inLength);
    return (TEventStr)retval;
}

void CFactory::Release()
{
    this->~CFactory();
    QT3DS_FREE(m_Foundation.getAllocator(), this);
}