char_string.hpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /**************************************************************************/
  2. /* char_string.hpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef GODOT_CHAR_STRING_HPP
  31. #define GODOT_CHAR_STRING_HPP
  32. #include <godot_cpp/templates/cowdata.hpp>
  33. #include <cstddef>
  34. #include <cstdint>
  35. namespace godot {
  36. template <class T>
  37. class CharStringT;
  38. template <class T>
  39. class CharProxy {
  40. template <class TS>
  41. friend class CharStringT;
  42. const int64_t _index;
  43. CowData<T> &_cowdata;
  44. static inline const T _null = 0;
  45. _FORCE_INLINE_ CharProxy(const int64_t &p_index, CowData<T> &p_cowdata) :
  46. _index(p_index),
  47. _cowdata(p_cowdata) {}
  48. public:
  49. _FORCE_INLINE_ CharProxy(const CharProxy<T> &p_other) :
  50. _index(p_other._index),
  51. _cowdata(p_other._cowdata) {}
  52. _FORCE_INLINE_ operator T() const {
  53. if (unlikely(_index == _cowdata.size())) {
  54. return _null;
  55. }
  56. return _cowdata.get(_index);
  57. }
  58. _FORCE_INLINE_ const T *operator&() const {
  59. return _cowdata.ptr() + _index;
  60. }
  61. _FORCE_INLINE_ void operator=(const T &p_other) const {
  62. _cowdata.set(_index, p_other);
  63. }
  64. _FORCE_INLINE_ void operator=(const CharProxy<T> &p_other) const {
  65. _cowdata.set(_index, p_other.operator T());
  66. }
  67. };
  68. template <class T>
  69. class CharStringT {
  70. friend class String;
  71. CowData<T> _cowdata;
  72. static inline const T _null = 0;
  73. public:
  74. _FORCE_INLINE_ T *ptrw() { return _cowdata.ptrw(); }
  75. _FORCE_INLINE_ const T *ptr() const { return _cowdata.ptr(); }
  76. _FORCE_INLINE_ int64_t size() const { return _cowdata.size(); }
  77. Error resize(int64_t p_size) { return _cowdata.resize(p_size); }
  78. _FORCE_INLINE_ T get(int64_t p_index) const { return _cowdata.get(p_index); }
  79. _FORCE_INLINE_ void set(int64_t p_index, const T &p_elem) { _cowdata.set(p_index, p_elem); }
  80. _FORCE_INLINE_ const T &operator[](int64_t p_index) const {
  81. if (unlikely(p_index == _cowdata.size())) {
  82. return _null;
  83. }
  84. return _cowdata.get(p_index);
  85. }
  86. _FORCE_INLINE_ CharProxy<T> operator[](int64_t p_index) { return CharProxy<T>(p_index, _cowdata); }
  87. _FORCE_INLINE_ CharStringT() {}
  88. _FORCE_INLINE_ CharStringT(const CharStringT<T> &p_str) { _cowdata._ref(p_str._cowdata); }
  89. _FORCE_INLINE_ void operator=(const CharStringT<T> &p_str) { _cowdata._ref(p_str._cowdata); }
  90. _FORCE_INLINE_ CharStringT(const T *p_cstr) { copy_from(p_cstr); }
  91. void operator=(const T *p_cstr);
  92. bool operator<(const CharStringT<T> &p_right) const;
  93. CharStringT<T> &operator+=(T p_char);
  94. int64_t length() const { return size() ? size() - 1 : 0; }
  95. const T *get_data() const;
  96. operator const T *() const { return get_data(); };
  97. protected:
  98. void copy_from(const T *p_cstr);
  99. };
  100. template <>
  101. const char *CharStringT<char>::get_data() const;
  102. template <>
  103. const char16_t *CharStringT<char16_t>::get_data() const;
  104. template <>
  105. const char32_t *CharStringT<char32_t>::get_data() const;
  106. template <>
  107. const wchar_t *CharStringT<wchar_t>::get_data() const;
  108. typedef CharStringT<char> CharString;
  109. typedef CharStringT<char16_t> Char16String;
  110. typedef CharStringT<char32_t> Char32String;
  111. typedef CharStringT<wchar_t> CharWideString;
  112. } // namespace godot
  113. #endif // GODOT_CHAR_STRING_HPP