iceoryx_hoofs  2.0.2
convert.hpp
1 // Copyright (c) 2019, 2021 by Robert Bosch GmbH. All rights reserved.
2 // Copyright (c) 2021 by Apex.AI Inc. All rights reserved.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 // SPDX-License-Identifier: Apache-2.0
17 #ifndef IOX_HOOFS_CXX_CONVERT_HPP
18 #define IOX_HOOFS_CXX_CONVERT_HPP
19 
20 #include "iceoryx_hoofs/cxx/string.hpp"
21 #include "iceoryx_hoofs/posix_wrapper/posix_call.hpp"
22 
23 #include <climits>
24 #include <cmath>
25 #include <cstdlib>
26 #include <cstring>
27 #include <iostream>
28 #include <limits>
29 #include <sstream>
30 #include <string>
31 
32 namespace iox
33 {
34 namespace cxx
35 {
46 class convert
47 {
48  public:
49  enum class NumberType
50  {
51  INTEGER,
52  UNSIGNED_INTEGER,
53  FLOAT
54  };
55 
56  static constexpr int32_t STRTOULL_BASE = 10;
57 
63  template <typename Source>
64  static typename std::enable_if<!std::is_convertible<Source, std::string>::value, std::string>::type
65  toString(const Source& t) noexcept;
66 
72  template <typename Source>
73  static typename std::enable_if<std::is_convertible<Source, std::string>::value, std::string>::type
74  toString(const Source& t) noexcept;
75 
81  template <typename Destination>
82  static bool fromString(const char* v, Destination& dest) noexcept;
83 
88  static bool stringIsNumber(const char* v, const NumberType type) noexcept;
89 
90  private:
91  static bool stringIsNumberWithErrorMessage(const char* v, const NumberType type) noexcept;
92 };
93 
94 } // namespace cxx
95 } // namespace iox
96 
97 #include "iceoryx_hoofs/internal/cxx/convert.inl"
98 
99 #endif // IOX_HOOFS_CXX_CONVERT_HPP
Collection of static methods for conversion from and to string.
Definition: convert.hpp:47
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 bool stringIsNumber(const char *v, const NumberType type) noexcept
checks if a given string v is a number
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 bool fromString(const char *v, Destination &dest) noexcept
Sets dest from a given string. If the conversion fails false is returned and the value of dest is und...
building block to easily create free function for logging in a library context
Definition: lockfree_queue.hpp:29