* qt/: Update to Subversion r548032.
[platform/upstream/dbus.git] / qt / src / qdbusreply.h
1 /* qdbusreply.h QDBusReply object - a reply from D-Bus
2  *
3  * Copyright (C) 2006 Trolltech AS. All rights reserved.
4  *    Author: Thiago Macieira <thiago.macieira@trolltech.com>
5  *
6  * Licensed under the Academic Free License version 2.1
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software Foundation
20  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21  *
22  */
23
24 #ifndef QDBUSREPLY_H
25 #define QDBUSREPLY_H
26
27 #include <QtCore/qglobal.h>
28 #include <QtCore/qvariant.h>
29
30 #include "qdbusmacros.h"
31 #include "qdbusmessage.h"
32 #include "qdbuserror.h"
33
34 #include "qdbustypehelper_p.h"
35
36 template<typename T>
37 class QDBusReply
38 {
39     typedef T Type;
40 public:
41     inline QDBusReply(const QDBusMessage &reply)
42         : m_data(Type())
43     {
44         *this = reply;
45     }
46     inline QDBusReply& operator=(const QDBusMessage& reply)
47     {
48         m_error = reply;
49         if (isSuccess())
50             m_data = QDBusTypeHelper<Type>::fromVariant(reply.at(0));
51         else
52             m_data = Type();
53         return *this;
54     }
55
56     inline QDBusReply(const QDBusError &error = QDBusError())
57         : m_error(error), m_data(Type())
58     {
59     }
60     inline QDBusReply& operator=(const QDBusError& error)
61     {
62         m_error = error;
63         m_data = Type();
64         return *this;
65     }
66
67     inline QDBusReply& operator=(const QDBusReply& other)
68     {
69         m_error = other.m_error;
70         m_data = other.m_data;
71         return *this;
72     }
73
74     inline bool isError() const { return m_error.isValid(); }
75     inline bool isSuccess() const { return !m_error.isValid(); }
76
77     inline const QDBusError& error() { return m_error; }
78
79     inline Type value()
80     {
81         return m_data;
82     }
83
84     inline operator Type ()
85     {
86         return m_data;
87     }
88
89     static QDBusReply<T> fromVariant(const QDBusReply<QVariant> &variantReply)
90     {
91         QDBusReply<T> retval;
92         retval.m_error = variantReply.m_error;
93         if (retval.isSuccess()) {
94             retval.m_data = qvariant_cast<Type>(variantReply.m_data);
95             if (!qVariantCanConvert<Type>(variantReply.m_data))
96                 retval.m_error = QDBusError(QDBusError::InvalidSignature,
97                                             QLatin1String("Unexpected reply signature"));
98         }
99         return retval;
100     }
101
102 private:
103     QDBusError m_error;
104     Type m_data;
105 };
106
107 # ifndef Q_QDOC
108 // specialize for void:
109 template<>
110 class QDBUS_EXPORT QDBusReply<void>
111 {
112 public:
113     inline QDBusReply(const QDBusMessage &reply)
114         : m_error(reply)
115     {
116     }
117     inline QDBusReply(const QDBusError &error)
118         : m_error(error)
119     {
120     }    
121
122     inline bool isError() const { return m_error.isValid(); }
123     inline bool isSuccess() const { return !m_error.isValid(); }
124
125     inline const QDBusError& error() { return m_error; }
126
127 private:
128     QDBusError m_error;
129 };
130 # endif
131
132 #endif