1 /* Copyright 2020 The TensorFlow Authors. 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_STREAM_EXECUTOR_TPU_STATUS_HELPER_H_
17 #define TENSORFLOW_STREAM_EXECUTOR_TPU_STATUS_HELPER_H_
18 
19 #include "tensorflow/core/platform/status.h"
20 #include "tensorflow/core/tpu/tpu_api.h"
21 #include "tensorflow/stream_executor/tpu/tpu_executor_c_api.h"
22 
23 class StatusHelper {
24  public:
StatusHelper()25   StatusHelper()
26       : c_status(tensorflow::tpu::ExecutorApiFn()->TpuStatus_NewFn()) {}
27 
~StatusHelper()28   ~StatusHelper() {
29     tensorflow::tpu::ExecutorApiFn()->TpuStatus_FreeFn(c_status);
30   }
31 
FromC(TF_Status * const c_status)32   static tensorflow::Status FromC(TF_Status* const c_status) {
33     if (tensorflow::tpu::ExecutorApiFn()->TpuStatus_OkFn(c_status)) {
34       return tensorflow::Status::OK();
35     } else {
36       return tensorflow::Status(
37           tensorflow::error::Code(
38               tensorflow::tpu::ExecutorApiFn()->TpuStatus_CodeFn(c_status)),
39           tensorflow::tpu::ExecutorApiFn()->TpuStatus_MessageFn(c_status));
40     }
41   }
42 
ok()43   bool ok() const {
44     return tensorflow::tpu::ExecutorApiFn()->TpuStatus_OkFn(c_status);
45   }
46 
status()47   tensorflow::Status status() const { return FromC(c_status); }
48 
49   TF_Status* const c_status;  // NOLINT
50 };
51 
52 #endif  // TENSORFLOW_STREAM_EXECUTOR_TPU_STATUS_HELPER_H_
53