TensorFlow Serving C++ API Documentation
unique_ptr_with_deps.h
1 /* Copyright 2016 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 
16 // A Unique Ptr like class that manages the lifecycle of any dependencies in
17 // addition to the primary owned object.
18 //
19 // This enables the use-case of returning ownership of an object to some client
20 // code without giving the client access to the dependencies, and automatically
21 // handling destruction upon destruction of the primary owned object.
22 #ifndef TENSORFLOW_SERVING_UTIL_UNIQUE_PTR_WITH_DEPS_H_
23 #define TENSORFLOW_SERVING_UTIL_UNIQUE_PTR_WITH_DEPS_H_
24 
25 #include <algorithm>
26 #include <memory>
27 #include <vector>
28 
29 #include "tensorflow/core/platform/macros.h"
30 #include "tensorflow/core/platform/types.h"
31 #include "tensorflow_serving/util/any_ptr.h"
32 
33 namespace tensorflow {
34 namespace serving {
35 
36 // Holds an object with its dependencies. On destruction deletes the main
37 // object and all dependencies, in the order inverse to the order of
38 // AddDependency/SetOwned calls.
39 template<typename T>
41  public:
43  explicit UniquePtrWithDeps(std::unique_ptr<T> object) {
44  SetOwned(std::move(object));
45  }
46  explicit UniquePtrWithDeps(T* owned_object) { SetOwnedPtr(owned_object); }
47  UniquePtrWithDeps(UniquePtrWithDeps&& other) = default;
48 
50  // Delete all dependencies, starting with the one added last. Order of
51  // destructing elements in vector/list is unspecified. The ownership of the
52  // main object is kept as one of the dependencies.
53  while (!deleters_.empty()) {
54  deleters_.pop_back();
55  }
56  }
57 
58  template <typename X>
59  X* AddDependency(std::unique_ptr<X> dependency) {
60  X* raw = dependency.get();
61  deleters_.emplace_back(std::move(dependency));
62  return raw;
63  }
64 
65  void SetOwned(std::unique_ptr<T> object) {
66  object_ = AddDependency<T>(std::move(object));
67  }
68  void SetOwnedPtr(T* owned_object) {
69  SetOwned(std::unique_ptr<T>(owned_object));
70  }
71 
72  T* get() const { return object_; }
73  const T& operator*() const { return *object_; }
74  T* operator->() const { return get(); }
75 
76  private:
77  std::vector<UniqueAnyPtr> deleters_;
78  T* object_ = nullptr;
79 };
80 
81 } // namespace serving
82 } // namespace tensorflow
83 
84 #endif // TENSORFLOW_SERVING_UTIL_UNIQUE_PTR_WITH_DEPS_H_