1 /* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef HETERNEURAL_NETWORK_OPS_REGISTRY_H 17 #define HETERNEURAL_NETWORK_OPS_REGISTRY_H 18 19 #include <functional> 20 #include <memory> 21 #include <unordered_map> 22 23 #include "ops_builder.h" 24 #include "interfaces/kits/c/neural_network_runtime/neural_network_runtime.h" 25 26 namespace OHOS { 27 namespace NeuralNetworkRuntime { 28 namespace Ops { 29 class OpsRegistry { 30 public: 31 struct Registrar { 32 Registrar() = delete; 33 Registrar(OH_NN_OperationType opsType, std::function<std::unique_ptr<OpsBuilder>()> createFunc); 34 }; 35 36 public: 37 static OpsRegistry& GetSingleton(); 38 std::unique_ptr<OpsBuilder> GetOpsBuilder(OH_NN_OperationType type) const; 39 40 private: OpsRegistry()41 OpsRegistry() {}; 42 OpsRegistry(const OpsRegistry&) = delete; 43 OpsRegistry& operator=(const OpsRegistry&) = delete; 44 45 private: 46 std::unordered_map<OH_NN_OperationType, std::function<std::unique_ptr<OpsBuilder>()>> m_opsRegedit; 47 }; 48 49 #define CREATE_FUNC(T) ([]()->std::unique_ptr<OpsBuilder> {return std::make_unique<T>();}) 50 #define REGISTER_OPS(T, opsType) static OpsRegistry::Registrar g_##T(opsType, CREATE_FUNC(T)) 51 } // namespace Ops 52 } // namespace NeuralNetworkRuntime 53 } // namespcae OHOS 54 #endif // HETERNEURAL_NETWORK_OPS_REGISTRY_H