TensorFlow Serving C++ API Documentation
servable_handle.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 #ifndef TENSORFLOW_SERVING_CORE_SERVABLE_HANDLE_H_
17 #define TENSORFLOW_SERVING_CORE_SERVABLE_HANDLE_H_
18 
19 #include <algorithm>
20 #include <cstddef>
21 #include <memory>
22 #include <type_traits>
23 
24 #include "tensorflow_serving/core/loader.h"
25 #include "tensorflow_serving/util/any_ptr.h"
26 
27 namespace tensorflow {
28 namespace serving {
29 
36  public:
37  virtual ~UntypedServableHandle() = default;
38 
39  virtual const ServableId& id() const = 0;
40 
41  virtual AnyPtr servable() = 0;
42 };
43 
74 template <typename T>
76  public:
77  static_assert(!std::is_pointer<T>::value,
78  "Servables are implicitly passed as pointers, please use T "
79  "instead of T*.");
80 
82  ServableHandle() = default;
83 
84  const ServableId& id() const { return untyped_handle_->id(); }
85 
86  // Smart pointer operations.
87 
88  T& operator*() const { return *get(); }
89 
90  T* operator->() const { return get(); }
91 
92  T* get() const { return servable_; }
93 
94  operator bool() const { return get() != nullptr; }
95 
96  // See the end of this file for comparison operators, which must be declared
97  // at namespace scope to support left-hand-side arguments of different types.
98 
99  private:
100  friend class Manager;
101 
102  explicit ServableHandle(std::unique_ptr<UntypedServableHandle> untyped_handle)
103  : untyped_handle_(std::move(untyped_handle)),
104  servable_(untyped_handle_ == nullptr
105  ? nullptr
106  : untyped_handle_->servable().get<T>()) {}
107 
108  std::unique_ptr<UntypedServableHandle> untyped_handle_;
109  T* servable_ = nullptr;
110 };
111 
115  public:
116  ~SharedPtrHandle() override = default;
117 
118  explicit SharedPtrHandle(const ServableId& id, std::shared_ptr<Loader> loader)
119  : id_(id), loader_(std::move(loader)) {}
120 
121  AnyPtr servable() override { return loader_->servable(); }
122 
123  const ServableId& id() const override { return id_; }
124 
125  private:
126  const ServableId id_;
127  std::shared_ptr<Loader> loader_;
128 };
129 
130 // We compare handles using both the servable pointer and the id. So if you
131 // share the same pointer amongst different versions, the handles won't be
132 // equal.
133 template <typename T, typename U>
134 constexpr bool operator==(const ServableHandle<T>& l,
135  const ServableHandle<U>& r) {
136  return l.get() == r.get() && l.id() == r.id();
137 }
138 
139 template <typename T, typename U>
140 constexpr bool operator!=(const ServableHandle<T>& l,
141  const ServableHandle<U>& r) {
142  return !(l == r);
143 }
144 
145 } // namespace serving
146 } // namespace tensorflow
147 
148 #endif // TENSORFLOW_SERVING_CORE_SERVABLE_HANDLE_H_
ServableHandle()=default
ServableHandle is null by default.