readers.xml 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <chapter xml:id="readers">
  2. <title><tt>__vic/readers/</tt></title>
  3. <chapter xml:id="reader-concept">
  4. <title><tt>Reader</tt> concept</title>
  5. <code-block lang="C++"><![CDATA[
  6. template<class ElementT>
  7. class ]]><nt>Reader</nt><![CDATA[
  8. {
  9. public:
  10. Reader(Reader &&o); or Reader(const Reader &o);
  11. bool read(ElementT &v); // throws on errors
  12. };
  13. ]]></code-block>
  14. <p>A concept used by the library algorithms to read logical sequence of elements
  15. of type <tt>ElementT</tt> element by element. It is a generalization and rework
  16. of the <tt>InputIterator</tt> concept. In particular it handles streams,
  17. NULL-terminated strings and other sequences where <tt>end</tt>-iterator is
  18. meaningless or expensive to obtain better. At the same time, a traditional
  19. [<tt>begin</tt>,<tt>end</tt>) pair of iterators is just a particular case and
  20. fully and well handled by <tt>__vic::iterator_reader</tt> adapter.</p>
  21. <p>When the particular class meets the requirements of this concept for some
  22. <tt>ElementT = T</tt> it is said <b>to model</b> the <tt>Reader&lt;T></tt>
  23. concept.</p>
  24. <p>Each instance of the class has to be move- or copy-constructible.</p>
  25. <section><title>Class members</title>
  26. <synopsis>
  27. <prototype>bool read(ElementT &amp;v)</prototype>
  28. <p>Attempts to get the next element of the sequence. Returns <tt>true</tt> and
  29. places the value to <tt>v</tt> on success. Returns <tt>false</tt> if no element
  30. available (EOF). Throws an exception in other cases.</p>
  31. </synopsis>
  32. </section>
  33. </chapter>
  34. <chapter xml:id="iterator_reader">
  35. <title><tt>iterator_reader</tt></title>
  36. <code-block lang="C++"><![CDATA[
  37. #include<__vic/readers/iterator.h>
  38. template<class InputIterator,
  39. class T = typename std::iterator_traits<InputIterator>::value_type>
  40. class iterator_reader
  41. {
  42. public:
  43. iterator_reader(InputIterator begin, InputIterator end);
  44. bool read(T &v);
  45. InputIterator position() const;
  46. };
  47. template<class InputIterator> iterator_reader<InputIterator>
  48. make_iterator_reader(InputIterator begin, InputIterator end);
  49. template<class T, class InputIterator> iterator_reader<InputIterator,T>
  50. make_iterator_reader_for(InputIterator begin, InputIterator end);
  51. ]]></code-block>
  52. <p>An adapter for a traditional [<tt>begin</tt>,<tt>end</tt>) pair of iterators.
  53. </p>
  54. <p>Additional <tt>position()</tt> function returns the current iterator
  55. position.</p>
  56. <p>Can be created using constructor or one of <tt>make_...</tt> functions.</p>
  57. </chapter>
  58. <chapter xml:id="iterator_reader_n">
  59. <title><tt>iterator_reader_n</tt></title>
  60. <code-block lang="C++"><![CDATA[
  61. #include<__vic/readers/iterator.h>
  62. template<class InputIterator,
  63. class T = typename std::iterator_traits<InputIterator>::value_type>
  64. class iterator_reader_n
  65. {
  66. public:
  67. iterator_reader_n(InputIterator begin, size_t n);
  68. bool read(T &v);
  69. InputIterator position() const;
  70. };
  71. template<class InputIterator> iterator_reader_n<InputIterator>
  72. make_iterator_reader_n(InputIterator begin, size_t n);
  73. template<class T, class InputIterator> iterator_reader_n<InputIterator,T>
  74. make_iterator_reader_n_for(InputIterator begin, size_t n);
  75. ]]></code-block>
  76. <p>An adapter for iterator + elements counter.</p>
  77. <p>Additional <tt>position()</tt> function returns the current iterator
  78. position.</p>
  79. <p>Can be created using constructor or one of <tt>make_...</tt> functions.</p>
  80. </chapter>
  81. <chapter xml:id="container_reader">
  82. <title><tt>container_reader</tt></title>
  83. <code-block lang="C++"><![CDATA[
  84. #include<__vic/readers/container.h>
  85. template<class Cont, class T = typename Cont::value_type>
  86. class container_reader
  87. {
  88. public:
  89. explicit container_reader(const Cont &c);
  90. bool read(T &v);
  91. typename Cont::const_iterator position() const;
  92. };
  93. template<class Cont>
  94. container_reader<Cont> make_container_reader(const Cont &c);
  95. template<class T, class Cont>
  96. container_reader<Cont,T> make_container_reader_for(const Cont &c);
  97. ]]></code-block>
  98. <p>An adapter for STL-style container classes with <tt>begin()</tt> and
  99. <tt>end()</tt> members.</p>
  100. <p>Additional <tt>position()</tt> function returns the current iterator
  101. position.</p>
  102. <p>Can be created using constructor or one of <tt>make_...</tt> functions.</p>
  103. </chapter>
  104. <chapter xml:id="cstring_reader">
  105. <title><tt>cstring_reader</tt></title>
  106. <code-block lang="C++"><![CDATA[
  107. #include<__vic/readers/cstring.h>
  108. template<class charT>
  109. class basic_cstring_reader
  110. {
  111. public:
  112. explicit basic_cstring_reader(const charT *s);
  113. bool read(charT &ch);
  114. const charT *position() const;
  115. };
  116. template<>
  117. class basic_cstring_reader<char>
  118. {
  119. public:
  120. explicit basic_cstring_reader(const char *s);
  121. bool read(char &ch);
  122. bool read(unsigned char &ch);
  123. const char *position() const;
  124. };
  125. using cstring_reader = basic_cstring_reader<char>;
  126. template<class charT>
  127. basic_cstring_reader<charT> make_cstring_reader(const charT *s);
  128. ]]></code-block>
  129. <p>An adapter for NULL-terminated C-style strings.</p>
  130. <p>Additional <tt>position()</tt> function returns the current pointer
  131. position.</p>
  132. <p><tt>char</tt> specialization models <tt>Reader&lt;unsigned char></tt> as
  133. well as <tt>Reader&lt;char></tt>.</p>
  134. <p>Can be created using constructor or <tt>make_...</tt> function.</p>
  135. </chapter>
  136. <chapter xml:id="string_reader">
  137. <title><tt>string_reader</tt></title>
  138. <code-block lang="C++"><![CDATA[
  139. #include<__vic/readers/string.h>
  140. template<
  141. class charT,
  142. class Tr = std::char_traits<charT>,
  143. class Al = std::allocator<charT>
  144. >
  145. class basic_string_reader
  146. {
  147. public:
  148. explicit basic_string_reader(const std::basic_string<charT,Tr,Al> &s);
  149. bool read(charT &ch);
  150. const charT *position() const;
  151. };
  152. template<class Tr, class Al>
  153. class basic_string_reader<char,Tr,Al>
  154. {
  155. public:
  156. explicit basic_string_reader(const std::basic_string<char,Tr,Al> &s);
  157. bool read(char &ch);
  158. bool read(unsigned char &ch);
  159. const char *position() const;
  160. };
  161. using string_reader = basic_string_reader<char>;
  162. template<class charT, class Tr, class Al>
  163. basic_string_reader<charT,Tr,Al>
  164. make_string_reader(const std::basic_string<charT,Tr,Al> &s);
  165. ]]></code-block>
  166. <p>An adapter for <tt>std::basic_string</tt>.</p>
  167. <p>Additional <tt>position()</tt> function returns the current pointer
  168. position.</p>
  169. <p><tt>char</tt> specialization models <tt>Reader&lt;unsigned char></tt> as
  170. well as <tt>Reader&lt;char></tt>.</p>
  171. <p>Can be created using constructor or <tt>make_...</tt> function.</p>
  172. </chapter>
  173. <chapter xml:id="cstream_reader">
  174. <title><tt>cstream_reader</tt></title>
  175. <code-block lang="C++"><![CDATA[
  176. #include<__vic/readers/cstream.h>
  177. class cstream_reader
  178. {
  179. public:
  180. explicit cstream_reader(std::FILE *fp);
  181. bool read(char &ch) { return __vic::read(fp, ch); }
  182. bool read(unsigned char &ch) { return __vic::read(fp, ch); }
  183. };
  184. cstream_reader make_cstream_reader(std::FILE *fp);
  185. ]]></code-block>
  186. <p>Models <tt>Reader&lt;char></tt> and <tt>Reader&lt;unsigned char></tt> for
  187. <tt>std::FILE</tt>.</p>
  188. <p>Can be created using constructor or <tt>make_...</tt> function.</p>
  189. </chapter>
  190. </chapter>