aboutsummaryrefslogtreecommitdiff
path: root/lib/events/event.h
AgeCommit message (Collapse)Author
2022-09-04More cleanupAlexey Rusakov
2022-09-04Remove #include "logging.h" from event.hAlexey Rusakov
We don't expose logging internals to the outside world.
2022-09-04Streamline event typesAlexey Rusakov
This commit introduces a few things to further reduce the boilerplate across event type definitions: - Event type is no more separately stored in Event and therefore no more passed to base event constructors. Until the previous commit, it was used by is() to quickly match the event type; with the new event metatype class, the same is achieved even quicker by comparing metatype pointers. - EventTemplate is a generalisation of StateEvent for all event types providing common constructor signatures and content() for (most) leaf event types. StateEvent therefore has become a partial specialisation of EventTemplate for types derived from StateEventBase; as the known client code base does not use it directly, a compatibility alias is not provided. Also, DEFINE_SIMPLE_EVENT now expands into a class deriving from EventTemplate. - On top of StateEvent->EventTemplate specialisation, KeyedStateEventBase and KeylessStateEventBase types are introduced with appropriate constructor signatures (with or without state_key, respectively) to allow `using` of them from derived event types. To facilitate writing of constraints, concepts for keyed and keyless state event types are also introduced; RoomStateView, e.g., makes use of those to provide appropriate method signatures. - typeId(), unknownEventTypeId(), UnknownEventTypeId are no more provided - they weren't used throughout the known code base (Quaternion, NeoChat), and the concept of "unknown event types" is hereby eliminated entirely. - RoomKeyEvent no more accepts senderId as a parameter; it has never been a good practice as the sender is assigned by Connection anyway.
2022-09-04EventMetaType, QUO_EVENT, QUO_BASE_EVENTAlexey Rusakov
The new metatype framework replaces EventFactory/DEFINE_EVENT_TYPEID/REGISTER_EVENT_TYPE; it is faster, more functional and extensible. Of note: - EventMetaType mostly reproduces the logic of EventFactory but supports custom base event types not just for loading (that part EventFactory also supported) but also for matching - previously you had to have Event::is*Event() for base type matching. Now Quotient::is() can match against both base and leaf types. - Instead of DEFINE_EVENT_TYPEID and REGISTER_EVENT_TYPE there's now a single macro, QUO_EVENT, intended for use in the way similar to Q_OBJECT. Actually, the entire framework borrows heavily from QMetaObject and Q_OBJECT. Making event types full-fledged QObjects is still not considered because half of QObject functions would not be applicable (e.g. signals/slots) while another half (in particular, using Matrix type ids to select event types) would still have to be done on top of QObject. And QML can just access events as const QJsonObjects which is arguably more lightweight as well. - QUO_BASE_EVENT is a new macro replacing EventFactory object definitions. This was necessary for the same reason why Q_OBJECT is a macro: aside from a static object definition, this macro introduces a virtual function override to resolve the metatype at runtime. This very mechanism is used to make event type matching/casting as quick as possible - QUO_BASE_EVENT and QUO_EVENT use the C++20 __VA_OPT__ feature that is only available with the new MSVC preprocessor (see https://docs.microsoft.com/en-us/cpp/preprocessor/preprocessor-experimental-overview); the respective switch was added to CMakeLists.txt.
2022-09-04DEFINE_SIMPLE_EVENT: support custom JSON keysAlexey Rusakov
2022-07-30moving eventCast(): disallow passing nullptrAlexey Rusakov
This is aligned with the non-moving version.
2022-07-29Moving eventCast()Alexey Rusakov
In a situation where you have an EventPtr that you want to place somewhere as an `event_ptr_tt<SomeMoreSpecificEventType>` you have to carefully check that the stored event is actually of SomeMoreSpecificType and if it is, release() that event pointer, downcast, and re-wrap it into that new event_ptr_tt - or, as can be seen from the diff here, re-loadEvent() from JSON, which is simpler but inefficient. To help clients, and the library, eventCast() can now accept an rvalue smart pointer and do all the necessary things with it.
2022-07-12fromJson()/toJson() refactoringAlexey Rusakov
fromJson() is generalised to accept any JSON-like type while passing QJsonObject to JsonConverter<>::load (instead of doLoad). This allows to (still) rely on JsonConverter<> as a customisation point while providing an opportunity to overload fromJson for custom types in a pointed way (specifically, by providing the overload for `fromJson(const QJsonObject&)`), instead of having to go with full-blown JsonConverter<> specialisation. This will be used in a further commit to simplify ReceiptEvent definition. Using if constexpr in combination with constraints (`requires()`) - the first such case in Quotient codebase - allowed to put the entire logic in a single JsonConverter<>::load() body instead of having a facility JsonExporter<> class for SFINAE. Aside from that, fromJson<QJsonValue, QJsonValue> is entirely dropped because it's not supposed to be used that way (it's no-op after all); reflecting that, Event::unsignedPart() and Event::contentPart() no more default to QJsonValue as the expected return type, you have to explicitly provide the type instead (and as one can see from the changes in the commit, it's actually better that way since it's better to validate the value inside JSON - e.g. check QString or QJsonObject for emptiness - than the QJsonValue envelope which may still wrap an empty value). toJson() is also generalised, replacing 3 functions with one that has a constexpr if to discern between two kinds of types.
2022-07-07Avoid std::derived_from and std::bind_frontAlexey Rusakov
Apple Clang doesn't have those yet.
2022-07-03Shorten switchOnType, function_traits and connect*Alexey Rusakov
...thanks to C++20 awesomeness. A notable change is that wrap_in_function() (and respectively function_traits<>::function_type) and fn_return_t alias are gone. The former are no more needed because connectUntil/connectSingleShot no more use std::function. The latter has been relatively underused and with the optimisation of switchOnType hereby, could be completely replaced with std::invoke_result_t. Rewriting connect* functions using constexpr and auto parameters made the implementation 30% more compact and much easier to understand (though still with a couple of - now thoroughly commented - tricky places). Dropping std::function<> from it may also bring some (quite modest, likely) performance benefits.
2022-05-08QUO_CONTENT_GETTERAlexey Rusakov
To streamline adding of simple getters of content parts.
2022-05-08Generalise DEFINE_SIMPLE_EVENTAlexey Rusakov
This macro was defined in accountdataevents.h but adding one more parameter (base class) makes it applicable to pretty much any event with the content that has one key-value pair (though state events already have a non-macro solution in the form of `StateEvent<EventContent::SingleKeyValue>`). Now CustomEvent definition in quotest.cpp can be replaced with a single line.
2022-05-08basic*EventJson() -> *Event::basicJson()Alexey Rusakov
This makes it easier and more intuitive to build a minimal JSON payload for a given event type. A common basicJson() call point is also convenient in template contexts (see next commits).
2022-02-14Revert "Don't #include "logging.h" from headers"Alexey Rusakov
This reverts commit 2cf44607cf0f057e147c2c4fe6dded6c13c58a8a (that was stupid, honestly).
2022-02-14Don't #include "logging.h" from headersAlexey Rusakov
Logging categories used by Quotient are not supposed to be exposed externally, which basically forbids usage of logging in header files. A more flexible solution would involve moving logging.h to private headers but Quotient doesn't have that thing yet.
2022-01-23Fix visit() return typeAlexey Rusakov
It's too restrictive compared to switchOnType() overloads and doesn't map to the case with a default value.
2022-01-18Revise inline keyword usageAlexey Rusakov
- Templates and constexpr imply inline - A function called from a single site better be inlined.
2022-01-05DEFINE_EVENT_TYPEID: fix up deprecation warningsAlexey Rusakov
2022-01-01Fix more Sonar warningsAlexey Rusakov
2022-01-01Don't use QUOTIENT_API inside REGISTER_EVENT_TYPEAlexey Rusakov
On Windows QUOTIENT_API expands to different things depending on whether the library is built or used. This results in confusing statements (and MSVC erroring out on them, in some cases - see below - quite legitimately) not only when the application includes Quotient headers but also when the application defines custom events and uses REGISTER_EVENT_TYPE to make them creatable from /sync responses. To avoid repeated registration when dynamic linking is involved, EventFactory<>::addMethod() now bluntly looks up the method for this type in the vector of already registered methods. It would surely be quicker to use a static variable instead; but since the refreshed API for addMethod returns a reference to the factory method it's necessary to do this lookup anyway. Once the primary goal of this branch is achieved across platforms I might experiment with lighter ways to register factory methods; for now here's a minimal change to make the code build on Windows.
2022-01-01Drop inline next to constexprAlexey Rusakov
Thanks to Sonar for reminding that constexpr implies inline.
2021-12-29Use QLatin1String for event typeId'sAlexey Rusakov
Before all, this fixes the problem with double-initialising of type ids; it could have been fixed with a smaller change but EventTypeRegistry is fairly superfluous now when inline variables are a thing and it's possible to have an extensible registry system using literally pointers to the memory that are guaranteed to be unique. That being said, event_type_t is still QLatin1String and not a bare const char* (or void*), mostly to stay on the safe side when it comes to type identities: unlike const char*, QLatin1String's are deep-compared, meaning that matching for switchOnType (former visit) occurs a bit slower now. This may change in the future; but this is the first step in getting rid of EventTypeRegistry. This change means that initializeTypeId is no more needed; also, two static member functions, typeId() and matrixTypeId(), are being replaced with a single inline static member variable, TypeId. This commit doesn't apply that transition across the event types, meaning that you'll get a pile of warnings when compiling the library. These warnings will be tackled in further commits within this branch.
2021-12-29Refactor EventFactory and move it out of _impl::Alexey Rusakov
Strictly speaking, EventFactory can be further instantiated if any client application figures they need a whole new base class for events and respectively a separate EventFactory specialisation for it. Where this whole commit started though was a linkage error because I did not plan to expose Quotient-specific logging categories for linkage (effectively, usage) from the client code - meanwhile the inline code of EventFactory uses qDebug(EVENTS), meaning I had to either add QUOTIENT_API to EVENTS or hide those invocations. This in turn led to trimming the EventFactory constructor back to trivial implementation and dropping the guard variable that was supposed to trace duplicate EventFactory<BaseEventT> objects for the same BaseEventT - with the reasoning that such situation is not really dangerous (unlike EventTypeRegistry double-initialisation fiasco, see #413), and at the same time it can be easily detected in the logs by duplicated factory method registration messages. And while I was at it, I replaced the meaningless bool in the return type of EventFactory<>::addMethod with the slightly more (but still barely) useful reference to the inserted factory method. One can (in theory) use it now if they need to turn some event JSON into an object of some specific event type or nullptr if the event type in the JSON payload doesn't match - but at the same rate (for now at least) one can call makeIfMatches<EventT>() directly. With this commit, both Quotest and Quaternion build and link using either Clang or GCC even under -fvisibility=hidden. However, running quotest now reproduces #413, which is a matter of event typeId infrastructure refactoring, coming in further commits.
2021-12-29Add QUOTIENT_API throughout non-generated codeAlexey Rusakov
This include all (hopefully) classes/structures and functions that have non-inline definitions, as well as namespaces with Q_NAMESPACE since those have non-inline (as of Qt 5.15) QMetaObject - for that a new macro, QUO_NAMESPACE, has been devised to accommodate the lack of Q_NAMESPACE_EXPORT in Qt before 5.14.
2021-12-27Key* strings: drop 'static'; add 'constexpr' where okAlexey Rusakov
2021-12-22Simplify the code around EventFactory<>Alexey Rusakov
The former code assumed that EventFactory<> is just a class-level shell for a bunch of functions and a static data member that only exists to allow specialisations to occur for the whole group together. On top of that, setupFactory() and registerEventType() strived to protect this group from double registration coming from static variables in an anonymous namespace produced by REGISTER_EVENT_TYPE. The whole thing is now de-static-ed: resolving the factory now relies on class-static Event/RoomEvent/StateEventBase::factory variables instead of factory_t type aliases; and REGISTER_EVENT_TYPE produces non-static inline variables instead, obviating the need of registerEventType/setupFactory kludge.
2021-12-02Improve function_traits<>; split out from util.*Alexey Rusakov
Quotient::function_traits<> did not support member functions in a proper way (i.e. the way std::invoke_result<> treats them, with the function's owning class represented as the first parameter). Now that I gained the skill and understanding in function_traits<> somewhat wicked machinery, I could properly support member functions. Overloads and generic lambdas are not supported but maybe we'll get to those one day.
2021-12-02visit(Event, ...) -> switchOnType()Alexey Rusakov
It has not much to do with the Visitor design pattern; also, std::visit() has different conventions on the order of parameters.
2021-11-28Comment on const return types in event.hAlexey Rusakov
Proper linters recognise that the returned types are not primitive, while people might still be confused a bit.
2021-11-28Event::unsignedPart()Alexey Rusakov
Similar to contentPart() - apparently there are enough places across the code that would benefit from it.
2021-11-27basicEventJson(): dismiss with the templateAlexey Rusakov
Given that QJsonObject only accepts QStrings in the list constructor, the template is useless cruft.
2021-11-27Event::content() -> contentPart()Alexey Rusakov
There's a clash between Event::content() (a template function) and RoomMessageEvent::content() (plain member). Out of these two, the name more fits to the RME's member function - strictly speaking, Event::content() retrieves a part of content, and so is renamed. In addition, contentPart() defaults to QJsonValue now, which is pretty intuitive (the function returns values from a JSON object) and allows to implement more elaborate logic such as if (const auto v = contentPart<>("key"_ls); v.isObject()) { // foo } else if (v.isString()) { // bar } else { // boo }
2021-11-26Revert "Drop #include "logging.h" from event.h"Alexey Rusakov
Doesn't really help build times, instead breaking the build on older Qt.
2021-11-26Event: deprecate originalJson[Object]()Alexey Rusakov
The "original JSON" wording is misleading: the returned JSON can be and is routinely edited as a part of event construction, redaction, editing. Also, originalJson() name is misleading in that it returns a stringified (in a very specific way) JSON and not an object. You have to call fullJson() to get the object, and originalJsonObject(), confusingly, returns exactly the same thing but as a value rather than as a reference. The original intention of keeping originalJsonObject() was to make it Q_INVOKABLE or use it as an accessor for a Q_PROPERTY. unfortunately, this was never really practical as discussed in the previous commit. All that implies that clients have to handle passing event JSON to QML themselves, in the form they prefer (as an object or a string). The added complexity is negligible though; on the other hand, there's added flexibility in, e.g., choosing a compact instead of default JSON layout or even generate a highlighted JSON representation.
2021-11-26Drop #include "logging.h" from event.hAlexey Rusakov
Makes compilation a tad lighter.
2021-11-26Drop Q_GADGET from most uncopyable classes; other minor cleanupAlexey Rusakov
Q_GADGET is generally used to enable two things outside of QObject: Q_PROPERTY/Q_INVOKABLE and Q_ENUM/Q_FLAG. While the latter can be used in its own right in QML, the former requires Q_GADGET instances to be passed to QML by value, which is not really possible with uncopyable/unassignable classes. Bottom line is that Q_PROPERTY in anything derived from Quotient::Event is not viable, making Q_GADGET macro useless unless there's a Q_ENUM/Q_FLAG (as is the case with RoomMessageEvent, e.g.).
2021-11-08Q_DISABLE_MOVE/COPY_MOVE; QT_IGNORE_DEPRECATIONSAlexey Rusakov
DISABLE_MOVE is no more; instead, the library provides Q_DISABLE_MOVE (and also Q_DISABLE_COPY_MOVE while at it) for Qt pre-5.13 that don't have it yet. Same for QT_IGNORE_DEPRECATIONS - it only arrived in 5.15 but all the building pieces existed prior so libQuotient has it regardless of the Qt version used for building.
2021-01-16Updated copyright statements upon Git auditKitsune Ral
After going through all the files and the history of commits on them it was clear that some copyright statements are obsolete (the code has been overwritten since) and some are missing. This commit tries best to remedy that, along with adding SPDX tags where they were still not used. Also, a minimal SPDX convention is documented for further contributions. Closes #426.
2021-01-15Merge pull request #428 from ognarb/licensingKitsune Ral
Port existing copyright statement to reuse using licensedigger
2020-12-28event.h: Fix breakage of AppVeyor CIKitsune Ral
The breakage was caused by 639f1d48.
2020-12-28event.h: Minor tweaks around visit<>Kitsune Ral
2020-12-26Port existing copyright statement to reuse using licensediggerCarl Schwan
2020-11-15Cleanup across event classesKitsune Ral
In particular: removed unnecessary #includes, deprecated and no more used constructs, replaced stored members with dynamic generation from JSON (TypingEvent and, especially promising for performance, ReceiptEvent)
2020-11-12More JSON key constantsKitsune Ral
2020-08-02Event::dumpTo: make protected, and RoomEvent overrideKitsune Ral
The override adds the event's origin timestamp
2020-03-26Refactoring around Connection::onSyncSuccess()Kitsune Ral
The method grew large and a bit unwieldy over the years.
2019-09-29Simplify visit() signatureKitsune Ral
Checking that BaseEventT descends from Event is really extraneous.
2019-08-11Now that we're C++17, remove old compatibility cruftKitsune Ral
2019-08-11Drop EventType namespace and DEFINE_EVENTTYPE_ALIAS macroKitsune Ral
They've been deprecated for almost a year by now.
2019-08-09Namespace: QMatrixClient -> Quotient (with back comp alias)Kitsune Ral