1
0
mirror of https://github.com/wqking/eventpp.git synced 2024-12-27 00:17:02 +08:00
eventpp/doc/anyid.md

158 lines
6.1 KiB
Markdown
Raw Normal View History

2021-01-31 16:54:48 +08:00
# Class AnyId reference
2022-05-31 17:27:15 +08:00
<!--begintoc-->
## Table Of Contents
* [Description](#a2_1)
* [API reference](#a2_2)
* [Header](#a3_1)
* [Class AnyId template parameters](#a3_2)
* [Public types](#a3_3)
* [Member functions](#a3_4)
* [Global type AnyHashableId](#a2_3)
* [Comparison AnyId](#a2_4)
* [When to use AnyId?](#a2_5)
<!--endtoc-->
<a id="a2_1"></a>
2021-01-29 21:32:57 +08:00
## Description
2021-01-31 16:54:48 +08:00
The template class `AnyId` can be used as the event ID type in `EventDispatcher` and `EventQueue`, then any types can be used as the event type.
2021-01-29 21:32:57 +08:00
For example,
```c++
2021-01-31 16:54:48 +08:00
eventpp::EventQueue<eventpp::AnyId<>, void()> eventQueue;
2021-01-29 21:32:57 +08:00
eventQueue.appendListener(3, []() {}); // listener 1
eventQueue.appendListener(std::string("hello"), []() {}); // listener 2
eventQueue.dispatch(3); // trigger listener 1
eventQueue.dispatch(std::string("hello")); // trigger listener 2
```
2021-02-01 09:24:15 +08:00
Note the `eventpp::AnyId<>` in the example code, it's an instantiation of `AnyId` with default template parameters. It's in the place of where an event type should be, such as int.
Without `AnyId`, a typical EventQueue looks like,
```c++
eventpp::EventQueue<int, void()> eventQueue;
eventQueue.appendListener(3, []() {});
// This doesn't compile because std::string can't be converted to int
// eventQueue.appendListener(std::string("hello"), []() {});
```
For an `int` event type, we can't use `std::string` as the event ID.
With `AnyId` in previous example code, we can pass any types as the event ID.
2022-05-31 17:27:15 +08:00
<a id="a2_2"></a>
2021-01-31 16:54:48 +08:00
## API reference
2021-01-29 21:32:57 +08:00
2022-05-31 17:27:15 +08:00
<a id="a3_1"></a>
2021-01-31 16:54:48 +08:00
### Header
2021-01-29 21:32:57 +08:00
2021-01-31 16:54:48 +08:00
eventpp/utilities/anyid.h
2021-01-29 21:32:57 +08:00
2022-05-31 17:27:15 +08:00
<a id="a3_2"></a>
2021-01-31 16:54:48 +08:00
### Class AnyId template parameters
2021-01-29 21:32:57 +08:00
2021-01-31 16:54:48 +08:00
```c++
template <template <typename> class Digester = std::hash, typename Storage = EmptyStorage>
class AnyId;
```
2022-05-31 17:27:15 +08:00
`Digester`: a template class that has one template parameter. It has a function call operator that receives one value and returns the digest of the value. The returned digest must be hashable, i.e, it must be able to be passed to `std::hash`. One of such `Digester` is `std::hash`. The parameter default value is `std::hash`. An event ID that's converted to `AnyId` must be able to pass to `Digester` function call operator. For example, if `Digester` is `std::hash`, the event ID must be hashable, aka, it must be able to be passed to `std::hash`, so `int` and `std::string` works, but `const char *` not.
2021-01-31 16:54:48 +08:00
`Storage`: a class that can be constructed with any types of values which are going to be used in `AnyId`. One of such `Storage` is `std::any` (in C++17). The parameter default value is an empty storage class that can be constructed with any types and it doesn't hold the value.
2021-01-29 21:32:57 +08:00
2021-01-31 16:54:48 +08:00
`Digester` is used to convert any types to a specified type and `AnyId` stores the digest instead of the value itself.
2022-05-31 17:27:15 +08:00
`Storage` is used to store the actual value.
2021-01-29 21:32:57 +08:00
2021-01-31 16:54:48 +08:00
A typical implementation of `Digester`:
```c++
template <typename T>
struct MyDigest
{
TheDigestTypeSuchAsSizeT operator() (const T & value) const {
// compute the digest of value and return the digest.
}
2021-01-31 16:54:48 +08:00
};
```
Note: the return type of the function call operator (here is TheDigestTypeSuchAsSizeT) must be the same for all T, it can't be different type for different T.
2021-01-29 21:32:57 +08:00
2021-01-31 16:54:48 +08:00
A typical implementation of `Storage`:
2021-01-29 21:32:57 +08:00
```c++
2021-01-31 16:54:48 +08:00
struct MyStorage
{
template <typename T>
MyStorage(const T & value) {
// store the value
}
// any other member functions can be added, such as getting the underlying value.
2021-01-31 16:54:48 +08:00
};
```
Or none template version:
```c++
// In this version, only value of `int` and `std::string` can be stored.
struct MyStorage
{
MyStorage(const int value) {}
MyStorage(const std::string & value) {}
// any other member functions can be added, such as getting the underlying value.
2021-01-31 16:54:48 +08:00
};
2021-01-29 21:32:57 +08:00
```
2022-05-31 17:27:15 +08:00
<a id="a3_3"></a>
2021-01-31 16:54:48 +08:00
### Public types
`DigestType`: the digest type that returned by `Digester`. If `Digester` is `std::hash`, `DigestType` is `std::size_t`.
2021-01-29 21:32:57 +08:00
2022-05-31 17:27:15 +08:00
<a id="a3_4"></a>
2021-01-31 16:54:48 +08:00
### Member functions
2021-01-29 21:32:57 +08:00
2021-01-31 16:54:48 +08:00
#### constructors
2021-01-29 21:32:57 +08:00
```c++
2021-01-31 16:54:48 +08:00
AnyId();
template <typename T>
AnyId(const T & value);
2021-01-29 21:32:57 +08:00
```
2021-01-31 16:54:48 +08:00
Any value can be converted to `AnyId` implicitly.
2021-01-29 21:32:57 +08:00
2021-01-31 16:54:48 +08:00
#### getDigest
```c++
DigestType getDigest() const;
```
Return the digest for the value that passed in the constructor.
2021-01-29 21:32:57 +08:00
```c++
2021-01-31 16:54:48 +08:00
const Storage & getValue() const;
```
Return the value that's stored in `Storage`. The default `Storage` is an empty structure, so you can't get the real value from it.
2021-02-01 09:24:15 +08:00
If `std::any` is used as the `Storage` parameter when instantiating the `AnyId` template, `getValue` returns the `std::any` thus the value can be obtained from the `std::any`.
2021-01-31 16:54:48 +08:00
2022-05-31 17:27:15 +08:00
<a id="a2_3"></a>
2021-01-31 16:54:48 +08:00
## Global type AnyHashableId
2021-01-29 21:32:57 +08:00
2021-01-31 16:54:48 +08:00
```c++
using AnyHashableId = AnyId<>;
2021-01-29 21:32:57 +08:00
```
2021-02-01 09:24:15 +08:00
`AnyHashableId` is an instantiation of `AnyId` with the default parameters. It can be used in place of the event ID in `EventDispatcher` or `EventQueue`.
2021-01-31 16:54:48 +08:00
In the example code in the beginning of this document, the `eventpp::AnyId<>` can be replaced with `eventpp::AnyHashableId`.
2022-05-31 17:27:15 +08:00
<a id="a2_4"></a>
2021-01-31 16:54:48 +08:00
## Comparison AnyId
`AnyId` supports `operator ==` for being used in `std::unordered_map`, and `operator <` for being used in `std::map` (which map is used depending on the policies), in `EventDispatcher` and `EventQueue`.
`AnyId` compares the digest first (the digest must be comparable).
2021-02-01 09:24:15 +08:00
If the `Storage` supports the operators, the values in the storage are compared. In this case, it doesn't matter if digest collides.
If the `Storage` doesn't support the operators, only the digests are compared. In this case, if digest collides, the result is in collision.
2021-01-31 16:54:48 +08:00
2022-05-31 17:27:15 +08:00
<a id="a2_5"></a>
2021-01-31 16:54:48 +08:00
## When to use AnyId?
2021-01-29 21:32:57 +08:00
2021-01-31 16:54:48 +08:00
Even though `AnyId` looks smart and very flexible, I highly don't encourage you to use it at all because that means the architecture has flaws. You should always prefer to single event type, such as `int`, or `std::string`, than mixing them.
2021-02-10 21:54:23 +08:00
If you want to use `AnyHashableId` (aka, `AnyId<>`), don't forget to take into account of the collision created by `std::hash`, and be sure your event IDs don't collide with each other. Instead of using `std::hash`, you may implement more safer digester, such as SHA256.
2021-01-31 16:54:48 +08:00
If you find there are good reasons to mix the event types and there are good cases to use `AnyId`, you can let me know.