qtmobility/src/contacts/qtcontactsglobal.h
changeset 4 90517678cc4f
parent 1 2b40d63a9c3d
child 11 06b8e2af4411
equal deleted inserted replaced
1:2b40d63a9c3d 4:90517678cc4f
    42 #ifndef QTCONTACTSGLOBAL_H
    42 #ifndef QTCONTACTSGLOBAL_H
    43 #define QTCONTACTSGLOBAL_H
    43 #define QTCONTACTSGLOBAL_H
    44 
    44 
    45 #include <qmobilityglobal.h>
    45 #include <qmobilityglobal.h>
    46 #include <QString>
    46 #include <QString>
       
    47 #include <QVariant>
    47 
    48 
    48 #define QTCONTACTS_VERSION_NAME "com.nokia.qt.mobility.contacts.api.version" 
    49 #define QTCONTACTS_VERSION_NAME "com.nokia.qt.mobility.contacts.api.version"
    49 #define QTCONTACTS_IMPLEMENTATION_VERSION_NAME "com.nokia.qt.mobility.contacts.implementation.version" 
    50 #define QTCONTACTS_IMPLEMENTATION_VERSION_NAME "com.nokia.qt.mobility.contacts.implementation.version"
    50 #define QTCONTACTS_VERSION 1 
    51 #define QTCONTACTS_VERSION 1
    51 
    52 
    52 QTM_BEGIN_NAMESPACE
    53 QTM_BEGIN_NAMESPACE
    53 
    54 
    54 typedef quint32 QContactLocalId; // XXX Put this else where
       
    55 
       
    56 /*
    55 /*
    57  * Latin1Literal
    56  * QLatin1Constant
    58  *
    57  *
    59  * The idea of the Latin1Literal is to provide a POD-esque container
    58  * The idea of the QLatin1Constant is to provide a POD-esque container
    60  * for constant strings which are defined in various places
    59  * for constant strings which are defined in various places
    61  * (e.g., detail leaf class definition names, field keys, constant field values, etc).
    60  * (e.g., detail leaf class definition names, field keys, constant field values, etc).
    62  * We would ideally like these to be stored in the .rodata section to allow
    61  * We would ideally like these to be stored in the .rodata section to allow
    63  * sharing / minimise footprint.
    62  * sharing / minimise footprint.
    64  *
    63  *
    65  * Given that the declare/define macros are const anyway, we changed the
    64  * Given that the declare/define macros are const anyway, we changed the
    66  * member to a char array from a const char array, in order to squash
    65  * member to a char array from a const char array, in order to squash
    67  * the compiler warning regarding uninitialised const value without
    66  * the compiler warning regarding uninitialised const value without
    68  * initialiser list in default ctor (POD requires default ctor).
    67  * initialiser list in default ctor (POD requires default ctor).
    69  * Does it work as hoped?
       
    70  */
    68  */
    71 
    69 
    72 template <int N> struct Latin1Literal
    70 template <int N> struct QLatin1Constant
    73 {
    71 {
    74     //const char str[N]; // causes compiler warning due to uninitialized const value
    72     char chars[N];
    75     char str[N];
       
    76 
    73 
    77     operator QLatin1String() const {return QLatin1String(str);}
    74     bool operator ==(const QLatin1Constant& other) const {return (chars == other.chars) || (qstrcmp(chars, other.chars) == 0);}
    78     operator QString() const {return QString::fromLatin1(str, N-1);}
    75     bool operator !=(const QLatin1Constant& other) const {return !operator==(other);}
       
    76 
       
    77     inline const char * latin1() const {return chars;}
       
    78 
       
    79     operator QLatin1String() const {return QLatin1String(chars);}
       
    80     operator QString() const {return QString::fromLatin1(chars, N-1);}
       
    81     operator QVariant() const {return QVariant(operator QString());}
    79 };
    82 };
    80 
    83 
    81 template<int N> bool operator==(const Latin1Literal<N>& a, const QLatin1String& b)
    84 /* Hash - this comes from qhash.cpp >.> */
       
    85 template<int N> uint qHash(const QLatin1Constant<N>& a)
    82 {
    86 {
    83     return QLatin1String(a.str) == b;
    87     uint h = 0;
       
    88     uint g;
       
    89     int n = N - 1;
       
    90     const register uchar*p = (const uchar*)a.chars;
       
    91 
       
    92     while (n--) {
       
    93         h = (h << 4) + *p++;
       
    94         if ((g = (h & 0xf0000000)) != 0)
       
    95             h ^= g >> 23;
       
    96         h &= ~g;
       
    97     }
       
    98     return h;
    84 }
    99 }
    85 
   100 
    86 template<int N> bool operator==(const Latin1Literal<N>& a, const QString& b)
   101 /* Operators for QLatin1Constant */
       
   102 template<int N, int M> bool operator==(const QLatin1Constant<N>&, const QLatin1Constant<M>&)
    87 {
   103 {
    88     return b == QLatin1String(a.str);
   104     // For N != M, this is always false
       
   105     // For N == M, the member function gets called
       
   106     return false;
       
   107 }
       
   108 template<int N, int M> bool operator!=(const QLatin1Constant<N>&, const QLatin1Constant<M>&)
       
   109 {
       
   110     // If N != M, this is always true
       
   111     // For N == M, the member function again gets called
       
   112     return true;
    89 }
   113 }
    90 
   114 
    91 template<int N> bool operator==(const QLatin1String& b, const Latin1Literal<N>& a)
   115 template<int N, int M> bool operator <(const QLatin1Constant<N>& a, const QLatin1Constant<M>& b)
    92 {
   116 {
    93     return QLatin1String(a.str) == b;
   117     return qstrcmp(a.chars, b.chars) < 0;
    94 }
   118 }
    95 
   119 
    96 template<int N> bool operator==(const QString& b, const Latin1Literal<N>& a)
   120 /* Operators for QLatin1String */
       
   121 template<int N> bool operator==(const QLatin1Constant<N>& a, const QLatin1String& b)
    97 {
   122 {
    98     return b == QLatin1String(a.str);
   123     return (a.chars == b.latin1()) || (qstrcmp(a.chars, b.latin1()) == 0);
    99 }
   124 }
   100 
   125 
   101 template<int N> bool operator!=(const Latin1Literal<N>& a, const QLatin1String& b)
   126 template<int N> bool operator==(const QLatin1String& b, const QLatin1Constant<N>& a)
   102 {
   127 {
   103     return QLatin1String(a.str) != b;
   128     return (a.chars == b.latin1()) || (qstrcmp(a.chars, b.latin1()) == 0);
   104 }
   129 }
   105 
   130 
   106 template<int N> bool operator!=(const Latin1Literal<N>& a, const QString& b)
   131 template<int N> bool operator!=(const QLatin1Constant<N>& a, const QLatin1String& b)
   107 {
   132 {
   108     return b != QLatin1String(a.str);
   133     return (a.chars != b.latin1()) && (qstrcmp(a.chars, b.latin1()) != 0);
   109 }
   134 }
   110 
   135 
   111 template<int N> bool operator!=(const QLatin1String& b, const Latin1Literal<N>& a)
   136 template<int N> bool operator!=(const QLatin1String& b, const QLatin1Constant<N>& a)
   112 {
   137 {
   113     return QLatin1String(a.str) != b;
   138     return (a.chars != b.latin1()) && (qstrcmp(a.chars, b.latin1()) != 0);
   114 }
   139 }
   115 
   140 
   116 template<int N> bool operator!=(const QString& b, const Latin1Literal<N>& a)
   141 /* Operators for QString */
       
   142 template<int N> bool operator==(const QLatin1Constant<N>& a, const QString& b)
   117 {
   143 {
   118     return b != QLatin1String(a.str);
   144     return b == QLatin1String(a.chars);
   119 }
   145 }
   120 
   146 
   121 #define Q_DECLARE_LATIN1_LITERAL(varname, str) static const Latin1Literal<sizeof(str)> varname
   147 template<int N> bool operator==(const QString& b, const QLatin1Constant<N>& a)
   122 #define Q_DEFINE_LATIN1_LITERAL(varname, str) const Latin1Literal<sizeof(str)> varname = {str}
   148 {
       
   149     return b == QLatin1String(a.chars);
       
   150 }
       
   151 
       
   152 template<int N> bool operator!=(const QLatin1Constant<N>& a, const QString& b)
       
   153 {
       
   154     return b != QLatin1String(a.chars);
       
   155 }
       
   156 
       
   157 template<int N> bool operator!=(const QString& b, const QLatin1Constant<N>& a)
       
   158 {
       
   159     return b != QLatin1String(a.chars);
       
   160 }
       
   161 
       
   162 #define Q_DECLARE_LATIN1_CONSTANT(varname, str) static const QLatin1Constant<sizeof(str)> varname
       
   163 #define Q_DEFINE_LATIN1_CONSTANT(varname, str) const QLatin1Constant<sizeof(str)> varname = {str}
   123 
   164 
   124 QTM_END_NAMESPACE
   165 QTM_END_NAMESPACE
   125 
   166 
   126 // Not needed since this is a typedef, and qglobal already does this for the base type
   167 // Not needed since this is a typedef, and qglobal already does this for the base type
   127 // Q_DECLARE_TYPEINFO(QTM_PREPEND_NAMESPACE(QContactLocalId), Q_PRIMITIVE_TYPE);
   168 // Q_DECLARE_TYPEINFO(QTM_PREPEND_NAMESPACE(QContactLocalId), Q_PRIMITIVE_TYPE);
   128 
   169 
       
   170 // Workaround for unannounced SC break
       
   171 #include "qcontactid.h"
       
   172 
   129 #endif
   173 #endif