]> git.proxmox.com Git - ceph.git/blame - ceph/src/arrow/docs/source/cpp/conventions.rst
import quincy 17.2.0
[ceph.git] / ceph / src / arrow / docs / source / cpp / conventions.rst
CommitLineData
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
23Conventions
24===========
25
26The Arrow C++ API follows a few simple guidelines. As with many rules,
27there may be exceptions.
28
29Language version
30----------------
31
32Arrow is C++11-compatible. A few backports are used for newer functionality,
33for example the :class:`std::string_view` class.
34
35Namespacing
36-----------
37
38All the Arrow API (except macros) is namespaced inside a ``arrow`` namespace,
39and nested namespaces thereof.
40
41Safe pointers
42-------------
43
44Arrow objects are usually passed and stored using safe pointers -- most of
45the time :class:`std::shared_ptr` but sometimes also :class:`std::unique_ptr`.
46
47Immutability
48------------
49
50Many Arrow objects are immutable: once constructed, their logical properties
51cannot change anymore. This makes it possible to use them in multi-threaded
52scenarios without requiring tedious and error-prone synchronization.
53
54There are obvious exceptions to this, such as IO objects or mutable data buffers.
55
56Error reporting
57---------------
58
59Most APIs indicate a successful or erroneous outcome by returning a
60:class:`arrow::Status` instance. Arrow doesn't throw exceptions of its
61own, but third-party exceptions might propagate through, especially
62:class:`std::bad_alloc` (but Arrow doesn't use the standard allocators for
63large data).
64
65When an API can return either an error code or a successful value, it usually
66does so by returning the template class
67:class:`arrow::Result <template\<class T\> arrow::Result>`. However,
68some APIs (usually deprecated) return :class:`arrow::Status` and pass the
69result value as an out-pointer parameter.
70
71Here 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
83If the caller function itself returns a :class:`arrow::Result` or
84:class:`arrow::Status` and wants to propagate any non-successful outcome, two
85convenience 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
94For 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>`