Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** \file
- *
- */
- #ifndef MESHPP_CORE_SHARED_STRING_H_2A0F6174_39EA_40FE_B526_9293C36939D7
- #define MESHPP_CORE_SHARED_STRING_H_2A0F6174_39EA_40FE_B526_9293C36939D7
- #pragma once
- #include <string>
- #include <memory>
- #include <utility>
- #include <string_view>
- namespace meshpp::core
- {
- //-----------------------------------------------------------------------------
- // Declaration of BasicSharedString
- //-----------------------------------------------------------------------------
- template<typename CHAR_T>
- class BasicSharedString
- {
- public:
- using traits_type = typename std::basic_string_view<CHAR_T>::traits_type;
- using value_type = typename std::basic_string_view<CHAR_T>::value_type;
- using allocator_type = typename std::basic_string_view<CHAR_T>::allocator_type;
- using size_type = typename std::basic_string_view<CHAR_T>::size_type;
- using difference_type = typename std::basic_string_view<CHAR_T>::difference_type;
- using reference = typename std::basic_string_view<CHAR_T>::reference;
- using const_reference = typename std::basic_string_view<CHAR_T>::const_reference;
- using pointer = typename std::basic_string_view<CHAR_T>::pointer;
- using const_pointer = typename std::basic_string_view<CHAR_T>::const_pointer;
- using iterator = typename std::basic_string_view<CHAR_T>::iterator;
- using const_iterator = typename std::basic_string_view<CHAR_T>::const_iterator;
- using reverse_iterator = typename std::basic_string_view<CHAR_T>::reverse_iterator;
- using const_reverse_iterator = typename std::basic_string_view<CHAR_T>::const_reverse_iterator;
- static const size_type npos = std::basic_string<CHAR_T>::npos;
- using StringView_t = std::basic_string_view<value_type, traits_type>;
- BasicSharedString(void);
- BasicSharedString(BasicSharedString &&);
- BasicSharedString(BasicSharedString const&);
- BasicSharedString& operator=(BasicSharedString &&);
- BasicSharedString& operator=(BasicSharedString const&);
- template<typename ... ARGS_T>
- BasicSharedString(ARGS_T && ... args);
- const_reference at(size_type pos) const;
- const_reference operator[](size_type pos) const;
- const_reference front(void) const;
- const_reference back(void) const;
- const_pointer data(void) const;
- const_pointer c_str(void) const;
- operator StringView_t(void) const noexcept;
- const_iterator begin(void) const;
- const_iterator cbegin(void) const;
- const_iterator end(void) const;
- const_iterator cend(void) const;
- const_reverse_iterator rbegin(void) const;
- const_reverse_iterator crbegin(void) const;
- const_reverse_iterator rend(void) const;
- const_reverse_iterator crend(void) const;
- [[nodiscard]] bool empty(void) const;
- size_type size(void) const noexcept;
- size_type length(void) const noexcept;
- size_type max_size(void) const;
- size_type capacity(void) const;
- template<typename ... ARGS_T>
- int compare(ARGS_T && ... args) const;
- int compare(BasicSharedString const& other) const;
- template<typename ... ARGS_T>
- bool starts_with(ARGS_T && ... args) const;
- template<typename ... ARGS_T>
- bool ends_with(ARGS_T && ... args) const;
- BasicSharedString substr(size_type pos = 0, size_type count = npos) const;
- size_type copy(pointer dest, size_type count, size_type pos = 0) const;
- void swap(BasicSharedString& other) noexcept(noexcept(std::declval<BasicSharedString>().m_pStrData.swap(other.m_pStrData)));
- template<typename ... ARGS_T>
- bool find(ARGS_T && ... args) const;
- template<typename ... ARGS_T>
- bool rfind(ARGS_T && ... args) const;
- template<typename ... ARGS_T>
- bool find_first_of(ARGS_T && ... args) const;
- template<typename ... ARGS_T>
- bool find_first_not_of(ARGS_T && ... args) const;
- template<typename ... ARGS_T>
- bool find_last_of(ARGS_T && ... args) const;
- template<typename ... ARGS_T>
- bool find_last_not_of(ARGS_T && ... args) const;
- private:
- /*
- * For use by functions that aren't allowed to throw an exception or
- * have well specified values to be returned when certain errors happen.
- */
- static constexpr value_type nullchar = traits_type::to_char_type(0);
- static constexpr auto smc_exceptionText = "This Basic Shared String has no string.";
- size_t m_size;
- std::shared_ptr<const CHAR_T[]> m_pStrData;
- }; // class BasicSharedString
- /**
- * The default implementation of a BasicSharedString.
- */
- using SharedString = BasicSharedString<char>;
- template<typename CHAR_T>
- bool operator==(BasicSharedString<CHAR_T> const& lhs, BasicSharedString<CHAR_T> const& rhs);
- template<typename CHAR_T>
- bool operator!=(BasicSharedString<CHAR_T> const& lhs, BasicSharedString<CHAR_T> const& rhs);
- template<typename CHAR_T>
- bool operator<(BasicSharedString<CHAR_T> const& lhs, BasicSharedString<CHAR_T> const& rhs);
- template<typename CHAR_T>
- bool operator<=(BasicSharedString<CHAR_T> const& lhs, BasicSharedString<CHAR_T> const& rhs);
- template<typename CHAR_T>
- bool operator>(BasicSharedString<CHAR_T> const& lhs, BasicSharedString<CHAR_T> const& rhs);
- template<typename CHAR_T>
- bool operator>=(BasicSharedString<CHAR_T> const& lhs, BasicSharedString<CHAR_T> const& rhs);
- template<typename CHAR_T, typename OTHER_T>
- bool operator==(BasicSharedString<CHAR_T> const& lhs, OTHER_T const& rhs);
- template<typename CHAR_T, typename OTHER_T>
- bool operator!=(BasicSharedString<CHAR_T> const& lhs, OTHER_T const& rhs);
- template<typename CHAR_T, typename OTHER_T>
- bool operator<(BasicSharedString<CHAR_T> const& lhs, OTHER_T const& rhs);
- template<typename CHAR_T, typename OTHER_T>
- bool operator<=(BasicSharedString<CHAR_T> const& lhs, OTHER_T const& rhs);
- template<typename CHAR_T, typename OTHER_T>
- bool operator>(BasicSharedString<CHAR_T> const& lhs, OTHER_T const& rhs);
- template<typename CHAR_T, typename OTHER_T>
- bool operator>=(BasicSharedString<CHAR_T> const& lhs, OTHER_T const& rhs);
- template<typename OTHER_T, typename CHAR_T>
- bool operator==(OTHER_T const& lhs, BasicSharedString<CHAR_T> const& rhs);
- template<typename OTHER_T, typename CHAR_T>
- bool operator!=(OTHER_T const& lhs, BasicSharedString<CHAR_T> const& rhs);
- template<typename OTHER_T, typename CHAR_T>
- bool operator<(OTHER_T const& lhs, BasicSharedString<CHAR_T> const& rhs);
- template<typename OTHER_T, typename CHAR_T>
- bool operator<=(OTHER_T const& lhs, BasicSharedString<CHAR_T> const& rhs);
- template<typename OTHER_T, typename CHAR_T>
- bool operator>(OTHER_T const& lhs, BasicSharedString<CHAR_T> const& rhs);
- template<typename OTHER_T, typename CHAR_T>
- bool operator>=(OTHER_T const& lhs, BasicSharedString<CHAR_T> const& rhs);
- //-----------------------------------------------------------------------------
- // Implementation of BasicSharedString
- //-----------------------------------------------------------------------------
- template<typename CHAR_T>
- BasicSharedString<CHAR_T>::BasicSharedString(void) = default;
- template<typename CHAR_T>
- BasicSharedString<CHAR_T>::BasicSharedString(BasicSharedString &&) = default;
- template<typename CHAR_T>
- BasicSharedString<CHAR_T>::BasicSharedString(BasicSharedString const&) = default;
- template<typename CHAR_T>
- auto BasicSharedString<CHAR_T>::operator=(BasicSharedString &&) -> BasicSharedString& = default;
- template<typename CHAR_T>
- auto BasicSharedString<CHAR_T>::operator=(BasicSharedString const&) -> BasicSharedString& = default;
- template<typename CHAR_T>
- template<typename ... ARGS_T>
- BasicSharedString<CHAR_T>::BasicSharedString(ARGS_T && ... args)
- : m_pStrData(std::make_shared<std::basic_string<CHAR_T>>(std::forward<ARGS_T>(args)...))
- { }
- template<typename CHAR_T>
- auto BasicSharedString<CHAR_T>::at(size_type pos) const -> const_reference
- {
- if( ! m_pStrData)
- {
- throw std::out_of_range(smc_exceptionText);
- }
- return std::string_view(m_pStrData.get(), m_size)->at(pos);
- }
- template<typename CHAR_T>
- auto BasicSharedString<CHAR_T>::operator[]( size_type pos ) const -> const_reference
- {
- if( ! m_pStrData)
- {
- throw std::out_of_range(smc_exceptionText);
- }
- return std::string_view(m_pStrData.get(), m_size)->operator[](pos);
- }
- template<typename CHAR_T>
- auto BasicSharedString<CHAR_T>::front(void) const -> const_reference
- {
- if( ! m_pStrData)
- {
- throw std::out_of_range(smc_exceptionText);
- }
- return std::string_view(m_pStrData.get(), m_size)->front();
- }
- template<typename CHAR_T>
- auto BasicSharedString<CHAR_T>::back(void) const -> const_reference
- {
- if( ! m_pStrData)
- {
- throw std::out_of_range(smc_exceptionText);
- }
- return std::string_view(m_pStrData.get(), m_size)->back();
- }
- template<typename CHAR_T>
- auto BasicSharedString<CHAR_T>::data(void) const -> const_pointer
- {
- if( ! m_pStrData)
- {
- // https://en.cppreference.com/w/cpp/string/basic_string/data
- // If empty() returns true, the pointer points to a single null character.
- return &nullchar;
- }
- return std::string_view(m_pStrData.get(), m_size)->data();
- }
- template<typename CHAR_T>
- auto BasicSharedString<CHAR_T>::c_str(void) const -> const_pointer
- {
- // https://en.cppreference.com/w/cpp/string/basic_string/c_str
- // c_str() and data() perform the same function.
- return data();
- }
- template<typename CHAR_T>
- BasicSharedString<CHAR_T>::operator StringView_t() const noexcept
- {
- if( ! m_pStrData)
- {
- return StringView_t();
- }
- return std::string_view(m_pStrData.get(), m_size)->operator StringView_t();
- }
- template<typename CHAR_T>
- auto BasicSharedString<CHAR_T>::begin(void) const -> const_iterator
- {
- if( ! m_pStrData)
- {
- return const_iterator(&nullchar);
- }
- return std::string_view(m_pStrData.get(), m_size)->begin();
- }
- template<typename CHAR_T>
- auto BasicSharedString<CHAR_T>::cbegin(void) const -> const_iterator
- {
- if( ! m_pStrData)
- {
- return const_iterator(&nullchar);
- }
- return std::string_view(m_pStrData.get(), m_size)->cbegin();
- }
- template<typename CHAR_T>
- auto BasicSharedString<CHAR_T>::end(void) const -> const_iterator
- {
- if( ! m_pStrData)
- {
- return const_iterator(&nullchar);
- }
- return std::string_view(m_pStrData.get(), m_size)->end();
- }
- template<typename CHAR_T>
- auto BasicSharedString<CHAR_T>::cend(void) const -> const_iterator
- {
- if( ! m_pStrData)
- {
- return const_iterator(&nullchar);
- }
- return std::string_view(m_pStrData.get(), m_size)->cend();
- }
- template<typename CHAR_T>
- auto BasicSharedString<CHAR_T>::rbegin(void) const -> const_reverse_iterator
- {
- if( ! m_pStrData)
- {
- return const_reverse_iterator(&nullchar);
- }
- return std::string_view(m_pStrData.get(), m_size)->rbegin();
- }
- template<typename CHAR_T>
- auto BasicSharedString<CHAR_T>::crbegin(void) const -> const_reverse_iterator
- {
- if( ! m_pStrData)
- {
- return const_reverse_iterator(&nullchar);
- }
- return std::string_view(m_pStrData.get(), m_size)->crbegin();
- }
- template<typename CHAR_T>
- auto BasicSharedString<CHAR_T>::rend(void) const -> const_reverse_iterator
- {
- if( ! m_pStrData)
- {
- return const_reverse_iterator(&nullchar);
- }
- return std::string_view(m_pStrData.get(), m_size)->rend();
- }
- template<typename CHAR_T>
- auto BasicSharedString<CHAR_T>::crend(void) const -> const_reverse_iterator
- {
- if( ! m_pStrData)
- {
- return const_reverse_iterator(&nullchar);
- }
- return std::string_view(m_pStrData.get(), m_size)->crend();
- }
- template<typename CHAR_T>
- bool BasicSharedString<CHAR_T>::empty(void) const
- {
- return ! m_pStrData || m_size == 0;
- }
- template<typename CHAR_T>
- auto BasicSharedString<CHAR_T>::size(void) const noexcept -> size_type
- {
- if( ! m_pStrData)
- {
- return 0;
- }
- return m_size;
- }
- template<typename CHAR_T>
- auto BasicSharedString<CHAR_T>::length(void) const noexcept -> size_type
- {
- if( ! m_pStrData)
- {
- return 0;
- }
- return m_size;
- }
- template<typename CHAR_T>
- auto BasicSharedString<CHAR_T>::max_size(void) const -> size_type
- {
- if( ! m_pStrData)
- {
- return 0;
- }
- return m_size;
- }
- template<typename CHAR_T>
- auto BasicSharedString<CHAR_T>::capacity(void) const -> size_type
- {
- if( ! m_pStrData)
- {
- return 0;
- }
- return m_size;
- }
- template<typename CHAR_T>
- template<typename ... ARGS_T>
- int BasicSharedString<CHAR_T>::compare(ARGS_T && ... args) const
- {
- if( ! m_pStrData)
- {
- throw std::out_of_range(smc_exceptionText);
- }
- return std::string_view(m_pStrData.get(), m_size)->compare(std::forward<ARGS_T>(args)...);
- }
- template<typename CHAR_T>
- int BasicSharedString<CHAR_T>::compare(BasicSharedString const& other) const
- {
- if(size() != other.size())
- {
- return size() - other.size();
- }
- else
- {
- return traits_type::compare(data(), other.data(), size());
- }
- }
- template<typename CHAR_T>
- template<typename ... ARGS_T>
- bool BasicSharedString<CHAR_T>::starts_with(ARGS_T && ... args) const
- {
- if( ! m_pStrData)
- {
- throw std::out_of_range(smc_exceptionText);
- }
- return std::string_view(m_pStrData.get(), m_size)->starts_with(std::forward<ARGS_T>(args)...);
- }
- template<typename CHAR_T>
- template<typename ... ARGS_T>
- bool BasicSharedString<CHAR_T>::ends_with(ARGS_T && ... args) const
- {
- if( ! m_pStrData)
- {
- throw std::out_of_range(smc_exceptionText);
- }
- return std::string_view(m_pStrData.get(), m_size)->ends_with(std::forward<ARGS_T>(args)...);
- }
- template<typename CHAR_T>
- auto BasicSharedString<CHAR_T>::substr(const size_type pos, const size_type count) const -> BasicSharedString
- {
- if( ! m_pStrData)
- {
- return BasicSharedString();
- }
- // TODO: Probably not worth supporting
- //return BasicSharedString(m_pStrData->substr(pos, count));
- }
- template<typename CHAR_T>
- auto BasicSharedString<CHAR_T>::copy(pointer dest, size_type count, size_type pos) const -> size_type
- {
- if( ! m_pStrData)
- {
- throw std::out_of_range(smc_exceptionText);
- }
- return std::string_view(m_pStrData.get(), m_size)->copy(dest, count, pos);
- }
- template<typename CHAR_T>
- void BasicSharedString<CHAR_T>::swap(BasicSharedString& other) noexcept(noexcept(std::declval<BasicSharedString>().m_pStrData.swap(other.m_pStrData)))
- {
- if(m_pStrData)
- {
- m_pStrData.swap(other.m_pStrData);
- }
- }
- template<typename CHAR_T>
- template<typename ... ARGS_T>
- bool BasicSharedString<CHAR_T>::find(ARGS_T && ... args) const
- {
- if( ! m_pStrData)
- {
- throw std::out_of_range(smc_exceptionText);
- }
- return std::string_view(m_pStrData.get(), m_size)->find(std::forward<ARGS_T>(args)...);
- }
- template<typename CHAR_T>
- template<typename ... ARGS_T>
- bool BasicSharedString<CHAR_T>::rfind(ARGS_T && ... args) const
- {
- if( ! m_pStrData)
- {
- throw std::out_of_range(smc_exceptionText);
- }
- return std::string_view(m_pStrData.get(), m_size)->rfind(std::forward<ARGS_T>(args)...);
- }
- template<typename CHAR_T>
- template<typename ... ARGS_T>
- bool BasicSharedString<CHAR_T>::find_first_of(ARGS_T && ... args) const
- {
- if( ! m_pStrData)
- {
- throw std::out_of_range(smc_exceptionText);
- }
- return std::string_view(m_pStrData.get(), m_size)->find_first_of(std::forward<ARGS_T>(args)...);
- }
- template<typename CHAR_T>
- template<typename ... ARGS_T>
- bool BasicSharedString<CHAR_T>::find_first_not_of(ARGS_T && ... args) const
- {
- if( ! m_pStrData)
- {
- throw std::out_of_range(smc_exceptionText);
- }
- return std::string_view(m_pStrData.get(), m_size)->find_first_not_of(std::forward<ARGS_T>(args)...);
- }
- template<typename CHAR_T>
- template<typename ... ARGS_T>
- bool BasicSharedString<CHAR_T>::find_last_of(ARGS_T && ... args) const
- {
- if( ! m_pStrData)
- {
- throw std::out_of_range(smc_exceptionText);
- }
- return std::string_view(m_pStrData.get(), m_size)->find_last_of(std::forward<ARGS_T>(args)...);
- }
- template<typename CHAR_T>
- template<typename ... ARGS_T>
- bool BasicSharedString<CHAR_T>::find_last_not_of(ARGS_T && ... args) const
- {
- if( ! m_pStrData)
- {
- throw std::out_of_range(smc_exceptionText);
- }
- return std::string_view(m_pStrData.get(), m_size)->find_last_not_of(std::forward<ARGS_T>(args)...);
- }
- template<typename CHAR_T>
- bool operator==(BasicSharedString<CHAR_T> const& lhs, BasicSharedString<CHAR_T> const& rhs)
- {
- return 0 == lhs.compare(rhs);
- }
- template<typename CHAR_T>
- bool operator!=(BasicSharedString<CHAR_T> const& lhs, BasicSharedString<CHAR_T> const& rhs)
- {
- return 0 != lhs.compare(rhs);
- }
- template<typename CHAR_T>
- bool operator<(BasicSharedString<CHAR_T> const& lhs, BasicSharedString<CHAR_T> const& rhs)
- {
- return 0 < lhs.compare(rhs);
- }
- template<typename CHAR_T>
- bool operator<=(BasicSharedString<CHAR_T> const& lhs, BasicSharedString<CHAR_T> const& rhs)
- {
- return 0 <= lhs.compare(rhs);
- }
- template<typename CHAR_T>
- bool operator>(BasicSharedString<CHAR_T> const& lhs, BasicSharedString<CHAR_T> const& rhs)
- {
- return 0 > lhs.compare(rhs);
- }
- template<typename CHAR_T>
- bool operator>=(BasicSharedString<CHAR_T> const& lhs, BasicSharedString<CHAR_T> const& rhs)
- {
- return 0 >= lhs.compare(rhs);
- }
- template<typename CHAR_T, typename OTHER_T>
- bool operator==(BasicSharedString<CHAR_T> const& lhs, OTHER_T const& rhs)
- {
- return 0 == lhs.compare(rhs);
- }
- template<typename CHAR_T, typename OTHER_T>
- bool operator!=(BasicSharedString<CHAR_T> const& lhs, OTHER_T const& rhs)
- {
- return 0 != lhs.compare(rhs);
- }
- template<typename CHAR_T, typename OTHER_T>
- bool operator<(BasicSharedString<CHAR_T> const& lhs, OTHER_T const& rhs)
- {
- return 0 < lhs.compare(rhs);
- }
- template<typename CHAR_T, typename OTHER_T>
- bool operator<=(BasicSharedString<CHAR_T> const& lhs, OTHER_T const& rhs)
- {
- return 0 <= lhs.compare(rhs);
- }
- template<typename CHAR_T, typename OTHER_T>
- bool operator>(BasicSharedString<CHAR_T> const& lhs, OTHER_T const& rhs)
- {
- return 0 > lhs.compare(rhs);
- }
- template<typename CHAR_T, typename OTHER_T>
- bool operator>=(BasicSharedString<CHAR_T> const& lhs, OTHER_T const& rhs)
- {
- return 0 >= lhs.compare(rhs);
- }
- template<typename OTHER_T, typename CHAR_T>
- bool operator==(OTHER_T const& lhs, BasicSharedString<CHAR_T> const& rhs)
- {
- return 0 == lhs.compare(rhs);
- }
- template<typename OTHER_T, typename CHAR_T>
- bool operator!=(OTHER_T const& lhs, BasicSharedString<CHAR_T> const& rhs)
- {
- return 0 != lhs.compare(rhs);
- }
- template<typename OTHER_T, typename CHAR_T>
- bool operator<(OTHER_T const& lhs, BasicSharedString<CHAR_T> const& rhs)
- {
- return 0 > lhs.compare(rhs);
- }
- template<typename OTHER_T, typename CHAR_T>
- bool operator<=(OTHER_T const& lhs, BasicSharedString<CHAR_T> const& rhs)
- {
- return 0 >= lhs.compare(rhs);
- }
- template<typename OTHER_T, typename CHAR_T>
- bool operator>(OTHER_T const& lhs, BasicSharedString<CHAR_T> const& rhs)
- {
- return 0 < lhs.compare(rhs);
- }
- template<typename OTHER_T, typename CHAR_T>
- bool operator>=(OTHER_T const& lhs, BasicSharedString<CHAR_T> const& rhs)
- {
- return 0 <= lhs.compare(rhs);
- }
- } // namespace meshpp::core
- #endif // MESHPP_CORE_SHARED_STRING_H_2A0F6174_39EA_40FE_B526_9293C36939D7
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement