• Main Page
  • Related Pages
  • Classes
  • Files
  • Examples
  • File List
  • File Members

tqdbusdatamap.h

Go to the documentation of this file.
00001 /* qdbusdatamap.h DBUS data mapping transport type
00002  *
00003  * Copyright (C) 2007 Kevin Krammer <kevin.krammer@gmx.at>
00004  *
00005  * Licensed under the Academic Free License version 2.1
00006  *
00007  * This program is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation; either version 2 of the License, or
00010  * (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
00020  * USA.
00021  *
00022  */
00023 
00024 #ifndef TQDBUSDATAMAP_H
00025 #define TQDBUSDATAMAP_H
00026 
00027 #include "tqdbusmacros.h"
00028 #include <tqmap.h>
00029 
00030 class TQT_DBusData;
00031 class TQT_DBusObjectPath;
00032 class TQT_DBusUnixFd;
00033 class TQT_DBusVariant;
00034 
00072 template <typename T>
00073 class TQDBUS_EXPORT TQT_DBusDataMap : private TQMap<T, TQT_DBusData>
00074 {
00075     friend class TQT_DBusData;
00076 
00077 public:
00082     typedef TQMapConstIterator<T, TQT_DBusData> const_iterator;
00083 
00089     TQT_DBusDataMap<T>()
00090         : TQMap<T, TQT_DBusData>(), m_valueType(TQT_DBusData::Invalid) {}
00091 
00103     explicit TQT_DBusDataMap<T>(TQT_DBusData::Type simpleValueType)
00104         : TQMap<T, TQT_DBusData>(), m_valueType(simpleValueType) {}
00105 
00116     explicit TQT_DBusDataMap<T>(const TQT_DBusData& containerValueType)
00117         : TQMap<T, TQT_DBusData>(), m_valueType(containerValueType.type())
00118     {
00119         if (hasContainerValueType()) m_containerValueType = containerValueType;
00120     }
00121 
00130     TQT_DBusDataMap<T>(const TQT_DBusDataMap<T>& other)
00131         : TQMap<T, TQT_DBusData>(other), m_valueType(other.m_valueType),
00132           m_containerValueType(other.m_containerValueType) {}
00133 
00150     TQT_DBusDataMap<T>(const TQMap<T, TQT_DBusData>& other)
00151         : TQMap<T, TQT_DBusData>(other), m_valueType(TQT_DBusData::Invalid)
00152     {
00153         const_iterator it = begin();
00154         if (it == end()) return;
00155 
00156         m_valueType = (*it).type();
00157 
00158         TQCString containerSignature;
00159         if (hasContainerValueType())
00160         {
00161             m_containerValueType = it.data();
00162             containerSignature = m_containerValueType.buildDBusSignature();
00163         }
00164 
00165         for (++it; it != end(); ++it)
00166         {
00167             if ((*it).type() != m_valueType)
00168             {
00169                 m_valueType = TQT_DBusData::Invalid;
00170                 m_containerValueType = TQT_DBusData();
00171 
00172                 clear();
00173                 return;
00174             }
00175             else if (hasContainerValueType())
00176             {
00177                 if (it.data().buildDBusSignature() != containerSignature)
00178                 {
00179                     m_valueType = TQT_DBusData::Invalid;
00180                     m_containerValueType = TQT_DBusData();
00181 
00182                     clear();
00183                     return;
00184                 }
00185             }
00186         }
00187     }
00188 
00201     TQT_DBusDataMap<T>(const TQMap<T, bool>& other)
00202         : TQMap<T, TQT_DBusData>(), m_valueType(TQT_DBusData::Bool)
00203     {
00204         typename TQMap<T, bool>::const_iterator it    = other.begin();
00205         typename TQMap<T, bool>::const_iterator endIt = other.end();
00206         for (; it != endIt; ++it)
00207         {
00208             insert(it.key(), TQT_DBusData::fromBool(it.data()));
00209         }
00210     }
00211 
00224     TQT_DBusDataMap<T>(const TQMap<T, TQ_UINT8>& other)
00225         : TQMap<T, TQT_DBusData>(), m_valueType(TQT_DBusData::Byte)
00226     {
00227         typename TQMap<T, TQ_UINT8>::const_iterator it    = other.begin();
00228         typename TQMap<T, TQ_UINT8>::const_iterator endIt = other.end();
00229         for (; it != endIt; ++it)
00230         {
00231             insert(it.key(), TQT_DBusData::fromByte(it.data()));
00232         }
00233     }
00234 
00247     TQT_DBusDataMap<T>(const TQMap<T, TQ_INT16>& other)
00248         : TQMap<T, TQT_DBusData>(), m_valueType(TQT_DBusData::Int16)
00249     {
00250         typename TQMap<T, TQ_INT16>::const_iterator it    = other.begin();
00251         typename TQMap<T, TQ_INT16>::const_iterator endIt = other.end();
00252         for (; it != endIt; ++it)
00253         {
00254             insert(it.key(), TQT_DBusData::fromInt16(it.data()));
00255         }
00256     }
00257 
00270     TQT_DBusDataMap<T>(const TQMap<T, TQ_UINT16>& other)
00271         : TQMap<T, TQT_DBusData>(), m_valueType(TQT_DBusData::UInt16)
00272     {
00273         typename TQMap<T, TQ_UINT16>::const_iterator it    = other.begin();
00274         typename TQMap<T, TQ_UINT16>::const_iterator endIt = other.end();
00275         for (; it != endIt; ++it)
00276         {
00277             insert(it.key(), TQT_DBusData::fromUInt16(it.data()));
00278         }
00279     }
00280 
00293     TQT_DBusDataMap<T>(const TQMap<T, TQ_INT32>& other)
00294         : TQMap<T, TQT_DBusData>(), m_valueType(TQT_DBusData::Int32)
00295     {
00296         typename TQMap<T, TQ_INT32>::const_iterator it    = other.begin();
00297         typename TQMap<T, TQ_INT32>::const_iterator endIt = other.end();
00298         for (; it != endIt; ++it)
00299         {
00300             insert(it.key(), TQT_DBusData::fromInt32(it.data()));
00301         }
00302     }
00303 
00316     TQT_DBusDataMap<T>(const TQMap<T, TQ_UINT32>& other)
00317         : TQMap<T, TQT_DBusData>(), m_valueType(TQT_DBusData::UInt32)
00318     {
00319         typename TQMap<T, TQ_UINT32>::const_iterator it    = other.begin();
00320         typename TQMap<T, TQ_UINT32>::const_iterator endIt = other.end();
00321         for (; it != endIt; ++it)
00322         {
00323             insert(it.key(), TQT_DBusData::fromUInt32(it.data()));
00324         }
00325     }
00326 
00339     TQT_DBusDataMap<T>(const TQMap<T, TQ_INT64>& other)
00340         : TQMap<T, TQT_DBusData>(), m_valueType(TQT_DBusData::Int64)
00341     {
00342         typename TQMap<T, TQ_INT64>::const_iterator it    = other.begin();
00343         typename TQMap<T, TQ_INT64>::const_iterator endIt = other.end();
00344         for (; it != endIt; ++it)
00345         {
00346             insert(it.key(), TQT_DBusData::fromInt64(it.data()));
00347         }
00348     }
00349 
00362     TQT_DBusDataMap<T>(const TQMap<T, TQ_UINT64>& other)
00363         : TQMap<T, TQT_DBusData>(), m_valueType(TQT_DBusData::UInt64)
00364     {
00365         typename TQMap<T, TQ_UINT64>::const_iterator it    = other.begin();
00366         typename TQMap<T, TQ_UINT64>::const_iterator endIt = other.end();
00367         for (; it != endIt; ++it)
00368         {
00369             insert(it.key(), TQT_DBusData::fromUInt64(it.data()));
00370         }
00371     }
00372 
00385     TQT_DBusDataMap<T>(const TQMap<T, double>& other)
00386         : TQMap<T, TQT_DBusData>(), m_valueType(TQT_DBusData::Double)
00387     {
00388         typename TQMap<T, double>::const_iterator it    = other.begin();
00389         typename TQMap<T, double>::const_iterator endIt = other.end();
00390         for (; it != endIt; ++it)
00391         {
00392             insert(it.key(), TQT_DBusData::fromDouble(it.data()));
00393         }
00394     }
00395 
00408     TQT_DBusDataMap<T>(const TQMap<T, TQString>& other)
00409         : TQMap<T, TQT_DBusData>(), m_valueType(TQT_DBusData::String)
00410     {
00411         typename TQMap<T, TQString>::const_iterator it    = other.begin();
00412         typename TQMap<T, TQString>::const_iterator endIt = other.end();
00413         for (; it != endIt; ++it)
00414         {
00415             insert(it.key(), TQT_DBusData::fromString(it.data()));
00416         }
00417     }
00418 
00431     TQT_DBusDataMap<T>(const TQMap<T, TQT_DBusObjectPath>& other)
00432         : TQMap<T, TQT_DBusData>(), m_valueType(TQT_DBusData::ObjectPath)
00433     {
00434         typename TQMap<T, TQT_DBusObjectPath>::const_iterator it    = other.begin();
00435         typename TQMap<T, TQT_DBusObjectPath>::const_iterator endIt = other.end();
00436         for (; it != endIt; ++it)
00437         {
00438             insert(it.key(), TQT_DBusData::fromObjectPath(it.data()));
00439         }
00440     }
00441 
00454     TQT_DBusDataMap<T>(const TQMap<T, TQT_DBusUnixFd>& other)
00455         : TQMap<T, TQT_DBusData>(), m_valueType(TQT_DBusData::UnixFd)
00456     {
00457         typename TQMap<T, TQT_DBusUnixFd>::const_iterator it    = other.begin();
00458         typename TQMap<T, TQT_DBusUnixFd>::const_iterator endIt = other.end();
00459         for (; it != endIt; ++it)
00460         {
00461             insert(it.key(), TQT_DBusData::fromUnixFd(it.data()));
00462         }
00463     }
00464 
00477     TQT_DBusDataMap<T>(const TQMap<T, TQT_DBusVariant>& other)
00478         : TQMap<T, TQT_DBusData>(), m_valueType(TQT_DBusData::Variant)
00479     {
00480         typename TQMap<T, TQT_DBusVariant>::const_iterator it    = other.begin();
00481         typename TQMap<T, TQT_DBusVariant>::const_iterator endIt = other.end();
00482         for (; it != endIt; ++it)
00483         {
00484             insert(it.key(), TQT_DBusData::fromVariant(it.data()));
00485         }
00486     }
00487 
00498     TQT_DBusDataMap<T>& operator=(const TQT_DBusDataMap<T>& other)
00499     {
00500         TQMap<T, TQT_DBusData>::operator=(other);
00501 
00502         m_valueType = other.m_valueType;
00503         m_containerValueType = other.m_containerValueType;
00504 
00505         return *this;
00506     }
00507 
00522     TQT_DBusDataMap<T>& operator=(const TQMap<T, TQT_DBusData>& other)
00523     {
00524         TQMap<T, TQT_DBusData>::operator=(other);
00525 
00526         m_valueType = TQT_DBusData::Invalid;
00527         m_containerValueType = TQT_DBusData();
00528 
00529         const_iterator it = begin();
00530         if (it == end()) return *this;
00531 
00532         m_valueType = (*it).type();
00533 
00534         TQCString containerSignature;
00535         if (hasContainerValueType())
00536         {
00537             m_containerValueType = it.data();
00538             containerSignature = m_containerValueType.buildDBusSignature();
00539         }
00540 
00541         for (++it; it != end(); ++it)
00542         {
00543             if ((*it).type() != m_valueType)
00544             {
00545                 m_valueType = TQT_DBusData::Invalid;
00546                 m_containerValueType = TQT_DBusData();
00547 
00548                 clear();
00549                 return *this;
00550             }
00551             else if (hasContainerValueType())
00552             {
00553                 if (it.data()->buildSignature() != containerSignature)
00554                 {
00555                     m_valueType = TQT_DBusData::Invalid;
00556                     m_containerValueType = TQT_DBusData();
00557 
00558                     clear();
00559                     return *this;
00560                 }
00561             }
00562         }
00563 
00564         return *this;
00565     }
00566 
00575     TQT_DBusData::Type keyType() const { return m_keyType; }
00576 
00586     TQT_DBusData::Type valueType() const { return m_valueType; }
00587 
00601     bool hasContainerValueType() const
00602     {
00603         return m_valueType == TQT_DBusData::List || m_valueType == TQT_DBusData::Struct
00604                                               || m_valueType == TQT_DBusData::Map;
00605     }
00606 
00622     TQT_DBusData containerValueType() const { return m_containerValueType; }
00623 
00631     inline bool isValid() const { return valueType() != TQT_DBusData::Invalid; }
00632 
00640     bool isEmpty() const { return TQMap<T, TQT_DBusData>::empty(); }
00641 
00649     uint count() const { return TQMap<T, TQT_DBusData>::count(); }
00650 
00664     bool operator==(const TQT_DBusDataMap<T>& other) const
00665     {
00666         if (m_valueType != other.m_valueType) return false;
00667 
00668         if (count() != other.count()) return false;
00669 
00670         if (hasContainerValueType() != other.hasContainerValueType()) return false;
00671 
00672         if (hasContainerValueType())
00673         {
00674             if (m_containerValueType.buildDBusSignature() !=
00675                 other.m_containerValueType.buildDBusSignature()) return false;
00676         }
00677 
00678         const_iterator it = begin();
00679         const_iterator otherIt = other.begin();
00680         for (; it != end() && otherIt != other.end(); ++it, ++otherIt)
00681         {
00682             if (it.key() != otherIt.key()) return false;
00683 
00684             if (!(it.data() == otherIt.data())) return false;
00685         }
00686 
00687         return true;
00688     }
00689 
00695     void clear() { TQMap<T, TQT_DBusData>::clear(); }
00696 
00702     const_iterator begin() const
00703     {
00704         return TQMap<T, TQT_DBusData>::begin();
00705     }
00706 
00712     const_iterator end() const
00713     {
00714         return TQMap<T, TQT_DBusData>::end();
00715     }
00716 
00735     bool insert(const T& key, const TQT_DBusData& data)
00736     {
00737         if (data.type() == TQT_DBusData::Invalid) return false;
00738 
00739         if (m_valueType == TQT_DBusData::Invalid)
00740         {
00741             m_valueType = data.type();
00742 
00743             // TODO: create empty copy of container
00744             if (hasContainerValueType()) m_containerValueType = data;
00745 
00746             TQMap<T, TQT_DBusData>::insert(key, data);
00747         }
00748         else if (data.type() != m_valueType)
00749         {
00750             tqWarning("TQT_DBusDataMap: trying to add data of type %s to map of type %s",
00751                      data.typeName(), TQT_DBusData::typeName(m_valueType));
00752         }
00753         else if (hasContainerValueType())
00754         {
00755             TQCString ourSignature  = m_containerValueType.buildDBusSignature();
00756             TQCString dataSignature = data.buildDBusSignature();
00757 
00758             if (ourSignature != dataSignature)
00759             {
00760                 tqWarning("TQT_DBusDataMap: trying to add data with signature %s "
00761                         "to map with value signature %s",
00762                         dataSignature.data(), ourSignature.data());
00763             }
00764             else
00765                 TQMap<T, TQT_DBusData>::insert(key, data);
00766         }
00767         else
00768             TQMap<T, TQT_DBusData>::insert(key, data);
00769 
00770         return true;
00771     }
00772 
00778     TQMap<T, TQT_DBusData> toTQMap() const { return *this; }
00779 
00796     TQMap<T, bool> toBoolMap(bool* ok = 0) const
00797     {
00798         if (m_valueType != TQT_DBusData::Bool)
00799         {
00800             if (ok != 0) *ok = false;
00801             return TQMap<T, bool>();
00802         }
00803 
00804         TQMap<T, bool> result;
00805 
00806         const_iterator it    = begin();
00807         const_iterator endIt = end();
00808         for (; it != endIt; ++it)
00809         {
00810             result.insert(it.key(), (*it).toBool());
00811         }
00812 
00813         if (ok != 0) *ok = true;
00814 
00815         return result;
00816     }
00817 
00834     TQMap<T, TQ_UINT8> toByteMap(bool* ok = 0) const
00835     {
00836         if (m_valueType != TQT_DBusData::Byte)
00837         {
00838             if (ok != 0) *ok = false;
00839             return TQMap<T, TQ_UINT8>();
00840         }
00841 
00842         TQMap<T, TQ_UINT8> result;
00843 
00844         const_iterator it    = begin();
00845         const_iterator endIt = end();
00846         for (; it != endIt; ++it)
00847         {
00848             result.insert(it.key(), (*it).toByte());
00849         }
00850 
00851         if (ok != 0) *ok = true;
00852 
00853         return result;
00854     }
00855 
00873     TQMap<T, TQ_INT16> toInt16Map(bool* ok = 0) const
00874     {
00875         if (m_valueType != TQT_DBusData::Int16)
00876         {
00877             if (ok != 0) *ok = false;
00878             return TQMap<T, TQ_INT16>();
00879         }
00880 
00881         TQMap<T, TQ_INT16> result;
00882 
00883         const_iterator it    = begin();
00884         const_iterator endIt = end();
00885         for (; it != endIt; ++it)
00886         {
00887             result.insert(it.key(), (*it).toInt16());
00888         }
00889 
00890         if (ok != 0) *ok = true;
00891 
00892         return result;
00893     }
00894 
00912     TQMap<T, TQ_UINT16> toUInt16Map(bool* ok = 0) const
00913     {
00914         if (m_valueType != TQT_DBusData::UInt16)
00915         {
00916             if (ok != 0) *ok = false;
00917             return TQMap<T, TQ_UINT16>();
00918         }
00919 
00920         TQMap<T, TQ_UINT16> result;
00921 
00922         const_iterator it    = begin();
00923         const_iterator endIt = end();
00924         for (; it != endIt; ++it)
00925         {
00926             result.insert(it.key(), (*it).toUInt16());
00927         }
00928 
00929         if (ok != 0) *ok = true;
00930 
00931         return result;
00932     }
00933 
00951     TQMap<T, TQ_INT32> toInt32Map(bool* ok = 0) const
00952     {
00953         if (m_valueType != TQT_DBusData::Int32)
00954         {
00955             if (ok != 0) *ok = false;
00956             return TQMap<T, TQ_INT32>();
00957         }
00958 
00959         TQMap<T, TQ_INT32> result;
00960 
00961         const_iterator it    = begin();
00962         const_iterator endIt = end();
00963         for (; it != endIt; ++it)
00964         {
00965             result.insert(it.key(), (*it).toInt32());
00966         }
00967 
00968         if (ok != 0) *ok = true;
00969 
00970         return result;
00971     }
00972 
00990     TQMap<T, TQ_UINT32> toUInt32Map(bool* ok = 0) const
00991     {
00992         if (m_valueType != TQT_DBusData::UInt32)
00993         {
00994             if (ok != 0) *ok = false;
00995             return TQMap<T, TQ_UINT32>();
00996         }
00997 
00998         TQMap<T, TQ_UINT32> result;
00999 
01000         const_iterator it    = begin();
01001         const_iterator endIt = end();
01002         for (; it != endIt; ++it)
01003         {
01004             result.insert(it.key(), (*it).toUInt32());
01005         }
01006 
01007         if (ok != 0) *ok = true;
01008 
01009         return result;
01010     }
01011 
01029     TQMap<T, TQ_INT64> toInt64Map(bool* ok = 0) const
01030     {
01031         if (m_valueType != TQT_DBusData::Int64)
01032         {
01033             if (ok != 0) *ok = false;
01034             return TQMap<T, TQ_INT64>();
01035         }
01036 
01037         TQMap<T, TQ_INT64> result;
01038 
01039         const_iterator it    = begin();
01040         const_iterator endIt = end();
01041         for (; it != endIt; ++it)
01042         {
01043             result.insert(it.key(), (*it).toInt64());
01044         }
01045 
01046         if (ok != 0) *ok = true;
01047 
01048         return result;
01049     }
01050 
01068     TQMap<T, TQ_UINT64> toUInt64Map(bool* ok = 0) const
01069     {
01070         if (m_valueType != TQT_DBusData::UInt64)
01071         {
01072             if (ok != 0) *ok = false;
01073             return TQMap<T, TQ_UINT64>();
01074         }
01075 
01076         TQMap<T, TQ_UINT64> result;
01077 
01078         const_iterator it    = begin();
01079         const_iterator endIt = end();
01080         for (; it != endIt; ++it)
01081         {
01082             result.insert(it.key(), (*it).toUInt64());
01083         }
01084 
01085         if (ok != 0) *ok = true;
01086 
01087         return result;
01088     }
01089 
01106     TQMap<T, double> toDoubleMap(bool* ok = 0) const
01107     {
01108         if (m_valueType != TQT_DBusData::Double)
01109         {
01110             if (ok != 0) *ok = false;
01111             return TQMap<T, double>();
01112         }
01113 
01114         TQMap<T, double> result;
01115 
01116         const_iterator it    = begin();
01117         const_iterator endIt = end();
01118         for (; it != endIt; ++it)
01119         {
01120             result.insert(it.key(), (*it).toDouble());
01121         }
01122 
01123         if (ok != 0) *ok = true;
01124 
01125         return result;
01126     }
01127 
01144     TQMap<T, TQString> toStringMap(bool* ok = 0) const
01145     {
01146         if (m_valueType != TQT_DBusData::String)
01147         {
01148             if (ok != 0) *ok = false;
01149             return TQMap<T, TQString>();
01150         }
01151 
01152         TQMap<T, TQString> result;
01153 
01154         const_iterator it    = begin();
01155         const_iterator endIt = end();
01156         for (; it != endIt; ++it)
01157         {
01158             result.insert(it.key(), (*it).toString());
01159         }
01160 
01161         if (ok != 0) *ok = true;
01162 
01163         return result;
01164     }
01165 
01179     TQMap<T, TQT_DBusObjectPath> toObjectPathMap(bool* ok = 0) const
01180     {
01181         if (m_valueType != TQT_DBusData::ObjectPath)
01182         {
01183             if (ok != 0) *ok = false;
01184             return TQMap<T, TQT_DBusObjectPath>();
01185         }
01186 
01187         TQMap<T, TQT_DBusObjectPath> result;
01188 
01189         const_iterator it    = begin();
01190         const_iterator endIt = end();
01191         for (; it != endIt; ++it)
01192         {
01193             result.insert(it.key(), (*it).toObjectPath());
01194         }
01195 
01196         if (ok != 0) *ok = true;
01197 
01198         return result;
01199     }
01200 
01214     TQMap<T, TQT_DBusObjectPath> toUnixFdMap(bool* ok = 0) const
01215     {
01216         if (m_valueType != TQT_DBusData::UnixFd)
01217         {
01218             if (ok != 0) *ok = false;
01219             return TQMap<T, TQT_DBusUnixFd>();
01220         }
01221 
01222         TQMap<T, TQT_DBusUnixFd> result;
01223 
01224         const_iterator it    = begin();
01225         const_iterator endIt = end();
01226         for (; it != endIt; ++it)
01227         {
01228             result.insert(it.key(), (*it).toUnixFd());
01229         }
01230 
01231         if (ok != 0) *ok = true;
01232 
01233         return result;
01234     }
01235 
01252     TQMap<T, TQT_DBusVariant> toVariantMap(bool* ok = 0) const
01253     {
01254         if (m_valueType != TQT_DBusData::Variant)
01255         {
01256             if (ok != 0) *ok = false;
01257             return TQMap<T, TQT_DBusVariant>();
01258         }
01259 
01260         TQMap<T, TQT_DBusVariant> result;
01261 
01262         const_iterator it    = begin();
01263         const_iterator endIt = end();
01264         for (; it != endIt; ++it)
01265         {
01266             result.insert(it.key(), (*it).toVariant());
01267         }
01268 
01269         if (ok != 0) *ok = true;
01270 
01271         return result;
01272     }
01273 
01274 private:
01275     TQT_DBusData::Type m_valueType;
01276     TQT_DBusData m_containerValueType;
01277 
01278     static const TQT_DBusData::Type m_keyType;
01279 };
01280 
01281 #endif

Generated on Fri Apr 26 2019 17:49:48 for DBus-1-TQt by  doxygen 1.7.1