doc/src/snippets/code/src_qdbus_qdbusargument.cpp
branchRCL_3
changeset 7 3f74d0d4af4c
equal deleted inserted replaced
6:dee5afe5301f 7:3f74d0d4af4c
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 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:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 //! [0]
       
    43 struct MyStructure
       
    44 {
       
    45     int count;
       
    46     QString name;
       
    47 };
       
    48 Q_DECLARE_METATYPE(MyStructure)
       
    49 
       
    50 // Marshall the MyStructure data into a D-Bus argument
       
    51 QDBusArgument &operator<<(QDBusArgument &argument, const MyStructure &mystruct)
       
    52 {
       
    53     argument.beginStructure();
       
    54     argument << mystruct.count << mystruct.name;
       
    55     argument.endStructure();
       
    56     return argument;
       
    57 }
       
    58 
       
    59 // Retrieve the MyStructure data from the D-Bus argument
       
    60 const QDBusArgument &operator>>(const QDBusArgument &argument, MyStructure &mystruct)
       
    61 {
       
    62     argument.beginStructure();
       
    63     argument >> mystruct.count >> mystruct.name;
       
    64     argument.endStructure();
       
    65     return argument;
       
    66 }
       
    67 //! [0]
       
    68 
       
    69 
       
    70 //! [1]
       
    71 qDBusRegisterMetaType<MyStructure>();
       
    72 //! [1]
       
    73 
       
    74 
       
    75 //! [2]
       
    76 MyType item = qdbus_cast<Type>(argument);
       
    77 //! [2]
       
    78 
       
    79 
       
    80 //! [3]
       
    81 MyType item;
       
    82 argument >> item;
       
    83 //! [3]
       
    84 
       
    85 
       
    86 //! [4]
       
    87 QDBusArgument &operator<<(QDBusArgument &argument, const MyStructure &mystruct)
       
    88 {
       
    89     argument.beginStructure();
       
    90     argument << mystruct.member1 << mystruct.member2 << ... ;
       
    91     argument.endStructure();
       
    92     return argument;
       
    93 }
       
    94 //! [4]
       
    95 
       
    96 
       
    97 //! [5]
       
    98 QDBusArgument &operator<<(QDBusArgument &argument, const MyStructure &mystruct)
       
    99 {
       
   100     argument.beginStructure();
       
   101     argument << mystruct.member1 << mystruct.member2;
       
   102 
       
   103     argument.beginStructure();
       
   104     argument << mystruct.member3.subMember1 << mystruct.member3.subMember2;
       
   105     argument.endStructure();
       
   106 
       
   107     argument << mystruct.member4;
       
   108     argument.endStructure();
       
   109     return argument;
       
   110 }
       
   111 //! [5]
       
   112 
       
   113 
       
   114 //! [6]
       
   115 // append an array of MyElement types
       
   116 QDBusArgument &operator<<(QDBusArgument &argument, const MyArray &myarray)
       
   117 {
       
   118     argument.beginArray( qMetaTypeId<MyElement>() );
       
   119     for ( int i = 0; i < myarray.length; ++i )
       
   120         argument << myarray.elements[i];
       
   121     argument.endArray();
       
   122     return argument;
       
   123 }
       
   124 //! [6]
       
   125 
       
   126 
       
   127 //! [7]
       
   128 // append a dictionary that associates ints to MyValue types
       
   129 QDBusArgument &operator<<(QDBusArgument &argument, const MyDictionary &mydict)
       
   130 {
       
   131     argument.beginMap( QVariant::Int, qMetaTypeId<MyValue>() );
       
   132     for ( int i = 0; i < mydict.length; ++i ) {
       
   133         argument.beginMapEntry();
       
   134         argument << mydict.data[i].key << mydict.data[i].value;
       
   135         argument.endMapEntry();
       
   136     }
       
   137     argument.endMap();
       
   138     return argument;
       
   139 }
       
   140 //! [7]
       
   141 
       
   142 
       
   143 //! [8]
       
   144 const QDBusArgument &operator>>(const QDBusArgument &argument, MyStructure &mystruct)
       
   145 {
       
   146     argument.beginStructure()
       
   147     argument >> mystruct.member1 >> mystruct.member2 >> mystruct.member3 >> ...;
       
   148     argument.endStructure();
       
   149     return argument;
       
   150 }
       
   151 //! [8]
       
   152 
       
   153 
       
   154 //! [9]
       
   155 // extract a MyArray array of MyElement elements
       
   156 const QDBusArgument &operator>>(const QDBusArgument &argument, MyArray &myarray)
       
   157 {
       
   158     argument.beginArray();
       
   159     myarray.clear();
       
   160 
       
   161     while ( !argument.atEnd() ) {
       
   162         MyElement element;
       
   163         argument >> element;
       
   164         myarray.append( element );
       
   165     }
       
   166 
       
   167     argument.endArray();
       
   168     return argument;
       
   169 }
       
   170 //! [9]
       
   171 
       
   172 
       
   173 //! [10]
       
   174 // extract a MyDictionary map that associates ints to MyValue elements
       
   175 const QDBusArgument &operator>>(const QDBusArgument &argument, MyDictionary &mydict)
       
   176 {
       
   177     argument.beginMap();
       
   178     mydict.clear();
       
   179 
       
   180     while ( !argMap.atEnd() ) {
       
   181         int key;
       
   182         MyValue value;
       
   183         argument.beginMapEntry();
       
   184         argument >> key >> value;
       
   185         argument.endMapEntry();
       
   186         mydict.append( key, value );
       
   187     }
       
   188 
       
   189     argument.endMap();
       
   190     return argument;
       
   191 }
       
   192 //! [10]