iceoryx2
C++ Language Bindings
Loading...
Searching...
No Matches
convert.hpp
Go to the documentation of this file.
1// Copyright (c) 2019, 2021 by Robert Bosch GmbH. All rights reserved.
2// Copyright (c) 2021 by Apex.AI Inc. All rights reserved.
3// Copyright (c) 2022 by NXP. All rights reserved.
4// Copyright (c) 2023 by Dennis Liu. All rights reserved.
5// Copyright (c) 2025 Contributors to the Eclipse Foundation
6//
7// See the NOTICE file(s) distributed with this work for additional
8// information regarding copyright ownership.
9//
10// This program and the accompanying materials are made available under the
11// terms of the Apache Software License 2.0 which is available at
12// https://www.apache.org/licenses/LICENSE-2.0, or the MIT license
13// which is available at https://opensource.org/licenses/MIT.
14//
15// SPDX-License-Identifier: Apache-2.0 OR MIT
16
17#ifndef IOX2_BB_UTILITY_CONVERT_HPP
18#define IOX2_BB_UTILITY_CONVERT_HPP
19
20#include "iox2/bb/optional.hpp"
22
23#include <climits>
24#include <cmath>
25#include <cstdint>
26#include <cstdlib>
27#include <cstring>
28#include <iostream>
29#include <limits>
30#include <sstream>
31#include <string>
32
33namespace iox2 {
34namespace legacy {
47class convert {
48 public:
49 enum class NumberType : uint8_t {
50 INTEGER,
52 FLOAT
53 };
54
55 static constexpr int32_t STRTOULL_BASE { 10 };
56 static constexpr int32_t STRTOUL_BASE { 10 };
57 static constexpr int32_t STRTOLL_BASE { 10 };
58 static constexpr int32_t STRTOL_BASE { 10 };
59
60 static constexpr uint32_t FLOAT_SIGNALING_NAN_MASK { static_cast<uint32_t>(1) << static_cast<uint32_t>(22) };
61 static constexpr uint64_t DOUBLE_SIGNALING_NAN_MASK { static_cast<uint64_t>(1) << static_cast<uint64_t>(51) };
62
68 template <typename Source>
69 static typename std::enable_if<!std::is_convertible<Source, std::string>::value, std::string>::type
70 toString(const Source& t) noexcept;
71
77 template <typename Source>
78 static typename std::enable_if<std::is_convertible<Source, std::string>::value, std::string>::type
79 toString(const Source& t) noexcept;
80
88 template <typename TargetType>
89 static bb::Optional<TargetType> from_string(const char* v) noexcept;
90
91 private:
92 template <typename TargetType, typename CallType>
93 static bb::Optional<TargetType> evaluate_return_value(CallType& call, const char* end_ptr, const char* v) noexcept;
94
95 template <typename TargetType, typename SourceType>
96 static bool check_edge_case(decltype(errno) errno_cache,
97 const char* end_ptr,
98 const char* v,
99 const SourceType& source_val) noexcept;
100
101 template <typename SourceType>
102 static bool is_valid_input(const char* end_ptr, const char* v, const SourceType& source_val) noexcept;
103
104 template <typename TargetType, typename SourceType>
105 static bool is_within_range(const SourceType& source_val) noexcept;
106
107 static bool is_valid_errno(decltype(errno) errno_cache, const char* v) noexcept;
108 static bool start_with_neg_sign(const char* v) noexcept;
109};
110
111} // namespace legacy
112} // namespace iox2
113
114#include "iox2/legacy/detail/convert.inl"
115
116#endif // IOX2_BB_UTILITY_CONVERT_HPP
Collection of static methods for conversion from and to string.
Definition convert.hpp:47
static constexpr uint32_t FLOAT_SIGNALING_NAN_MASK
Definition convert.hpp:60
static constexpr int32_t STRTOLL_BASE
Definition convert.hpp:57
static bb::Optional< TargetType > from_string(const char *v) noexcept
convert the input based on the 'TargetType', allowing only numeric types as valid destination types
static constexpr int32_t STRTOL_BASE
Definition convert.hpp:58
static constexpr int32_t STRTOULL_BASE
Definition convert.hpp:55
static std::enable_if< std::is_convertible< Source, std::string >::value, std::string >::type toString(const Source &t) noexcept
Converts every type which is either a pod (plain old data) type or is convertable to a string (this m...
static std::enable_if<!std::is_convertible< Source, std::string >::value, std::string >::type toString(const Source &t) noexcept
Converts every type which is either a pod (plain old data) type or is convertable to a string (this m...
static constexpr uint64_t DOUBLE_SIGNALING_NAN_MASK
Definition convert.hpp:61
static constexpr int32_t STRTOUL_BASE
Definition convert.hpp:56
iox2::bb::variation::Optional< T > Optional
Definition optional.hpp:25
constexpr bool always_false_v
Helper value to bind a static_assert to a type.