iceoryx_doc  1.0.1
string_internal.hpp
1 // Copyright (c) 2020 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_STRING_INTERNAL_HPP
17 #define IOX_UTILS_CXX_STRING_INTERNAL_HPP
18 
19 #include <cstdint>
20 #include <cstring>
21 #include <string>
22 
23 namespace iox
24 {
25 namespace cxx
26 {
27 template <uint64_t>
28 class string;
29 
30 namespace internal
31 {
32 template <uint64_t N>
33 using charArray = char[N];
34 
37 template <typename T>
38 struct GetCapa
39 {
40  static constexpr uint64_t capa = 0U;
41 };
42 
43 template <uint64_t N>
44 struct GetCapa<string<N>>
45 {
46  static constexpr uint64_t capa = N;
47 };
48 
49 template <uint64_t N>
50 struct GetCapa<char[N]>
51 {
52  static constexpr uint64_t capa = N - 1U;
53 };
54 
56 template <typename T>
57 struct GetSize;
58 
59 template <uint64_t N>
60 struct GetSize<string<N>>
61 {
62  static uint64_t call(const string<N>& data) noexcept
63  {
64  return data.size();
65  }
66 };
67 
68 template <uint64_t N>
69 struct GetSize<char[N]>
70 {
71  static uint64_t call(const charArray<N>& data) noexcept
72  {
73  return strnlen(data, N);
74  }
75 };
76 
77 template <>
78 struct GetSize<std::string>
79 {
80  static uint64_t call(const std::string& data) noexcept
81  {
82  return data.size();
83  }
84 };
85 
87 template <typename T>
88 struct GetData;
89 
90 template <uint64_t N>
91 struct GetData<string<N>>
92 {
93  static const char* call(const string<N>& data) noexcept
94  {
95  return data.c_str();
96  }
97 };
98 
99 template <uint64_t N>
100 struct GetData<char[N]>
101 {
102  static const char* call(const charArray<N>& data) noexcept
103  {
104  return &data[0];
105  }
106 };
107 
108 template <>
109 struct GetData<std::string>
110 {
111  static const char* call(const std::string& data) noexcept
112  {
113  return data.data();
114  }
115 };
116 
118 template <typename... Targs>
119 struct SumCapa;
120 
121 template <>
122 struct SumCapa<>
123 {
124  static constexpr uint64_t value = 0U;
125 };
126 
127 template <typename T, typename... Targs>
128 struct SumCapa<T, Targs...>
129 {
130  static constexpr uint64_t value = GetCapa<T>::capa + SumCapa<Targs...>::value;
131 };
132 
134 template <typename T>
136 {
137  static constexpr bool value = false;
138 };
139 
140 template <uint64_t N>
141 struct IsCharArray<char[N]>
142 {
143  static constexpr bool value = true;
144 };
145 
147 template <typename T>
149 {
150  static constexpr bool value = false;
151 };
152 
153 template <uint64_t N>
154 struct IsCxxString<string<N>>
155 {
156  static constexpr bool value = true;
157 };
158 } // namespace internal
159 } // namespace cxx
160 } // namespace iox
161 #endif // IOX_UTILS_CXX_STRING_INTERNAL_HPP
string implementation with some adjustments in the API, because we are not allowed to throw exception...
Definition: string.hpp:86
constexpr uint64_t size() const noexcept
returns the number of characters stored in the string
Definition: string.inl:278
const char * c_str() const noexcept
returns a pointer to the char array of self
Definition: string.inl:272
building block to easily create free function for logging in a library context
Definition: lockfree_queue.hpp:28
struct to get capacity of fixed string/string literal
Definition: string_internal.hpp:39
struct to get a pointer to the char array of the fixed string/string literal/std::string
Definition: string_internal.hpp:88
struct to get size of fixed string/string literal/std::string
Definition: string_internal.hpp:57
struct to check whether an argument is a char array
Definition: string_internal.hpp:136
struct to check whether an argument is a cxx string
Definition: string_internal.hpp:149
struct to get the sum of the capacities of fixed strings/string literals
Definition: string_internal.hpp:119