JASL  [1.2.0]-2018-09-11
jasl_static_string.hpp
Go to the documentation of this file.
1 // JASL: For more information see https://github.com/matepek/jasl
2 //
3 // Copyright (c) 2018 Mate Pek
4 //
5 // This code is licensed under the MIT License (MIT).
6 
9 #pragma once
10 
11 #include <string>
12 
14 #include "jasl/jasl_internal/jasl_feature_test_macro.hpp"
15 #include "jasl/jasl_internal/jasl_string_view_bridge.hpp"
17 
18 namespace jasl {
19 
35 template <typename CharT, class Traits = std::char_traits<CharT>>
37  : public inner::string_view_bridge<basic_string_view<CharT, Traits>> {
38  public:
41  typedef typename bridge_type::bridge_to_type base_type;
42 
43  private:
44  constexpr basic_static_string(const bridge_type& other) noexcept(
45  std::is_nothrow_constructible<bridge_type, const bridge_type&>::value)
46  : bridge_type(other) {}
47 
48  public:
49  JASL_CONSTEXPR_CXX14 basic_static_string() noexcept(
50  std::is_nothrow_constructible<bridge_type>::value)
51  : bridge_type() {}
52 
53  template <size_t N>
54  constexpr basic_static_string(const CharT (&str)[N]) noexcept(
55  std::is_nothrow_constructible<bridge_type, const CharT*, size_t>::value)
56  : bridge_type(str, str[N - 1] == 0 ? N - 1 : N) {}
57 
58  constexpr basic_static_string(const basic_static_string& other) noexcept(
59  std::is_nothrow_constructible<bridge_type, const bridge_type&>::value)
60  : bridge_type(static_cast<const bridge_type&>(other)) {}
61 
62  ~basic_static_string() noexcept(
63  std::is_nothrow_destructible<bridge_type>::value) = default;
64 
65  template <size_t N>
66  JASL_CONSTEXPR_CXX14 basic_static_string& operator=(
67  const CharT (&str)[N]) noexcept(bridge_type::is_nothrow_settable) {
68  bridge_type::set(str, str[N - 1] == 0 ? N - 1 : N);
69  return *this;
70  }
71 
72  JASL_CONSTEXPR_CXX14 basic_static_string&
73  operator=(const basic_static_string& other) noexcept(
74  std::is_nothrow_assignable<bridge_type, const bridge_type&>::value) {
75  bridge_type::operator=(static_cast<const bridge_type&>(other));
76  return *this;
77  }
78 
79 #if defined(JASL_SUPPORT_JASL_TO_STD)
80 # if defined(JASL_cpp_lib_string_view)
81  operator std::basic_string_view<CharT, Traits>() const noexcept(
82  std::is_nothrow_constructible<std::basic_string_view<CharT, Traits>,
83  const CharT*,
84  size_t>::value) {
85  return std::basic_string_view<CharT, Traits>(bridge_type::data(),
86  bridge_type::size());
87  }
88 # else
89  template <typename AllocatorT>
90  operator std::basic_string<CharT, Traits, AllocatorT>() const
91  noexcept(std::is_nothrow_constructible<
92  std::basic_string<CharT, Traits, AllocatorT>,
93  const CharT*,
94  size_t>::value) {
95  return std::basic_string<CharT, Traits, AllocatorT>(bridge_type::data(),
96  bridge_type::size());
97  }
98 # endif
99 #endif
100 
101  JASL_CONSTEXPR_CXX14 void swap(basic_static_string& other) noexcept(
102  JASL_is_nothrow_swappable_value(bridge_type)) {
103  bridge_type::swap(other);
104  }
105 
106  constexpr basic_static_string substr(
107  typename bridge_type::size_type pos,
108  typename bridge_type::size_type count = bridge_type::npos) const {
109  return basic_static_string(bridge_type::substr(pos, count));
110  }
111 };
112 
113 template <typename CharT, typename Traits>
114 void swap(
116  basic_static_string<CharT, Traits>& rhs) noexcept(noexcept(lhs.swap(rhs))) {
117  lhs.swap(rhs);
118 }
119 
120 typedef basic_static_string<char> static_string;
121 typedef basic_static_string<wchar_t> static_wstring;
122 typedef basic_static_string<char16_t> static_u16string;
123 typedef basic_static_string<char32_t> static_u32string;
124 
125 } // namespace jasl
Definition: jasl_static_string.hpp:18
Definition: jasl_string_view_bridge.hpp:17
Definition: jasl_static_string.hpp:36
Definition: jasl_string_view.hpp:24