TensorFlow Serving C++ API Documentation
proto_util.h
1 /* Copyright 2022 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 THIRD_PARTY_TENSORFLOW_SERVING_UTIL_PROTO_UTIL_H_
16 #define THIRD_PARTY_TENSORFLOW_SERVING_UTIL_PROTO_UTIL_H_
17 
18 #include "tensorflow/core/lib/core/errors.h"
19 #include "tensorflow/core/lib/core/status.h"
20 #include "tensorflow/core/platform/env.h"
21 #include "tensorflow/core/platform/protobuf.h"
22 
23 namespace tensorflow {
24 namespace serving {
25 
26 template <typename ProtoType>
27 tensorflow::Status ParseProtoTextFile(const string& file, ProtoType* proto) {
28  std::unique_ptr<tensorflow::ReadOnlyMemoryRegion> file_data;
29  TF_RETURN_IF_ERROR(
30  tensorflow::Env::Default()->NewReadOnlyMemoryRegionFromFile(file,
31  &file_data));
32  string file_data_str(static_cast<const char*>(file_data->data()),
33  file_data->length());
34  if (tensorflow::protobuf::TextFormat::ParseFromString(file_data_str, proto)) {
35  return tensorflow::OkStatus();
36  } else {
37  return tensorflow::errors::InvalidArgument("Invalid protobuf file: '", file,
38  "'");
39  }
40 }
41 
42 } // namespace serving
43 } // namespace tensorflow
44 
45 #endif // THIRD_PARTY_TENSORFLOW_SERVING_UTIL_PROTO_UTIL_H_