55 // |
55 // |
56 |
56 |
57 #include "qcontactdetail.h" |
57 #include "qcontactdetail.h" |
58 |
58 |
59 #include <QSharedData> |
59 #include <QSharedData> |
60 #include <QMap> |
|
61 #include <QString> |
60 #include <QString> |
|
61 #include <QHash> |
62 |
62 |
63 QTM_BEGIN_NAMESPACE |
63 QTM_BEGIN_NAMESPACE |
|
64 |
|
65 /* |
|
66 Yet another string class |
|
67 |
|
68 Mostly a wrapper around char *, where we do pointer equality checks before |
|
69 a strcmp, given our common use case of equivalent pointers. |
|
70 |
|
71 Also handles when things get passed in as a QString, by converting to Latin1 |
|
72 and caching the result, and caching conversions to QString (hit by definitionName a lot) |
|
73 */ |
|
74 class QContactStringHolder |
|
75 { |
|
76 public: |
|
77 QContactStringHolder() |
|
78 : m_str(0) |
|
79 { |
|
80 } |
|
81 |
|
82 ~QContactStringHolder() |
|
83 { |
|
84 } |
|
85 |
|
86 QContactStringHolder(const QContactStringHolder& other) |
|
87 : m_str(other.m_str) |
|
88 { |
|
89 } |
|
90 |
|
91 QContactStringHolder& operator=(const QContactStringHolder& other) |
|
92 { |
|
93 m_str = other.m_str; // checking for ==this is not worth the effort |
|
94 return *this; |
|
95 } |
|
96 |
|
97 QContactStringHolder(const char *str) |
|
98 : m_str(str) |
|
99 { |
|
100 } |
|
101 |
|
102 QContactStringHolder& operator=(const char *str) |
|
103 { |
|
104 m_str = str; |
|
105 return *this; |
|
106 } |
|
107 |
|
108 explicit QContactStringHolder(const QString& str) |
|
109 { |
|
110 *this = str; |
|
111 } |
|
112 |
|
113 QContactStringHolder& operator=(const QString& str) |
|
114 { |
|
115 m_str = s_allocated.value(str, 0); |
|
116 if (!m_str) { |
|
117 m_str = qstrdup(str.toLatin1().constData()); |
|
118 s_allocated.insert(str, const_cast<char*>(m_str)); // it's my pointer |
|
119 } |
|
120 return *this; |
|
121 } |
|
122 |
|
123 bool operator==(const char* other) const |
|
124 { |
|
125 return other == m_str || (qstrcmp(other, m_str) == 0); |
|
126 } |
|
127 |
|
128 bool operator==(const QString& other) const |
|
129 { |
|
130 return (s_allocated.value(other, 0) == m_str) || (other == QLatin1String(m_str)); |
|
131 } |
|
132 |
|
133 bool operator==(const QContactStringHolder& other) const |
|
134 { |
|
135 return (other.m_str == m_str) || (qstrcmp(other.m_str, m_str) == 0); |
|
136 } |
|
137 |
|
138 operator QString() const |
|
139 { |
|
140 QString s = s_qstrings.value(m_str); |
|
141 if (!s.isEmpty()) |
|
142 return s; |
|
143 s = QString::fromLatin1(m_str); |
|
144 s_qstrings.insert(m_str, s); |
|
145 return s; |
|
146 } |
|
147 |
|
148 public: |
|
149 // The only data we have |
|
150 const char* m_str; |
|
151 |
|
152 static QHash<QString, char*> s_allocated; |
|
153 static QHash<const char *, QString> s_qstrings; |
|
154 }; |
64 |
155 |
65 class QContactDetailPrivate : public QSharedData |
156 class QContactDetailPrivate : public QSharedData |
66 { |
157 { |
67 public: |
158 public: |
68 QContactDetailPrivate() |
159 QContactDetailPrivate() |
75 QContactDetailPrivate(const QContactDetailPrivate& other) |
166 QContactDetailPrivate(const QContactDetailPrivate& other) |
76 : QSharedData(other), |
167 : QSharedData(other), |
77 m_id(other.m_id), |
168 m_id(other.m_id), |
78 m_definitionName(other.m_definitionName), |
169 m_definitionName(other.m_definitionName), |
79 m_values(other.m_values), |
170 m_values(other.m_values), |
80 m_preferredActions(other.m_preferredActions), |
|
81 m_access(other.m_access) |
171 m_access(other.m_access) |
82 { |
172 { |
83 } |
173 } |
84 |
174 |
85 ~QContactDetailPrivate() {} |
175 ~QContactDetailPrivate() |
|
176 { |
|
177 } |
86 |
178 |
87 int m_id; // internal, unique id. |
179 int m_id; // internal, unique id. |
88 QString m_definitionName; |
180 QContactStringHolder m_definitionName; |
89 QVariantMap m_values; // the value(s) stored in this field. |
181 QHash<QContactStringHolder, QVariant> m_values; |
90 QList<QContactActionDescriptor> m_preferredActions; |
182 QContactDetail::AccessConstraints m_access; |
91 |
183 |
92 static QAtomicInt lastDetailKey; |
184 static QAtomicInt lastDetailKey; |
93 QContactDetail::AccessConstraints m_access; |
|
94 |
185 |
95 static void setAccessConstraints(QContactDetail *d, QContactDetail::AccessConstraints constraint) |
186 static void setAccessConstraints(QContactDetail *d, QContactDetail::AccessConstraints constraint) |
96 { |
187 { |
97 d->d->m_access = constraint; |
188 d->d->m_access = constraint; |
98 } |
189 } |
|
190 |
|
191 static const QContactDetailPrivate* detailPrivate(const QContactDetail& detail) |
|
192 { |
|
193 return detail.d.constData(); |
|
194 } |
99 }; |
195 }; |
100 |
196 |
101 QTM_END_NAMESPACE |
197 QTM_END_NAMESPACE |
102 |
198 |
|
199 Q_DECLARE_TYPEINFO(QTM_PREPEND_NAMESPACE(QContactStringHolder), Q_MOVABLE_TYPE); |
|
200 |
103 #endif |
201 #endif |