iceoryx_doc  1.0.1
string.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_STRING_HPP
17 #define IOX_UTILS_CXX_STRING_HPP
18 
19 #include "iceoryx_utils/internal/cxx/string_internal.hpp"
20 #include "optional.hpp"
21 
22 #include <cstring>
23 #include <iostream>
24 
25 namespace iox
26 {
27 namespace cxx
28 {
39 template <typename T1, typename T2>
40 typename std::enable_if<(internal::IsCharArray<T1>::value || internal::IsCxxString<T1>::value)
41  && (internal::IsCharArray<T2>::value || internal::IsCxxString<T2>::value),
42  string<internal::GetCapa<T1>::capa + internal::GetCapa<T2>::capa>>::type
43 concatenate(const T1& t1, const T2& t2);
44 
55 template <typename T1, typename T2, typename... Targs>
56 typename std::enable_if<(internal::IsCharArray<T1>::value || internal::IsCxxString<T1>::value)
57  && (internal::IsCharArray<T2>::value || internal::IsCxxString<T2>::value),
58  string<internal::SumCapa<T1, T2, Targs...>::value>>::type
59 concatenate(const T1& t1, const T2& t2, const Targs&... targs);
60 
67 template <typename T1, typename T2>
68 typename std::enable_if<(internal::IsCharArray<T1>::value && internal::IsCxxString<T2>::value)
69  || (internal::IsCxxString<T1>::value && internal::IsCharArray<T2>::value)
70  || (internal::IsCxxString<T1>::value && internal::IsCxxString<T2>::value),
71  string<internal::GetCapa<T1>::capa + internal::GetCapa<T2>::capa>>::type
72 operator+(const T1& t1, const T2& t2);
73 
77 {
78  explicit TruncateToCapacity_t() = default;
79 };
80 constexpr TruncateToCapacity_t TruncateToCapacity{};
81 
84 template <uint64_t Capacity>
85 class string
86 {
87  static_assert(Capacity > 0U, "The capacity of the fixed string must be greater than 0!");
88 
89  public:
91  constexpr string() noexcept = default;
92 
96  string(const string& other) noexcept;
97 
101  string(string&& other) noexcept;
102 
108  string& operator=(const string& rhs) noexcept;
109 
115  string& operator=(string&& rhs) noexcept;
116 
121  template <uint64_t N>
122  string(const string<N>& other) noexcept;
123 
128  template <uint64_t N>
129  string(string<N>&& other) noexcept;
130 
137  template <uint64_t N>
138  string& operator=(const string<N>& rhs) noexcept;
139 
146  template <uint64_t N>
147  string& operator=(string<N>&& rhs) noexcept;
148 
166  template <uint64_t N>
167  string(const char (&other)[N]) noexcept;
168 
186  string(TruncateToCapacity_t, const char* const other) noexcept;
187 
206  string(TruncateToCapacity_t, const std::string& other) noexcept;
207 
226  string(TruncateToCapacity_t, const char* const other, const uint64_t count) noexcept;
227 
247  template <uint64_t N>
248  string& operator=(const char (&rhs)[N]) noexcept;
249 
256  template <uint64_t N>
257  string& assign(const string<N>& str) noexcept;
258 
281  template <uint64_t N>
282  string& assign(const char (&str)[N]) noexcept;
283 
290  bool unsafe_assign(const char* const str) noexcept;
291 
298  bool unsafe_assign(const std::string& str) noexcept;
299 
307  template <uint64_t N>
308  int64_t compare(const string<N>& other) const noexcept;
309 
315  template <uint64_t N>
316  bool operator==(const string<N>& rhs) const noexcept;
317 
323  template <uint64_t N>
324  bool operator!=(const string<N>& rhs) const noexcept;
325 
331  template <uint64_t N>
332  bool operator<(const string<N>& rhs) const noexcept;
333 
339  template <uint64_t N>
340  bool operator<=(const string<N>& rhs) const noexcept;
341 
347  template <uint64_t N>
348  bool operator>(const string<N>& rhs) const noexcept;
349 
355  template <uint64_t N>
356  bool operator>=(const string<N>& rhs) const noexcept;
357 
371  bool operator==(const char* const rhs) const noexcept;
372 
386  bool operator!=(const char* const rhs) const noexcept;
387 
391  const char* c_str() const noexcept;
392 
396  constexpr uint64_t size() const noexcept;
397 
401  constexpr uint64_t capacity() const noexcept;
402 
406  constexpr bool empty() const noexcept;
407 
411  operator std::string() const noexcept;
412 
416  template <typename T>
417  string& operator+=(const T&) noexcept;
418 
432  template <typename T>
433  typename std::enable_if<internal::IsCharArray<T>::value || internal::IsCxxString<T>::value, string&>::type
434  append(TruncateToCapacity_t, const T& t) noexcept;
435 
442  template <typename T>
443  typename std::enable_if<internal::IsCharArray<T>::value || internal::IsCxxString<T>::value, bool>::type
444  unsafe_append(const T& t) noexcept;
445 
455  iox::cxx::optional<string<Capacity>> substr(const uint64_t pos, const uint64_t count) const noexcept;
456 
464  iox::cxx::optional<string<Capacity>> substr(const uint64_t pos = 0U) const noexcept;
465 
474  template <typename T>
475  typename std::enable_if<std::is_same<T, std::string>::value || internal::IsCharArray<T>::value
476  || internal::IsCxxString<T>::value,
477  iox::cxx::optional<uint64_t>>::type
478  find(const T& t, const uint64_t pos = 0U) const noexcept;
479 
489  template <typename T>
490  typename std::enable_if<std::is_same<T, std::string>::value || internal::IsCharArray<T>::value
491  || internal::IsCxxString<T>::value,
492  iox::cxx::optional<uint64_t>>::type
493  find_first_of(const T& t, const uint64_t pos = 0U) const noexcept;
494 
503  template <typename T>
504  typename std::enable_if<std::is_same<T, std::string>::value || internal::IsCharArray<T>::value
505  || internal::IsCxxString<T>::value,
506  iox::cxx::optional<uint64_t>>::type
507  find_last_of(const T& t, const uint64_t pos = Capacity) const noexcept;
508 
509  template <uint64_t N>
510  friend class string;
511 
512  template <typename T1, typename T2>
513  friend typename std::enable_if<(internal::IsCharArray<T1>::value || internal::IsCxxString<T1>::value)
514  && (internal::IsCharArray<T2>::value || internal::IsCxxString<T2>::value),
515  string<internal::GetCapa<T1>::capa + internal::GetCapa<T2>::capa>>::type
516  concatenate(const T1& t1, const T2& t2);
517 
518  private:
525  template <uint64_t N>
526  string& copy(const string<N>& rhs) noexcept;
527 
534  template <uint64_t N>
535  string& move(string<N>&& rhs) noexcept;
536 
537  char m_rawstring[Capacity + 1U]{'\0'};
538  uint64_t m_rawstringSize{0U};
539 };
540 
547 template <uint64_t Capacity>
548 inline bool operator==(const std::string& lhs, const string<Capacity>& rhs);
549 
556 template <uint64_t Capacity>
557 inline bool operator==(const string<Capacity>& lhs, const std::string& rhs);
558 
565 template <uint64_t Capacity>
566 inline bool operator!=(const std::string& lhs, const string<Capacity>& rhs);
567 
574 template <uint64_t Capacity>
575 inline bool operator!=(const string<Capacity>& lhs, const std::string& rhs);
576 
586 template <uint64_t Capacity>
587 inline bool operator==(const char* const lhs, const string<Capacity>& rhs);
588 
598 template <uint64_t Capacity>
599 inline bool operator!=(const char* const lhs, const string<Capacity>& rhs);
600 
607 template <uint64_t Capacity>
608 inline std::ostream& operator<<(std::ostream& stream, const string<Capacity>& str);
609 } // namespace cxx
610 } // namespace iox
611 #include "iceoryx_utils/internal/cxx/string.inl"
612 
613 #endif // IOX_UTILS_CXX_STRING_HPP
Optional implementation from the C++17 standard with C++11. The interface is analog to the C++17 stan...
Definition: optional.hpp:63
string implementation with some adjustments in the API, because we are not allowed to throw exception...
Definition: string.hpp:86
iox::cxx::optional< string< Capacity > > substr(const uint64_t pos, const uint64_t count) const noexcept
creates a substring containing the characters from pos until count; if pos+count is greater than the ...
Definition: string.inl:506
constexpr bool empty() const noexcept
returns if the string is empty or not
Definition: string.inl:290
constexpr string() noexcept=default
creates an empty string with size 0
friend std::enable_if<(internal::IsCharArray< T1 >::value||internal::IsCxxString< T1 >::value) &&(internal::IsCharArray< T2 >::value||internal::IsCxxString< T2 >::value), string< internal::GetCapa< T1 >::capa+internal::GetCapa< T2 >::capa > >::type concatenate(const T1 &t1, const T2 &t2)
concatenates two fixed strings/string literals
Definition: string.inl:429
std::enable_if< internal::IsCharArray< T >::value||internal::IsCxxString< T >::value, string & >::type append(TruncateToCapacity_t, const T &t) noexcept
appends a fixed string or string literal to the end of this. If this' capacity is too small for appen...
Definition: string.inl:487
std::enable_if< internal::IsCharArray< T >::value||internal::IsCxxString< T >::value, bool >::type unsafe_append(const T &t) noexcept
appends a fixed string or string literal to the end of this. The appending fails if the sum of both s...
Definition: string.inl:465
std::enable_if< std::is_same< T, std::string >::value||internal::IsCharArray< T >::value||internal::IsCxxString< T >::value, iox::cxx::optional< uint64_t > >::type find_first_of(const T &t, const uint64_t pos=0U) const noexcept
finds the first occurence of a character equal to one of the characters of the given character sequen...
Definition: string.inl:556
bool unsafe_assign(const char *const str) noexcept
assigns a cstring to string. The assignment fails if the cstring size is greater than the string capa...
Definition: string.inl:178
std::enable_if< std::is_same< T, std::string >::value||internal::IsCharArray< T >::value||internal::IsCxxString< T >::value, iox::cxx::optional< uint64_t > >::type find(const T &t, const uint64_t pos=0U) const noexcept
finds the first occurence of the given character sequence; returns the position of the first characte...
Definition: string.inl:537
string & assign(const string< N > &str) noexcept
fixed string assignment with compile time check if capacity of str is less than or equal to this' cap...
constexpr uint64_t size() const noexcept
returns the number of characters stored in the string
Definition: string.inl:278
int64_t compare(const string< N > &other) const noexcept
compares two strings
Definition: string.inl:215
std::enable_if< std::is_same< T, std::string >::value||internal::IsCharArray< T >::value||internal::IsCxxString< T >::value, iox::cxx::optional< uint64_t > >::type find_last_of(const T &t, const uint64_t pos=Capacity) const noexcept
finds the last occurence of a character equal to one of the characters of the given character sequenc...
Definition: string.inl:580
const char * c_str() const noexcept
returns a pointer to the char array of self
Definition: string.inl:272
constexpr uint64_t capacity() const noexcept
returns the maximum number of characters that can be stored in the string
Definition: string.inl:284
building block to easily create free function for logging in a library context
Definition: lockfree_queue.hpp:28
struct used to define a compile time variable which is used to distinguish between constructors with ...
Definition: string.hpp:77