libQuotient
A Qt library for building matrix clients
event.h File Reference
#include "single_key_value.h"
#include <Quotient/converters.h>
#include <Quotient/function_traits.h>
#include <span>
Include dependency graph for event.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  Quotient::AbstractEventMetaType
 The base class for event metatypes. More...
 
class  Quotient::EventMetaType< EventT >
 A family of event meta-types to load and match events. More...
 
class  Quotient::Event
 
class  Quotient::EventTemplate< EventT, BaseEventT, ContentT >
 A template base class to derive your event type from. More...
 

Namespaces

 Quotient
 

Macros

#define QUO_BASE_EVENT(CppType_, BaseCppType_, ...)
 Supply event metatype information in base event types. More...
 
#define QUO_EVENT(CppType_, MatrixType_)
 Supply event metatype information in (specific) event types. More...
 
#define QUO_CONTENT_GETTER_X(PartType_, PartName_, JsonKey_)
 
#define QUO_CONTENT_GETTER(PartType_, PartName_)    QUO_CONTENT_GETTER_X(PartType_, PartName_, toSnakeCase(#PartName_##_L1))
 Define an inline method obtaining a content part. More...
 
#define DEFINE_SIMPLE_EVENT(Name_, Base_, TypeId_, ValueType_, GetterName_, JsonKey_)
 Define a new event class with a single key-value pair in the content. More...
 

Typedefs

template<typename EventT >
using Quotient::event_ptr_tt = std::unique_ptr< EventT >
 
using Quotient::event_type_t = QLatin1String
 
using Quotient::EventPtr = event_ptr_tt< Event >
 
template<EventClass EventT>
using Quotient::EventsArray = std::vector< event_ptr_tt< EventT > >
 
using Quotient::Events = EventsArray< Event >
 

Functions

template<EventClass EventT>
bool Quotient::is (const Event &e)
 
bool Quotient::operator== (const AbstractEventMetaType &lhs, const AbstractEventMetaType &rhs)
 
template<EventClass EventT, typename... ArgTs>
event_ptr_tt< EventT > Quotient::makeEvent (ArgTs &&... args)
 Create an event of arbitrary type from its arguments. More...
 
template<EventClass EventT>
constexpr const auto & Quotient::mostSpecificMetaType ()
 
template<EventClass EventT>
event_ptr_tt< EventT > Quotient::loadEvent (const QJsonObject &fullJson)
 Create an event with proper type from a JSON object. More...
 
template<EventClass EventT>
event_ptr_tt< EventT > Quotient::loadEvent (const QString &matrixType, const auto &... otherBasicJsonParams)
 Create an event from a type string and content JSON. More...
 
template<EventClass EventT, typename BasePtrT >
auto Quotient::eventCast (const BasePtrT &eptr) -> decltype(static_cast< EventT * >(std::to_address(eptr)))
 Cast the event pointer down in a type-safe way. More...
 
template<EventClass EventT, typename BaseEventT >
auto Quotient::eventCast (event_ptr_tt< BaseEventT > &&eptr)
 Cast the event pointer down in a type-safe way, with moving. More...
 
template<EventClass BaseT, typename TailT >
auto Quotient::switchOnType (const BaseT &event, TailT &&tail)
 
template<typename FnT1 , typename... FnTs>
auto Quotient::switchOnType (const EventClass auto &event, FnT1 &&fn1, FnTs &&... fns)
 

Variables

constexpr auto Quotient::TypeKey = "type"_L1
 
constexpr auto Quotient::ContentKey = "content"_L1
 
constexpr auto Quotient::SenderKey = "sender"_L1
 
constexpr auto Quotient::UnsignedKey = "unsigned"_L1
 
template<typename EventT , typename BaseEventT = Event>
concept Quotient::EventClass = std::derived_from<EventT, BaseEventT>
 

Macro Definition Documentation

◆ DEFINE_SIMPLE_EVENT

