aboutsummaryrefslogtreecommitdiff
path: root/include/sibs/PubsubKey.hpp
blob: f1239ca180574c57bba7509df3d4d9f536d07cf5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#pragma once

#include "../FnvHash.hpp"
#include "../types.hpp"
#include <array>
#include <unordered_map>
#include <string>

namespace sibs
{
    const usize PUBSUB_KEY_LENGTH = 20;
    
    class PubsubKey
    {
    public:
        PubsubKey();
        // Create pubsub key from existing data, data will be cutoff at PUBSUB_KEY_LENGTH. If @size is less than PUBSUB_KEY_LENGTH then data is appended with 0
        PubsubKey(const void *data, const usize size);
        bool operator == (const PubsubKey &other) const;
        bool operator != (const PubsubKey &other) const;

        std::string toString() const;

        std::array<u8, PUBSUB_KEY_LENGTH> data;
    };
    
    struct PubsubKeyHasher
    {
        size_t operator()(const PubsubKey &pubsubKey) const
        {
            return fnvHash((const unsigned char*)pubsubKey.data.data(), PUBSUB_KEY_LENGTH);
        }
    };
    
    template <typename ValueType>
    using PubsubKeyMap = std::unordered_map<PubsubKey, ValueType, PubsubKeyHasher>;
}