]>
Commit | Line | Data |
---|---|---|
1d09f67e TL |
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 | .. default-domain:: cpp | |
19 | .. highlight:: cpp | |
20 | ||
21 | .. cpp:namespace:: arrow | |
22 | ||
23 | Conventions | |
24 | =========== | |
25 | ||
26 | The Arrow C++ API follows a few simple guidelines. As with many rules, | |
27 | there may be exceptions. | |
28 | ||
29 | Language version | |
30 | ---------------- | |
31 | ||
32 | Arrow is C++11-compatible. A few backports are used for newer functionality, | |
33 | for example the :class:`std::string_view` class. | |
34 | ||
35 | Namespacing | |
36 | ----------- | |
37 | ||
38 | All the Arrow API (except macros) is namespaced inside a ``arrow`` namespace, | |
39 | and nested namespaces thereof. | |
40 | ||
41 | Safe pointers | |
42 | ------------- | |
43 | ||
44 | Arrow objects are usually passed and stored using safe pointers -- most of | |
45 | the time :class:`std::shared_ptr` but sometimes also :class:`std::unique_ptr`. | |
46 | ||
47 | Immutability | |
48 | ------------ | |
49 | ||
50 | Many Arrow objects are immutable: once constructed, their logical properties | |
51 | cannot change anymore. This makes it possible to use them in multi-threaded | |
52 | scenarios without requiring tedious and error-prone synchronization. | |
53 | ||
54 | There are obvious exceptions to this, such as IO objects or mutable data buffers. | |
55 | ||
56 | Error reporting | |
57 | --------------- | |
58 | ||
59 | Most APIs indicate a successful or erroneous outcome by returning a | |
60 | :class:`arrow::Status` instance. Arrow doesn't throw exceptions of its | |
61 | own, but third-party exceptions might propagate through, especially | |
62 | :class:`std::bad_alloc` (but Arrow doesn't use the standard allocators for | |
63 | large data). | |
64 | ||
65 | When an API can return either an error code or a successful value, it usually | |
66 | does so by returning the template class | |
67 | :class:`arrow::Result <template\<class T\> arrow::Result>`. However, | |
68 | some APIs (usually deprecated) return :class:`arrow::Status` and pass the | |
69 | result value as an out-pointer parameter. | |
70 | ||
71 | Here is an example of checking the outcome of an operation:: | |
72 | ||
73 | const int64_t buffer_size = 4096; | |
74 | ||
75 | auto maybe_buffer = arrow::AllocateBuffer(buffer_size, &buffer); | |
76 | if (!maybe_buffer.ok()) { | |
77 | // ... handle error | |
78 | } else { | |
79 | std::shared_ptr<arrow::Buffer> buffer = *maybe_buffer; | |
80 | // ... use allocated buffer | |
81 | } | |
82 | ||
83 | If the caller function itself returns a :class:`arrow::Result` or | |
84 | :class:`arrow::Status` and wants to propagate any non-successful outcome, two | |
85 | convenience macros are available: | |
86 | ||
87 | * :c:macro:`ARROW_RETURN_NOT_OK` takes a :class:`arrow::Status` parameter | |
88 | and returns it if not successful. | |
89 | ||
90 | * :c:macro:`ARROW_ASSIGN_OR_RAISE` takes a :class:`arrow::Result` parameter, | |
91 | assigns its result to a *lvalue* if successful, or returns the corresponding | |
92 | :class:`arrow::Status` on error. | |
93 | ||
94 | For example:: | |
95 | ||
96 | arrow::Status DoSomething() { | |
97 | const int64_t buffer_size = 4096; | |
98 | std::shared_ptr<arrow::Buffer> buffer; | |
99 | ARROW_ASSIGN_OR_RAISE(buffer, arrow::AllocateBuffer(buffer_size)); | |
100 | // ... allocation successful, do something with buffer below | |
101 | ||
102 | // return success at the end | |
103 | return Status::OK(); | |
104 | } | |
105 | ||
106 | .. seealso:: | |
107 | :doc:`API reference for error reporting <api/support>` |