#define DEFINE_SIMPLE_EVENT (   Name_,
  Base_,
  TypeId_,
  ValueType_,
  GetterName_,
  JsonKey_ 
)
Value:
constexpr inline auto Name_##ContentKey = JsonKey_##_L1; \
class QUOTIENT_API Name_ \
: public ::Quotient::EventTemplate< \
Name_, Base_, EventContent::SingleKeyValue<ValueType_, Name_##ContentKey>> { \
public: \
QUO_EVENT(Name_, TypeId_) \
using value_type = ValueType_; \
using EventTemplate::EventTemplate; \
QUO_CONTENT_GETTER_X(ValueType_, GetterName_, Name_##ContentKey) \
}; \
constexpr auto ContentKey
Definition: event.h:22
#define QUOTIENT_API

Define a new event class with a single key-value pair in the content.

This macro defines a new event class Name_ derived from Base_, with Matrix event type TypeId_, providing a getter named GetterName_ for a single value of type ValueType_ inside the event content. To retrieve the value the getter uses a JSON key name that corresponds to its own (getter's) name but written in snake_case. GetterName_ must be in camelCase, no quotes (an identifier, not a literal).

Definition at line 476 of file event.h.

◆ QUO_BASE_EVENT

#define QUO_BASE_EVENT (   CppType_,
  BaseCppType_,
  ... 
)
Value:
friend class EventMetaType<CppType_>; \
static inline EventMetaType<CppType_> BaseMetaType{ \
#CppType_, &BaseCppType_::BaseMetaType __VA_OPT__(, ) __VA_ARGS__ \
}; \
static_assert(&CppType_::BaseMetaType == &BaseMetaType, \
#CppType_ " is wrong here - check for copy-pasta"); \
const AbstractEventMetaType& metaType() const override \
{ \
return BaseMetaType; \
} \

Supply event metatype information in base event types.

Use this macro in a public section of your base event class to provide type identity and enable dynamic loading of generic events of that type. Do not add this macro if your class is an intermediate wrapper and is not supposed to be instantiated on its own. Provides BaseMetaType static field initialised by parameters passed to the macro, and a metaType() override pointing to that BaseMetaType.

See also
EventMetaType

Definition at line 408 of file event.h.

◆ QUO_CONTENT_GETTER

#define QUO_CONTENT_GETTER (   PartType_,
  PartName_ 
)     QUO_CONTENT_GETTER_X(PartType_, PartName_, toSnakeCase(#PartName_##_L1))

Define an inline method obtaining a content part.

This macro adds a const method that extracts a JSON value at the key toSnakeCase(PartName_) (sic) and converts it to the type PartType_. Effectively, the generated method is an equivalent of

contentPart<PartType_>(Quotient::toSnakeCase(#PartName_##_L1));
auto toSnakeCase(QLatin1String s)
Definition: converters.h:564

Definition at line 465 of file event.h.

◆ QUO_CONTENT_GETTER_X

#define QUO_CONTENT_GETTER_X (   PartType_,
  PartName_,
  JsonKey_ 
)
Value:
PartType_ PartName_() const \
{ \
static const auto PartName_##JsonKey = JsonKey_; \
return contentPart<PartType_>(PartName_##JsonKey); \
}

Definition at line 450 of file event.h.

◆ QUO_EVENT

#define QUO_EVENT (   CppType_,
  MatrixType_ 
)
Value:
friend class EventMetaType<CppType_>; \
static inline const EventMetaType<CppType_> MetaType{ #CppType_, \
&BaseMetaType, \
MatrixType_ }; \
static_assert(&CppType_::MetaType == &MetaType, \
#CppType_ " is wrong here - check for copy-pasta"); \
static inline const auto& TypeId = MetaType.matrixId; \
const AbstractEventMetaType& metaType() const override \
{ \
return MetaType; \
} \

Supply event metatype information in (specific) event types.

Use this macro in a public section of your event class to provide type identity and enable dynamic loading of generic events of that type. Do not use this macro if your class is an intermediate wrapper and is not supposed to be instantiated on its own. Provides MetaType static field initialised as described below; a metaType() override pointing to it; and the TypeId static field that is equal to MetaType.matrixId.

The first two macro parameters are used as the first two EventMetaType constructor parameters; the third EventMetaType parameter is always BaseMetaType; and additional base types can be passed in extra macro parameters if you need to include the same event type in more than one event factory hierarchy (e.g., EncryptedEvent).

See also
EventMetaType

Definition at line 436 of file event.h.