1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements. See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership. The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License. You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied. See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18#ifndef ARROW_COMPUTE_CONTEXT_H
19#define ARROW_COMPUTE_CONTEXT_H
20
21#include <cstdint>
22#include <memory>
23
24#include "arrow/memory_pool.h"
25#include "arrow/status.h"
26#include "arrow/util/macros.h"
27#include "arrow/util/visibility.h"
28
29namespace arrow {
30
31class Buffer;
32
33namespace internal {
34class CpuInfo;
35} // namespace internal
36
37namespace compute {
38
39#define RETURN_IF_ERROR(ctx) \
40 if (ARROW_PREDICT_FALSE(ctx->HasError())) { \
41 Status s = ctx->status(); \
42 ctx->ResetStatus(); \
43 return s; \
44 }
45
46/// \brief Container for variables and options used by function evaluation
47class ARROW_EXPORT FunctionContext {
48 public:
49 explicit FunctionContext(MemoryPool* pool ARROW_MEMORY_POOL_DEFAULT);
50 MemoryPool* memory_pool() const;
51
52 /// \brief Allocate buffer from the context's memory pool
53 Status Allocate(const int64_t nbytes, std::shared_ptr<Buffer>* out);
54
55 /// \brief Indicate that an error has occurred, to be checked by a parent caller
56 /// \param[in] status a Status instance
57 ///
58 /// \note Will not overwrite a prior set Status, so we will have the first
59 /// error that occurred until FunctionContext::ResetStatus is called
60 void SetStatus(const Status& status);
61
62 /// \brief Clear any error status
63 void ResetStatus();
64
65 /// \brief Return true if an error has occurred
66 bool HasError() const { return !status_.ok(); }
67
68 /// \brief Return the current status of the context
69 const Status& status() const { return status_; }
70
71 internal::CpuInfo* cpu_info() const { return cpu_info_; }
72
73 private:
74 Status status_;
75 MemoryPool* pool_;
76 internal::CpuInfo* cpu_info_;
77};
78
79} // namespace compute
80} // namespace arrow
81
82#endif // ARROW_COMPUTE_CONTEXT_H
83