TensorFlow Serving C++ API Documentation
servable_data.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_DATA_H_
17 #define TENSORFLOW_SERVING_CORE_SERVABLE_DATA_H_
18 
19 #include <algorithm>
20 #include <type_traits>
21 
22 #include "tensorflow/core/lib/core/status.h"
23 #include "tensorflow/core/platform/logging.h"
24 #include "tensorflow_serving/core/servable_id.h"
25 
26 namespace tensorflow {
27 namespace serving {
28 
29 // A servable id, and associated with it either an error, or data of type T.
30 // REQUIRES: T is move-constructible. T cannot be Status.
31 template <typename T>
32 class ServableData {
33  static_assert(std::is_move_constructible<T>::value,
34  "ServableData<T> requires that T is move-constructible.");
35  static_assert(
36  !std::is_same<T, Status>::value,
37  "ServableData<T> requires that T not be tensorflow::serving::Status.");
38 
39  public:
40  // Creates a ServableData<T> with an okay status.
41  ServableData(const ServableId& id, T data);
42 
43  // Creates a ServableData<T> with an error status. Uses a default-constructed
44  // data value. CHECK-fails if 'error.ok()'.
45  ServableData(const ServableId& id, const Status& error);
46 
47  // Copy constructor.
48  ServableData(const ServableData& other) = default;
49 
50  // Assignment operator.
51  ServableData& operator=(const ServableData& other) = default;
52 
53  // Move constructor.
54  ServableData(ServableData&& other) = default;
55 
56  const ServableId& id() const { return id_; }
57 
58  const Status& status() const { return status_; }
59 
60  // Returns a reference to the data, or CHECK-fails if !this->ok().
61  const T& DataOrDie() const;
62  T& DataOrDie();
63 
64  // Moves the data out of this object and returns it, or CHECK-fails if
65  // !this->ok().
66  T ConsumeDataOrDie();
67 
68  private:
69  ServableData() = delete;
70 
71  const ServableId id_;
72  const Status status_;
73  T data_;
74 };
75 
76 // Helper static method to create a ServableData object. Caller may skip
77 // specifying the template argument because of ADL (argument dependent lookup)
78 // unlike the ctor.
79 template <typename T>
80 ServableData<T> CreateServableData(const ServableId& id, T data);
81 
83 // Implementation details follow. API users need not read.
84 
85 template <typename T>
87  : id_(id), status_(Status()), data_(std::move(data)) {}
88 
89 template <typename T>
90 ServableData<T>::ServableData(const ServableId& id, const Status& error)
91  : id_(id), status_(error) {
92  CHECK(!error.ok());
93 }
94 
95 template <typename T>
96 const T& ServableData<T>::DataOrDie() const {
97  CHECK(status_.ok());
98  return data_;
99 }
100 
101 template <typename T>
102 T& ServableData<T>::DataOrDie() {
103  CHECK(status_.ok());
104  return data_;
105 }
106 
107 template <typename T>
108 T ServableData<T>::ConsumeDataOrDie() {
109  return std::move(DataOrDie());
110 }
111 
112 template <typename T>
113 ServableData<T> CreateServableData(const ServableId& id, T data) {
114  return ServableData<T>(id, std::move(data));
115 }
116 
117 } // namespace serving
118 } // namespace tensorflow
119 
120 #endif // TENSORFLOW_SERVING_CORE_SERVABLE_DATA_H_