melder_strvec.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #ifndef _melder_strvec_h_
  2. #define _melder_strvec_h_
  3. /* melder_strvec.h
  4. *
  5. * Copyright (C) 1992-2018 Paul Boersma
  6. *
  7. * This code is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * This code is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. * See the GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this work. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. template <typename T>
  21. class _stringvector {
  22. public:
  23. T** at = nullptr;
  24. integer size = 0;
  25. _stringvector () { }
  26. _stringvector (T** givenAt, integer givenSize): at (givenAt), size (givenSize) { }
  27. T* & operator[] (integer i) {
  28. return our at [i];
  29. }
  30. };
  31. typedef _stringvector <char32> string32vector;
  32. typedef _stringvector <char> string8vector;
  33. template <typename T>
  34. class _conststringvector {
  35. public:
  36. const T* const * at = nullptr;
  37. integer size = 0;
  38. _conststringvector () { }
  39. _conststringvector (const T* const * givenAt, integer givenSize): at (givenAt), size (givenSize) { }
  40. _conststringvector (_stringvector<T> other): at (other.at), size (other.size) { }
  41. const T* const & operator[] (integer i) {
  42. return our at [i];
  43. }
  44. };
  45. typedef _conststringvector <char32> conststring32vector;
  46. typedef _conststringvector <char> conststring8vector;
  47. template <class T>
  48. class _autostringvector {
  49. _autostring <T> * _ptr;
  50. public:
  51. integer size;
  52. _autostringvector () {
  53. our _ptr = nullptr;
  54. our size = 0;
  55. }
  56. _autostringvector<T> (integer initialSize) {
  57. our _ptr = NUMvector <_autostring <T>> (1, initialSize, true);
  58. our size = initialSize;
  59. }
  60. _autostringvector (const _autostringvector &) = delete;
  61. _autostringvector (_autostringvector&& other) {
  62. our _ptr = other. _ptr;
  63. our size = other. size;
  64. other. _ptr = nullptr;
  65. other. size = 0;
  66. }
  67. _autostringvector& operator= (const _autostringvector &) = delete; // disable copy assignment
  68. _autostringvector& operator= (_autostringvector&& other) noexcept { // enable move assignment
  69. if (& other != this) {
  70. our reset ();
  71. our _ptr = other. _ptr;
  72. our size = other. size;
  73. other. _ptr = nullptr;
  74. other. size = 0;
  75. }
  76. return *this;
  77. }
  78. ~ _autostringvector<T> () {
  79. our reset ();
  80. }
  81. explicit operator bool () const { return !! our _ptr; }
  82. _autostring <T> & operator[] (integer i) {
  83. return our _ptr [i];
  84. }
  85. _stringvector<T> get () const {
  86. return _stringvector<T> { (T**) our _ptr, our size };
  87. }
  88. T** peek2 () const { // can be assigned to a [const] mutablestring32* and to a const conststring32*, but not to a conststring32*
  89. return (T**) our _ptr;
  90. }
  91. void reset () {
  92. if (our _ptr) {
  93. for (integer i = 1; i <= our size; i ++)
  94. our _ptr [i]. reset ();
  95. NUMvector_free (our _ptr, 1);
  96. our _ptr = nullptr;
  97. our size = 0;
  98. }
  99. }
  100. void copyElementsFrom (_conststringvector<T> other) {
  101. Melder_assert (other. size == our size);
  102. for (integer i = 1; i <= our size; i ++)
  103. our _ptr [i] = Melder_dup (other [i]);
  104. }
  105. void copyElementsFrom_upTo (_conststringvector<T> other, integer to) {
  106. Melder_assert (to <= other. size && to <= our size);
  107. for (integer i = 1; i <= to; i ++)
  108. our _ptr [i] = Melder_dup (other [i]);
  109. }
  110. };
  111. typedef _autostringvector <char32> autostring32vector;
  112. typedef _autostringvector <char> autostring8vector;
  113. using STRVEC = _stringvector <char32>;
  114. using constSTRVEC = _conststringvector <char32>;
  115. using autoSTRVEC = _autostringvector <char32>;
  116. inline static autoSTRVEC STRVECclone (constSTRVEC strvec) {
  117. autoSTRVEC result (strvec.size);
  118. for (integer i = 1; i <= result.size; i ++)
  119. result [i] = Melder_dup (strvec [i]);
  120. return result;
  121. }
  122. /* End of file melder_strvec.h */
  123. #endif