readonly_cstring.h.xml 7.2 KB

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