iceoryx_doc  1.0.1
convert.hpp
1 // Copyright (c) 2019 by Robert Bosch GmbH. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 // SPDX-License-Identifier: Apache-2.0
16 #ifndef IOX_UTILS_CXX_CONVERT_HPP
17 #define IOX_UTILS_CXX_CONVERT_HPP
18 
19 #include "iceoryx_utils/cxx/smart_c.hpp"
20 
21 #include <climits>
22 #include <cmath>
23 #include <cstdlib>
24 #include <cstring>
25 #include <limits>
26 #include <sstream>
27 #include <string>
28 
29 namespace iox
30 {
31 namespace cxx
32 {
43 class convert
44 {
45  public:
46  enum class NumberType
47  {
48  INTEGER,
49  UNSIGNED_INTEGER,
50  FLOAT
51  };
52 
53  static constexpr int32_t STRTOULL_BASE = 10;
54 
60  template <typename Source>
61  static typename std::enable_if<!std::is_convertible<Source, std::string>::value, std::string>::type
62  toString(const Source& t);
63 
69  template <typename Source>
70  static typename std::enable_if<std::is_convertible<Source, std::string>::value, std::string>::type
71  toString(const Source& t);
72 
78  template <typename Destination>
79  static bool fromString(const char* v, Destination& dest);
80 
85  static bool stringIsNumber(const char* v, const NumberType type);
86 
87  private:
88  static bool stringIsNumberWithErrorMessage(const char* v, const NumberType type);
89 };
90 
91 } // namespace cxx
92 } // namespace iox
93 
94 #include "iceoryx_utils/internal/cxx/convert.inl"
95 
96 #endif // IOX_UTILS_CXX_CONVERT_HPP
Collection of static methods for conversion from and to string.
Definition: convert.hpp:44
static bool fromString(const char *v, Destination &dest)
Sets dest from a given string. If the conversion fails false is returned and the value of dest is und...
static std::enable_if< std::is_convertible< Source, std::string >::value, std::string >::type toString(const Source &t)
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)
Converts every type which is either a pod (plain old data) type or is convertable to a string (this m...
Definition: convert.inl:25
static bool stringIsNumber(const char *v, const NumberType type)
checks if a given string v is a number
Definition: convert.inl:73
building block to easily create free function for logging in a library context
Definition: lockfree_queue.hpp:28