Skip to content

iceoryx_hoofs/cxx/string.hpp🔗

Namespaces🔗

Name
iox
building block to easily create free function for logging in a library context
iox::cxx

Classes🔗

Name
struct iox::cxx::TruncateToCapacity_t
struct used to define a compile time variable which is used to distinguish between constructors with certain behavior
class iox::cxx::string
string implementation with some adjustments in the API, because we are not allowed to throw exceptions or use heap.

Source code🔗

// Copyright (c) 2019 by Robert Bosch GmbH. All rights reserved.
// Copyright (c) 2021 by Apex.AI Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0
#ifndef IOX_HOOFS_CXX_STRING_HPP
#define IOX_HOOFS_CXX_STRING_HPP

#include "iceoryx_hoofs/cxx/type_traits.hpp"
#include "iceoryx_hoofs/internal/cxx/string_internal.hpp"
#include "optional.hpp"

#include <algorithm>
#include <cstdint>
#include <cstring>
#include <iostream>

namespace iox
{
namespace cxx
{
template <typename T1, typename T2>
typename 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) noexcept;

template <typename T1, typename T2, typename... Targs>
typename std::enable_if<(internal::IsCharArray<T1>::value || internal::IsCxxString<T1>::value)
                            && (internal::IsCharArray<T2>::value || internal::IsCxxString<T2>::value),
                        string<internal::SumCapa<T1, T2, Targs...>::value>>::type
concatenate(const T1& t1, const T2& t2, const Targs&... targs) noexcept;

template <typename T1, typename T2>
typename std::enable_if<(internal::IsCharArray<T1>::value && internal::IsCxxString<T2>::value)
                            || (internal::IsCxxString<T1>::value && internal::IsCharArray<T2>::value)
                            || (internal::IsCxxString<T1>::value && internal::IsCxxString<T2>::value),
                        string<internal::GetCapa<T1>::capa + internal::GetCapa<T2>::capa>>::type
operator+(const T1& t1, const T2& t2) noexcept;

struct TruncateToCapacity_t
{
    explicit TruncateToCapacity_t() = default;
};
constexpr TruncateToCapacity_t TruncateToCapacity{};

template <uint64_t Capacity>
class string
{
    static_assert(Capacity > 0U, "The capacity of the fixed string must be greater than 0!");

  public:
    constexpr string() noexcept = default;

    string(const string& other) noexcept;

    string(string&& other) noexcept;

    string& operator=(const string& rhs) noexcept;

    string& operator=(string&& rhs) noexcept;

    template <uint64_t N>
    string(const string<N>& other) noexcept;

    template <uint64_t N>
    string(string<N>&& other) noexcept;

    template <uint64_t N>
    string& operator=(const string<N>& rhs) noexcept;

    template <uint64_t N>
    string& operator=(string<N>&& rhs) noexcept;

    template <uint64_t N>
    string(const char (&other)[N]) noexcept;

    string(TruncateToCapacity_t, const char* const other) noexcept;

    string(TruncateToCapacity_t, const std::string& other) noexcept;

    string(TruncateToCapacity_t, const char* const other, const uint64_t count) noexcept;

    template <uint64_t N>
    string& operator=(const char (&rhs)[N]) noexcept;

    template <uint64_t N>
    string& assign(const string<N>& str) noexcept;

    template <uint64_t N>
    string& assign(const char (&str)[N]) noexcept;

    bool unsafe_assign(const char* const str) noexcept;

    bool unsafe_assign(const std::string& str) noexcept;

    template <uint64_t N>
    int64_t compare(const string<N>& other) const noexcept;

    template <uint64_t N>
    bool operator==(const string<N>& rhs) const noexcept;

    template <uint64_t N>
    bool operator!=(const string<N>& rhs) const noexcept;

    template <uint64_t N>
    bool operator<(const string<N>& rhs) const noexcept;

    template <uint64_t N>
    bool operator<=(const string<N>& rhs) const noexcept;

    template <uint64_t N>
    bool operator>(const string<N>& rhs) const noexcept;

    template <uint64_t N>
    bool operator>=(const string<N>& rhs) const noexcept;

    bool operator==(const char* const rhs) const noexcept;

    bool operator!=(const char* const rhs) const noexcept;

    const char* c_str() const noexcept;

    constexpr uint64_t size() const noexcept;

    static constexpr uint64_t capacity() noexcept;

    constexpr bool empty() const noexcept;

    operator std::string() const noexcept;

    template <typename T>
    string& operator+=(const T&) noexcept;

    template <typename T>
    typename std::enable_if<internal::IsCharArray<T>::value || internal::IsCxxString<T>::value, string&>::type
    append(TruncateToCapacity_t, const T& t) noexcept;

    template <typename T>
    typename std::enable_if<internal::IsCharArray<T>::value || internal::IsCxxString<T>::value, bool>::type
    unsafe_append(const T& t) noexcept;

    iox::cxx::optional<string<Capacity>> substr(const uint64_t pos, const uint64_t count) const noexcept;

    iox::cxx::optional<string<Capacity>> substr(const uint64_t pos = 0U) const noexcept;

    template <typename T>
    typename 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;

    template <typename T>
    typename 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;

    template <typename T>
    typename 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;

    template <uint64_t N>
    friend class string;

    template <typename T1, typename T2>
    friend typename 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) noexcept;

  private:
    template <uint64_t N>
    string& copy(const string<N>& rhs) noexcept;

    template <uint64_t N>
    string& move(string<N>&& rhs) noexcept;

    char m_rawstring[Capacity + 1U]{'\0'};
    uint64_t m_rawstringSize{0U};
};

template <uint64_t Capacity>
inline bool operator==(const std::string& lhs, const string<Capacity>& rhs) noexcept;

template <uint64_t Capacity>
inline bool operator==(const string<Capacity>& lhs, const std::string& rhs) noexcept;

template <uint64_t Capacity>
inline bool operator!=(const std::string& lhs, const string<Capacity>& rhs) noexcept;

template <uint64_t Capacity>
inline bool operator!=(const string<Capacity>& lhs, const std::string& rhs) noexcept;

template <uint64_t Capacity>
inline bool operator==(const char* const lhs, const string<Capacity>& rhs) noexcept;

template <uint64_t Capacity>
inline bool operator!=(const char* const lhs, const string<Capacity>& rhs) noexcept;

template <uint64_t Capacity>
inline std::ostream& operator<<(std::ostream& stream, const string<Capacity>& str) noexcept;
} // namespace cxx
} // namespace iox
#include "iceoryx_hoofs/internal/cxx/string.inl"

#endif // IOX_HOOFS_CXX_STRING_HPP

Updated on 2 April 2022 at 16:37:47 CEST