Skip to content

feat: Add strong view aliases - #7194

Merged
pajlada merged 6 commits into
Chatterino:masterfrom
Nerixyz:feat/strong-view-types
Aug 23, 2026
Merged

pajlada merged 6 commits into
Chatterino:masterfrom
Nerixyz:feat/strong-view-types

Conversation

@Nerixyz

@Nerixyz Nerixyz commented Aug 22, 2026

Copy link
Copy Markdown
Contributor

We have some strong aliases/typedefs like UserId, UserName, EmoteName, etc. Currently, they wrap a QString. In some cases, we don't want to use owned types. Notably, when looking up items in a map. There, it would be fine to look them up by QStringView. However, QStringView isn't "tagged" to contain a user ID, username, etc.

This PR adds -View types as "tagged" versions of QStringView. So now EmoteName has a corresponding EmoteNameView. The view can be created from the owned type, and they can be compared to each other.

The containers don't know about this relationship - for example, std::unordered_map<EmoteName, V> doesn't know that you can do lookup by EmoteNameView. To tell them, we need to provide both a transparent hasher (i.e. a hasher working for both types) and a transparent comparator. For the hasher, there's EmoteNameHash which forwards to the QStringView hash. For the comparison, std::equal_to<> can be used. See the tests for examples.

I didn't modify any uses of the types yet.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-tidy made some suggestions

Comment thread src/common/Aliases.hpp Outdated
Comment thread src/common/Aliases.hpp
Comment thread src/common/Aliases.hpp
Comment thread tests/src/Aliases.cpp
@pajlada
pajlada enabled auto-merge (squash) August 23, 2026 11:33
@pajlada
pajlada merged commit e07c250 into Chatterino:master Aug 23, 2026
18 checks passed
invisibl3cat pushed a commit to invisibl3cat/chatterino2 that referenced this pull request Aug 23, 2026
We have some strong aliases/typedefs like `UserId`, `UserName`,
`EmoteName`, etc. Currently, they wrap a `QString`. In some cases, we
don't want to use owned types. Notably, when looking up items in a map.
There, it would be fine to look them up by `QStringView`. However,
`QStringView` isn't "tagged" to contain a user ID, username, etc.

This PR adds `-View` types as "tagged" versions of `QStringView`. So now
`EmoteName` has a corresponding `EmoteNameView`. The view can be created
from the owned type, and they can be compared to each other.

The containers don't know about this relationship - for example,
`std::unordered_map<EmoteName, V>` doesn't know that you can do lookup
by `EmoteNameView`. To tell them, we need to provide both a transparent
hasher (i.e. a hasher working for both types) and a transparent
comparator. For the hasher, there's `EmoteNameHash` which forwards to
the `QStringView` hash. For the comparison,
[`std::equal_to<>`](https://en.cppreference.com/cpp/utility/functional/equal_to_void)
can be used. See the tests for examples.

Reviewed-by: pajlada <rasmus.karlsson@pajlada.com>
pajlada pushed a commit that referenced this pull request Aug 28, 2026
#7194 added view types for our aliases and this PR uses them for emote
lookup. Most of this only changes the parameter types. Notably, it makes
`MessageBuilder::tryAppendEmote` and the input highlighter's `isEmote`
accept a view.

It's not much yet, since other parts still expect owned strings.

Reviewed-by: pajlada <rasmus.karlsson@pajlada.com>
babicekmarek pushed a commit to babicekmarek/streamally-chatterino that referenced this pull request Sep 14, 2026
We have some strong aliases/typedefs like `UserId`, `UserName`,
`EmoteName`, etc. Currently, they wrap a `QString`. In some cases, we
don't want to use owned types. Notably, when looking up items in a map.
There, it would be fine to look them up by `QStringView`. However,
`QStringView` isn't "tagged" to contain a user ID, username, etc.

This PR adds `-View` types as "tagged" versions of `QStringView`. So now
`EmoteName` has a corresponding `EmoteNameView`. The view can be created
from the owned type, and they can be compared to each other.

The containers don't know about this relationship - for example,
`std::unordered_map<EmoteName, V>` doesn't know that you can do lookup
by `EmoteNameView`. To tell them, we need to provide both a transparent
hasher (i.e. a hasher working for both types) and a transparent
comparator. For the hasher, there's `EmoteNameHash` which forwards to
the `QStringView` hash. For the comparison,
[`std::equal_to<>`](https://en.cppreference.com/cpp/utility/functional/equal_to_void)
can be used. See the tests for examples.

Reviewed-by: pajlada <rasmus.karlsson@pajlada.com>
babicekmarek pushed a commit to babicekmarek/streamally-chatterino that referenced this pull request Sep 14, 2026
Chatterino#7194 added view types for our aliases and this PR uses them for emote
lookup. Most of this only changes the parameter types. Notably, it makes
`MessageBuilder::tryAppendEmote` and the input highlighter's `isEmote`
accept a view.

It's not much yet, since other parts still expect owned strings.

Reviewed-by: pajlada <rasmus.karlsson@pajlada.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants