resource_container.h 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // Copyright 2017 Google Inc. 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. #ifndef NLP_SAFT_OPENSOURCE_DRAGNN_CORE_RESOURCE_CONTAINER_H_
  16. #define NLP_SAFT_OPENSOURCE_DRAGNN_CORE_RESOURCE_CONTAINER_H_
  17. #include <memory>
  18. #include "syntaxnet/base.h"
  19. #include "tensorflow/core/framework/resource_mgr.h"
  20. namespace syntaxnet {
  21. namespace dragnn {
  22. using tensorflow::strings::StrCat;
  23. // Wrapper to store a data type T in the ResourceMgr. There should be one per
  24. // Session->Run() call that may happen concurrently.
  25. template <class T>
  26. class ResourceContainer : public tensorflow::ResourceBase {
  27. public:
  28. explicit ResourceContainer(std::unique_ptr<T> data)
  29. : data_(std::move(data)) {}
  30. ~ResourceContainer() override {}
  31. T *get() { return data_.get(); }
  32. std::unique_ptr<T> release() { return std::move(data_); }
  33. string DebugString() override { return "ResourceContainer"; }
  34. private:
  35. std::unique_ptr<T> data_;
  36. };
  37. } // namespace dragnn
  38. } // namespace syntaxnet
  39. #endif // NLP_SAFT_OPENSOURCE_DRAGNN_CORE_RESOURCE_CONTAINER_H_