Ensure that scarce resources work with var properties
[profile/ivi/qtdeclarative.git] / doc / src / snippets / declarative / integrating-javascript / scarceresources / avatarExample.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the documentation of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:BSD$
10 ** You may use this file under the terms of the BSD license as follows:
11 **
12 ** "Redistribution and use in source and binary forms, with or without
13 ** modification, are permitted provided that the following conditions are
14 ** met:
15 **   * Redistributions of source code must retain the above copyright
16 **     notice, this list of conditions and the following disclaimer.
17 **   * Redistributions in binary form must reproduce the above copyright
18 **     notice, this list of conditions and the following disclaimer in
19 **     the documentation and/or other materials provided with the
20 **     distribution.
21 **   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
22 **     the names of its contributors may be used to endorse or promote
23 **     products derived from this software without specific prior written
24 **     permission.
25 **
26 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
37 ** $QT_END_LICENSE$
38 **
39 ****************************************************************************/
40
41 #include "avatarExample.h"
42 #include <QDeclarativeEngine>
43 #include <QDeclarativeComponent>
44
45 void registerTypes()
46 {
47 //![0]
48     qmlRegisterType<AvatarExample>("Qt.example", 1, 0, "AvatarExample");
49 //![0]
50 }
51
52 void expectOne()
53 {
54 //![1]
55 QDeclarativeComponent component(&engine, "exampleOne.qml");
56 QObject *object = component.create();
57 // The scarce resource will have been released automatically
58 // by this point, after the binding expression was evaluated.
59 delete object;
60 //![1]
61 }
62
63 void expectTwo()
64 {
65 //![2]
66 QDeclarativeComponent component(&engine, "exampleTwo.qml");
67 QObject *object = component.create();
68 // The scarce resource will not have been released automatically
69 // after the binding expression was evaluated.
70 // Since the scarce resource was not released explicitly prior
71 // to the binding expression being evaluated, we get:
72 bool expectedResult = (object->property("avatar").isValid() == true);
73 delete object;
74 //![2]
75 }
76
77 void expectThree()
78 {
79 //![3]
80 QDeclarativeComponent component(&engine, "exampleThree.qml");
81 QObject *object = component.create();
82 // The resource was preserved explicitly during evaluation of the
83 // JavaScript expression.  Thus, during property assignment, the
84 // scarce resource was still valid, and so we get:
85 bool expectedResult = (object->property("avatar").isValid() == true);
86 // The scarce resource will not be released until all references to
87 // the resource are released, and the JavaScript garbage collector runs.
88 delete object;
89 //![3]
90 }
91
92 void expectFour()
93 {
94 //![4]
95 QDeclarativeComponent component(&engine, "exampleFour.qml");
96 QObject *object = component.create();
97 // The scarce resource was explicitly preserved by the client during
98 // the importAvatar() function, and so the scarce resource
99 // remains valid until the explicit call to releaseAvatar().  As such,
100 // we get the expected results:
101 bool expectedResultOne = (object->property("avatarOne").isValid() == true);
102 bool expectedResultTwo = (object->property("avatarTwo").isValid() == false);
103 // Because the scarce resource referenced by avatarTwo was released explicitly,
104 // it will no longer be consuming any system resources (beyond what a normal
105 // JS Object would; that small overhead will exist until the JS GC runs, as per
106 // any other JavaScript object).
107 delete object;
108 //![4]
109 }
110
111 void expectFive()
112 {
113 //![5]
114 QDeclarativeComponent component(&engine, "exampleFive.qml");
115 QObject *object = component.create();
116 // We have the expected results:
117 bool expectedResultOne = (object->property("avatarOne").isValid() == false);
118 bool expectedResultTwo = (object->property("avatarTwo").isValid() == false);
119 // Because although only avatarTwo was explicitly released,
120 // avatarOne and avatarTwo were referencing the same
121 // scarce resource.
122 delete object;
123 //![5]
124 }