aboutsummaryrefslogtreecommitdiff
path: root/reference_model/include/custom_registry.h
blob: f1a9b8c5b9b549af72ad319e857bc857dfb29bbb (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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Copyright (c) 2023, ARM Limited.
//
//    Licensed under the Apache License, Version 2.0 (the "License");
//    you may not use this file except in compliance with the License.
//    You may obtain a copy of the License at
//
//         http://www.apache.org/licenses/LICENSE-2.0
//
//    Unless required by applicable law or agreed to in writing, software
//    distributed under the License is distributed on an "AS IS" BASIS,
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//    See the License for the specific language governing permissions and
//    limitations under the License.

#ifndef CUSTOMREGISTRY_H
#define CUSTOMREGISTRY_H

#include "custom_op_interface.h"
#include <dlfcn.h>
#include <unordered_map>

using namespace tosa;

namespace TosaReference
{

typedef CustomOpInterface* (*op_creation_function_t)();
typedef int (*registration_callback_t)(const std::string& domain_name,
                                       const std::string& operator_name,
                                       const op_creation_function_t& op_creation_function);

class MasterRegistry
{
public:
    static int register_function(const std::string& domain_name,
                                 const std::string& operator_name,
                                 const op_creation_function_t& op_creation_function)
    {
        std::string unique_id    = domain_name + "::" + operator_name;
        MasterRegistry& instance = get_instance();
        if (instance.op_creation_map.find(unique_id) != instance.op_creation_map.end())
        {
            std::cout << std::endl;
            printf("domain_name: %s and operator_name: %s pair has already been registered", domain_name.c_str(),
                   operator_name.c_str());
            return 1;
        }
        instance.op_creation_map[unique_id] = op_creation_function;
        return 0;
    }

    static MasterRegistry& get_instance()
    {
        static MasterRegistry instance;
        return instance;
    }

    MasterRegistry(const MasterRegistry&) = delete;
    void operator=(const MasterRegistry&) = delete;

    std::unordered_map<std::string, op_creation_function_t> get_ops() const
    {
        return op_creation_map;
    }

    static op_creation_function_t get_op(const std::string& domain_name, const std::string& operator_name)
    {
        std::string unique_id    = domain_name + "::" + operator_name;
        MasterRegistry& instance = get_instance();
        auto all_ops_map         = instance.get_ops();
        if (all_ops_map.find(unique_id) == all_ops_map.end())
        {
            return nullptr;
        }
        else
        {
            op_creation_function_t& op_creation_function = all_ops_map[unique_id];
            return op_creation_function;
        }
    }

private:
    MasterRegistry() = default;
    std::unordered_map<std::string, op_creation_function_t> op_creation_map;
};
}    // namespace TosaReference

#endif