xref: /aosp_15_r20/external/llvm-libc/src/__support/str_to_num_result.h (revision 71db0c75aadcf003ffe3238005f61d7618a3fead)
1 //===-- A data structure for str_to_number to return ------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // -----------------------------------------------------------------------------
10 //                               **** WARNING ****
11 // This file is shared with libc++. You should also be careful when adding
12 // dependencies to this file, since it needs to build for all libc++ targets.
13 // -----------------------------------------------------------------------------
14 
15 #ifndef LLVM_LIBC_SRC___SUPPORT_STR_TO_NUM_RESULT_H
16 #define LLVM_LIBC_SRC___SUPPORT_STR_TO_NUM_RESULT_H
17 
18 #include "src/__support/macros/attributes.h" // LIBC_INLINE
19 #include "src/__support/macros/config.h"
20 
21 #include <stddef.h>
22 
23 namespace LIBC_NAMESPACE_DECL {
24 
25 // -----------------------------------------------------------------------------
26 //                               **** WARNING ****
27 // This interface is shared with libc++, if you change this interface you need
28 // to update it in both libc and libc++.
29 // -----------------------------------------------------------------------------
30 template <typename T> struct StrToNumResult {
31   T value;
32   int error;
33   ptrdiff_t parsed_len;
34 
StrToNumResultStrToNumResult35   LIBC_INLINE constexpr StrToNumResult(T value)
36       : value(value), error(0), parsed_len(0) {}
StrToNumResultStrToNumResult37   LIBC_INLINE constexpr StrToNumResult(T value, ptrdiff_t parsed_len)
38       : value(value), error(0), parsed_len(parsed_len) {}
StrToNumResultStrToNumResult39   LIBC_INLINE constexpr StrToNumResult(T value, ptrdiff_t parsed_len, int error)
40       : value(value), error(error), parsed_len(parsed_len) {}
41 
has_errorStrToNumResult42   LIBC_INLINE constexpr bool has_error() { return error != 0; }
43 
TStrToNumResult44   LIBC_INLINE constexpr operator T() { return value; }
45 };
46 } // namespace LIBC_NAMESPACE_DECL
47 
48 #endif // LLVM_LIBC_SRC___SUPPORT_STR_TO_NUM_RESULT_H
49