Apache GraphAr C++ Library
The C++ Library for Apache GraphAr
result.h
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 
20 #pragma once
21 
22 #include <utility>
23 
24 #include "graphar/fwd.h"
25 #include "graphar/status.h"
26 
27 #define GAR_ASSIGN_OR_RAISE_IMPL(result_name, lhs, rexpr) \
28  auto&& result_name = (rexpr); \
29  GAR_RETURN_IF_((result_name).has_error(), (result_name).error(), \
30  GAR_STRINGIFY(rexpr)); \
31  lhs = std::move(result_name).value();
32 
33 #define GAR_ASSIGN_OR_RAISE_ERROR_IMPL(result_name, lhs, rexpr) \
34  auto&& result_name = (rexpr); \
35  GAR_RAISE_ERROR_IF_((result_name).has_error(), (result_name).error(), \
36  GAR_STRINGIFY(rexpr)); \
37  lhs = std::move(result_name).value();
38 
39 #define GAR_ASSIGN_OR_RAISE_NAME(x, y) GAR_CONCAT(x, y)
40 
64 #define GAR_ASSIGN_OR_RAISE(lhs, rexpr) \
65  GAR_ASSIGN_OR_RAISE_IMPL( \
66  GAR_ASSIGN_OR_RAISE_NAME(_error_or_value, __COUNTER__), lhs, rexpr);
67 
79 #define GAR_ASSIGN_OR_RAISE_ERROR(lhs, rexpr) \
80  GAR_ASSIGN_OR_RAISE_ERROR_IMPL( \
81  GAR_ASSIGN_OR_RAISE_NAME(_error_or_value, __COUNTER__), lhs, rexpr);
82 
83 #define GAR_RETURN_ON_ARROW_ERROR_AND_ASSIGN_IMPL(result_name, lhs, rexpr) \
84  auto&& result_name = (rexpr); \
85  if (!result_name.status().ok()) { \
86  return ::graphar::Status::ArrowError(result_name.status().ToString()); \
87  } \
88  lhs = std::move(result_name).ValueOrDie();
89 
101 #define GAR_RETURN_ON_ARROW_ERROR_AND_ASSIGN(lhs, rexpr) \
102  GAR_RETURN_ON_ARROW_ERROR_AND_ASSIGN_IMPL( \
103  GAR_ASSIGN_OR_RAISE_NAME(_error_or_value, __COUNTER__), lhs, rexpr);
104 
105 namespace graphar::internal {
106 
107 // Extract Status from Status or Result<T>
108 // Useful for the status check macros such as RETURN_NOT_OK.
109 template <typename T>
110 inline const Status& GenericToStatus(const Result<T>& res) {
111  return res.status();
112 }
113 template <typename T>
114 inline Status GenericToStatus(Result<T>&& res) {
115  return std::move(res).status();
116 }
117 
118 } // namespace graphar::internal