File: generictypes.dox

package info (click to toggle)
grantlee5 5.1.0-2.1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 4,152 kB
  • sloc: cpp: 24,299; python: 413; sh: 108; ruby: 24; makefile: 20
file content (407 lines) | stat: -rw-r--r-- 12,078 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
namespace Grantlee
{

/**

  @page generic_types_and_templates Generic type and template support

  @section generic_types Generic type support

  %Grantlee offers powerful support for using any type or container in a QVariant as part of the Context. Although
  QObject based introspection based on Q_PROPERTY is the most convenient way to access properties on objects in the
  context, sometimes it is necessary to use classes which do not derive from QObject (and therefore can't have Q_PROPERTY macros)
  and where it would not be practical to create QObject wrappers around all related classes.

  In such cases the metatype can be registered with %Grantlee and an introspection method can be written.

  @code
    // Non-QObject
    class Person
    {
    public:
      Person() :age(0) {}
      Person(const QString &name, int age)
        : m_name(name), m_age(age)
      {
      }

      QString name() const
      {
        return m_name;
      }

      int age() const
      {
        return m_age;
      }

    private:
      QString m_name;
      int m_age;
    };

    // Make it possible to put Person in a QVariant.
    Q_DECLARE_METATYPE(Person)

    // Read-only introspection of Person object.
    GRANTLEE_BEGIN_LOOKUP(Person)
      if ( property == "name" )
        return object.name();
      else if ( property == "age" )
        return object.age();
    GRANTLEE_END_LOOKUP

    void someInitializer()
    {
      // Register the Person type with the %Grantlee introspection system.
      Grantlee::registerMetaType<Person>();
    }

    Grantlee::Context getContext()
    {
      Grantlee::Context c;
      Person leader("The Leader", 19);

      QList<Person> followers;
      for (int i = 0; i < 3; ++i)
      {
        Person follower("Follower" + QString::number(i), 18);
        people.append(follower);
      }

      c.insert("leader", QVariant::fromValue(leader));
      c.insert("followers", QVariant::fromValue(followers));
      return c;
    }

    QString createOutput()
    {
      Template t = m_engine->newTemplate(
        "<h1>{{ leader.name }}</h1>"
        "<ul>"
        "{% for person in followers %}"
          "<li>{{ person.name }}, {{ person.age }}</li>"
        "{% endfor %}"
        "</ul>"
        );

      Context c = getContext();

      return t->render(&c);
    }
  @endcode

  There are several necessary steps and consequences.
  @li The type must be registered as a QMetaType with Q_DECLARE_METATYPE. Note that this is not needed for QObject derived types.
  @li All containers are supported. (See @ref generic_containers)
  @li The GRANTLEE_BEGIN_LOOKUP and GRANTLEE_END_LOOKUP macros help to define the introspection of the type. Between them is the definition of a method with the signature QVariant getProperty(const Type &object, const QString &property).
  @li Grantlee::registerMetaType must be called at some point in the program before attempting to use the type in a Context.
  @li The Context is created and used as normal.

  @section generic_containers Generic container support

  %Grantlee supports most Qt and stl containers by default if they are registered with the QMetaType system as shown in @ref generic_types.
  Where a container does not have built in support it can easily be added (See @ref third_party_containers).

  The following containers have built in support:

  @li QList&lt;T&gt;
  @li QVector&lt;T&gt;
  @li QSet&lt;T&gt;
  @li QLinkedList&lt;T&gt;
  @li QStack&lt;T&gt;
  @li QQueue&lt;T&gt;
  @li std::vector&lt;T&gt;
  @li std::deque&lt;T&gt;
  @li std::list&lt;T&gt;
  @li QHash&lt;QString, T&gt;
  @li QHash&lt;qint16, T&gt;
  @li QHash&lt;qint32, T&gt;
  @li QHash&lt;qint64, T&gt;
  @li QHash&lt;quint16, T&gt;
  @li QHash&lt;quint32, T&gt;
  @li QHash&lt;quint64, T&gt;
  @li QMap&lt;QString, T&gt;
  @li QMap&lt;qint16, T&gt;
  @li QMap&lt;qint32, T&gt;
  @li QMap&lt;qint64, T&gt;
  @li QMap&lt;quint16, T&gt;
  @li QMap&lt;quint32, T&gt;
  @li QMap&lt;quint64, T&gt;
  @li std::map&lt;QString, T&gt;
  @li std::map&lt;qint16, T&gt;
  @li std::map&lt;qint32, T&gt;
  @li std::map&lt;qint64, T&gt;
  @li std::map&lt;quint16, T&gt;
  @li std::map&lt;quint32, T&gt;
  @li std::map&lt;quint64, T&gt;

  where T is one of

  @li bool
  @li qint16
  @li qint32
  @li qint64
  @li quint16
  @li quint32
  @li quint64
  @li float
  @li double
  @li QVariant
  @li QString
  @li QDateTime
  @li A pointer to a type which inherits QObject
  @li Any type registered with Grantlee::registerMetaType
  @li Any supported container

  Note that QSet&lt;T&gt; is an exception and will only work with types for which qHash(T) is defined. This means that QSet&lt;QVariant&gt;
  is not possible for example.

  Note also that containers of pointers to QObject derived types can be stored in containers, and they do not need to be explicitly
  registered with %Grantlee. Where the class has sufficient Q_PROPERTYs defined, the introspection method described above with
  GRANTLEE_BEGIN_LOOKUP and GRANTLEE_END_LOOKUP is also not necessary. Note also that any type or container can be used through a Q_PROPERTY.

  @code
    class PersonObject : public QObject
    {
      Q_OBJECT
      Q_PROPERTY(QString name READ name)
      Q_PROPERTY(int age READ age)
    public:
      PersonObject(const QString &name, int age, QObject *parent = 0);

      QString name() const;
      int age() const;
    };

    class SportsClub : public QObject
    {
      Q_OBJECT
      Q_PROPERTY(QString name READ name)
      Q_PROPERTY(QString sport READ sport)
      Q_PROPERTY(std::vector<QObject*> members READ members)
    public:
      SportsClub(const QString &name, const QString &sport, QObject *parent = 0);

      QString name() const;
      QString sport() const;
      std::vector<QObject*> members() const;
      void setMembers(std::vector<QObject*> members);
    };

    void someInitializer()
    {
      // QObject* already has built in support. No need to register the types
      // with Grantlee::registerMetaType
    }

    Grantlee::Context SomeClass::getContext()
    {
      Grantlee::Context c;

      QSet<QObject*> clubs;
      {
        SportsClub *club = new SportsClub("Smithfield Tennis Club", "Tennis", this);

        std::vector<QObject*> members;
        {
          QObject *member = new PersonObject("Alice", 21, this);
          members.push_back(member);
        }
        {
          QObject *member = new PersonObject("Bob", 22, this);
          members.push_back(member);
        }
        club.setMembers(members);
      }

      // ... specify other clubs and members

      c.insert("sportsClubs", QVariant::fromValue(clubs));
      return c;
    }

    QString createOutput()
    {
      Template t = m_engine->newTemplate(
        "{% regroup sportsClubs by sport as groupedSports %}"
        "{% for groupedClub in groupedSports %}"
          "<h1>{{ groupedClub.grouper }}</h1>"
          "{% for club in groupedClub.list %}"
            "<h2>{{ club.name }}</h2>"
            "<ul>"
            "{% for member in club.members %}"
              "<li>{{ member.name, }}, {{ member.age }}"
            "{% endfor %}"
          "</ul>"
          "{% endfor %}"
        "{% endfor %}"
        );

      Context c = getContext();

      return t->render(&c);
    }
  @endcode

  @see <a href="http://docs.djangoproject.com/en/1.1/ref/templates/builtins/#regroup">The regroup tag</a>

  The output would be something like

  @verbatim
    <h1>Tennis</h1>
      <h2>Smithfield Tennis Club</h2>
      <ul>
        <li>Alice, 21</li>
        <li>Bob, 22</li>
      </ul>
      <h2>Greenore Lawn Tennis and Fitness Club</h2>
      <ul>
        <li>Charlie, 23</li>
        <li>David, 24</li>
        <li>Elaine, 25</li>
        <li>Frank, 26</li>
      </ul>
    <h1>Basketball</h1>
      <h2>Sandyford Basketball Club</h2>
      <ul>
        <li>Gilian, 27</li>
        <li>Henry, 28</li>
      </ul>
  @endverbatim

  Of course, it is possible to use containers of pointers to concrete QObject subclasses, such as QSet&lt;PersonObject*&gt; and std::vector&lt;SportsClub*&gt; too.

  Because any supported container can also be used as a contained type, nested containers are also supported.

  Note that if a type is registered with Grantlee::registerMetaType, built in containers of that type do not also need to be registered.
  Third party containers do need to be registered however (See @ref third_party_containers)

  @code
    Q_DECLARE_METATYPE(Person)

    void someInitializer()
    {
      Grantlee::registerMetaType<Person>();

      // Now any of the nested containers can be put in a Context and used in a Template.
    }
  @endcode

  @section third_party_containers Third party containers

  To support another, non-built in container it is necessary to use some macros to register it with %Grantlee.

  For a sequential container, Q_DECLARE_SEQUENTIAL_CONTAINER_METATYPE,
  and Q_DECLARE_ASSOCIATIVE_CONTAINER_METATYPE are needed.

  @code
    #include <boost/circular_buffer>

    // Enable looping over the contents of the container
    Q_DECLARE_SEQUENTIAL_CONTAINER_METATYPE(boost::circular_buffer)

    Q_DECLARE_METATYPE(boost::circular_buffer<Person>)

    void someInitializer()
    {
      Grantlee::registerMetaType<Person>();
    }

    Grantlee::Context getContext()
    {
      Grantlee::Context c;

      boost::circular_buffer<Person> buffer(5);
      // loop
      {
        Person p("Grant", 21);
        b.push_back(p);
      }
      c.insert("people", QVariant::fromValue(buffer));
    }
  @endcode

  @see @ref lookups

  For associative containers Q_DECLARE_ASSOCIATIVE_CONTAINER_METATYPE is needed.

  @section smart_pointers Smart Pointers

  Shared pointer types containing a custom type should be introspected as normal using GRANTLEE_BEGIN_LOOKUP and GRANTLEE_END_LOOKUP

  @code
    Q_DECLARE_METATYPE(QSharedPointer<Person>)

    void someInitializer()
    {
      Grantlee::registerMetaType<QSharedPointer<Person> >();
    }

    GRANTLEE_BEGIN_LOOKUP(QSharedPointer<Person>)
      if (property == "name")
        return object->name();
    GRANTLEE_END_LOOKUP
  @endcode

  Note that if only shared pointers to the type is in your introspectable API you only need to define the property access for the
  shared pointer. In the case above, there is no need to use Q_DECLARE_METATYPE or GRANTLEE_BEGIN_LOOKUP with Person or Person*.

  This is of course true of any smart pointer:

  @code
    Q_DECLARE_METATYPE(boost::shared_ptr<Person>)

    GRANTLEE_BEGIN_LOOKUP(boost::shared_ptr<Person>)
      if (property == "name")
        return object->name();
    GRANTLEE_END_LOOKUP
  @endcode

  QSharedPointers containing QObject derived types get special treatment.

  QObjects are automatically introspected for their Q_PROPERTYs (See @ref custom_objects).

  If you have QSharedPointer&lt;PersonObject&gt; where PersonObject is derived from QObject it will be automatically
  introspected just like a QObject* is without requiring the GRANTLEE_BEGIN_LOOKUP and GRANTLEE_END_LOOKUP macros,
  the Q_DECLARE_METATYPE macro, or registration with the %Grantlee metatype system. All of the access registration
  is handled by Qt.

  @code
    void getContext()
    {
      Grantlee::Context c;
      QSharedPointer<PersonObject> p( new PersonObject );
      c.insert("person", QVariant::fromValue(p));
      return c;
    }

    QString createOutput()
    {
      // Uses Q_PROPERTYs defined on PersonObject for name and age
      Template t = m_engine->newTemplate( "{{ person.name }}, {{ person.age }}" );

      Context c = getContext();

      return t->render(&c);
    }
  @endcode

  The same technique can be used to support QObject derived types in third party shared pointers, but that requires
  an extra macro, Q_DECLARE_SMART_POINTER_METATYPE.

  @code
    Q_DECLARE_SMART_POINTER_METATYPE(boost::shared_ptr)

    void getContext()
    {
      Grantlee::Context c;
      boost::shared_ptr<PersonObject> p( new PersonObject );
      c.insert("person", QVariant::fromValue(p));
      return c;
    }
  @endcode

*/

}