sensorfw
datatypes/datarange.h
Go to the documentation of this file.
00001 
00027 #ifndef DATARANGE_H
00028 #define DATARANGE_H
00029 
00030 #include <QObject>
00031 #include <QDBusArgument>
00032 #include <QPair>
00033 
00034 /* Datatype for storing integer ranges. */
00035 typedef QPair<unsigned int, unsigned int> IntegerRange;
00036 
00037 /* Datatype for storing list of integer ranges. */
00038 typedef QList<IntegerRange> IntegerRangeList;
00039 
00040 Q_DECLARE_METATYPE( IntegerRange )
00041 Q_DECLARE_METATYPE( IntegerRangeList )
00042 
00046 class DataRange : public QObject {
00047     Q_OBJECT;
00048 public:
00049 
00053     DataRange() : QObject(), min(0), max(0), resolution(0) {}
00054 
00060     DataRange(const DataRange &other) :
00061         QObject(), min(other.min), max(other.max), resolution(other.resolution) {}
00062 
00070     DataRange(double min, double max, double resolution) :
00071         QObject(), min(min), max(max), resolution(resolution) {}
00072 
00073     double min;         
00074     double max;         
00075     double resolution;  
00082     DataRange& operator=(const DataRange& origin)
00083     {
00084         min = origin.min;
00085         max = origin.max;
00086         resolution = origin.resolution;
00087         return *this;
00088     }
00089 
00096     bool operator==(const DataRange& right) const
00097     {
00098         return (min == right.min &&
00099                 max == right.max &&
00100                 resolution == right.resolution);
00101     }
00102 };
00103 
00104 /* Datatype for list of data ranges */
00105 typedef QList<DataRange> DataRangeList;
00106 
00107 Q_DECLARE_METATYPE( DataRange )
00108 Q_DECLARE_METATYPE( DataRangeList )
00109 
00117 inline QDBusArgument &operator<<(QDBusArgument &argument, const DataRange &data)
00118 {
00119     argument.beginStructure();
00120     argument << data.min << data.max << data.resolution;
00121     argument.endStructure();
00122     return argument;
00123 }
00124 
00132 inline const QDBusArgument &operator>>(const QDBusArgument &argument, DataRange &data)
00133 {
00134     argument.beginStructure();
00135     argument >> data.min >> data.max >> data.resolution;
00136     argument.endStructure();
00137     return argument;
00138 }
00139 
00147 inline QDBusArgument &operator<<(QDBusArgument &argument, const DataRangeList &data)
00148 {
00149     argument.beginArray(qMetaTypeId<DataRange>());
00150     foreach(const DataRange& range, data)
00151     {
00152         argument << range;
00153     }
00154     argument.endArray();
00155     return argument;
00156 }
00157 
00165 inline const QDBusArgument &operator>>(const QDBusArgument &argument, DataRangeList &data)
00166 {
00167     argument.beginArray();
00168     data.clear();
00169     while ( !argument.atEnd() ) {
00170         DataRange element;
00171         argument >> element;
00172         data.append( element );
00173     }
00174     argument.endArray();
00175     return argument;
00176 }
00177 
00185 inline QDBusArgument &operator<<(QDBusArgument &argument, const IntegerRange &data)
00186 {
00187     argument.beginStructure();
00188     argument << data.first << data.second;
00189     argument.endStructure();
00190     return argument;
00191 }
00192 
00200 inline const QDBusArgument &operator>>(const QDBusArgument &argument, IntegerRange &data)
00201 {
00202     argument.beginStructure();
00203     argument >> data.first >> data.second;
00204     argument.endStructure();
00205     return argument;
00206 }
00207 
00215 inline QDBusArgument &operator<<(QDBusArgument &argument, const IntegerRangeList &data)
00216 {
00217     argument.beginArray(qMetaTypeId<IntegerRange>());
00218     foreach(const IntegerRange& range, data)
00219     {
00220         argument << range;
00221     }
00222     argument.endArray();
00223     return argument;
00224 }
00225 
00233 inline const QDBusArgument &operator>>(const QDBusArgument &argument, IntegerRangeList &data)
00234 {
00235     argument.beginArray();
00236     data.clear();
00237     while ( !argument.atEnd() ) {
00238         IntegerRange element;
00239         argument >> element;
00240         data.append( element );
00241     }
00242     argument.endArray();
00243     return argument;
00244 }
00245 
00249 class DataRangeRequest
00250 {
00251 public:
00252 
00253     int       id;     
00254     DataRange range;  
00261     DataRangeRequest(int newId) :
00262         id(newId) {};
00263 
00270     DataRangeRequest(int newId, const DataRange& newRange) :
00271         id(newId),
00272         range(newRange) {};
00273 
00280     bool operator==(const DataRangeRequest& right) const
00281     {
00282         return (id == right.id && range == right.range);
00283     }
00284 };
00285 
00289 class IntervalRequest {
00290 public:
00291     int      id;     
00292     unsigned value;  
00300     IntervalRequest(int newId, unsigned newValue) :
00301         id(newId),
00302         value(newValue) {}
00303 
00310     bool operator==(const IntervalRequest& right) const
00311     {
00312         return (id == right.id && value == right.value);
00313     }
00314 };
00315 
00323 template<typename T, typename U>
00324 inline bool isInRange(T ref, const U& container)
00325 {
00326     foreach(const typename U::value_type& value, container)
00327     {
00328         if(ref >= value.first && ref <= value.second)
00329             return true;
00330     }
00331     return false;
00332 }
00333 
00334 #endif // DATARANGE_H