KDeclarative

qmlobjectsharedengine.cpp
1 /*
2  SPDX-FileCopyrightText: 2015 Marco Martin <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "qmlobjectsharedengine.h"
8 
9 #include <QQmlEngine>
10 #include <QQmlContext>
11 
12 #include <QDebug>
13 #include <kdeclarative.h>
14 
15 namespace KDeclarative {
16 
17 class QmlObjectSharedEnginePrivate
18 {
19 public:
20  QmlObjectSharedEnginePrivate()
21  {
22  //ensure the engine is present, then ref it
23  engine();
24  engineRef = s_engine;
25  }
26 
27  ~QmlObjectSharedEnginePrivate()
28  {
29  //when the only remaining are out two refs, reset the pointers, causing deletion
30  //when the refcount is 2, we are sure that the only refs are s_engine and our copy
31  //of engineRef
32  if (engineRef.use_count() == 2) {
33  s_engine.reset();
34  }
35  }
36 
37  static QQmlEngine *engine()
38  {
39  if (!s_engine) {
40  s_engine = std::make_shared<QQmlEngine>();
41  KDeclarative::setupEngine(s_engine.get());
42  }
43  return s_engine.get();
44  }
45 
46  //used to delete it
47  std::shared_ptr<QQmlEngine> engineRef;
48 
49  static std::shared_ptr<QQmlEngine> s_engine;
50 };
51 
52 std::shared_ptr<QQmlEngine> QmlObjectSharedEnginePrivate::s_engine = std::shared_ptr<QQmlEngine>();
53 
55  : QmlObject(QmlObjectSharedEnginePrivate::engine(), new QQmlContext(QmlObjectSharedEnginePrivate::engine()), this /*don't call setupEngine*/, parent),
56  d(new QmlObjectSharedEnginePrivate())
57 {
58  rootContext()->setParent(this);
59 }
60 
61 QmlObjectSharedEngine::~QmlObjectSharedEngine()
62 {
63 }
64 
65 
66 }
67 
68 #include "moc_qmlobjectsharedengine.cpp"
static void setupEngine(QQmlEngine *engine)
Setup a QML engine for use with any KDeclarative object.
An object that instantiates an entire QML context, with its own declarative engine.
Definition: qmlobject.h:38
void setParent(QObject *parent)
QmlObjectSharedEngine(QObject *parent=nullptr)
Constructs a new QmlObjectSharedEngine.
QQmlContext * rootContext() const
The components&#39;s creation context.
Definition: qmlobject.cpp:259
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Tue Jan 19 2021 22:41:07 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.