1/* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations under the License.
14==============================================================================*/
15
16#include "tensorflow/c/tf_status.h"
17
18#include "tensorflow/c/tf_status_internal.h"
19#include "tensorflow/core/platform/errors.h"
20#include "tensorflow/core/platform/status.h"
21
22using ::tensorflow::Status;
23using ::tensorflow::error::Code;
24using ::tensorflow::errors::IOError;
25
26TF_Status* TF_NewStatus() { return new TF_Status; }
27
28void TF_DeleteStatus(TF_Status* s) { delete s; }
29
30void TF_SetStatus(TF_Status* s, TF_Code code, const char* msg) {
31 if (code == TF_OK) {
32 s->status = ::tensorflow::OkStatus();
33 return;
34 }
35 s->status = Status(static_cast<Code>(code), tensorflow::StringPiece(msg));
36}
37
38void TF_SetPayload(TF_Status* s, const char* key, const char* value) {
39 s->status.SetPayload(key, value);
40}
41
42void TF_SetStatusFromIOError(TF_Status* s, int error_code,
43 const char* context) {
44 // TODO(b/139060984): Handle windows when changing its filesystem
45 s->status = IOError(context, error_code);
46}
47
48TF_Code TF_GetCode(const TF_Status* s) {
49 return static_cast<TF_Code>(s->status.code());
50}
51
52const char* TF_Message(const TF_Status* s) {
53 return s->status.error_message().c_str();
54}
55