readonly_cstring.h.xml 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <chapter xml:id="readonly_cstring.h">
  2. <title><tt>__vic/readonly_cstring.h</tt></title>
  3. <chapter xml:id="readonly_cstring">
  4. <title><tt>readonly_cstring</tt></title>
  5. <code-block lang="C++"><![CDATA[
  6. class readonly_cstring
  7. {
  8. public:
  9. readonly_cstring();
  10. readonly_cstring(const char *str);
  11. readonly_cstring(const char *begin, const char *end);
  12. readonly_cstring(const char *chars, size_t n);
  13. readonly_cstring(const readonly_cstring &str);
  14. ~readonly_cstring() noexcept;
  15. // BEGIN C++11
  16. readonly_cstring(readonly_cstring &&str) noexcept;
  17. readonly_cstring &operator=(readonly_cstring &&str) noexcept;
  18. // END C++11
  19. readonly_cstring &operator=(const char *str);
  20. readonly_cstring &operator=(const readonly_cstring &str);
  21. readonly_cstring &assign(const char *begin, const char *end);
  22. readonly_cstring &assign(const char *chars, size_t n);
  23. bool empty() const;
  24. const char *c_str() const;
  25. operator const char*() const;
  26. char *reserve(size_t n);
  27. void swap(readonly_cstring &str) noexcept;
  28. };
  29. int compare(const readonly_cstring &s1, const readonly_cstring &s2);
  30. int compare(const readonly_cstring &s1, const char *s2);
  31. int compare(const char *s1, const readonly_cstring &s2);
  32. bool operator==(const readonly_cstring &s1, const readonly_cstring &s2);
  33. bool operator!=(const readonly_cstring &s1, const readonly_cstring &s2);
  34. bool operator<(const readonly_cstring &s1, const readonly_cstring &s2);
  35. bool operator>(const readonly_cstring &s1, const readonly_cstring &s2);
  36. bool operator<=(const readonly_cstring &s1, const readonly_cstring &s2);
  37. bool operator>=(const readonly_cstring &s1, const readonly_cstring &s2);
  38. bool operator==(const readonly_cstring &s1, const char *s2);
  39. bool operator!=(const readonly_cstring &s1, const char *s2);
  40. bool operator<(const readonly_cstring &s1, const char *s2);
  41. bool operator>(const readonly_cstring &s1, const char *s2);
  42. bool operator<=(const readonly_cstring &s1, const char *s2);
  43. bool operator>=(const readonly_cstring &s1, const char *s2);
  44. bool operator==(const char *s1, const readonly_cstring &s2);
  45. bool operator!=(const char *s1, const readonly_cstring &s2);
  46. bool operator<(const char *s1, const readonly_cstring &s2);
  47. bool operator>(const char *s1, const readonly_cstring &s2);
  48. bool operator<=(const char *s1, const readonly_cstring &s2);
  49. bool operator>=(const char *s1, const readonly_cstring &s2);
  50. void swap(readonly_cstring &s1, readonly_cstring &s2) noexcept;
  51. ]]></code-block>
  52. <p>The simple non-mutable null-terminated string class with automatic memory
  53. management. Has simple and predictable structure that can be useful when
  54. ABI compatibility/stability is required or when usage of <tt>std::string</tt>
  55. is objectionable for some reason. The functionality provided by the class is
  56. also minimal. It provides string copying and storing and read access to it.
  57. One cannot modify string parts - only replace the whole value.</p>
  58. <p>If one need to store a string value in a class, usage of this class may be a
  59. good choice. It's easier-to-use, more clear and safer than array of chars
  60. (<tt>char[]</tt>) and can be more efficient than <tt>std::string</tt>,
  61. though, of course, less universal. If modifications of the string parts are
  62. expected, usage of another string class should be considered, for instance,
  63. <tt>__vic::string_buffer</tt>. Class <tt>readonly_cstring</tt> is not designed
  64. for such purposes.</p>
  65. <section><title>Guarantees provided by the class design</title>
  66. <list style="bulleted">
  67. <item>Null pointer value is treated as an empty string.</item>
  68. <item>Cast to C-string (<tt>const char *</tt>) always returns valid
  69. pointer, never <tt>nullptr</tt>.</item>
  70. <item>For each string value, exact amount of memory is allocated. No extra
  71. memory reserved.</item>
  72. <item>Class contains single data member - pointer to the holded string,
  73. so the total object's size is likely one pointer.</item>
  74. </list>
  75. </section>
  76. <section><title>Class members</title>
  77. <synopsis>
  78. <prototype>readonly_cstring()</prototype>
  79. <p>Creates an empty string.</p>
  80. <postcondition><tt>empty() == true</tt></postcondition>
  81. </synopsis>
  82. <synopsis>
  83. <prototype>readonly_cstring(const char *str)</prototype>
  84. <prototype>readonly_cstring(const readonly_cstring &amp;str)</prototype>
  85. <p>Creates a copy of <tt>str</tt>.</p>
  86. </synopsis>
  87. <synopsis>
  88. <prototype>readonly_cstring(const char *begin, const char *end)</prototype>
  89. <prototype>readonly_cstring(const char *chars, size_t n)</prototype>
  90. <p>Creates a string from characters range.</p>
  91. </synopsis>
  92. <synopsis>
  93. <prototype>readonly_cstring &amp;operator=(const char *str)</prototype>
  94. <prototype>readonly_cstring &amp;operator=(const readonly_cstring &amp;str)</prototype>
  95. <p>Assigns <tt>str</tt>.</p>
  96. </synopsis>
  97. <synopsis>
  98. <prototype>readonly_cstring(readonly_cstring &amp;&amp;str) noexcept <sign>C++11</sign></prototype>
  99. <prototype>readonly_cstring &amp;operator=(readonly_cstring &amp;&amp;str) noexcept <sign>C++11</sign></prototype>
  100. <p>Move operations for C++11 mode.</p>
  101. </synopsis>
  102. <synopsis>
  103. <prototype>readonly_cstring &amp;assign(const char *begin, const char *end)</prototype>
  104. <prototype>readonly_cstring &amp;assign(const char *chars, size_t n)</prototype>
  105. <p>Assigns the string constructed from characters range.</p>
  106. </synopsis>
  107. <synopsis>
  108. <prototype>bool empty() const</prototype>
  109. <p>Returns <tt>true</tt> if string is empty.</p>
  110. </synopsis>
  111. <synopsis>
  112. <prototype>const char *c_str() const</prototype>
  113. <prototype>operator const char*() const</prototype>
  114. <p>Returns a pointer to the stored string. The pointer is never null.</p>
  115. </synopsis>
  116. <synopsis>
  117. <prototype>char *reserve(size_t n)</prototype>
  118. <p>Allocates internal buffer for <tt>n</tt> chars and returns the pointer to
  119. it. Can be useful in conjunction with functions like <tt>std::sprintf()</tt>.</p>
  120. <note>Try to avoid this unsafe function!</note>
  121. </synopsis>
  122. <synopsis>
  123. <prototype>void swap(readonly_cstring &amp;str) noexcept</prototype>
  124. <p>Swaps the value with <tt>str</tt>.</p>
  125. </synopsis>
  126. </section>
  127. <section><title>Free functions</title>
  128. <synopsis>
  129. <prototype>int compare(const readonly_cstring &amp;s1, const readonly_cstring &amp;s2)</prototype>
  130. <prototype>int compare(const readonly_cstring &amp;s1, const char *s2)</prototype>
  131. <prototype>int compare(const char *s1, const readonly_cstring &amp;s2)</prototype>
  132. <p>Compares two strings as <tt>std::strcmp</tt> does.</p>
  133. </synopsis>
  134. <synopsis>
  135. <prototype>bool operator==(const readonly_cstring &amp;s1, const readonly_cstring &amp;s2)</prototype>
  136. <prototype>...</prototype>
  137. <prototype>bool operator>=(const char *s1, const readonly_cstring &amp;s2)</prototype>
  138. <p>Full set of comparators for <tt>readonly_cstring</tt> and <tt>const char *</tt>
  139. in all combinations.</p>
  140. </synopsis>
  141. <synopsis>
  142. <prototype>void swap(readonly_cstring &amp;s1, readonly_cstring &amp;s2) noexcept</prototype>
  143. <p>Specialization of the standard algorithm.</p>
  144. </synopsis>
  145. </section>
  146. <section><title>Example</title>
  147. <code-block lang="C++">
  148. class C
  149. {
  150. __vic::readonly_cstring st;
  151. public:
  152. explicit C(const char *str) : st(str) {}
  153. const char *get_str() const { return st; }
  154. };
  155. </code-block>
  156. </section>
  157. </chapter>
  158. </chapter>