1 /* Copyright 2018 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_UTIL_PROTO_DESCRIPTOR_POOL_REGISTRY_H_ 17 #define TENSORFLOW_CORE_UTIL_PROTO_DESCRIPTOR_POOL_REGISTRY_H_ 18 19 #include <functional> 20 #include <memory> 21 #include <string> 22 #include <unordered_map> 23 #include <utility> 24 25 #include "tensorflow/core/lib/core/status.h" 26 #include "tensorflow/core/lib/hash/hash.h" 27 #include "tensorflow/core/platform/protobuf.h" 28 29 namespace tensorflow { 30 31 class DescriptorPoolRegistry { 32 public: 33 typedef std::function<Status( 34 tensorflow::protobuf::DescriptorPool const** desc_pool, 35 std::unique_ptr<tensorflow::protobuf::DescriptorPool>* owned_desc_pool)> 36 DescriptorPoolFn; 37 38 // Returns a pointer to a global DescriptorPoolRegistry object. 39 static DescriptorPoolRegistry* Global(); 40 41 // Returns a pointer to a descriptor pool function for the given source. 42 DescriptorPoolFn* Get(const string& source); 43 44 // Registers a descriptor pool factory. 45 void Register(const string& source, const DescriptorPoolFn& pool_fn); 46 47 private: 48 std::map<string, DescriptorPoolFn> fns_; 49 }; 50 51 namespace descriptor_pool_registration { 52 53 class DescriptorPoolRegistration { 54 public: DescriptorPoolRegistration(const string & source,const DescriptorPoolRegistry::DescriptorPoolFn & pool_fn)55 DescriptorPoolRegistration( 56 const string& source, 57 const DescriptorPoolRegistry::DescriptorPoolFn& pool_fn) { 58 DescriptorPoolRegistry::Global()->Register(source, pool_fn); 59 } 60 }; 61 62 } // namespace descriptor_pool_registration 63 64 #define REGISTER_DESCRIPTOR_POOL(source, pool_fn) \ 65 REGISTER_DESCRIPTOR_POOL_UNIQ_HELPER(__COUNTER__, source, pool_fn) 66 67 #define REGISTER_DESCRIPTOR_POOL_UNIQ_HELPER(ctr, source, pool_fn) \ 68 REGISTER_DESCRIPTOR_POOL_UNIQ(ctr, source, pool_fn) 69 70 #define REGISTER_DESCRIPTOR_POOL_UNIQ(ctr, source, pool_fn) \ 71 static descriptor_pool_registration::DescriptorPoolRegistration \ 72 descriptor_pool_registration_fn_##ctr(source, pool_fn) 73 74 } // namespace tensorflow 75 76 #endif // TENSORFLOW_CORE_UTIL_PROTO_DESCRIPTOR_POOL_REGISTRY_H_ 77