libQuotient
A Qt library for building matrix clients
Quotient::EventMetaType< EventT > Class Template Reference

A family of event meta-types to load and match events. More...

#include <event.h>

Inheritance diagram for Quotient::EventMetaType< EventT >:
Collaboration diagram for Quotient::EventMetaType< EventT >:

Public Member Functions

event_ptr_tt< EventT > loadFrom (const QJsonObject &fullJson, const QString &type) const
 Try to load an event from JSON, with dynamic type resolution. More...
 
 AbstractEventMetaType (const char *className, AbstractEventMetaType *nearestBase=nullptr, const char *matrixId=nullptr)
 
- Public Member Functions inherited from Quotient::AbstractEventMetaType
 AbstractEventMetaType (const char *className, AbstractEventMetaType *nearestBase=nullptr, const char *matrixId=nullptr)
 
void addDerived (const AbstractEventMetaType *newType)
 
virtual ~AbstractEventMetaType ()=default
 

Additional Inherited Members

- Public Attributes inherited from Quotient::AbstractEventMetaType
const char *const className
 C++ class name this metatype is for. More...
 
const AbstractEventMetaType *const baseType
 
const event_type_t matrixId
 

Detailed Description

template<class EventT>
class Quotient::EventMetaType< EventT >

A family of event meta-types to load and match events.

TL;DR for the loadFrom() story:

  • for base event types, use QUO_BASE_EVENT and, if you have additional validation (e.g., JSON has to contain a certain key - see StateEvent for a real example), define it in the static EventT::isValid() member function accepting QJsonObject and returning bool.
  • for leaf (specific) event types - simply use QUO_EVENT and it will do everything necessary, including the TypeId definition.
    See also
    QUO_EVENT, QUO_BASE_EVENT

Definition at line 101 of file event.h.

Member Function Documentation

◆ AbstractEventMetaType()

template<class EventT >
Quotient::AbstractEventMetaType::AbstractEventMetaType
inlineexplicit

Definition at line 54 of file event.h.

◆ loadFrom()

template<class EventT >
event_ptr_tt<EventT> Quotient::EventMetaType< EventT >::loadFrom ( const QJsonObject &  fullJson,
const QString &  type 
) const
inline

Try to load an event from JSON, with dynamic type resolution.

The generic logic defined in this class template and invoked applies to all event types defined in the library and boils down to the following: 1. a. If EventT has TypeId defined (which normally is a case of all leaf - specific - event types, via QUO_EVENT macro) and type doesn't exactly match it, nullptr is immediately returned. b. In absence of TypeId, an event type is assumed to be a base; its derivedTypes are examined, and this algorithm is applied recursively on each.

  1. Optional validation: if EventT (or, due to the way inheritance works, any of its base event types) has a static isValid() predicate and the event JSON does not satisfy it, nullptr is immediately returned to the upper level or to the loadFrom() caller. This is how existence of state_key is checked in any type derived from StateEvent.
  2. If step 1b above returned non-nullptr, immediately return it. 4. a. If EventT::isValid() or EventT::TypeId (either, or both) exist and are satisfied (see steps 1a and 2 above), an object of this type is created from the passed JSON and returned. In case of a base event type, this will be a generic (aka "unknown") event. b. If neither exists, a generic event is only created and returned when on the top level (i.e., outside of recursion into derivedTypes); lower levels return nullptr instead and the type lookup continues. The latter is a case of a derived base event metatype (e.g. RoomEvent) called from its base event metatype (i.e., Event). If no matching type derived from RoomEvent is found, the nested lookup returns nullptr rather than a generic RoomEvent, so that other types derived from Event could be examined.

Definition at line 137 of file event.h.


The documentation for this class was generated from the following file: