1 | //===----------------------------------------------------------------------===// |
2 | // DuckDB |
3 | // |
4 | // duckdb/common/optional_idx.hpp |
5 | // |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #pragma once |
10 | |
11 | #include "duckdb/common/exception.hpp" |
12 | |
13 | namespace duckdb { |
14 | |
15 | class optional_idx { |
16 | static constexpr const idx_t INVALID_INDEX = idx_t(-1); |
17 | |
18 | public: |
19 | optional_idx() : index(INVALID_INDEX) { |
20 | } |
21 | optional_idx(idx_t index) : index(index) { // NOLINT: allow implicit conversion from idx_t |
22 | if (index == INVALID_INDEX) { |
23 | throw InternalException("optional_idx cannot be initialized with an invalid index" ); |
24 | } |
25 | } |
26 | |
27 | static optional_idx Invalid() { |
28 | return INVALID_INDEX; |
29 | } |
30 | |
31 | bool IsValid() const { |
32 | return index != DConstants::INVALID_INDEX; |
33 | } |
34 | idx_t GetIndex() { |
35 | if (index == INVALID_INDEX) { |
36 | throw InternalException("Attempting to get the index of an optional_idx that is not set" ); |
37 | } |
38 | return index; |
39 | } |
40 | |
41 | private: |
42 | idx_t index; |
43 | }; |
44 | |
45 | } // namespace duckdb |
46 | |