source: trunk/doc/src/snippets/code/src_qdbus_qdbusargument.cpp@ 973

Last change on this file since 973 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 5.3 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
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//! [0]
42struct MyStructure
43{
44 int count;
45 QString name;
46};
47Q_DECLARE_METATYPE(MyStructure)
48
49// Marshall the MyStructure data into a D-Bus argument
50QDBusArgument &operator<<(QDBusArgument &argument, const MyStructure &mystruct)
51{
52 argument.beginStructure();
53 argument << mystruct.count << mystruct.name;
54 argument.endStructure();
55 return argument;
56}
57
58// Retrieve the MyStructure data from the D-Bus argument
59const QDBusArgument &operator>>(const QDBusArgument &argument, MyStructure &mystruct)
60{
61 argument.beginStructure();
62 argument >> mystruct.count >> mystruct.name;
63 argument.endStructure();
64 return argument;
65}
66//! [0]
67
68
69//! [1]
70qDBusRegisterMetaType<MyStructure>();
71//! [1]
72
73
74//! [2]
75MyType item = qdbus_cast<Type>(argument);
76//! [2]
77
78
79//! [3]
80MyType item;
81argument >> item;
82//! [3]
83
84
85//! [4]
86QDBusArgument &operator<<(QDBusArgument &argument, const MyStructure &mystruct)
87{
88 argument.beginStructure();
89 argument << mystruct.member1 << mystruct.member2 << ... ;
90 argument.endStructure();
91 return argument;
92}
93//! [4]
94
95
96//! [5]
97QDBusArgument &operator<<(QDBusArgument &argument, const MyStructure &mystruct)
98{
99 argument.beginStructure();
100 argument << mystruct.member1 << mystruct.member2;
101
102 argument.beginStructure();
103 argument << mystruct.member3.subMember1 << mystruct.member3.subMember2;
104 argument.endStructure();
105
106 argument << mystruct.member4;
107 argument.endStructure();
108 return argument;
109}
110//! [5]
111
112
113//! [6]
114// append an array of MyElement types
115QDBusArgument &operator<<(QDBusArgument &argument, const MyArray &myarray)
116{
117 argument.beginArray( qMetaTypeId<MyElement>() );
118 for ( int i = 0; i < myarray.length; ++i )
119 argument << myarray.elements[i];
120 argument.endArray();
121 return argument;
122}
123//! [6]
124
125
126//! [7]
127// append a dictionary that associates ints to MyValue types
128QDBusArgument &operator<<(QDBusArgument &argument, const MyDictionary &mydict)
129{
130 argument.beginMap( QVariant::Int, qMetaTypeId<MyValue>() );
131 for ( int i = 0; i < mydict.length; ++i ) {
132 argument.beginMapEntry();
133 argument << mydict.data[i].key << mydict.data[i].value;
134 argument.endMapEntry();
135 }
136 argument.endMap();
137 return argument;
138}
139//! [7]
140
141
142//! [8]
143const QDBusArgument &operator>>(const QDBusArgument &argument, MyStructure &mystruct)
144{
145 argument.beginStructure()
146 argument >> mystruct.member1 >> mystruct.member2 >> mystruct.member3 >> ...;
147 argument.endStructure();
148 return argument;
149}
150//! [8]
151
152
153//! [9]
154// extract a MyArray array of MyElement elements
155const QDBusArgument &operator>>(const QDBusArgument &argument, MyArray &myarray)
156{
157 argument.beginArray();
158 myarray.clear();
159
160 while ( !argument.atEnd() ) {
161 MyElement element;
162 argument >> element;
163 myarray.append( element );
164 }
165
166 argument.endArray();
167 return argument;
168}
169//! [9]
170
171
172//! [10]
173// extract a MyDictionary map that associates ints to MyValue elements
174const QDBusArgument &operator>>(const QDBusArgument &argument, MyDictionary &mydict)
175{
176 argument.beginMap();
177 mydict.clear();
178
179 while ( !argMap.atEnd() ) {
180 int key;
181 MyValue value;
182 argument.beginMapEntry();
183 argument >> key >> value;
184 argument.endMapEntry();
185 mydict.append( key, value );
186 }
187
188 argument.endMap();
189 return argument;
190}
191//! [10]
Note: See TracBrowser for help on using the repository browser.