iceberg-cpp
Loading...
Searching...
No Matches
macros.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 <cassert>
23
24#include "iceberg/exception.h"
25#include "iceberg/result.h"
26
27#define ICEBERG_RETURN_UNEXPECTED(expr) \
28 if (auto&& result_name = expr; !result_name) [[unlikely]] { \
29 return std::unexpected<Error>(result_name.error()); \
30 }
31
32#define ICEBERG_ASSIGN_OR_RAISE_IMPL(result_name, lhs, rexpr) \
33 auto&& result_name = (rexpr); \
34 ICEBERG_RETURN_UNEXPECTED(result_name) \
35 lhs = std::move(result_name.value());
36
37#define ICEBERG_CONCAT(x, y) x##y
38
39#define ICEBERG_ASSIGN_OR_RAISE_NAME(x, y) ICEBERG_CONCAT(x, y)
40
41#define ICEBERG_ASSIGN_OR_RAISE(lhs, rexpr) \
42 ICEBERG_ASSIGN_OR_RAISE_IMPL(ICEBERG_ASSIGN_OR_RAISE_NAME(result_, __COUNTER__), lhs, \
43 rexpr)
44
45// Macro for debug checks
46#define ICEBERG_DCHECK(expr, message) assert((expr) && (message))
47
48// Macro for precondition checks, usually used for function arguments
49#define ICEBERG_PRECHECK(expr, ...) \
50 do { \
51 if (!(expr)) [[unlikely]] { \
52 return InvalidArgument(__VA_ARGS__); \
53 } \
54 } while (0)
55
56// Macro for state checks, usually used for unexpected states
57#define ICEBERG_CHECK(expr, ...) \
58 do { \
59 if (!(expr)) [[unlikely]] { \
60 return ValidationFailed(__VA_ARGS__); \
61 } \
62 } while (0)
63
64#define ERROR_TO_EXCEPTION(error) \
65 if (error.kind == iceberg::ErrorKind::kInvalidExpression) { \
66 throw iceberg::ExpressionError(error.message); \
67 } else { \
68 throw iceberg::IcebergError(error.message); \
69 }
70
71#define ICEBERG_THROW_NOT_OK(expr) \
72 if (auto&& result_name = expr; !result_name) [[unlikely]] { \
73 ERROR_TO_EXCEPTION(result_name.error()); \
74 }
75
76#define ICEBERG_ASSIGN_OR_THROW_IMPL(result_name, lhs, rexpr) \
77 auto&& result_name = (rexpr); \
78 ICEBERG_THROW_NOT_OK(result_name); \
79 lhs = std::move(result_name.value());
80
81#define ICEBERG_ASSIGN_OR_THROW(lhs, rexpr) \
82 ICEBERG_ASSIGN_OR_THROW_IMPL( \
83 ICEBERG_ASSIGN_OR_RAISE_NAME(_error_or_value, __COUNTER__), lhs, rexpr);