1 /* Copyright 2021 The TensorFlow Authors. All Rights Reserved. 2 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 TENSORFLOW_CORE_COMMON_RUNTIME_REQUEST_COST_ACCESSOR_REGISTRY_H_ 17 #define TENSORFLOW_CORE_COMMON_RUNTIME_REQUEST_COST_ACCESSOR_REGISTRY_H_ 18 19 #include <functional> 20 #include <memory> 21 #include <string> 22 23 #include "absl/memory/memory.h" 24 #include "absl/strings/string_view.h" 25 #include "tensorflow/core/common_runtime/request_cost_accessor.h" 26 27 namespace tensorflow { 28 29 // TODO(b/185852990): Create a template Registry that allows registering 30 // different types (e.g RequestCostAccessor, CostMeasurement). 31 // 32 // RequestCostAccessorRegistry allows to 33 // - register a RequestCostAccessor type to the global map 34 // - create an instance of registered RequestCostAccessor. 35 class RequestCostAccessorRegistry { 36 public: 37 // Creates an instance of registered RequestCostAccessor by name. If the named 38 // RequestCostAccessor is not registered yet, returns nullptr. 39 static std::unique_ptr<RequestCostAccessor> CreateByNameOrNull( 40 absl::string_view name); 41 42 using Creator = std::function<std::unique_ptr<RequestCostAccessor>()>; 43 44 // Registers a RequestCostAccessor type to the global map. Registering 45 // different types of RequestCostAccessor with the same name is prohibited. 46 static void RegisterRequestCostAccessor(absl::string_view name, 47 Creator creator); 48 }; 49 50 // Registers a RequestCostAccessor type to the global map. Registering different 51 // types of RequestCostAccessor with the same name is prohibited. 52 class RequestCostAccessorRegistrar { 53 public: RequestCostAccessorRegistrar(absl::string_view name,RequestCostAccessorRegistry::Creator creator)54 explicit RequestCostAccessorRegistrar( 55 absl::string_view name, RequestCostAccessorRegistry::Creator creator) { 56 RequestCostAccessorRegistry::RegisterRequestCostAccessor( 57 name, std::move(creator)); 58 } 59 }; 60 61 #define REGISTER_REQUEST_COST_ACCESSOR(name, MyRequestCostAccessorClass) \ 62 namespace { \ 63 static ::tensorflow::RequestCostAccessorRegistrar \ 64 MyRequestCostAccessorClass##_registrar((name), [] { \ 65 return std::make_unique<MyRequestCostAccessorClass>(); \ 66 }); \ 67 } // namespace 68 69 } // namespace tensorflow 70 71 #endif // TENSORFLOW_CORE_COMMON_RUNTIME_REQUEST_COST_ACCESSOR_REGISTRY_H_ 72