Generic type support
Cutelee offers powerful support for using any type or container in a QVariant as part of the Context. Qt introspection based on Q_PROPERTY is the most convenient way to access properties on QObject derived type or types decorated with the Q_GADGET macro. However, sometimes it is necessary to use classes which do can't have Q_PROPERTY macros (perhaps because they are defined in third-party headers) and where it would not be practical to create QObject/Q_GADGET wrappers around all related classes.
In such cases the metatype can be registered with Cutelee and an introspection method can be written.
{
public:
: 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;
};
Q_DECLARE_METATYPE(Person)
if ( property == "name" )
return object.name();
else if ( property == "age" )
return object.age();
void someInitializer()
{
}
Cutelee::Context getContext()
{
Cutelee::Context c;
Person leader("The Leader", 19);
QList<Person> followers;
for (int i = 0; i < 3; ++i)
{
people.append(follower);
}
return c;
}
QString createOutput()
{
"<h1>{{ leader.name }}</h1>"
"<ul>"
"{% for person in followers %}"
"<li>{{ person.name }}, {{ person.age }}</li>"
"{% endfor %}"
"</ul>"
);
return t->render(&c);
}
The Context class holds the context to render a Template with.
void insert(const QString &name, QObject *object)
The Template class is a tree of nodes which may be rendered.
int registerMetaType()
Registers the type RealType with the metatype system.
QString number(double n, char format, int precision)
QVariant fromValue(T &&value)
There are several necessary steps and consequences.
- The type must be registered as a QMetaType with Q_DECLARE_METATYPE. Note that this is not needed for QObject derived types.
- All containers are supported. (See Generic container support)
- The CUTELEE_BEGIN_LOOKUP and CUTELEE_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).
- Cutelee::registerMetaType must be called at some point in the program before attempting to use the type in a Context.
- The Context is created and used as normal.
Generic container support
Cutelee supports most Qt and STL containers by default if they are registered with the QMetaType system as shown in Generic type support. Where a container does not have built in support it can easily be added (See Third party containers).
The following containers have built in support:
- QList<T>
- QVector<T>
- QSet<T>
- QLinkedList<T>
- QStack<T>
- QQueue<T>
- std::vector<T>
- std::deque<T>
- std::list<T>
- QHash<QString, T>
- QHash<qint16, T>
- QHash<qint32, T>
- QHash<qint64, T>
- QHash<quint16, T>
- QHash<quint32, T>
- QHash<quint64, T>
- QMap<QString, T>
- QMap<qint16, T>
- QMap<qint32, T>
- QMap<qint64, T>
- QMap<quint16, T>
- QMap<quint32, T>
- QMap<quint64, T>
- std::map<QString, T>
- std::map<qint16, T>
- std::map<qint32, T>
- std::map<qint64, T>
- std::map<quint16, T>
- std::map<quint32, T>
- std::map<quint64, T>
where T is one of
Note that QSet<T> is an exception and will only work with types for which qHash(T) is defined. This means that QSet<QVariant> 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 Cutelee. Where the class has sufficient Q_PROPERTYs defined, the introspection method described above with CUTELEE_BEGIN_LOOKUP and CUTELEE_END_LOOKUP is also not necessary. Note also that any type or container can be used through a Q_PROPERTY.
{
Q_OBJECT
Q_PROPERTY(int age READ age)
public:
int age() const;
};
{
Q_PROPERTY(std::vector<QObject*> members READ members)
public:
std::vector<QObject*> members() const;
void setMembers(std::vector<QObject*> members);
};
void someInitializer()
{
}
Cutelee::Context SomeClass::getContext()
{
Cutelee::Context c;
QSet<QObject*> clubs;
{
auto club = new SportsClub("Smithfield Tennis Club", "Tennis", this);
std::vector<QObject*> members;
{
auto member = new PersonObject("Alice", 21, this);
members.push_back(member);
}
{
auto member = new PersonObject("Bob", 22, this);
members.push_back(member);
}
club.setMembers(members);
}
return c;
}
QString createOutput()
{
auto 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 %}"
);
auto c = getContext();
return t->render(&c);
}
QObject * parent() const const
- See also
- The regroup tag
The output would be something like
<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>
Of course, it is possible to use containers of pointers to concrete QObject subclasses, such as QSet<PersonObject*> and std::vector<SportsClub*> too.
Because any supported container can also be used as a contained type, nested containers such as QVector<QList<PersonObject*>> are also supported.
Note that if a type is registered with Cutelee::registerMetaType, built in containers of that type do not also need to be registered. Third party containers do need to be registered however (See Third party containers)
void someInitializer()
{
}
Third party containers
To support another, non-built in container it is necessary to use some macros to register it with Cutelee.
For a sequential container, Q_DECLARE_SEQUENTIAL_CONTAINER_METATYPE, and Q_DECLARE_ASSOCIATIVE_CONTAINER_METATYPE are needed.
#include <boost/circular_buffer>
Q_DECLARE_SEQUENTIAL_CONTAINER_METATYPE(boost::circular_buffer)
Q_DECLARE_METATYPE(boost::circular_buffer<Person>)
void someInitializer()
{
}
{
boost::circular_buffer<Person> buffer(5);
{
b.push_back(p);
}
}
- See also
- Variable lookups
For associative containers Q_DECLARE_ASSOCIATIVE_CONTAINER_METATYPE is needed.
Smart Pointers
Shared pointer types containing a custom type should be introspected as normal using CUTELEE_BEGIN_LOOKUP and CUTELEE_END_LOOKUP
Q_DECLARE_METATYPE(std::shared_ptr<Person>)
void someInitializer()
{
}
if (property == "name")
return object->name();
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 CUTELEE_BEGIN_LOOKUP with Person or Person*.
This is of course true of any smart pointer:
Q_DECLARE_METATYPE(boost::shared_ptr<Person>)
if (property == "name")
return object->name();
std::shared_ptrs containing QObject derived types get special treatment.
QObjects are automatically introspected for their Q_PROPERTYs (See Custom objects).
If you have std::shared_ptr<PersonObject> where PersonObject is derived from QObject it will be automatically introspected just like a QObject* is without requiring the CUTELEE_BEGIN_LOOKUP and CUTELEE_END_LOOKUP macros, the Q_DECLARE_METATYPE macro, or registration with the Cutelee metatype system. All of the access registration is handled by Qt.
void getContext()
{
auto p = std::shared_ptr<PersonObject>::create();
return c;
}
{
auto t = m_engine->newTemplate( "{{ person.name }}, {{ person.age }}" );
auto c = getContext();
return t->render(&c);
}
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.
Q_DECLARE_SMART_POINTER_METATYPE(boost::shared_ptr)
void getContext()
{
return c;
}