00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef QDBUSDATAMAP_H
00025 #define QDBUSDATAMAP_H
00026
00027 #include "dbus/qdbusmacros.h"
00028 #include <qmap.h>
00029
00030 class QDBusData;
00031 class QDBusObjectPath;
00032 class QDBusVariant;
00033
00071 template <typename T>
00072 class QDBUS_EXPORT QDBusDataMap : private QMap<T, QDBusData>
00073 {
00074 friend class QDBusData;
00075
00076 public:
00081 typedef QMapConstIterator<T, QDBusData> const_iterator;
00082
00088 QDBusDataMap<T>()
00089 : QMap<T, QDBusData>(), m_valueType(QDBusData::Invalid) {}
00090
00102 explicit QDBusDataMap<T>(QDBusData::Type simpleValueType)
00103 : QMap<T, QDBusData>(), m_valueType(simpleValueType) {}
00104
00115 explicit QDBusDataMap<T>(const QDBusData& containerValueType)
00116 : QMap<T, QDBusData>(), m_valueType(containerValueType.type())
00117 {
00118 if (hasContainerValueType()) m_containerValueType = containerValueType;
00119 }
00120
00129 QDBusDataMap<T>(const QDBusDataMap<T>& other)
00130 : QMap<T, QDBusData>(other), m_valueType(other.m_valueType),
00131 m_containerValueType(other.m_containerValueType) {}
00132
00149 QDBusDataMap<T>(const QMap<T, QDBusData>& other)
00150 : QMap<T, QDBusData>(other), m_valueType(QDBusData::Invalid)
00151 {
00152 const_iterator it = begin();
00153 if (it == end()) return;
00154
00155 m_valueType = (*it).type();
00156
00157 QCString containerSignature;
00158 if (hasContainerValueType())
00159 {
00160 m_containerValueType = it.data();
00161 containerSignature = m_containerValueType.buildDBusSignature();
00162 }
00163
00164 for (++it; it != end(); ++it)
00165 {
00166 if ((*it).type() != m_valueType)
00167 {
00168 m_valueType = QDBusData::Invalid;
00169 m_containerValueType = QDBusData();
00170
00171 clear();
00172 return;
00173 }
00174 else if (hasContainerValueType())
00175 {
00176 if (it.data().buildDBusSignature() != containerSignature)
00177 {
00178 m_valueType = QDBusData::Invalid;
00179 m_containerValueType = QDBusData();
00180
00181 clear();
00182 return;
00183 }
00184 }
00185 }
00186 }
00187
00200 QDBusDataMap<T>(const QMap<T, bool>& other)
00201 : QMap<T, QDBusData>(), m_valueType(QDBusData::Bool)
00202 {
00203 typename QMap<T, bool>::const_iterator it = other.begin();
00204 typename QMap<T, bool>::const_iterator endIt = other.end();
00205 for (; it != endIt; ++it)
00206 {
00207 insert(it.key(), QDBusData::fromBool(it.data()));
00208 }
00209 }
00210
00223 QDBusDataMap<T>(const QMap<T, Q_UINT8>& other)
00224 : QMap<T, QDBusData>(), m_valueType(QDBusData::Byte)
00225 {
00226 typename QMap<T, Q_UINT8>::const_iterator it = other.begin();
00227 typename QMap<T, Q_UINT8>::const_iterator endIt = other.end();
00228 for (; it != endIt; ++it)
00229 {
00230 insert(it.key(), QDBusData::fromByte(it.data()));
00231 }
00232 }
00233
00246 QDBusDataMap<T>(const QMap<T, Q_INT16>& other)
00247 : QMap<T, QDBusData>(), m_valueType(QDBusData::Int16)
00248 {
00249 typename QMap<T, Q_INT16>::const_iterator it = other.begin();
00250 typename QMap<T, Q_INT16>::const_iterator endIt = other.end();
00251 for (; it != endIt; ++it)
00252 {
00253 insert(it.key(), QDBusData::fromInt16(it.data()));
00254 }
00255 }
00256
00269 QDBusDataMap<T>(const QMap<T, Q_UINT16>& other)
00270 : QMap<T, QDBusData>(), m_valueType(QDBusData::UInt16)
00271 {
00272 typename QMap<T, Q_UINT16>::const_iterator it = other.begin();
00273 typename QMap<T, Q_UINT16>::const_iterator endIt = other.end();
00274 for (; it != endIt; ++it)
00275 {
00276 insert(it.key(), QDBusData::fromUInt16(it.data()));
00277 }
00278 }
00279
00292 QDBusDataMap<T>(const QMap<T, Q_INT32>& other)
00293 : QMap<T, QDBusData>(), m_valueType(QDBusData::Int32)
00294 {
00295 typename QMap<T, Q_INT32>::const_iterator it = other.begin();
00296 typename QMap<T, Q_INT32>::const_iterator endIt = other.end();
00297 for (; it != endIt; ++it)
00298 {
00299 insert(it.key(), QDBusData::fromInt32(it.data()));
00300 }
00301 }
00302
00315 QDBusDataMap<T>(const QMap<T, Q_UINT32>& other)
00316 : QMap<T, QDBusData>(), m_valueType(QDBusData::UInt32)
00317 {
00318 typename QMap<T, Q_UINT32>::const_iterator it = other.begin();
00319 typename QMap<T, Q_UINT32>::const_iterator endIt = other.end();
00320 for (; it != endIt; ++it)
00321 {
00322 insert(it.key(), QDBusData::fromUInt32(it.data()));
00323 }
00324 }
00325
00338 QDBusDataMap<T>(const QMap<T, Q_INT64>& other)
00339 : QMap<T, QDBusData>(), m_valueType(QDBusData::Int64)
00340 {
00341 typename QMap<T, Q_INT64>::const_iterator it = other.begin();
00342 typename QMap<T, Q_INT64>::const_iterator endIt = other.end();
00343 for (; it != endIt; ++it)
00344 {
00345 insert(it.key(), QDBusData::fromInt64(it.data()));
00346 }
00347 }
00348
00361 QDBusDataMap<T>(const QMap<T, Q_UINT64>& other)
00362 : QMap<T, QDBusData>(), m_valueType(QDBusData::UInt64)
00363 {
00364 typename QMap<T, Q_UINT64>::const_iterator it = other.begin();
00365 typename QMap<T, Q_UINT64>::const_iterator endIt = other.end();
00366 for (; it != endIt; ++it)
00367 {
00368 insert(it.key(), QDBusData::fromUInt64(it.data()));
00369 }
00370 }
00371
00384 QDBusDataMap<T>(const QMap<T, double>& other)
00385 : QMap<T, QDBusData>(), m_valueType(QDBusData::Double)
00386 {
00387 typename QMap<T, double>::const_iterator it = other.begin();
00388 typename QMap<T, double>::const_iterator endIt = other.end();
00389 for (; it != endIt; ++it)
00390 {
00391 insert(it.key(), QDBusData::fromDouble(it.data()));
00392 }
00393 }
00394
00407 QDBusDataMap<T>(const QMap<T, QString>& other)
00408 : QMap<T, QDBusData>(), m_valueType(QDBusData::String)
00409 {
00410 typename QMap<T, QString>::const_iterator it = other.begin();
00411 typename QMap<T, QString>::const_iterator endIt = other.end();
00412 for (; it != endIt; ++it)
00413 {
00414 insert(it.key(), QDBusData::fromString(it.data()));
00415 }
00416 }
00417
00430 QDBusDataMap<T>(const QMap<T, QDBusObjectPath>& other)
00431 : QMap<T, QDBusData>(), m_valueType(QDBusData::ObjectPath)
00432 {
00433 typename QMap<T, QDBusObjectPath>::const_iterator it = other.begin();
00434 typename QMap<T, QDBusObjectPath>::const_iterator endIt = other.end();
00435 for (; it != endIt; ++it)
00436 {
00437 insert(it.key(), QDBusData::fromObjectPath(it.data()));
00438 }
00439 }
00440
00453 QDBusDataMap<T>(const QMap<T, QDBusVariant>& other)
00454 : QMap<T, QDBusData>(), m_valueType(QDBusData::Variant)
00455 {
00456 typename QMap<T, QDBusVariant>::const_iterator it = other.begin();
00457 typename QMap<T, QDBusVariant>::const_iterator endIt = other.end();
00458 for (; it != endIt; ++it)
00459 {
00460 insert(it.key(), QDBusData::fromVariant(it.data()));
00461 }
00462 }
00463
00474 QDBusDataMap<T>& operator=(const QDBusDataMap<T>& other)
00475 {
00476 QMap<T, QDBusData>::operator=(other);
00477
00478 m_valueType = other.m_valueType;
00479 m_containerValueType = other.m_containerValueType;
00480
00481 return *this;
00482 }
00483
00498 QDBusDataMap<T>& operator=(const QMap<T, QDBusData>& other)
00499 {
00500 QMap<T, QDBusData>::operator=(other);
00501
00502 m_valueType = QDBusData::Invalid;
00503 m_containerValueType = QDBusData();
00504
00505 const_iterator it = begin();
00506 if (it == end()) return *this;
00507
00508 m_valueType = (*it).type();
00509
00510 QCString containerSignature;
00511 if (hasContainerValueType())
00512 {
00513 m_containerValueType = it.data();
00514 containerSignature = m_containerValueType.buildDBusSignature();
00515 }
00516
00517 for (++it; it != end(); ++it)
00518 {
00519 if ((*it).type() != m_valueType)
00520 {
00521 m_valueType = QDBusData::Invalid;
00522 m_containerValueType = QDBusData();
00523
00524 clear();
00525 return *this;
00526 }
00527 else if (hasContainerValueType())
00528 {
00529 if (it.data()->buildSignature() != containerSignature)
00530 {
00531 m_valueType = QDBusData::Invalid;
00532 m_containerValueType = QDBusData();
00533
00534 clear();
00535 return *this;
00536 }
00537 }
00538 }
00539
00540 return *this;
00541 }
00542
00551 QDBusData::Type keyType() const { return m_keyType; }
00552
00562 QDBusData::Type valueType() const { return m_valueType; }
00563
00577 bool hasContainerValueType() const
00578 {
00579 return m_valueType == QDBusData::List || m_valueType == QDBusData::Struct
00580 || m_valueType == QDBusData::Map;
00581 }
00582
00598 QDBusData containerValueType() const { return m_containerValueType; }
00599
00607 inline bool isValid() const { return valueType() != QDBusData::Invalid; }
00608
00616 bool isEmpty() const { return QMap<T, QDBusData>::empty(); }
00617
00625 uint count() const { return QMap<T, QDBusData>::count(); }
00626
00640 bool operator==(const QDBusDataMap<T>& other) const
00641 {
00642 if (m_valueType != other.m_valueType) return false;
00643
00644 if (count() != other.count()) return false;
00645
00646 if (hasContainerValueType() != other.hasContainerValueType()) return false;
00647
00648 if (hasContainerValueType())
00649 {
00650 if (m_containerValueType.buildDBusSignature() !=
00651 other.m_containerValueType.buildDBusSignature()) return false;
00652 }
00653
00654 const_iterator it = begin();
00655 const_iterator otherIt = other.begin();
00656 for (; it != end() && otherIt != other.end(); ++it, ++otherIt)
00657 {
00658 if (it.key() != otherIt.key()) return false;
00659
00660 if (!(it.data() == otherIt.data())) return false;
00661 }
00662
00663 return true;
00664 }
00665
00671 void clear() { QMap<T, QDBusData>::clear(); }
00672
00678 const_iterator begin() const
00679 {
00680 return QMap<T, QDBusData>::begin();
00681 }
00682
00688 const_iterator end() const
00689 {
00690 return QMap<T, QDBusData>::end();
00691 }
00692
00711 bool insert(const T& key, const QDBusData& data)
00712 {
00713 if (data.type() == QDBusData::Invalid) return false;
00714
00715 if (m_valueType == QDBusData::Invalid)
00716 {
00717 m_valueType = data.type();
00718
00719
00720 if (hasContainerValueType()) m_containerValueType = data;
00721
00722 QMap<T, QDBusData>::insert(key, data);
00723 }
00724 else if (data.type() != m_valueType)
00725 {
00726 qWarning("QDBusDataMap: trying to add data of type %s to map of type %s",
00727 data.typeName(), QDBusData::typeName(m_valueType));
00728 }
00729 else if (hasContainerValueType())
00730 {
00731 QCString ourSignature = m_containerValueType.buildDBusSignature();
00732 QCString dataSignature = data.buildDBusSignature();
00733
00734 if (ourSignature != dataSignature)
00735 {
00736 qWarning("QDBusDataMap: trying to add data with signature %s "
00737 "to map with value signature %s",
00738 dataSignature.data(), ourSignature.data());
00739 }
00740 else
00741 QMap<T, QDBusData>::insert(key, data);
00742 }
00743 else
00744 QMap<T, QDBusData>::insert(key, data);
00745
00746 return true;
00747 }
00748
00754 QMap<T, QDBusData> toQMap() const { return *this; }
00755
00772 QMap<T, bool> toBoolMap(bool* ok = 0) const
00773 {
00774 if (m_valueType != QDBusData::Bool)
00775 {
00776 if (ok != 0) *ok = false;
00777 return QMap<T, bool>();
00778 }
00779
00780 QMap<T, bool> result;
00781
00782 const_iterator it = begin();
00783 const_iterator endIt = end();
00784 for (; it != endIt; ++it)
00785 {
00786 result.insert(it.key(), (*it).toBool());
00787 }
00788
00789 if (ok != 0) *ok = true;
00790
00791 return result;
00792 }
00793
00810 QMap<T, Q_UINT8> toByteMap(bool* ok = 0) const
00811 {
00812 if (m_valueType != QDBusData::Byte)
00813 {
00814 if (ok != 0) *ok = false;
00815 return QMap<T, Q_UINT8>();
00816 }
00817
00818 QMap<T, Q_UINT8> result;
00819
00820 const_iterator it = begin();
00821 const_iterator endIt = end();
00822 for (; it != endIt; ++it)
00823 {
00824 result.insert(it.key(), (*it).toByte());
00825 }
00826
00827 if (ok != 0) *ok = true;
00828
00829 return result;
00830 }
00831
00849 QMap<T, Q_INT16> toInt16Map(bool* ok = 0) const
00850 {
00851 if (m_valueType != QDBusData::Int16)
00852 {
00853 if (ok != 0) *ok = false;
00854 return QMap<T, Q_INT16>();
00855 }
00856
00857 QMap<T, Q_INT16> result;
00858
00859 const_iterator it = begin();
00860 const_iterator endIt = end();
00861 for (; it != endIt; ++it)
00862 {
00863 result.insert(it.key(), (*it).toInt16());
00864 }
00865
00866 if (ok != 0) *ok = true;
00867
00868 return result;
00869 }
00870
00888 QMap<T, Q_UINT16> toUInt16Map(bool* ok = 0) const
00889 {
00890 if (m_valueType != QDBusData::UInt16)
00891 {
00892 if (ok != 0) *ok = false;
00893 return QMap<T, Q_UINT16>();
00894 }
00895
00896 QMap<T, Q_UINT16> result;
00897
00898 const_iterator it = begin();
00899 const_iterator endIt = end();
00900 for (; it != endIt; ++it)
00901 {
00902 result.insert(it.key(), (*it).toUInt16());
00903 }
00904
00905 if (ok != 0) *ok = true;
00906
00907 return result;
00908 }
00909
00927 QMap<T, Q_INT32> toInt32Map(bool* ok = 0) const
00928 {
00929 if (m_valueType != QDBusData::Int32)
00930 {
00931 if (ok != 0) *ok = false;
00932 return QMap<T, Q_INT32>();
00933 }
00934
00935 QMap<T, Q_INT32> result;
00936
00937 const_iterator it = begin();
00938 const_iterator endIt = end();
00939 for (; it != endIt; ++it)
00940 {
00941 result.insert(it.key(), (*it).toInt32());
00942 }
00943
00944 if (ok != 0) *ok = true;
00945
00946 return result;
00947 }
00948
00966 QMap<T, Q_UINT32> toUInt32Map(bool* ok = 0) const
00967 {
00968 if (m_valueType != QDBusData::UInt32)
00969 {
00970 if (ok != 0) *ok = false;
00971 return QMap<T, Q_UINT32>();
00972 }
00973
00974 QMap<T, Q_UINT32> result;
00975
00976 const_iterator it = begin();
00977 const_iterator endIt = end();
00978 for (; it != endIt; ++it)
00979 {
00980 result.insert(it.key(), (*it).toUInt32());
00981 }
00982
00983 if (ok != 0) *ok = true;
00984
00985 return result;
00986 }
00987
01005 QMap<T, Q_INT64> toInt64Map(bool* ok = 0) const
01006 {
01007 if (m_valueType != QDBusData::Int64)
01008 {
01009 if (ok != 0) *ok = false;
01010 return QMap<T, Q_INT64>();
01011 }
01012
01013 QMap<T, Q_INT64> result;
01014
01015 const_iterator it = begin();
01016 const_iterator endIt = end();
01017 for (; it != endIt; ++it)
01018 {
01019 result.insert(it.key(), (*it).toInt64());
01020 }
01021
01022 if (ok != 0) *ok = true;
01023
01024 return result;
01025 }
01026
01044 QMap<T, Q_UINT64> toUInt64Map(bool* ok = 0) const
01045 {
01046 if (m_valueType != QDBusData::UInt64)
01047 {
01048 if (ok != 0) *ok = false;
01049 return QMap<T, Q_UINT64>();
01050 }
01051
01052 QMap<T, Q_UINT64> result;
01053
01054 const_iterator it = begin();
01055 const_iterator endIt = end();
01056 for (; it != endIt; ++it)
01057 {
01058 result.insert(it.key(), (*it).toUInt64());
01059 }
01060
01061 if (ok != 0) *ok = true;
01062
01063 return result;
01064 }
01065
01082 QMap<T, double> toDoubleMap(bool* ok = 0) const
01083 {
01084 if (m_valueType != QDBusData::Double)
01085 {
01086 if (ok != 0) *ok = false;
01087 return QMap<T, double>();
01088 }
01089
01090 QMap<T, double> result;
01091
01092 const_iterator it = begin();
01093 const_iterator endIt = end();
01094 for (; it != endIt; ++it)
01095 {
01096 result.insert(it.key(), (*it).toDouble());
01097 }
01098
01099 if (ok != 0) *ok = true;
01100
01101 return result;
01102 }
01103
01120 QMap<T, QString> toStringMap(bool* ok = 0) const
01121 {
01122 if (m_valueType != QDBusData::String)
01123 {
01124 if (ok != 0) *ok = false;
01125 return QMap<T, QString>();
01126 }
01127
01128 QMap<T, QString> result;
01129
01130 const_iterator it = begin();
01131 const_iterator endIt = end();
01132 for (; it != endIt; ++it)
01133 {
01134 result.insert(it.key(), (*it).toString());
01135 }
01136
01137 if (ok != 0) *ok = true;
01138
01139 return result;
01140 }
01141
01155 QMap<T, QDBusObjectPath> toObjectPathMap(bool* ok = 0) const
01156 {
01157 if (m_valueType != QDBusData::ObjectPath)
01158 {
01159 if (ok != 0) *ok = false;
01160 return QMap<T, QDBusObjectPath>();
01161 }
01162
01163 QMap<T, QDBusObjectPath> result;
01164
01165 const_iterator it = begin();
01166 const_iterator endIt = end();
01167 for (; it != endIt; ++it)
01168 {
01169 result.insert(it.key(), (*it).toObjectPath());
01170 }
01171
01172 if (ok != 0) *ok = true;
01173
01174 return result;
01175 }
01176
01193 QMap<T, QDBusVariant> toVariantMap(bool* ok = 0) const
01194 {
01195 if (m_valueType != QDBusData::Variant)
01196 {
01197 if (ok != 0) *ok = false;
01198 return QMap<T, QDBusVariant>();
01199 }
01200
01201 QMap<T, QDBusVariant> result;
01202
01203 const_iterator it = begin();
01204 const_iterator endIt = end();
01205 for (; it != endIt; ++it)
01206 {
01207 result.insert(it.key(), (*it).toVariant());
01208 }
01209
01210 if (ok != 0) *ok = true;
01211
01212 return result;
01213 }
01214
01215 private:
01216 QDBusData::Type m_valueType;
01217 QDBusData m_containerValueType;
01218
01219 static const QDBusData::Type m_keyType;
01220 };
01221
01222 #endif