home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cset21v1.zip / IBMCPP / HELP / DDE4SCL.INF (.txt) < prev    next >
OS/2 Help File  |  1993-10-19  |  387KB  |  12,985 lines

  1.  
  2. ΓòÉΓòÉΓòÉ <hidden> attach Selection ΓòÉΓòÉΓòÉ
  3.  
  4. Please select one of the following functions: 
  5.  
  6. o filebuf::attach 
  7. o fstream::attach 
  8.  
  9.  
  10. ΓòÉΓòÉΓòÉ <hidden> cancel Selection ΓòÉΓòÉΓòÉ
  11.  
  12. Please select one of the following functions: 
  13.  
  14. o sched::cancel 
  15. o task::cancel 
  16.  
  17.  
  18. ΓòÉΓòÉΓòÉ <hidden> close Selection ΓòÉΓòÉΓòÉ
  19.  
  20. Please select one of the following functions: 
  21.  
  22. o fstreambase::close 
  23. o filebuf::close 
  24.  
  25.  
  26. ΓòÉΓòÉΓòÉ <hidden> cut Selection ΓòÉΓòÉΓòÉ
  27.  
  28. Please select one of the following functions: 
  29.  
  30. o qhead::cut 
  31. o qtail::cut 
  32.  
  33.  
  34. ΓòÉΓòÉΓòÉ <hidden> doallocate Selection ΓòÉΓòÉΓòÉ
  35.  
  36. Please select one of the following functions: 
  37.  
  38. o streambuf::doallocate 
  39. o strstreambuf::doallocate 
  40.  
  41.  
  42. ΓòÉΓòÉΓòÉ <hidden> draw Selection ΓòÉΓòÉΓòÉ
  43.  
  44. Please select one of the following functions: 
  45.  
  46. o randint::draw 
  47. o urand::draw 
  48. o erand::draw 
  49.  
  50.  
  51. ΓòÉΓòÉΓòÉ <hidden> get Selection ΓòÉΓòÉΓòÉ
  52.  
  53. Please select one of the following functions: 
  54.  
  55. o istream::get - Extract characters and store in array 
  56. o istream::get - Extract characters and store in stream 
  57. o istream::get - Extract character and store in char 
  58. o istream::get - Extract character and return it 
  59. o qhead::get - Extract object from queue 
  60.  
  61.  
  62. ΓòÉΓòÉΓòÉ <hidden> o_type Selection ΓòÉΓòÉΓòÉ
  63.  
  64. Please select one of the following functions: 
  65.  
  66. o object::o_type - return class object type 
  67. o task::o_type - return class object type 
  68. o timer::o_type - return class object type 
  69. o qhead::o_type - return class type 
  70. o qtail::o_type - return class type 
  71. o Interrupt_handler::o_type - return class 
  72.  
  73.  
  74. ΓòÉΓòÉΓòÉ <hidden> open Selection ΓòÉΓòÉΓòÉ
  75.  
  76. Please select one of the following functions: 
  77.  
  78. o filebuf::open 
  79. o fstream::open 
  80. o ifstream::open 
  81. o ofstream::open 
  82.  
  83.  
  84. ΓòÉΓòÉΓòÉ <hidden> overflow Selection ΓòÉΓòÉΓòÉ
  85.  
  86. Please select one of the following functions: 
  87.  
  88. o streambuf::overflow - Clear Put Area 
  89. o strstreambuf::overflow - Clear Put Area 
  90.  
  91.  
  92. ΓòÉΓòÉΓòÉ <hidden> pending Selection ΓòÉΓòÉΓòÉ
  93.  
  94. Please select one of the following functions: 
  95.  
  96. o object::pending - Return object state 
  97. o sched::pending - Return state 
  98. o qhead::pending - Is queue empty? 
  99. o qtail::pending - Is queue full? 
  100. o Interrupt_handler::pending - Did a signal occur 
  101.  
  102.  
  103. ΓòÉΓòÉΓòÉ <hidden> print Selection ΓòÉΓòÉΓòÉ
  104.  
  105. Please select one of the following functions: 
  106.  
  107. o object::print 
  108. o sched::print 
  109. o task::print 
  110. o timer::print 
  111. o qhead::print 
  112. o qtail::print 
  113. o Interrupt_handler::print 
  114. o histogram::print 
  115.  
  116.  
  117. ΓòÉΓòÉΓòÉ <hidden> put Selection ΓòÉΓòÉΓòÉ
  118.  
  119. Please select one of the following functions: 
  120.  
  121. o streambuf::put 
  122. o qtail::put 
  123.  
  124.  
  125. ΓòÉΓòÉΓòÉ <hidden> putback Selection ΓòÉΓòÉΓòÉ
  126.  
  127. Please select one of the following functions: 
  128.  
  129. o istream::putback 
  130. o qhead::putback 
  131.  
  132.  
  133. ΓòÉΓòÉΓòÉ <hidden> rdbuf Selection ΓòÉΓòÉΓòÉ
  134.  
  135. Please select one of the following functions: 
  136.  
  137. o ios::rdbuf 
  138. o fstream::rdbuf 
  139. o ifstream::rdbuf 
  140. o ofstream::rdbuf 
  141. o strstreambase::rdbuf 
  142. o stdiobuf::rdbuf 
  143.  
  144.  
  145. ΓòÉΓòÉΓòÉ <hidden> rdmax Selection ΓòÉΓòÉΓòÉ
  146.  
  147. Please select one of the following functions: 
  148.  
  149. o qhead::rdmax 
  150. o qtail::rdmax 
  151.  
  152.  
  153. ΓòÉΓòÉΓòÉ <hidden> rdmode Selection ΓòÉΓòÉΓòÉ
  154.  
  155. Please select one of the following functions: 
  156.  
  157. o qhead::rdmode 
  158. o qtail::rdmode 
  159.  
  160.  
  161. ΓòÉΓòÉΓòÉ <hidden> rdstate Selection ΓòÉΓòÉΓòÉ
  162.  
  163. Please select one of the following functions: 
  164.  
  165. o ios::rdstate 
  166. o sched::rdstate 
  167.  
  168.  
  169. ΓòÉΓòÉΓòÉ <hidden> seekoff Selection ΓòÉΓòÉΓòÉ
  170.  
  171. Please select one of the following functions: 
  172.  
  173. o streambuf::seekoff 
  174. o filebuf::seekoff 
  175. o strstreambuf::seekoff 
  176.  
  177.  
  178. ΓòÉΓòÉΓòÉ <hidden> setbuf Selection ΓòÉΓòÉΓòÉ
  179.  
  180. Please select one of the following functions: 
  181.  
  182. o streambuf::setbuf 
  183. o filebuf::setbuf 
  184. o fstreambase::setbuf 
  185. o strstreambuf::setbuf 
  186.  
  187.  
  188. ΓòÉΓòÉΓòÉ <hidden> setmax Selection ΓòÉΓòÉΓòÉ
  189.  
  190. Please select one of the following functions: 
  191.  
  192. o qhead::setmax 
  193. o qtail::setmax 
  194.  
  195.  
  196. ΓòÉΓòÉΓòÉ <hidden> setmode Selection ΓòÉΓòÉΓòÉ
  197.  
  198. Please select one of the following functions: 
  199.  
  200. o qhead::setmode 
  201. o qtail::setmode 
  202.  
  203.  
  204. ΓòÉΓòÉΓòÉ <hidden> setwho Selection ΓòÉΓòÉΓòÉ
  205.  
  206. Please select one of the following functions: 
  207.  
  208. o sched::setwho 
  209. o task::setwho 
  210. o timer::setwho 
  211.  
  212.  
  213. ΓòÉΓòÉΓòÉ <hidden> splice Selection ΓòÉΓòÉΓòÉ
  214.  
  215. Please select one of the following functions: 
  216.  
  217. o qhead::splice 
  218. o qtail::splice 
  219.  
  220.  
  221. ΓòÉΓòÉΓòÉ <hidden> str Selection ΓòÉΓòÉΓòÉ
  222.  
  223. Please select one of the following functions: 
  224.  
  225. o strstream::str 
  226. o ostrstream::str 
  227.  
  228.  
  229. ΓòÉΓòÉΓòÉ <hidden> sync Selection ΓòÉΓòÉΓòÉ
  230.  
  231. Please select one of the following functions: 
  232.  
  233. o streambuf::sync 
  234. o istream::sync 
  235. o filebuf::sync 
  236.  
  237.  
  238. ΓòÉΓòÉΓòÉ 1. How to Use the Online Standard Class Library Guide ΓòÉΓòÉΓòÉ
  239.  
  240. The IBM C/C++ Tools Standard Class Library Guide is a guide for C++ 
  241. programmers. It provides information about three standard libraries of 
  242. predefined C++ classes, including code examples, to enable you to use the 
  243. classes in your C++ programs. 
  244.  
  245. This document is a reference rather than a tutorial. It assumes you are already 
  246. familiar with C++ programming concepts. 
  247.  
  248. The information contained within this document correspond to the three basic 
  249. class libraries: 
  250.  
  251. o The Complex Mathematics Library 
  252. o The I/O Stream Library 
  253. o The Task Library 
  254.  
  255. Before you begin to use this information, it would be helpful to understand how 
  256. you can: 
  257.  
  258. o Expand the Contents to see all available topics 
  259. o Obtain additional information for a highlighted word or phrase 
  260. o Use action bar choices 
  261.  
  262. How to Use the Contents 
  263.  
  264. When the Contents window first appears, some topics have a plus (+) sign beside 
  265. them. The plus sign indicates that additional topics are available. 
  266.  
  267. To expand the Contents if you are using a mouse, click on the plus sign. If you 
  268. are using the keyboard, use the Up or Down Arrow key to highlight the topic, 
  269. and press the plus (+) key. To see additional topics for those headings, click 
  270. on the plus sign or highlight that topic and press the plus (+) key. 
  271.  
  272. To view a topic, double-click on the topic (or press the Up or Down Arrow key 
  273. to highlight the topic, and then press the Enter key). 
  274.  
  275. How to Obtain Additional Information 
  276.  
  277. After you select a topic, the information for that topic appears in a window. 
  278. Highlighted words or phrases indicate that additional information is available. 
  279. You will notice that certain words and phrases are highlighted in green 
  280. letters, or in white letters on a black background. These are called hypertext 
  281. terms. If you are using a mouse, double-click on the highlighted word. If you 
  282. are using a keyboard, press the Tab key to move to the highlighted word, and 
  283. then press the Enter key. Additional information then appears in a window. 
  284.  
  285. How to Use Action Bar Choices 
  286.  
  287. Several choices are available for managing information presented in the C/C++ 
  288. Tools Online Standard Class Library Guide. There are three pull-down menus on 
  289. the action bar: the Services menu, the Options menu, and the Help menu. 
  290.  
  291. The actions that are selectable from the Services menu operate on the active 
  292. window currently displayed on the screen. These actions include the following: 
  293.  
  294. Bookmark 
  295.   Allows you to set a placeholder so you can retrieve information of interest 
  296.   to you. 
  297.  
  298.   When you place a bookmark on a topic, it is added to a list of bookmarks you 
  299.   have previously set. You can view the list, and you can remove one or all 
  300.   bookmarks from the list. If you have not set any bookmarks, the list is 
  301.   empty. 
  302.  
  303.   To set a bookmark, do the following: 
  304.  
  305.     1. Select a topic from the Contents. 
  306.     2. When that topic appears, choose the Bookmark option from the Services 
  307.        pull-down. 
  308.     3. If you want to change the name used for the bookmark, type the new name 
  309.        in the field. 
  310.     4. Click on the Place radio button (or press the Up or Down Arrow key to 
  311.        select it). 
  312.     5. Click on OK (or select it and press Enter). The bookmark is then added 
  313.        to the bookmark list. 
  314. Search 
  315.   Allows you to find occurrences of a word or phrase in the current topic, 
  316.   selected topics, or all topics. 
  317.  
  318.   You can specify a word or phrase to be searched. You can also limit the 
  319.   search to a set of topics by first marking the topics in the Contents list. 
  320.  
  321.   To search for a word or phrase in all topics, do the following: 
  322.  
  323.     1. Choose the Search option from the Services pull-down. 
  324.     2. Type the word or words to be searched for. 
  325.     3. Click on All sections (or press the Up or Down Arrow keys to select it). 
  326.     4. Click on Search (or select it and press Enter) to begin the search. 
  327.     5. The list of topics where the word or phrase appears is displayed. 
  328. Print 
  329.   Allows you to print one or more topics. You can also print a set of topics by 
  330.   first marking the topics in the Contents list. 
  331.  
  332.   To print the document Contents list, do the following: 
  333.  
  334.     1. Choose Print from the Services pull-down. 
  335.     2. Click on Contents (or press the Up or Down Arrow key to select it). 
  336.     3. Click on Print (or select it and press Enter). 
  337.     4. The Contents list is printed on your printer. 
  338. Copy 
  339.   Allows you to copy a topic that you are viewing to the System Clipboard or to 
  340.   a file that you can edit. This is particularly useful for copying program 
  341.   samples into the application that you are developing. 
  342.  
  343.   You can copy a topic that you are viewing in two ways: 
  344.  
  345.    o Copy copies the topic that you are viewing into the System Clipboard. If 
  346.      you are using a Presentation Manager* (PM) editor (for example, the 
  347.      Enhanced Editor) that copies or cuts (or both) to the System Clipboard, 
  348.      and pastes to the System Clipboard, you can easily add the copied 
  349.      information to your program source module. 
  350.  
  351.    o Copy to file copies the topic that you are viewing into a temporary file 
  352.      named TEXT.TMP. You can later edit that file by using any editor. TEXT.TMP 
  353.      is placed in the directory where your viewable document resides. 
  354.  
  355.   To copy a topic, do the following: 
  356.  
  357.     1. Expand the Contents list and select a topic. 
  358.     2. When the topic appears, choose Copy to file from the Services pull-down. 
  359.     3. The system puts the text pertaining to that topic into the temporary 
  360.        file TEXT.TMP. 
  361.  
  362. For information on any of the choices in the Services pull-down, highlight the 
  363. choice and press the F1 key. 
  364.  
  365. The actions that are selectable from the Options menu allow you to change the 
  366. way your Contents list is displayed. To expand the Contents and show all levels 
  367. for all topics, choose Expand all from the Options pull-down. You can also 
  368. press the Ctrl and * keys together. 
  369.  
  370. For information on any of the choices in the Options pull-down, highlight the 
  371. choice and press the F1 key. 
  372.  
  373. The actions that are selectable from the Help menu allow you to select 
  374. different types of help information. You can also press the F1 key for help 
  375. information about the Information Presentation Facility (IPF). 
  376.  
  377.  
  378. ΓòÉΓòÉΓòÉ 1.1. Related Information ΓòÉΓòÉΓòÉ
  379.  
  380. o Copyright 
  381. o Edition Notice 
  382. o Notices 
  383. o Trademarks and Service Marks 
  384.  
  385.  
  386. ΓòÉΓòÉΓòÉ 1.2. Copyright ΓòÉΓòÉΓòÉ
  387.  
  388. Copyright International Business Machines Corporation, 1993. All rights 
  389. reserved. 
  390.  
  391. Note to U.S. Government Users - Documentation related to restricted rights - 
  392. Use, duplication, or disclosure is subject to restrictions set forth in GSA ADP 
  393. Schedule Contract with IBM* Corp. 
  394.  
  395.  
  396. ΓòÉΓòÉΓòÉ 1.3. Edition Notice ΓòÉΓòÉΓòÉ
  397.  
  398. Second Edition, November 1993. 
  399.  
  400. This edition applies to Version 2.01 of IBM C/C++ Tools (82G3745, 82G3746, 
  401. 82G3747) and to all subsequent releases and modifications until otherwise 
  402. indicated in new editions.  This publication could include technical 
  403. inaccuracies or typographical errors.  Changes are periodically made to the 
  404. information herein; any such changes will be reported in subsequent revisions. 
  405.  
  406. Requests for publications and for technical information about IBM* products 
  407. should be made to your IBM Authorized Dealer or your IBM Marketing 
  408. Representative. 
  409.  
  410. When you send information to IBM, you grant IBM a nonexclusive right to use or 
  411. distribute the information in any ways it believes appropriate without 
  412. incurring any obligation to you. 
  413.  
  414.  
  415. ΓòÉΓòÉΓòÉ 1.4. Notices ΓòÉΓòÉΓòÉ
  416.  
  417. References in this publication to IBM* products, programs, or services do not 
  418. imply that IBM intends to make these available in all countries in which IBM 
  419. operates. Any reference to an IBM product, program, or service is not intended 
  420. to state or imply that only IBM's product, program, or service may be used. Any 
  421. functionally equivalent product, program, or service that does not infringe any 
  422. of IBM's intellectual property rights may be used instead of the IBM product, 
  423. program, or service. Evaluation and verification of operation in conjunction 
  424. with other products, programs, or services, except those expressly designated 
  425. by IBM, are the user's responsibility. 
  426.  
  427. IBM may have patents or pending patent applications covering subject matter in 
  428. this document. The furnishing of this document does not give you any license to 
  429. these patents. You can send license inquiries, in writing, to the IBM Director 
  430. of Commercial Relations, IBM Corporation, Purchase, NY 10577. 
  431.  
  432. This online information may contain coding examples of programs used in daily 
  433. business operations.  To illustrate them as completely as possible, the 
  434. examples may include the names of individuals, companies, brands, and products. 
  435. Any such names are fictitious and any similarity to the names and addresses 
  436. used by an actual business enterprise is entirely coincidental. 
  437.  
  438.  
  439. ΓòÉΓòÉΓòÉ 1.5. Trademarks and Service Marks ΓòÉΓòÉΓòÉ
  440.  
  441. The following terms, denoted by an asterisk (*) in this publication, are 
  442. trademarks or service marks of IBM* Corporation in the United States or other 
  443. countries: 
  444.  
  445. C/2 
  446. C/C++ Tools 
  447. C Set ++ 
  448. C Set/2 
  449. CUA 
  450. IBM 
  451. Operating System/2 
  452. OS/2 
  453. Presentation Manager 
  454. SAA 
  455. Systems Application Architecture 
  456.  
  457. The following terms, denoted by a double asterisk (**) in this publication, are 
  458. trademarks of another company as follows: 
  459.  
  460. AT&T      AT&T Corporation 
  461. UNIX      UNIX System Laboratories, Inc. 
  462.  
  463.  
  464. ΓòÉΓòÉΓòÉ <hidden> IBM Trademark ΓòÉΓòÉΓòÉ
  465.  
  466. Trademark of the IBM Corporation. 
  467.  
  468.  
  469. ΓòÉΓòÉΓòÉ <hidden> Non-IBM Trademarks ΓòÉΓòÉΓòÉ
  470.  
  471. AT&T is a trademark of AT&T Corporation. 
  472.  
  473.  
  474. ΓòÉΓòÉΓòÉ 2. General Information ΓòÉΓòÉΓòÉ
  475.  
  476. Introduction Introduction to the book's organization, with guidance on how to 
  477.             use the book. Related books are also identified. 
  478. What Are the Class Libraries? Introduction to the three class libraries that 
  479.             are described in this book: the Complex Mathematics, I/O Stream, 
  480.             and Task Libraries. 
  481.  
  482.  
  483. ΓòÉΓòÉΓòÉ 2.1. Introduction ΓòÉΓòÉΓòÉ
  484.  
  485. This book describes the standard class libraries included with IBM C/C++ Tools. 
  486. They provide sets of predefined classes that you can use to perform common 
  487. functions such as input and output or complex arithmetic. 
  488.  
  489. The following information is contained in this chapter: 
  490.  
  491. o Who should use this book 
  492. o How to use this book 
  493. o A note about examples 
  494. o Related documentation 
  495.  
  496. The three class libraries are: 
  497.  
  498. o The Complex Mathematics Library 
  499. o The I/O Stream Library 
  500. o The Task Library 
  501.  
  502.  
  503. ΓòÉΓòÉΓòÉ 2.1.1. Who Should Use This Book ΓòÉΓòÉΓòÉ
  504.  
  505. This book is a reference for people who are experienced in using the C++ 
  506. language, and understand the characteristics of C++ classes. This book should 
  507. help you to use the class libraries that are included with C/C++ Tools. 
  508.  
  509.  
  510. ΓòÉΓòÉΓòÉ 2.1.2. How to Use This Book ΓòÉΓòÉΓòÉ
  511.  
  512. This book has four parts. Part 1 gives general information. Part 2 describes 
  513. the Complex Mathematics Library. Part 3 describes the I/O Stream Library. Part 
  514. 4 describes the Task Library. Each of these parts can be read independently. 
  515.  
  516. If you need information about the I/O Stream Library, begin by reading 
  517. Introduction to the I/O Stream Library. It contains important information about 
  518. the organization of the I/O Stream Library, as well as some essential 
  519. definitions. This chapter is especially important for you if you are familiar 
  520. with the AT&T** or UNIX** System Laboratories C++ Language System Iostream 
  521. Library. New terms are defined in Introduction to the I/O Stream Library to 
  522. describe certain aspects of the I/O Stream Library. These terms are not used in 
  523. the corresponding AT&T** documentation for the Iostream Library. 
  524.  
  525.  
  526. ΓòÉΓòÉΓòÉ 2.1.2.1. Organization of Chapters ΓòÉΓòÉΓòÉ
  527.  
  528. o Part 1. General Information 
  529.  
  530.    - Introduction, (the chapter you are reading now) describes the organization 
  531.      of the book and how to use it. 
  532.    - What Are the Class Libraries?, introduces the three basic class libraries 
  533.      that are described in this book. 
  534.  
  535. o Part 2. The Complex Mathematics Library 
  536.  
  537.    - complex Class, provides a review of complex arithmetic, and describes 
  538.      complex, the class that lets you manipulate complex numbers. 
  539.    - c_exception Class, describes c_exception, the class that provides runtime 
  540.      error-handling facilities for the Complex Mathematics Library. 
  541.  
  542. o Part 3. The I/O Stream Library 
  543.  
  544.    - Introduction to the I/O Stream Library, lists the classes that make up the 
  545.      I/O Stream Library and the header files that contain their declarations. 
  546.      It also describes the key concept of stream buffers. 
  547.    - streambuf Protected Interface, describes those members of the streambuf 
  548.      class that you need to know about to create your own classes derived from 
  549.      streambuf. 
  550.    - streambuf Public Interface, describes those members of the streambuf class 
  551.      that you can use directly to manipulate an object of filebuf, stdiobuf, or 
  552.      strstreambuf, the predefined classes that are derived from streambuf. 
  553.    - ios Class, describes ios, the base class for the classes that format data 
  554.      that comes from the stream buffer. 
  555.    - istream and istream_withassign Class, describes istream, the class that 
  556.      lets you extract data from a stream buffer. This chapter also describes 
  557.      istream_withassign, a class derived from istream that includes an 
  558.      assignment operator. 
  559.    - ostream and ostream_withassign Classes, describes ostream, the class that 
  560.      lets you insert data into a stream buffer. This chapter also describes 
  561.      ostream_withassign, a class derived from ostream that includes an 
  562.      assignment operator. 
  563.    - iostream and iostream_withassign Classes, describes iostream, the class 
  564.      that is derived from istream and ostream, and iostream_withassign, the 
  565.      class that is derived from istream_withassign and ostream_withassign. 
  566.    - filebuf Class, describes filebuf, the class that adapts streambuf to use 
  567.      files. 
  568.    - fstream, ifstream, and ofstream Classes, describes fstream, ifstream, and 
  569.      ofstream, the classes that adapt istream, ostream, and iostream, 
  570.      respectively, to use files. 
  571.    - strstreambuf Class, describes strstreambuf, the class that adapts 
  572.      streambuf to use an array of bytes in memory as the source or target of 
  573.      data. 
  574.    - strstream, istrstream, and ostrstream Classes, describes istrstream, 
  575.      ostrstream, and strstream, the classes that adapt istream, ostream, and 
  576.      iostream, respectively, to use an array of bytes in memory as the source 
  577.      or target of data. 
  578.    - stdiobuf and stdiostream Classes, describes stdiobuf, the class that lets 
  579.      you mix standard C input and output functions with the C++ I/O Stream 
  580.      Library input and output functions. 
  581.    - Manipulators, describes the manipulators that allow you to use the input 
  582.      operator or the output operator to change the state of the stream that is 
  583.      being used as the source or target of data. 
  584.  
  585. o Part 4. The Task Library 
  586.  
  587.    - Introduction to the Task Library, gives an overview of the Task Library 
  588.      and lists the run-time error messages generated by the Task Library. 
  589.    - Task Handling Classes, describes object, sched, task, and timer, the task 
  590.      handling classes. These classes give you the facilities to create and 
  591.      manage tasks. 
  592.    - Queue Management Classes, describes qhead and qtail, the queue management 
  593.      classes. These classes let you implement a wide range of message-passing 
  594.      and data-buffering schemes. 
  595.    - Interrupt_handler Class, describes Interrupt_handler, the class that 
  596.      allows your tasks to handle external events in the form of signals from 
  597.      the operating system. 
  598.    - Simulation Classes, describes histogram, randint, urand, and erand, the 
  599.      simulation classes. These classes provide utilities for writing program 
  600.      simulations and gathering statistics about them. 
  601.  
  602. o Part 5. Appendixes, Glossary, and Index 
  603.  
  604.    - Extended Complex Mathematics Library Examples, contains examples of how to 
  605.      determine the roots of a complex number and how to define your own 
  606.      complex_error function. 
  607.    - Extended Task Library Examples, contains examples of how to use the Task 
  608.      Library to implement a parallel algorithm and an event-driven simulation. 
  609.  
  610.  
  611. ΓòÉΓòÉΓòÉ 2.1.2.2. Organization of Class Descriptions ΓòÉΓòÉΓòÉ
  612.  
  613. The chapters that contain detailed descriptions of classes have the following 
  614. format: 
  615.  
  616. o An introduction. 
  617.  
  618. o A discussion of how to use the classes, if appropriate. 
  619.  
  620. o An excerpt from the appropriate header files that shows the relevant 
  621.   declarations for the classes being described. These excerpts supplement the 
  622.   member function descriptions; they are not complete listings of the header 
  623.   files. If you need to know the value of an element in an enumeration, or any 
  624.   other specific detail about the declaration of a class, you should look at 
  625.   the header file itself. 
  626.  
  627. o A description of each member function of each class that the chapter 
  628.   describes. This begins with a description of the constructor and destructor, 
  629.   if they exist. Descriptions of the remaining members follow in alphabetical 
  630.   order. For the more complicated classes like streambuf, the descriptions of 
  631.   members with related operations are grouped together. In most classes, only 
  632.   public and protected members are described. Private members are only 
  633.   described if they are needed to describe a public member. In addition, none 
  634.   of the inherited members in a derived class is described in the discussion of 
  635.   that class, except for virtual functions that do not use the inherited 
  636.   function definition. 
  637.  
  638.  
  639. ΓòÉΓòÉΓòÉ 2.1.2.3. How to Find the Description of a Particular Function ΓòÉΓòÉΓòÉ
  640.  
  641. The organization of the class libraries can be difficult to understand. The I/O 
  642. Stream Library, especially, has a complicated hierarchy of classes. A function 
  643. may be defined in more than one class, and it may be overloaded in a single 
  644. class. Function name overloading gives C++ many advantages, but it also makes 
  645. it difficult to describe the functions in a simple, linear fashion. 
  646.  
  647. If you know the class in which a function is defined, you can go directly to 
  648. the chapter where that class is described. There, the functions are grouped 
  649. according to their purpose and listed alphabetically within each group. If a 
  650. class has more than one version of a function, all of the versions are 
  651. described in one section. 
  652.  
  653.  
  654. ΓòÉΓòÉΓòÉ 2.1.2.4. Fonts Used in This Book ΓòÉΓòÉΓòÉ
  655.  
  656. In this book, C++ code and code fragments appear in special font. Variables 
  657. that are not used in actual examples, and for which other variable names can be 
  658. substituted, appear in italics. 
  659.  
  660.  
  661. ΓòÉΓòÉΓòÉ 2.1.3. A Note about Examples ΓòÉΓòÉΓòÉ
  662.  
  663. The examples in this book explain elements of the C++ class libraries. They are 
  664. coded in a simple style. They do not try to conserve storage, check for errors, 
  665. achieve fast run times, or demonstrate all possible uses of a language element. 
  666.  
  667.  
  668. ΓòÉΓòÉΓòÉ 2.1.4. Related Documentation ΓòÉΓòÉΓòÉ
  669.  
  670. You might want to refer to the following publications for additional 
  671. information: 
  672.  
  673. IBM Publications: 
  674.  
  675. IBM C/C++ Tools: C++ Language Reference, S61G-1185, describes the C++ language. 
  676.  
  677. IBM C/C++ Tools: Collection Class Library Reference, S61G-1178, describes the 
  678. C++ collection classes, a set of C++ classes that implement commonly used 
  679. abstract data types such as sets, stacks, and queues. 
  680.  
  681. IBM C/C++ Tools: Application Support Class Library Reference, S82G-3905, 
  682. describes the C++ application support classes, a set of C++, classes that 
  683. provide string handling, exception handling, and date and time capabilities to 
  684. C++ applications. 
  685.  
  686. IBM C/C++ Tools: User Interface Class Library Reference, S61G-1179, describes 
  687. the C++ user interface classes, a set of C++ classes that you can use to create 
  688. C++ applications with a graphical user interface that conforms to the Common 
  689. User Access (CUA) interface design. 
  690.  
  691. IBM C/C++ Tools: Class Libraries Reference Summary, S61G-1186, lists the member 
  692. functions of the Standard, Collection, and User Interface class libraries. For 
  693. each member function, the declaration is provided and the class the function 
  694. belongs to is indicated. 
  695.  
  696. IBM C/C++ Tools: Programming Guide, S61G-1181, describes the C++ component of 
  697. the common programming interface. 
  698.  
  699. The following is a sample of some non-IBM C++ publications that are generally 
  700. available. It is not an exhaustive list. IBM does not specifically recommend 
  701. any of these books, and other C++ publications may be available in your 
  702. locality. 
  703.  
  704. C++ Language System Release 3.0 Library Manual, UNIX** System Laboratories. 
  705.  
  706. The Annotated C++ Reference Manual by Margaret A. Ellis and Bjarne Stroustrup, 
  707. Addison-Wesley Publishing Company. 
  708.  
  709. The C++ Programming Language (Second Edition) by Bjarne Stroustrup, 
  710. Addison-Wesley Publishing Company. 
  711.  
  712. C++ Primer (Second Edition) by Stanley B. Lippman, Addison-Wesley Publishing 
  713. Company. 
  714.  
  715.  
  716. ΓòÉΓòÉΓòÉ 2.2. What Are the Class Libraries? ΓòÉΓòÉΓòÉ
  717.  
  718. This chapter introduces the three class libraries that are described in this 
  719. book: 
  720.  
  721. o The Complex Mathematics Library provides you with the facilities to 
  722.   manipulate complex numbers and perform standard mathematical operations on 
  723.   them. 
  724.  
  725. o The I/O Stream Library (also known as the Iostream Library, see History of 
  726.   the Class Libraries). provides you with the facilities to deal with many 
  727.   varieties of input and output. You can derive classes from this library to 
  728.   customize the input and output facilities for your own particular needs. 
  729.  
  730. o The Task Library provides you with the facilities to write programs that are 
  731.   made up of tasks. Tasks are lightweight (that is, they require fewer 
  732.   processor resources than standard OS/2 processes) and are nonpreemptive. The 
  733.   Task Library follows the coroutine paradigm. 
  734.  
  735. The following sections provide information on using the class libraries: 
  736.  
  737. o History of the class libraries 
  738. o Including a class library in your source code 
  739. o Other class libraries 
  740.  
  741.  
  742. ΓòÉΓòÉΓòÉ 2.2.1. History of the Class Libraries ΓòÉΓòÉΓòÉ
  743.  
  744. The basic class libraries described in this book are based on the class 
  745. libraries from the UNIX** System Laboratories C++ Language System Release 3.0. 
  746. (Earlier releases of this product are known as the AT&T** C++ Language System.) 
  747. In the Unix System Laboratories product, the class library that corresponds to 
  748. the I/O Stream Library is called the Iostream Library. Prior to Release 2.0 of 
  749. the AT&T** C++ Language System, a class library called the Stream Library 
  750. provided input and output facilities. The I/O Stream Library includes obsolete 
  751. functions, described in this book, in order to provide compatibility with the 
  752. Stream Library. 
  753.  
  754.  
  755. ΓòÉΓòÉΓòÉ 2.2.2. Including a Class Library ΓòÉΓòÉΓòÉ
  756.  
  757. A class library is a collection of header and library files. The header files 
  758. provide the interface to the class libraries. 
  759.  
  760. To use the classes, functions, and operators available in a class library, you 
  761. must include the parts of the library's interface that you need in your C++ 
  762. source program. To include an interface, use the directive #include <filename>, 
  763. where filename is the name of the header file. Place this statement at the 
  764. beginning of the program that requires any of the classes, functions, or 
  765. operators defined in the header file. Then, in the body of your program, you 
  766. can use a class, function, or operator defined in the header file as well as 
  767. derive new classes and overload the functions and operators. 
  768.  
  769.  
  770. ΓòÉΓòÉΓòÉ 2.2.3. Other Class Libraries ΓòÉΓòÉΓòÉ
  771.  
  772. In addition to the three standard class libraries, OS/2 C/C++ also supports the 
  773. Collection Class Library.  This library provides you with the facilities to 
  774. create objects of common data structures such as sets, maps, sequences, trees, 
  775. and sorted collections. See the Collection Class Library Reference for details 
  776. about this class library. 
  777.  
  778. The C/C++ Tools product also supports the User Interface Class Library, a class 
  779. library that can be used to create C++ applications with a graphical user 
  780. interface that conforms to the Common User Access (CUA) interface design. The 
  781. User Interface Class Library is described in the User Interface Class Library 
  782. Reference. 
  783.  
  784.  
  785. ΓòÉΓòÉΓòÉ 3. The Complex Mathematics Library ΓòÉΓòÉΓòÉ
  786.  
  787. complex Class 
  788.   Introduction to the Complex Mathematics Library and review of complex 
  789.   numbers. It describes complex, the class that lets you manipulate complex 
  790.   numbers. 
  791. c_exception Class 
  792.   Describes c_exception, the class that provides runtime error-handling 
  793.   facilities for the functions and operations in the complex class. 
  794.  
  795.  
  796. ΓòÉΓòÉΓòÉ 3.1. complex Class ΓòÉΓòÉΓòÉ
  797.  
  798. This chapter reviews the concept of complex numbers, and then describes 
  799. complex, the class that provides you with the facilities to manipulate complex 
  800. numbers. 
  801.  
  802. Appendix A, "Extended Complex Mathematics Library Examples" contains examples 
  803. of how you might use the Complex Mathematics Library. 
  804.  
  805. The following topics are described in this chapter: 
  806.  
  807. o Review of complex numbers 
  808. o Declarations for the complex class in the complex.h header file 
  809. o Complex constructors 
  810. o Complex mathematical operators 
  811. o Complex input and output operators 
  812. o Complex mathematical functions 
  813. o Complex trigonometric functions 
  814. o Complex magnitude 
  815. o Complex conversions 
  816. o Linking to the Complex Library 
  817.  
  818.  
  819. ΓòÉΓòÉΓòÉ 3.1.1. Review of Complex Numbers ΓòÉΓòÉΓòÉ
  820.  
  821. A complex number is made up of two parts: a real part and an imaginary part. A 
  822. complex number can be represented by an ordered pair (a,b), where a is the 
  823. value of the real part of the number and b is the value of the imaginary part. 
  824. If (a,b) and (c,d) are imaginary numbers, then the following statements are 
  825. true: 
  826.  
  827. o (a,b) + (c,d) = (a+c,b+d) 
  828.  
  829. o (a,b) - (c,d) = (a-c,b-d) 
  830.  
  831. o (a,b) * (c,d) = (ac-bd,ad+bc) 
  832.  
  833. o (a,b) / (c,d) = ((ac+bd) / (c┬ñ+d┬ñ), (bc-ad) / (c┬ñ+d┬ñ)) 
  834.  
  835. o The conjugate of a complex number (a,b) is (a,-b) 
  836.  
  837. o The absolute value or magnitude of a complex number (a,b) is the positive 
  838.   square root of the value a┬ñ + b┬ñ 
  839.  
  840. o The polar representation of (a,b) is (r,theta), where r is the distance from 
  841.   the origin to the point (a,b) in the complex plane, and theta is the angle 
  842.   from the real axis to the vector (a,b) in the complex plane. The angle theta 
  843.   can be positive or negative.  illustrates the polar representation (r,theta) 
  844.   of the complex number (a,b). 
  845.  
  846. Polar Representation of Complex Number (a,b) 
  847.  
  848.  
  849. ΓòÉΓòÉΓòÉ 3.1.2. Declarations for complex in complex.h ΓòÉΓòÉΓòÉ
  850.  
  851. You must include the following statement in any file that uses the complex 
  852. class: 
  853.  
  854. #include <complex.h>
  855.  
  856. The following is an excerpt from the complex.h header file that shows the 
  857. relevant declarations for the member functions of the complex class: 
  858.  
  859. #include <iostream.h>
  860. #include <errno.h>
  861. #include <math.h>
  862.  
  863. #define DOMAIN        1
  864. #define SING          2
  865. #define OVERFLOW      3
  866. #define UNDERFLOW     4
  867.  
  868. #define M_E           2.7182818284590452354
  869. #define M_LOG2E       1.4426950408889634074
  870. #define M_LOG10E      0.43429448190325182765
  871. #define M_LN2         0.69314718055994530942
  872. #define M_LN10        2.30258509299404568402
  873. #define M_PI          3.14159265358979323846
  874. #define M_PI_2        1.57079632679489661923
  875. #define M_PI_4        0.78539816339744830962
  876. #define M_1_PI        0.31830988618379067154
  877. #define M_2_PI        0.63661977236758134308
  878. #define M_2_SQRTPI    1.12837916709551257390
  879. #define M_SQRT2       1.41421356237309504880
  880. #define M_SQRT1_2     0.70710678118654752440
  881.  
  882. class complex {
  883.  
  884.      double     re, im;
  885. public:
  886.      complex();.
  887.      complex(double r, double i = 0.0);
  888.      friend double    real(const complex&);
  889.      friend double    imag(const complex&);
  890.      friend double    abs(complex);
  891.      friend double    norm(complex);
  892.      friend double    arg(complex);
  893.      friend complex   conj(complex);
  894.      friend complex   cos(complex);
  895.      friend complex   cosh(complex);
  896.      friend complex   exp(complex);
  897.      friend complex   log(complex);
  898.      friend complex   pow(double, complex);
  899.      friend complex   pow(complex, int);
  900.      friend complex   pow(complex, double);
  901.      friend complex   pow(complex, complex);
  902.      friend complex   polar(double, double = 0);
  903.      friend complex   sin(complex);
  904.      friend complex   sinh(complex);
  905.      friend complex   sqrt(complex);
  906.      friend complex   operator+(complex, complex);
  907.      friend complex   operator-(complex);
  908.      friend complex   operator-(complex, complex);
  909.      friend complex   operator*(complex, complex);
  910.      friend complex   operator/(complex, complex);
  911.      friend int       operator==(complex, complex);
  912.      friend int       operator!=(complex, complex);
  913.      void             operator+=(complex);
  914.      void             operator-=(complex);
  915.      void             operator*=(complex);
  916.      void             operator/=(complex);
  917. };
  918.  
  919. istream& operator>>(istream&, complex&);
  920. ostream& operator<<(ostream&, complex);
  921.  
  922. extern const complex complex_zero(0,0);
  923.  
  924. Note:  This header file excerpt, and all of the other header file excerpts in 
  925. this book, are unpublished proprietary source code of AT&T** and UNIX** System 
  926. Laboratories. (C) Copyright 1991 AT&T** and UNIX** System Laboratories, Inc. 
  927. (C)Copyright 1984, 1989, 1990 AT&T**. All rights reserved. 
  928.  
  929.  
  930. ΓòÉΓòÉΓòÉ 3.1.2.1. Constants Defined in complex.h ΓòÉΓòÉΓòÉ
  931.  
  932. The following table lists the mathematical constants that the Complex 
  933. Mathematics Library defines (if they have not been previously defined): 
  934.  
  935. ΓöîΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÉ
  936. Γöé      Constants Defined in complex.h             Γöé
  937. Γö£ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓö¼ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöñ
  938. Γöé CONSTANT NAME       Γöé DESCRIPTION              Γöé
  939. Γö£ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓö╝ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöñ
  940. Γöé M_E            Γöé The constant e            Γöé
  941. Γö£ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓö╝ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöñ
  942. Γöé M_LOG2E          Γöé The logarithm of e to the base of 2  Γöé
  943. Γö£ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓö╝ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöñ
  944. Γöé M_LOG10E         Γöé The logarithm of e to the base of 10 Γöé
  945. Γö£ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓö╝ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöñ
  946. Γöé M_LN2           Γöé The natural logarithm of 2      Γöé
  947. Γö£ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓö╝ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöñ
  948. Γöé M_LN10          Γöé The natural logarithm of 10      Γöé
  949. Γö£ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓö╝ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöñ
  950. Γöé M_PI           Γöé pi                  Γöé
  951. Γö£ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓö╝ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöñ
  952. Γöé M_PI_2          Γöé pi  / 2                Γöé
  953. Γö£ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓö╝ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöñ
  954. Γöé M_PI_4          Γöé pi  / 4                Γöé
  955. Γö£ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓö╝ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöñ
  956. Γöé M_1_PI          Γöé 1 / pi                Γöé
  957. Γö£ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓö╝ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöñ
  958. Γöé M_2_PI          Γöé 2 / pi                Γöé
  959. Γö£ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓö╝ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöñ
  960. Γöé M_2_SQRTPI        Γöé 2 divided by the square root of pi  Γöé
  961. Γö£ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓö╝ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöñ
  962. Γöé M_SQRT2          Γöé The square root of 2         Γöé
  963. Γö£ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓö╝ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöñ
  964. Γöé M_SQRT1_2         Γöé The square root of 1 / 2       Γöé
  965. ΓööΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓö┤ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÿ
  966.  
  967.  
  968. ΓòÉΓòÉΓòÉ 3.1.2.2. Constructors for complex ΓòÉΓòÉΓòÉ
  969.  
  970. Class: complex 
  971.  
  972. There are two versions of the complex constructor: 
  973.  
  974. complex();
  975. complex(double r, double i=0.0);
  976.  
  977. If you declare a complex object without specifying any values for the real or 
  978. imaginary part of the complex value, the constructor that takes no arguments is 
  979. used and the complex value is initialized to (0, 0). For example, the following 
  980. declaration gives the object comp the value (0, 0): 
  981.  
  982. complex comp;
  983.  
  984. If you give either one or two values in your declaration, the constructor that 
  985. takes two arguments is used. If you only give one value, the real part of the 
  986. complex object is initialized to that value, and the imaginary part is 
  987. initialized to 0. 
  988.  
  989. For example, the following declaration gives the object comp2 the value 
  990. (3.14, 0): 
  991.  
  992. complex comp2(3.14);
  993.  
  994. If you give two values in the declaration, the real part of the complex object 
  995. is initialized to the first value and the imaginary part is initialized to the 
  996. second value. For example, the following declaration gives the object comp3 the 
  997. value (3.14, 6.44): 
  998.  
  999. complex comp3(3.14, 6.44);
  1000.  
  1001. There is no explicit complex destructor. 
  1002.  
  1003.  
  1004. ΓòÉΓòÉΓòÉ 3.1.2.3. Using the Complex Constructor to Initialize Arrays ΓòÉΓòÉΓòÉ
  1005.  
  1006. You can use the complex constructor to initialize arrays of complex numbers. If 
  1007. the list of initial values is made up of complex values, each array element is 
  1008. initialized to the corresponding value in the list of initial values. If the 
  1009. list of initial values is not made up of complex values, the real parts of the 
  1010. array elements are initialized to these initial values and the imaginary parts 
  1011. of the array elements are initialized to 0. In the following example, the 
  1012. elements of array b are initialized to the values in the initial value list, 
  1013. but only the real parts of elements of array a are initialized to the values in 
  1014. the initial value list. 
  1015.  
  1016. #include <complex.h>
  1017.  
  1018. void main() {
  1019.      complex a[3] = {1.0, 2.0, 3.0};
  1020.      complex b[3] = {complex(1.0, 1.0), complex(2.0, 2.0),
  1021.                     complex(3.0, 3.0)};
  1022.      cout << "Here is the first element of a: " << a[0] << endl;
  1023.      cout << "Here is the first element of b: " << b[0] << endl;
  1024. }
  1025.  
  1026. This example produces the following output: 
  1027.  
  1028. Here is the first element of a: ( 1, 0)
  1029. Here is the first element of b: ( 1, 1)
  1030.  
  1031.  
  1032. ΓòÉΓòÉΓòÉ 3.1.3. Mathematical Operators for complex ΓòÉΓòÉΓòÉ
  1033.  
  1034. The complex operators described in this section have the same precedence as the 
  1035. corresponding real operators. 
  1036.  
  1037. The following operators are described: 
  1038.  
  1039. o Addition operator 
  1040. o Subtraction operator 
  1041. o Negation operator 
  1042. o Multiplication operator 
  1043. o Division operator 
  1044. o Equality operator 
  1045. o Inequality operator 
  1046. o Mathematical assignment operators 
  1047.  
  1048.  
  1049. ΓòÉΓòÉΓòÉ 3.1.3.1. Addition ΓòÉΓòÉΓòÉ
  1050.  
  1051. Class: complex 
  1052.  
  1053. friend complex operator+(complex x, complex y);
  1054.  
  1055. The addition operator returns the sum of x and y. 
  1056.  
  1057.  
  1058. ΓòÉΓòÉΓòÉ 3.1.3.2. Subtraction ΓòÉΓòÉΓòÉ
  1059.  
  1060. Class: complex 
  1061.  
  1062. friend complex operator-(complex x, complex y);
  1063.  
  1064. The subtraction operator returns the difference between x and y. 
  1065.  
  1066.  
  1067. ΓòÉΓòÉΓòÉ 3.1.3.3. Negation ΓòÉΓòÉΓòÉ
  1068.  
  1069. Class: complex 
  1070.  
  1071. friend complex operator-(complex x);
  1072.  
  1073. The negation operator returns (- a, - b) when its argument is (a, b). 
  1074.  
  1075.  
  1076. ΓòÉΓòÉΓòÉ 3.1.3.4. Multiplication ΓòÉΓòÉΓòÉ
  1077.  
  1078. Class: complex 
  1079.  
  1080. friend complex operator*(complex x, complex y)
  1081.  
  1082. The multiplication operator returns the product of x and y. 
  1083.  
  1084.  
  1085. ΓòÉΓòÉΓòÉ 3.1.3.5. Division ΓòÉΓòÉΓòÉ
  1086.  
  1087. Class: complex 
  1088.  
  1089. friend complex operator/(complex x, complex y)
  1090.  
  1091. The division operator returns the quotient of x divided by y. 
  1092.  
  1093.  
  1094. ΓòÉΓòÉΓòÉ 3.1.3.6. Equality ΓòÉΓòÉΓòÉ
  1095.  
  1096. Class: complex 
  1097.  
  1098. friend int operator==(complex x, complex y);
  1099.  
  1100. The  equality operator "==" returns a nonzero value if x equals y. This 
  1101. operator tests for equality by testing that the two real components are equal 
  1102. and that the two imaginary components are equal. 
  1103.  
  1104. Because both components are double values, the equality operator tests for an 
  1105. exact match between the two sets of values. If you want an equality operator 
  1106. that can test for an absolute difference within a certain tolerance between the 
  1107. two pairs of corresponding components, you can use a function such as the 
  1108. is_equal function defined in Inequality. 
  1109.  
  1110.  
  1111. ΓòÉΓòÉΓòÉ 3.1.3.7. Inequality ΓòÉΓòÉΓòÉ
  1112.  
  1113. Class: complex 
  1114.  
  1115. friend int operator!=(complex x, complex y);
  1116.  
  1117. The inequality operator "! =" returns a nonzero value if x does not equal y. 
  1118. This operator tests for inequality by testing that the two real components are 
  1119. not equal and that the two imaginary components are not equal. 
  1120.  
  1121. Because both components are double values, the inequality operator returns 
  1122. false only when both the real and imaginary components of the two values are 
  1123. identical. If you want an inequality operator that can test for an absolute 
  1124. difference within a certain tolerance between the two pairs of corresponding 
  1125. components, you can use a function such as the is_not_equal function defined in 
  1126. the following example. In the example, the functions is_equal and is_not_equal 
  1127. are defined to test whether two complex values are equal or not equal, 
  1128. respectively, within a certain tolerance. You should use functions like 
  1129. is_equal and is_not_equal, rather than the equality and inequality operators, 
  1130. if you want a reliable comparison between two complex values. 
  1131.  
  1132.  
  1133. #include <complex.h>
  1134.  
  1135. int is_equal(const complex &a, const complex &b,
  1136.                const double tol=0.0001)
  1137. {
  1138.      return (abs(real(a) - real(b)) < tol &&
  1139.                abs(imag(a) - imag(b)) < tol);
  1140. }
  1141.  
  1142. int is_not_equal(const complex &a, const complex &b,
  1143.                const double tol=0.0001)
  1144. {
  1145.      return !is_equal(a, b, tol);
  1146. }
  1147.  
  1148. void main()
  1149. {
  1150.      complex c1 = complex(1.0, 2.0);
  1151.      complex c2 = c1;
  1152.      complex c3 = complex(3.0, 4.0);
  1153.      if (is_equal(c1, c2))
  1154.           cout << "c1 is equal to c2" << endl;
  1155.      else
  1156.           cout << "c1 is not equal to c2 - not possible!" << endl;
  1157.      if (is_not_equal(c1, c3))
  1158.           cout << "c1 is not equal to c3" << endl;
  1159.      else
  1160.           cout << "c1 is equal to c3 - not possible!" << endl;
  1161. }
  1162.  
  1163. This example produces the following output: 
  1164.  
  1165. c1 is equal to c2
  1166. c1 is not equal to c3
  1167.  
  1168.  
  1169. ΓòÉΓòÉΓòÉ 3.1.3.8. Mathematical Assignment Operators ΓòÉΓòÉΓòÉ
  1170.  
  1171. Class: complex 
  1172.  
  1173. void operator+=(complex x);
  1174. void operator-=(complex x);
  1175. void operator*=(complex x);
  1176. void operator/=(complex x);
  1177.  
  1178. The following list describes the functions of the mathematical assignment 
  1179. operators: 
  1180.  
  1181. o x + = y assigns the value of x + y to x. 
  1182. o x -  = y assigns the value of x - y to x. 
  1183. o x * = y assigns the value of x * y to x. 
  1184. o x / = y assigns the value of x / y to x. 
  1185.  
  1186. Note:  The assignment operators do not produce a value that can be used in an 
  1187. expression. The following code, for example, produces a compile-time error: 
  1188.  
  1189.      complex x, y, z;     // valid declaration
  1190.      x = (y += z);        // invalid assignment causes a
  1191.                           // compile-time error
  1192.  
  1193.  
  1194. ΓòÉΓòÉΓòÉ 3.1.3.9. Example of Using the Complex Mathematical Operators ΓòÉΓòÉΓòÉ
  1195.  
  1196. The following example shows how you can use the complex mathematical operators: 
  1197.  
  1198. #include <complex.h>
  1199.  
  1200. void main() {
  1201.      complex a, b;
  1202.      cout << "enter two complex values" << endl;
  1203.      cin >> a >> b;
  1204.      cout << "The sum of these two numbers is " << a+b << endl;
  1205.      cout << "The difference between these two numbers is "
  1206.           << a-b << endl;
  1207.      cout << "The product of these two numbers is "
  1208.           << a*b << endl;
  1209.      cout << "The first number divided by the second is "
  1210.           << a/b << endl;
  1211.      }
  1212.  
  1213. If you run this program with the values (1,1) and (2,2) for input, the output 
  1214. will look like this: 
  1215.  
  1216. enter two complex values
  1217.  
  1218. The sum of these two numbers is ( 3, 3)
  1219. The difference between these two numbers is ( -1, -1)
  1220. The product of these two numbers is ( 0, 4)
  1221. The first number divided by the second is ( 0.5, 0)
  1222.  
  1223.  
  1224. ΓòÉΓòÉΓòÉ 3.1.4. Input and Output Operators for complex ΓòÉΓòÉΓòÉ
  1225.  
  1226. The following topics are described: 
  1227.  
  1228. o The input operator 
  1229. o The output operator 
  1230. o An example of using the input and output operators. 
  1231.  
  1232.  
  1233. ΓòÉΓòÉΓòÉ 3.1.4.1. Input Operator ΓòÉΓòÉΓòÉ
  1234.  
  1235. Class: complex 
  1236.  
  1237. istream& operator>>(istream& is, complex& c);
  1238.  
  1239. The input (or extraction) operator >> takes complex value c from the stream is 
  1240. in the form (a,b). The parentheses and comma are mandatory delimiters for input 
  1241. when the imaginary part of the complex number being read is nonzero. Otherwise, 
  1242. they are optional. In both cases, whitespace is optional. 
  1243.  
  1244.  
  1245. ΓòÉΓòÉΓòÉ 3.1.4.2. Output Operator ΓòÉΓòÉΓòÉ
  1246.  
  1247. Class: complex 
  1248.  
  1249. ostream& operator<<(ostream& os, complex c);
  1250.  
  1251. The output (or insertion) operator <<  writes complex value c to the stream os 
  1252. in the form (a,b). 
  1253.  
  1254.  
  1255. ΓòÉΓòÉΓòÉ 3.1.4.3. Example of Using the Complex Input and Output Operators ΓòÉΓòÉΓòÉ
  1256.  
  1257. #include <complex.h>
  1258.  
  1259. void main(){
  1260.      complex x;
  1261.      cout << "Enter a complex number in the form (a,b):" << endl;
  1262.      cin >> x;
  1263.      cout << "Here is the complex value: " << x << endl;
  1264. }
  1265.  
  1266. This example produces the following output if you enter the complex value (3,4) 
  1267. at the prompt: 
  1268.  
  1269. Enter a complex number in the form (a,b):
  1270. Here is the complex value: (3,4)
  1271.  
  1272.  
  1273. ΓòÉΓòÉΓòÉ 3.1.5. Mathematical Functions for complex ΓòÉΓòÉΓòÉ
  1274.  
  1275. The following mathematical functions are described: 
  1276.  
  1277. o exp - Exponent 
  1278. o log - Logarithm 
  1279. o pow - Power 
  1280. o sqrt - Square Root 
  1281.  
  1282. There is also an example of using the complex mathematical functions. 
  1283.  
  1284.  
  1285. ΓòÉΓòÉΓòÉ 3.1.5.1. exp - Exponent ΓòÉΓòÉΓòÉ
  1286.  
  1287. Class: complex 
  1288.  
  1289. friend complex exp(complex x);
  1290.  
  1291. exp() returns the complex value equal to e to the power of x where x is the 
  1292. argument. Results of the Default Error-Handling Procedures shows the values 
  1293. returned by the default error-handling procedure for exp(). 
  1294.  
  1295.  
  1296. ΓòÉΓòÉΓòÉ 3.1.5.2. log -  Logarithm ΓòÉΓòÉΓòÉ
  1297.  
  1298. Class: complex 
  1299.  
  1300. friend complex log(complex x);
  1301.  
  1302. log() returns the natural logarithm of the argument x. Results of the Default 
  1303. Error-Handling Procedures shows the values returned by the default 
  1304. error-handling procedure for log(). 
  1305.  
  1306.  
  1307. ΓòÉΓòÉΓòÉ 3.1.5.3. pow - Power ΓòÉΓòÉΓòÉ
  1308.  
  1309. Class: complex 
  1310.  
  1311. friend complex pow(double d, complex z);
  1312. friend complex pow(complex c, int i);
  1313. friend complex pow(complex c, double d);
  1314. friend complex pow(complex c, complex z);
  1315.  
  1316. Note:  Exponents in the following text are shown in square brackets. 
  1317.  
  1318. pow() returns the complex value x[y], where x is the first argument and y is 
  1319. the second argument. pow() is overloaded four times. If d is a double value, i 
  1320. is an integer value, and c and z are complex values, then pow() can produce any 
  1321. of the following results: 
  1322.  
  1323. o d[z] 
  1324. o c[i] 
  1325. o c[d] 
  1326. o c[z] 
  1327.  
  1328.  
  1329. ΓòÉΓòÉΓòÉ 3.1.5.4. sqrt - Square Root ΓòÉΓòÉΓòÉ
  1330.  
  1331. Class: complex 
  1332.  
  1333. friend complex sqrt(complex x)
  1334.  
  1335. sqrt() returns the square root of its argument. If c and d are real values, 
  1336. then every complex number (a,b), where: 
  1337.  
  1338. o a = c┬ñ - d┬ñ 
  1339. o b = 2cd 
  1340.  
  1341. has two square roots: 
  1342.  
  1343. o (c,d) 
  1344. o (-c,-d ) 
  1345.  
  1346. sqrt() returns the square root that has a positive real part, that is, the 
  1347. square root that is contained in the first or fourth quadrants of the complex 
  1348. plane. 
  1349.  
  1350.  
  1351. ΓòÉΓòÉΓòÉ 3.1.5.5. Example of Using the Complex Mathematical Functions ΓòÉΓòÉΓòÉ
  1352.  
  1353. The following program shows how you can use the complex mathematical functions: 
  1354.  
  1355. #include <complex.h>
  1356.  
  1357. void main() {
  1358.      complex a, b;
  1359.      int i;
  1360.      double f;
  1361.      //
  1362.      // prompt the user for an argument for calls to
  1363.      // exp(), log(), and sqrt()
  1364.      //
  1365.      cout << "Enter a complex value" << endl;
  1366.      cin >> a;
  1367.      cout << "The value of exp() for " << a << "  is: "
  1368.                << exp(a) << endl;
  1369.      cout << "The natural logarithm of " << a << " is: "
  1370.                << log(a) << endl;
  1371.      cout << "The square root of " << a << " is: "
  1372.                << sqrt(a) << endl << endl;
  1373.      //
  1374.      // prompt the user for arguments for calls to pow()
  1375.      //
  1376.      cout << "Enter 2 complex values (a and b), an integer (i),";
  1377.      cout << " and a floating point value (f)" << endl;
  1378.      cin >> a >> b >> i >> f;
  1379.      cout << "a is " << a << ", b is " << b << ", i is " << i;
  1380.      cout << ", f is " << f << endl;
  1381.      cout << "The value of f**a is: " << pow(f, a) << endl;
  1382.      cout << "The value of a**i is: " << pow(a, i) << endl;
  1383.      cout << "The value of a**f is: " << pow(a, f) << endl;
  1384.      cout << "The value of a**b is: " << pow(a, b) << endl;
  1385.      }
  1386.  
  1387. This program produces the following output when you give it (1,1), (1,1), 
  1388. (1,0), 1, and 1.0 as input: 
  1389.  
  1390. Enter a complex value
  1391.  
  1392. The value of exp() for ( 1, 1)  is: ( 1.46869, 2.28736)
  1393. The natural logarithm of ( 1, 1) is: ( 0.346574, 0.785398)
  1394. The square root of ( 1, 1) is: ( 1.09868, 0.45509)
  1395.  
  1396. Enter 2 complex values (a and b), an integer (i),
  1397. and a floating point value (f)
  1398.  
  1399. a is ( 1, 1), b is ( 1, 0), i is 1, f is 1
  1400. The value of f**a is: ( 1, 0)
  1401. The value of a**i is: ( 1, 1)
  1402. The value of a**f is: ( 1, 1)
  1403. The value of a**b is: ( 1, 1)
  1404.  
  1405.  
  1406. ΓòÉΓòÉΓòÉ 3.1.6. Trigonometric Functions for complex ΓòÉΓòÉΓòÉ
  1407.  
  1408. The following trigonometric functions are described: 
  1409.  
  1410. o cos - Cosine 
  1411. o cosh - Hyperbolic cosine 
  1412. o sin - Sine 
  1413. o sinh - Hyperbolic sine 
  1414.  
  1415. There is also an example of using the complex trigonometric functions. 
  1416.  
  1417.  
  1418. ΓòÉΓòÉΓòÉ 3.1.6.1. cos - Cosine ΓòÉΓòÉΓòÉ
  1419.  
  1420. Class: complex 
  1421.  
  1422. friend complex cos(complex x);
  1423.  
  1424. cos() returns the cosine of x. 
  1425.  
  1426.  
  1427. ΓòÉΓòÉΓòÉ 3.1.6.2. cosh -  Hyperbolic Cosine ΓòÉΓòÉΓòÉ
  1428.  
  1429. Class: complex 
  1430.  
  1431. friend complex cosh(complex x);
  1432.  
  1433. cosh() returns the hyperbolic cosine of x. Results of the Default 
  1434. Error-Handling Procedures shows the values returned by the default 
  1435. error-handling procedure for cosh(). 
  1436.  
  1437.  
  1438. ΓòÉΓòÉΓòÉ 3.1.6.3. sin - Sine ΓòÉΓòÉΓòÉ
  1439.  
  1440. Class: complex 
  1441.  
  1442. friend complex sin(complex x);
  1443.  
  1444. sin() returns the sine of x. 
  1445.  
  1446.  
  1447. ΓòÉΓòÉΓòÉ 3.1.6.4. sinh - Hyperbolic Sine ΓòÉΓòÉΓòÉ
  1448.  
  1449. Class: complex 
  1450.  
  1451. friend complex sinh(complex x);
  1452.  
  1453. sinh() returns the hyperbolic sine of x. Results of the Default Error-Handling 
  1454. Procedures shows the values returned by the default error-handling procedure 
  1455. for sinh(). 
  1456.  
  1457.  
  1458. ΓòÉΓòÉΓòÉ 3.1.6.5. Example of Using the Complex Trigonometric Functions ΓòÉΓòÉΓòÉ
  1459.  
  1460. The following example shows how you can use the complex trigonometric 
  1461. functions: 
  1462.  
  1463. #include <complex.h>
  1464.  
  1465. void main() {
  1466.    complex a  = (M_PI, M_PI_2);  // a = (pi,pi/2)
  1467.      // display the values of cos(), cosh(), sin(), and sinh()
  1468.      // for (pi,pi/2)
  1469.    cout << "The value of cos()  for (pi,pi/2) is: " << cos(a) << endl;
  1470.    cout << "The value of cosh() for (pi,pi/2) is: " << cosh(a) << endl;
  1471.    cout << "The value of sin()  for (pi,pi/2) is: " << sin(a) << endl;
  1472.    cout << "The value of sinh() for (pi,pi/2) is: " << sinh(a) << endl;
  1473.    }
  1474.  
  1475. This program produces the following output: 
  1476.  
  1477. The value of cos()  for (pi,pi/2) is: ( 6.12323e-17, 0)
  1478. The value of cosh() for (pi,pi/2) is: ( 2.50918, 0)
  1479. The value of sin()  for (pi,pi/2) is: ( 1, -0)
  1480. The value of sinh() for (pi,pi/2) is: ( 2.3013, 0)
  1481.  
  1482.  
  1483. ΓòÉΓòÉΓòÉ 3.1.7. Magnitude Functions for complex ΓòÉΓòÉΓòÉ
  1484.  
  1485. The following magnitude functions are described: 
  1486.  
  1487. o abs - Absolute value 
  1488. o norm - Square magnitude 
  1489.  
  1490.  
  1491. ΓòÉΓòÉΓòÉ 3.1.7.1. abs - Absolute Value ΓòÉΓòÉΓòÉ
  1492.  
  1493. Class: complex 
  1494.  
  1495. friend double abs(complex x);
  1496.  
  1497. abs() returns the absolute value or magnitude of its argument. The absolute 
  1498. value of a complex value (a,b) is the positive square root of a┬ñ+b┬ñ. 
  1499.  
  1500.  
  1501. ΓòÉΓòÉΓòÉ 3.1.7.2. norm - Square Magnitude ΓòÉΓòÉΓòÉ
  1502.  
  1503. Class: complex 
  1504.  
  1505. friend double norm(complex x);
  1506.  
  1507. norm() returns the square of the magnitude of its argument. If the argument x 
  1508. is equal to the complex number (a,b), norm() returns the value a┬ñ+b┬ñ. norm() is 
  1509. faster than  abs(), but it is more likely to cause overflow errors. 
  1510.  
  1511.  
  1512. ΓòÉΓòÉΓòÉ 3.1.8. Conversion Functions for complex ΓòÉΓòÉΓòÉ
  1513.  
  1514. The conversion functions in the Complex Mathematics Library allow you to 
  1515. convert between the polar and standard complex representations of a value and 
  1516. to extract the real and imaginary parts of a complex value. 
  1517.  
  1518. The following conversion functions are described: 
  1519.  
  1520. o arg - Angle in radians 
  1521. o conj - Conjugation 
  1522. o polar - Polar to complex 
  1523. o real - Extract real part 
  1524. o imag - Extract imaginary part 
  1525.  
  1526. There is also an example of using the complex conversion functions. 
  1527.  
  1528.  
  1529. ΓòÉΓòÉΓòÉ 3.1.8.1. arg - Angle in Radians ΓòÉΓòÉΓòÉ
  1530.  
  1531. Class: complex 
  1532.  
  1533. friend double arg(complex x);
  1534.  
  1535. arg() returns the angle (in radians) of the polar representation of its 
  1536. argument. If the argument x is equal to the complex number (a,b), the angle 
  1537. returned is the angle in radians on the complex plane between the real axis and 
  1538. the vector (a,b). The return value has a range of -pi  to pi . See  for an 
  1539. illustration of the polar representation of complex numbers. 
  1540.  
  1541.  
  1542. ΓòÉΓòÉΓòÉ 3.1.8.2. conj - Conjugation ΓòÉΓòÉΓòÉ
  1543.  
  1544. Class: complex 
  1545.  
  1546. friend complex conj(complex x);
  1547.  
  1548. conj() returns the complex value equal to (a,-b) if the input argument x is 
  1549. equal to (a,b). 
  1550.  
  1551.  
  1552. ΓòÉΓòÉΓòÉ 3.1.8.3. polar - Polar to Complex ΓòÉΓòÉΓòÉ
  1553.  
  1554. Class: complex 
  1555.  
  1556. friend complex polar(double a, double b= 0);
  1557.  
  1558. polar() returns the standard complex representation of the complex number that 
  1559. has a polar representation (a,b). 
  1560.  
  1561.  
  1562. ΓòÉΓòÉΓòÉ 3.1.8.4. real - Extract Real Part ΓòÉΓòÉΓòÉ
  1563.  
  1564. Class: complex 
  1565.  
  1566. friend double real(const complex& x);
  1567.  
  1568. real() extracts the real part of the complex number x. 
  1569.  
  1570.  
  1571. ΓòÉΓòÉΓòÉ 3.1.8.5. imag - Extract Imaginary Part ΓòÉΓòÉΓòÉ
  1572.  
  1573. Class: complex 
  1574.  
  1575. friend double imag(const complex& x);
  1576.  
  1577. imag() extracts the imaginary part of the complex number x. 
  1578.  
  1579.  
  1580. ΓòÉΓòÉΓòÉ 3.1.8.6. Example of Using the Complex Conversion Functions ΓòÉΓòÉΓòÉ
  1581.  
  1582. The following program shows how you can use the complex conversion functions: 
  1583.  
  1584.  
  1585. #include <complex.h>
  1586.  
  1587. void main() {
  1588.      complex a;
  1589.      // for a value supplied by the user, display the real part,
  1590.      // the imaginary part, and the polar representation
  1591.      cout << "enter a complex value" << endl;
  1592.      cin >> a;
  1593.      cout << "the real part of this value is " << real(a) << endl;
  1594.      cout << "the imaginary part of this value is " << imag(a) << endl;
  1595.      cout << "the polar representation of this value is "
  1596.           << "("<< abs(a)<< ","<< arg(a)<< ")"<< endl;     }
  1597.  
  1598. This program produces the following output for an input of (1,1): 
  1599.  
  1600. enter a complex value
  1601.  
  1602. the real part of this value is 1
  1603. the imaginary part of this value is 1
  1604. the polar representation of this value is (1.41421,0.785398)
  1605.  
  1606.  
  1607. ΓòÉΓòÉΓòÉ 3.1.9. Linking to the Complex Library ΓòÉΓòÉΓòÉ
  1608.  
  1609. You must specify the following library names when compiling or linking programs 
  1610. that use the Complex Library: 
  1611.  
  1612. o COMPLEX.LIB - for single-tasking programs 
  1613. o COMPLEXM.LIB - for multitasking programs. 
  1614.  
  1615. No dynamically linkable version of this library is provided. 
  1616.  
  1617.  
  1618. ΓòÉΓòÉΓòÉ 3.2. c_exception Class ΓòÉΓòÉΓòÉ
  1619.  
  1620. This chapter describes c_exception, the class that lets you handle errors that 
  1621. are created by the functions and operations in the complex class. 
  1622.  
  1623. o Declarations for c_exception in the complex.h header file 
  1624. o Constructor for c_exception 
  1625. o Data members of c_exception 
  1626. o Errors handled by the Complex Mathematics Library 
  1627. o Errors handled outside of the Complex Mathematics Library 
  1628.  
  1629. Note:  The c_exception class is not related to the C++ exception handling 
  1630. mechanism that uses the try, catch and throw statements. 
  1631.  
  1632.  
  1633. ΓòÉΓòÉΓòÉ 3.2.1. Declarations for c_exception in complex.h ΓòÉΓòÉΓòÉ
  1634.  
  1635. You must include the following statement in any file that uses members of the 
  1636. c_exception class: 
  1637.  
  1638. #include <complex.h>
  1639.  
  1640. The following is an excerpt from the complex.h header file that shows the 
  1641. relevant declarations for the members of c_exception: 
  1642.  
  1643. #define DOMAIN        1
  1644. #define SING          2
  1645. #define OVERFLOW      3
  1646. #define UNDERFLOW     4
  1647.  
  1648. class c_exception {
  1649.      int         type;
  1650.      char        *name;
  1651.      complex     arg1;
  1652.      complex     arg2;
  1653.      complex     retval;
  1654. public:
  1655.      c_exception( char *n, const complex& a1, const complex& a2 = complex_zero);
  1656.      friend int complex_error( c_exception& );
  1657.      friend complex exp( complex );
  1658.      friend complex sinh( complex );
  1659.      friend complex cosh( complex );
  1660.      friend complex log( complex );
  1661. };
  1662.  
  1663.  
  1664. ΓòÉΓòÉΓòÉ 3.2.2. Constructor for c_exception ΓòÉΓòÉΓòÉ
  1665.  
  1666. c_exception(char *n, const complex& a1,
  1667.                 const complex& a2 = complex_zero);
  1668.  
  1669. The c_exception constructor creates a c_exception object with name member equal 
  1670. to n, arg1 member equal to a1, and arg2 member equal to a2. 
  1671.  
  1672.  
  1673. ΓòÉΓòÉΓòÉ 3.2.3. Data Members of c_exception ΓòÉΓòÉΓòÉ
  1674.  
  1675. The following data members are described: 
  1676.  
  1677. o arg1, arg2 - Arguments of the function that caused the error 
  1678. o name - Name of the function that caused the error 
  1679. o retval - Value returned by the default definition of the error handling 
  1680.   function 
  1681. o type - Type of error that has occurred 
  1682.  
  1683.  
  1684. ΓòÉΓòÉΓòÉ 3.2.3.1. arg1, arg2 - Arguments of the Function that Caused the Error ΓòÉΓòÉΓòÉ
  1685.  
  1686. Class: c_exception 
  1687.  
  1688. complex arg1;
  1689. complex arg2;
  1690.  
  1691. arg1 and arg2 are the arguments with which the function that caused the error 
  1692. was called. 
  1693.  
  1694.  
  1695. ΓòÉΓòÉΓòÉ 3.2.3.2. name - Name of the Function that Caused the Error ΓòÉΓòÉΓòÉ
  1696.  
  1697. Class: c_exception 
  1698.  
  1699. char *name;
  1700.  
  1701. name is a string that contains the name of the function where the error 
  1702. occurred. 
  1703.  
  1704.  
  1705. ΓòÉΓòÉΓòÉ 3.2.3.3. retval - Value Returned by the Default Definition of the Error Handling Function ΓòÉΓòÉΓòÉ
  1706.  
  1707. Class: c_exception 
  1708.  
  1709. complex retval;
  1710.  
  1711. retval is the value that the default definition of the error handling function 
  1712. complex_error() returns. You can make your own definition of complex_error() 
  1713. return a different value. 
  1714.  
  1715.  
  1716. ΓòÉΓòÉΓòÉ 3.2.3.4. type - Type of Error that Has Occurred ΓòÉΓòÉΓòÉ
  1717.  
  1718. Class: c_exception 
  1719.  
  1720. int type;
  1721.  
  1722. type describes the type of error that has occurred. It can take the following 
  1723. values that are defined in the complex.h header file: 
  1724.  
  1725. o SING argument singularity 
  1726. o OVERFLOW overflow range error 
  1727. o UNDERFLOW underflow range error 
  1728.  
  1729.  
  1730. ΓòÉΓòÉΓòÉ 3.2.4. Errors Handled by the Complex Mathematics Library ΓòÉΓòÉΓòÉ
  1731.  
  1732. The following topics are described: 
  1733.  
  1734. o complex_error - the error-handling function 
  1735. o Results of the default error-handling procedures. 
  1736.  
  1737.  
  1738. ΓòÉΓòÉΓòÉ 3.2.4.1. complex_error - the Error-Handling Function ΓòÉΓòÉΓòÉ
  1739.  
  1740. Class: c_exception 
  1741.  
  1742. friend int complex_error(c_exception& ce);
  1743.  
  1744. complex_error() is invoked by member functions of the Complex Mathematics 
  1745. Library when errors are detected. The argument ce refers to the c_exception 
  1746. object that contains information about the error. You can define your own 
  1747. procedures for handling errors by defining a function called complex_error with 
  1748. return type int and a single parameter of type c_exception&. 
  1749.  
  1750. If you define your own complex_error function and this function returns a 
  1751. nonzero value, no error message will be generated and the external variable 
  1752. errno will not be set. If this function returns zero, errno is given the value 
  1753. of one of the following constants: 
  1754.  
  1755. o ERANGE if the result is too large or too small 
  1756. o EDOM if there is a domain error within a mathematical function. 
  1757.  
  1758. These constants are defined in errno.h. 
  1759.  
  1760. If you define your own version of complex_error, when you compile your program 
  1761. you must: 
  1762.  
  1763. o Ensure that the name of the source file or .OBJ that contains your version of 
  1764.   complex_error appears on the command line before complex.lib. 
  1765.  
  1766. o Use the /NOE linker option 
  1767.  
  1768. For example, if the source file containing your definition of complex_error is 
  1769. source1.cpp, then you would invoke the compiler like this: 
  1770.  
  1771. icc source1.cpp complex.lib /B"/NOE"
  1772.  
  1773.  
  1774. ΓòÉΓòÉΓòÉ 3.2.4.2. Results of the Default Error-Handling Procedures ΓòÉΓòÉΓòÉ
  1775.  
  1776. Class: c_exception 
  1777.  
  1778. If you do not define your own complex_error, the default error-handling 
  1779. procedures will be invoked when an error occurs. The results for a given input 
  1780. complex value (a, b) depend on the kind of error and the sign of the cosine and 
  1781. sine of b. The following table shows the return value of the default 
  1782. error-handling procedure and the value given to errno for each function with 
  1783. input equal to the complex value (a, b). 
  1784.  
  1785. Note:  The following symbols appear in this table: 
  1786.  
  1787.  1. NA - not applicable. The result of the error depends on the sign of the 
  1788.     cosine and sine of b (the imaginary part of the argument) unless "NA" 
  1789.     appears in the Cosine b or Sine b columns. 
  1790.  
  1791.  2. HUGE - the maximum double value. This value is defined in math.h. 
  1792.  
  1793. Function Error        Cosine b  Sine b   Return Value   errno Value
  1794.  
  1795. cosh     a too large  nonneg.   nonneg.  (+HUGE,+HUGE)  ERANGE
  1796. cosh     a too large  nonneg.   nenneg.  (+HUGE,-HUGE)  ERANGE
  1797. cosh     a too small  nonneg.   nonneg.  (+HUGE,-HUGE)  ERANGE
  1798. cosh     a too small  nonneg.   nenneg.  (+HUGE,+HUGE)  ERANGE
  1799. cosh     a too small  negative  nonneg.  (-HUGE,-HUGE)  ERANGE
  1800. cosh     a too small  negative  nenneg.  (-HUGE,+HUGE)  ERANGE
  1801. cosh     b too large  negative  nonneg.  (-HUGE,+HUGE)  ERANGE
  1802. cosh     b too large  negative  nenneg.  (-HUGE,-HUGE)  ERANGE
  1803. cosh     b too small  NA        NA       (0,0)          ERANGE
  1804. exp      a too large  positive  positive (+HUGE,+HUGE)  ERANGE
  1805. exp      a too large  positive  positive (+HUGE,-HUGE)  ERANGE
  1806. exp      a too large  nonpos.   positive (-HUGE,+HUGE)  ERANGE
  1807. exp      a too large  nonpos.   positive (-HUGE,-HUGE)  ERANGE
  1808. exp      a too small  NA        NA       (0,0)          ERANGE
  1809. exp      b too large  NA        NA       (0,0)          ERANGE
  1810. exp      b too small  NA        NA       (0,0)          ERANGE
  1811. log      a too large  positive  positive (+HUGE,0)      EDOM
  1812.                                   (a message is also produced)
  1813. sinh     a too large  nonneg.   nonneg.  (+HUGE,+HUGE)  ERANGE
  1814. sinh     a too large  nonneg.   negative (+HUGE,-HUGE)  ERANGE
  1815. sinh     a too large  negative  nonneg.  (-HUGE,+HUGE)  ERANGE
  1816. sinh     a too large  negative  negative (-HUGE,-HUGE)  ERANGE
  1817. sinh     a too small  nonneg.   nonneg.  (-HUGE,+HUGE)  ERANGE
  1818. sinh     a too small  nonneg.   negative (-HUGE,-HUGE)  ERANGE
  1819. sinh     a too small  negative  nonneg.  (+HUGE,+HUGE)  ERANGE
  1820. sinh     a too small  negative  negative (+HUGE,-HUGE)  ERANGE
  1821. sinh     b too large  NA        NA       (0,0)          ERANGE
  1822. sinh     b too small  NA        NA       (0,0)          ERANGE
  1823.  
  1824. Note:  errno is set to EDOM when the error for log() is detected. The message 
  1825. is stored in DDE4.MSG. The message number in DDE4.MSG is 90. When this message 
  1826. is displayed by the C/C++ Runtime Library, it is changed to 5090. For 
  1827. information on binding this message, see "Binding Runtime Messages" in the IBM 
  1828. C/C++ Tools: Programming Guide. 
  1829.  
  1830.  
  1831. ΓòÉΓòÉΓòÉ 3.2.5. Errors Handled Outside of the Complex Mathematics Library ΓòÉΓòÉΓòÉ
  1832.  
  1833. There are some cases where member functions of the Complex Mathematics Library 
  1834. call functions in the math library. These calls can cause underflow and 
  1835. overflow conditions that are handled by the matherr() function that is declared 
  1836. in the math.h header file. For example, the overflow conditions that are caused 
  1837. by the following calls are handled by matherr(): 
  1838.  
  1839. o exp(complex(MAXDOUBLE, MAXDOUBLE)) 
  1840. o pow(complex(MAXDOUBLE, MAXDOUBLE), INT_MAX) 
  1841. o norm(complex(MAXDOUBLE, MAXDOUBLE)) 
  1842.  
  1843. MAXDOUBLE is the maximum valid double value. INT_MAX is the maximum int value. 
  1844. Both these constants are defined in limits.h. 
  1845.  
  1846. If you do not want the default error-handling defined by matherr(), you should 
  1847. define your own version of matherr(). 
  1848.  
  1849.  
  1850. ΓòÉΓòÉΓòÉ 4. The I/O Stream Library ΓòÉΓòÉΓòÉ
  1851.  
  1852. Introduction to the I/O Stream Library 
  1853.   Introduces the I/O Stream Library, its classes and header files, and 
  1854.   describes stream buffers. 
  1855. streambuf Protected Interface 
  1856.   Describes the protected members of the streambuf class. You can use these 
  1857.   members to create classes derived from streambuf. 
  1858. streambuf Public Interface 
  1859.   Describes the public members of the streambuf class. You can use these 
  1860.   members to manipulate objects of filebuf, stdiobuf, or strstreambuf, the 
  1861.   predefined classes derived from streambuf. 
  1862. ios Class 
  1863.   Describes ios, the base class for the classes that format data that comes 
  1864.   from the stream buffer. 
  1865. istream and istream_withassign Class 
  1866.   Describes istream, the class that lets you extract data from a stream buffer, 
  1867.   and istream_withassign, a class derived from istream that includes an 
  1868.   assignment operator. 
  1869. ostream and ostream_withassign Classes 
  1870.   Describes ostream, the class that lets you insert data into a stream buffer, 
  1871.   and ostream_withassign, a class derived from ostream that includes an 
  1872.   assignment operator. 
  1873. iostream and iostream_withassign Classes 
  1874.   Describes iostream, the class derived from istream and ostream, and 
  1875.   iostream_withassign, the class derived from istream_withassign and 
  1876.   ostream_withassign. 
  1877. filebuf Class 
  1878.   Describes filebuf, the class that specializes streambuf to use files. 
  1879. fstream, ifstream, and ofstream Classes 
  1880.   Describes fstream, ifstream, and ofstream, the classes that adapt istream, 
  1881.   ostream, and iostream, respectively, to use files. 
  1882. strstreambuf Class 
  1883.   Describes strstreambuf, the class that specializes streambuf to use an array 
  1884.   of bytes in memory as the source or target of data. 
  1885. strstream, istrstream, and ostrstream Classes 
  1886.   Describes istrstream, ostrsteam, and strstream, the classes that adapt 
  1887.   istream, ostream, and iostream, respectively, to use an array of bytes in 
  1888.   memory as the source or target of data. 
  1889. stdiobuf and stdiostream Classes 
  1890.   Describes stdiobuf, the class that lets you mix standard C input and output 
  1891.   functions with C++ I/O Stream Library functions. 
  1892. Manipulators 
  1893.   Describes the parameterized manipulators that allow you to use the input or 
  1894.   output operator to change the state of a stream. 
  1895.  
  1896.  
  1897. ΓòÉΓòÉΓòÉ 4.1. Introduction to the I/O Stream Library ΓòÉΓòÉΓòÉ
  1898.  
  1899. This chapter describes the overall structure of the I/O Stream Library. This 
  1900. class library provides you with the facilities to deal with many varieties of 
  1901. input and output. 
  1902.  
  1903. The following topics are described in this chapter: 
  1904.  
  1905. o Linking to the I/O Stream Library 
  1906. o The I/O Stream Library and stdio.h in C 
  1907. o Overview of the I/O Stream Library 
  1908. o The I/O Stream Library class hierarchy 
  1909. o The I/O Stream Library header files 
  1910. o Predefined streams 
  1911. o Anonymous streams 
  1912. o Stream buffers 
  1913.  
  1914.  
  1915. ΓòÉΓòÉΓòÉ 4.1.1. Linking to the I/O Stream Library ΓòÉΓòÉΓòÉ
  1916.  
  1917. The I/O Stream Library is part of the C/C++ libraries and is linked in 
  1918. automatically unless you specify the /Gn option. 
  1919.  
  1920.  
  1921. ΓòÉΓòÉΓòÉ 4.1.2. The I/O Stream Library and stdio.h ΓòÉΓòÉΓòÉ
  1922.  
  1923. In both C++ and C, input and output are described in terms of sequences of 
  1924. characters, or streams. The I/O Stream Library provides the same facilities in 
  1925. C++ that stdio.h provides in C, but it also has the following advantages over 
  1926. stdio.h: 
  1927.  
  1928. o The input or extraction (>>) operator and the output or insertion (<<) 
  1929.   operator are typesafe. They are also easy to use. 
  1930. o You can define input and output for your own types or classes by overloading 
  1931.   the input and output operators. This gives you a uniform way of performing 
  1932.   input and output for different types of data. 
  1933. o The input and output operators are more efficient than scanf() and printf(), 
  1934.   the analogous C functions defined in stdio.h. Both scanf() and printf() take 
  1935.   format strings as arguments, and these format strings have to be parsed at 
  1936.   run time. The bindings for the output and input operators are performed at 
  1937.   compile time. 
  1938.  
  1939.  
  1940. ΓòÉΓòÉΓòÉ 4.1.3. Overview of the I/O Stream Library ΓòÉΓòÉΓòÉ
  1941.  
  1942. The I/O Stream Library is the standard input and output library for C++. In 
  1943. C++, input and output are described in terms of sequences of characters, or 
  1944. streams. The processing of these streams is done at two levels. The first level 
  1945. treats the data as sequences of characters; the second level treats it as a 
  1946. series of values of a particular type. 
  1947.  
  1948. There are two primary base classes for the I/O Stream Library: 
  1949.  
  1950.  1. The streambuf class and the classes derived from it (strstream, stdiobuf, 
  1951.     and filebuf) implement the stream buffers. Stream buffers act as temporary 
  1952.     repositories for characters that are coming from the ultimate producers of 
  1953.     input or are being sent to the ultimate consumers of output. See Stream 
  1954.     Buffers for more details. 
  1955.  
  1956.  2. The ios class maintains formatting and error state information for these 
  1957.     streams. The classes derived from ios implement the formatting of these 
  1958.     streams. This formatting involves converting sequences of characters from 
  1959.     the stream buffer into values of a particular type and converting values of 
  1960.     a particular type into their external display format. 
  1961.  
  1962. The I/O Stream Library predefines streams for standard input, standard output, 
  1963. and standard error. See Predefined Streams for more details on the predefined 
  1964. streams. If you want to open your own streams for input or output, you must 
  1965. create an iostream object. The iostream constructor takes as an argument a 
  1966. pointer to a streambuf object. This object is associated with the device, file, 
  1967. or array of bytes in memory that is going to be the ultimate producer of input 
  1968. or the ultimate consumer of output. 
  1969.  
  1970.  
  1971. ΓòÉΓòÉΓòÉ 4.1.3.1. Combining Input and Output of Different Types ΓòÉΓòÉΓòÉ
  1972.  
  1973. The I/O Stream Library overloads the input (>>) and output (<<) operators for 
  1974. the built-in types. As a result, you can combine input or output of values with 
  1975. different types in a single statement without having to state the type of the 
  1976. values. The following example shows how values with a variety of types can be 
  1977. combined in a single output statement: 
  1978.  
  1979.  
  1980. #include <iostream.h>
  1981.  
  1982. void main(){
  1983.      int i = 3;
  1984.      long l = 88888888L;
  1985.      double d = 3.14;
  1986.      float f = 2.1F;
  1987.      char c = 'b';
  1988.      cout << "A variety of values: int " << i
  1989.           << ", long " << l << ", double "<< d << ",\n"
  1990.           << "float " << f << ", char " << c<< "." << endl;
  1991. }
  1992.  
  1993. This example produces the following output: 
  1994.  
  1995. A variety of values: int 3, long 88888888, double 3.14,
  1996. float 2.1, char b.
  1997.  
  1998.  
  1999. ΓòÉΓòÉΓòÉ 4.1.3.2. Input and Output for User-Defined Classes ΓòÉΓòÉΓòÉ
  2000.  
  2001. You can overload the input and output operators for the classes that you create 
  2002. yourself. Once you have overloaded the input and output operators for a class, 
  2003. you can perform input and output operations on objects of that class in the 
  2004. same way that you would perform input and output on char, int, double, and the 
  2005. other built-in types. 
  2006.  
  2007. The following example shows how you can overload the input and output operators 
  2008. for your own classes. The class circle has two private members that record the 
  2009. center and radius of a circle. In the example, the value of a circle object is 
  2010. printed using the overloaded output operator. The user is then prompted to 
  2011. supply values that are assigned to another circle object. The value of this 
  2012. circle object is then printed. 
  2013.  
  2014.  
  2015. #include <iostream.h>
  2016.  
  2017. class Circle {
  2018. public:
  2019.      Circle(double c=0.0, double r=0.0): center(c), radius(r){}
  2020.  
  2021. private:
  2022.      double center;
  2023.      double radius;
  2024.      friend ostream& operator<<(ostream& o, const Circle& c);
  2025.      friend istream& operator>>(istream& i, Circle& c);
  2026. };
  2027.  
  2028. ostream& operator<<(ostream& o, const Circle& c)
  2029. {
  2030.      o << c.center << " " << c.radius << endl;
  2031.      return o;
  2032. }
  2033.  
  2034. istream& operator>>(istream& i, Circle& c)
  2035. {
  2036.      i >> c.center >> c.radius;
  2037.      return i;
  2038. }
  2039.  
  2040. void main()
  2041. {
  2042.      Circle c(1.5, 3.0);
  2043.      Circle d;
  2044.      cout << "Circle c " << c << endl;
  2045.      cout << "Enter the center and radius of yourcircle: " << endl;
  2046.      cin >> d;
  2047.      cout << "Here is your circle " << d << endl;
  2048. }
  2049.  
  2050. If you supply the values 3.0, 5.6 to this program, the following output is 
  2051. produced: 
  2052.  
  2053. Circle c 1.5 3
  2054. Enter the center and radius of your circle:
  2055. Here is your circle 3 5.6
  2056.  
  2057.  
  2058. ΓòÉΓòÉΓòÉ 4.1.4. The I/O Stream Library Class Hierarchy ΓòÉΓòÉΓòÉ
  2059.  
  2060. The I/O Stream Library has two base classes that correspond to the two levels 
  2061. of processing described in "Overview of the Stream Library": 
  2062.  
  2063. o The streambuf class implements stream buffers (see Stream Buffers).  It is 
  2064.   the base class for the following classes: 
  2065.  
  2066.    - strstreambuf 
  2067.    - stdiobuf 
  2068.    - filebuf 
  2069.  
  2070. o The ios class maintains formatting and error state information for streams. 
  2071.   Streams are implemented as objects of the following classes that are derived 
  2072.   from ios: 
  2073.  
  2074.    - stdiostream 
  2075.    - istream 
  2076.    - ostream 
  2077.  
  2078. The classes that are derived from ios are themselves base classes: 
  2079.  
  2080. o istream is the input stream class. It implements stream buffer input, or 
  2081.   input operations. The following classes are derived from istream: 
  2082.  
  2083.    - istrstream 
  2084.    - ifstream 
  2085.    - istream_withassign 
  2086.    - iostream 
  2087.  
  2088. o ostream is the output stream class. It implements stream buffer output, or 
  2089.   output operations. The following classes are derived from ostream: 
  2090.  
  2091.    - ostrsteam 
  2092.    - ofstream 
  2093.    - ostream_withassign 
  2094.    - iostream 
  2095.  
  2096. o iostream is the class that combines istream and ostream to implement input 
  2097.   and output to stream buffers. The following classes are derived from 
  2098.   iostream: 
  2099.  
  2100.    - strstream 
  2101.    - iostream_withassign 
  2102.    - fstream 
  2103.  
  2104. Note:  The I/O Stream Library defines other classes in addition to those listed 
  2105. above. These classes include fstreambase and strstreambase. These classes are 
  2106. meant for the internal use of the I/O Stream Library. They are described in 
  2107. this book because their member functions are used directly by one or more of 
  2108. the classes listed above. You should not use these internal classes directly. 
  2109.  
  2110. Derivation Relationships in the I/O Stream Library shows the relationship 
  2111. between the two base classes, ios and streambuf, and their derived classes. In 
  2112. the figure, two classes are connected by an arrow if the class at the pointed 
  2113. end of the arrow is derived from the class at the other end of the arrow. 
  2114.  
  2115.              ios
  2116.         ΓöîΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÿΓöéΓööΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÉ
  2117.       istream   stdiostream  ostream
  2118.   ΓöîΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÿ Γöé Γöé ΓööΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÉ  ΓöîΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÿ Γöé Γöé ΓööΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÉ
  2119.   Γöé     Γöé ΓööΓöÇΓöÇΓöÇΓöÉ   Γöé  Γöé   ΓöîΓöÇΓöÇΓöÇΓöÿ ΓööΓöÇΓöÉ    Γöé
  2120.  istream_   Γöé ifstream Γöé  Γöé  ostream_  Γöé  ofstream
  2121. withassign   Γöé      Γöé  Γöé withassign  Γöé
  2122.      istrstream    Γöé  Γöé     ostrstream
  2123.             iostream
  2124.         ΓöîΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÿΓöéΓööΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÉ
  2125.       iostream_  strstream   fstream
  2126.      withassign
  2127.  
  2128.             streambuf
  2129.         ΓöîΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÿΓöéΓööΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÉ
  2130.      strstreambuf  stdiobuf   filebuf
  2131.  
  2132. Derivation Relationships in the I/O Stream Library 
  2133.  
  2134.  
  2135. ΓòÉΓòÉΓòÉ 4.1.5. The I/O Stream Library Header Files ΓòÉΓòÉΓòÉ
  2136.  
  2137. To use a class in the I/O Stream Library, you must include the appropriate 
  2138. header files for that class. The following is a list of the I/O Stream Library 
  2139. header files and the classes that they cover: 
  2140.  
  2141. o iostream.h contains declarations for the basic classes of the library: 
  2142.  
  2143.    - streambuf 
  2144.    - ios 
  2145.    - istream 
  2146.    - istream_withassign 
  2147.    - ostream 
  2148.    - ostream_withassign 
  2149.    - iostream 
  2150.    - iostream_withassign 
  2151.  
  2152. o fstream.h contains declarations for the classes that deal with files: 
  2153.  
  2154.    - filebuf 
  2155.    - ifstream 
  2156.    - ofstream 
  2157.    - fstream 
  2158.  
  2159. o stdiostr.h contains declarations for stdiobuf and stdiostream, the classes 
  2160.   that specialize streambuf and ios, respectively, to use the FILE structures 
  2161.   defined in the C header file stdio.h. 
  2162.  
  2163. o strstrea.h contains declarations for the classes that deal with character 
  2164.   strings. The first "str" in each of these names stands for "string": 
  2165.  
  2166.    - istrstream 
  2167.    - ostrsteam 
  2168.    - strstream 
  2169.    - strstreambuf 
  2170.  
  2171. o iomanip.h contains declarations for the parameterized manipulators. 
  2172.   Manipulators are values that you can insert into streams or extract from 
  2173.   streams to affect or query the behavior of the streams. 
  2174.  
  2175. o stream.h is used for compatibility with earlier versions of the I/O Stream 
  2176.   Library. It includes iostream.h, fstream.h, stdiostr.h, and iomanip.h, along 
  2177.   with some definitions needed for compatibility with the AT&T** C++ Language 
  2178.   System Release 1.2. Only use this header file with existing code; do not use 
  2179.   it with new C++ code. 
  2180.  
  2181.   Note:  If you use the obsolete function form() declared in stream.h, there is 
  2182.   a limit to the size of the format specifier.  If you call form() with a 
  2183.   format specifier string longer than this limit, a runtime message (EDC5091) 
  2184.   will be generated and the program will terminate. 
  2185.  
  2186.  
  2187. ΓòÉΓòÉΓòÉ 4.1.6. Predefined Streams ΓòÉΓòÉΓòÉ
  2188.  
  2189. In addition to giving you the facilities to define your own streams for input 
  2190. and output, the I/O Stream Library also provides the following predefined 
  2191. streams: 
  2192.  
  2193. o cin is the standard input stream. 
  2194.  
  2195. o cout is the standard output stream. 
  2196.  
  2197. o cerr is the standard error stream. Output to this stream is unit-buffered. 
  2198.   Characters that are sent to this stream are flushed after each output 
  2199.   operation. 
  2200.  
  2201. o clog is also an error stream, but unlike the output to cerr, the output to 
  2202.   clog is buffered. 
  2203.  
  2204. The predefined streams are initialized before the constructors for any static 
  2205. objects are called. You can use the predefined streams in the constructors for 
  2206. static objects. 
  2207.  
  2208. The predefined streams cin, cerr, and clog are tied  to cout. As a result, if 
  2209. you use cin, cerr, or clog, cout is flushed. That is, the contents of cout are 
  2210. sent to their ultimate consumer. 
  2211.  
  2212.  
  2213. ΓòÉΓòÉΓòÉ 4.1.7. Anonymous Streams ΓòÉΓòÉΓòÉ
  2214.  
  2215. An anonymous stream is a stream that is created as a temporary object. Because 
  2216. it is a temporary object, an anonymous stream requires a const type modifier 
  2217. and is not a modifiable lvalue. Unlike the AT&T** C++ Language System Release 
  2218. 2.1., the C/C++ Tools does not allow a non-const reference argument to be 
  2219. matched with a temporary object. User-defined input and output operators 
  2220. usually accept a non-const reference (such as a reference to an istream or 
  2221. ostream object) as an argument. Such an argument cannot be initialized by an 
  2222. anonymous stream, and thus an attempt to use an anonymous stream as an argument 
  2223. to a user-defined input or output operator will usually result in a 
  2224. compile-time error. 
  2225.  
  2226. In the following example, an object of the class Circle (defined in Input and 
  2227. Output for User-Defined Classes) is declared, and the value of an anonymous 
  2228. stream is assigned to it using the input operator. The following statement: 
  2229.  
  2230. Circle c;
  2231. istrstream("10 15") >> c;
  2232.  
  2233. causes the following compile-time error: 
  2234.  
  2235. CIRCLE.C(32:1) : error EDC3070: Call does not match any argument list for 
  2236. "istream::operator>>". 
  2237.  
  2238.  
  2239. ΓòÉΓòÉΓòÉ 4.1.8. Stream Buffers ΓòÉΓòÉΓòÉ
  2240.  
  2241. One of the most important concepts in the I/O Stream Library is the stream 
  2242. buffer. The streambuf class implements some of the member functions that define 
  2243. stream buffers, but other specialized member functions are left to the classes 
  2244. that are derived from streambuf: strstreambuf, stdiobuf, and filebuf. 
  2245.  
  2246. Note:  The AT&T** and UNIX** System Laboratories C++ Language System 
  2247. documentation use the terms reserve area and buffer instead of stream buffer. 
  2248.  
  2249.  
  2250. ΓòÉΓòÉΓòÉ 4.1.8.1. What Does a Stream Buffer Do? ΓòÉΓòÉΓòÉ
  2251.  
  2252. A stream buffer acts as a buffer between the ultimate producer (the source of 
  2253. data) or ultimate consumer (the target of data) and the member functions of the 
  2254. classes derived from ios that format this raw data. The ultimate producer can 
  2255. be a file, a device, or an array of bytes in memory. The ultimate consumer can 
  2256. also be a file, a device, or an array of bytes in memory. 
  2257.  
  2258.  
  2259. ΓòÉΓòÉΓòÉ 4.1.8.2. Why Use a Stream Buffer? ΓòÉΓòÉΓòÉ
  2260.  
  2261. In most operating systems, a system call to read data from the ultimate 
  2262. producer or write it to the ultimate consumer is an expensive operation. If 
  2263. your applications can reduce the number of system calls they have to make, they 
  2264. will usually be more efficient.  By acting as a buffer between the ultimate 
  2265. producer or ultimate consumer and the formatting functions, a stream buffer can 
  2266. reduce the number of system calls that are made. 
  2267.  
  2268. Consider, for example, an application that is reading data from the ultimate 
  2269. producer. If there is no buffer, the application has to make a system call for 
  2270. each character that is read. However, if the application uses a stream buffer, 
  2271. system calls will only be made when the buffer is empty. Each system call will 
  2272. read enough characters from the ultimate producer (if they are available) to 
  2273. fill the buffer again. 
  2274.  
  2275.  
  2276. ΓòÉΓòÉΓòÉ 4.1.8.3. How Is a Stream Buffer Implemented? ΓòÉΓòÉΓòÉ
  2277.  
  2278. A stream buffer is implemented as an array of bytes. For each stream buffer, 
  2279. pointers are defined that point to elements in this array to define the get 
  2280. area, or the space that is available to accept bytes from the ultimate 
  2281. producer, and the put area, or the space that is available to store bytes that 
  2282. are on their way to the ultimate consumer. 
  2283.  
  2284. A stream buffer does not necessarily have separate get and put areas. A stream 
  2285. buffer that is used for input, such as one that is attached to an istream 
  2286. object, has a get area. A stream buffer that is used for output, such as one 
  2287. that is attached to an ostream object, has a put area. A stream buffer that is 
  2288. used for both input and output, such as one that is attached to an iostream 
  2289. object, has both a get area and a put area. In stream buffers implemented by 
  2290. the filebuf class that are specialized to use files as an ultimate producer or 
  2291. ultimate consumer, the get and put areas overlap. 
  2292.  
  2293. The following member functions of the streambuf class return pointers to 
  2294. boundaries of areas in a stream buffer: 
  2295.  
  2296. o base() returns a pointer to the beginning of the stream buffer. 
  2297. o eback() returns a pointer to the beginning of the space available for 
  2298.   putback. Characters that are putback are returned to the get area of the 
  2299.   stream buffer. 
  2300. o gptr() returns the get pointer, a pointer to the beginning of the get area. 
  2301.   The space between gptr() and egptr() has been filled by the ultimate 
  2302.   producer. These characters are waiting to be extracted from the stream 
  2303.   buffer. The space between  eback() and gptr() is available for putback. 
  2304. o egptr() returns a pointer to the end of the get area. 
  2305. o pbase() returns a pointer to the beginning of the space available for the put 
  2306.   area. 
  2307. o pptr() returns the put pointer, a pointer to the beginning of the put area. 
  2308.   The space between pbase() and pptr() is filled with bytes that are waiting to 
  2309.   be sent to the ultimate consumer. The space between pptr() and epptr() is 
  2310.   available to accept characters from the application program that are on their 
  2311.   way to the ultimate consumer. 
  2312. o epptr() returns a pointer to the end of the put area. 
  2313. o ebuf() returns a pointer to the end of the stream buffer. 
  2314.  
  2315. Note:  In the actual implementation of stream buffers, the pointers returned by 
  2316. these functions point at char values. In the abstract concept of stream 
  2317. buffers, on the other hand, these pointers point to the boundary between char 
  2318. values. To establish a correspondence between the abstract concept and the 
  2319. actual implementation, you should think of the pointers as pointing just before 
  2320. the character that they actually point at. 
  2321.  
  2322. The Structure of Stream Buffers shows how the pointers returned by these 
  2323. functions delineate the stream buffer. 
  2324.  
  2325.  
  2326.     ΓöîΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇ  ΓöîΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÉ ΓöÇΓöÇΓöÇΓöÇbase()
  2327.     Γöé        Γö£ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöñ
  2328.     Γöé        Γöé    ΓöÇΓöÇΓöÇΓöÇ    Γöé
  2329.     Γöé        Γö£ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöñ ΓöÇΓöÇΓöÇΓöÇeback()
  2330.     Γöé      ΓöîΓöÇΓöÇ  Γö£ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöñ ΓöÇΓöÇΓöÇΓöÇgptr()
  2331.     Γöé      Γöé   Γö£ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöñ
  2332.     Γöé get area Γöñ   Γöé    ΓöÇΓöÇΓöÇΓöÇ    Γöé ΓöÇULTIMATE PRODUCER
  2333.     Γöé      Γöé   Γö£ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöñ
  2334.     Γöé      ΓööΓöÇΓöÇ  Γö£ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöñ ΓöÇΓöÇΓöÇΓöÇegptr()
  2335. stream Γöñ        Γöé    ΓöÇΓöÇΓöÇΓöÇ    Γöé
  2336. buffer Γöé        Γö£ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöñ ΓöÇΓöÇΓöÇΓöÇpbase()
  2337.     Γöé      ΓöîΓöÇΓöÇ  Γö£ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöñ ΓöÇΓöÇΓöÇΓöÇpptr()
  2338.     Γöé      Γöé   Γö£ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöñ
  2339.     Γöé put area Γöñ   Γöé    ΓöÇΓöÇΓöÇΓöÇ    Γöé ΓöÇULTIMATE CONSUMER
  2340.     Γöé      Γöé   Γö£ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöñ
  2341.     Γöé      ΓööΓöÇΓöÇ  Γö£ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöñ ΓöÇΓöÇΓöÇΓöÇepptr()
  2342.     Γöé        Γöé    ΓöÇΓöÇΓöÇΓöÇ    Γöé
  2343.     Γöé        Γö£ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöñ
  2344.     ΓööΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇ  ΓööΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÿ ΓöÇΓöÇΓöÇΓöÇebuf()
  2345.  
  2346. The Structure of Stream Buffers 
  2347.  
  2348.  
  2349. ΓòÉΓòÉΓòÉ 4.2. streambuf Protected Interface ΓòÉΓòÉΓòÉ
  2350.  
  2351. This chapter describes the protected interface of the streambuf class. The 
  2352. protected interface consists of the member functions of streambuf that you need 
  2353. to use to derive your own classes from streambuf. See What Is the streambuf 
  2354. Protected Interface? for more details. 
  2355.  
  2356. The streambuf class implements the concept of stream buffers. A stream buffer 
  2357. acts as a buffer between the ultimate producer or ultimate consumer of data and 
  2358. the member functions of the classes derived from ios (such as istream and 
  2359. ostream) that format this data. See Stream Buffers for a more detailed 
  2360. description of stream buffers. 
  2361.  
  2362. The following topics are described in this chapter: 
  2363.  
  2364. o What is the streambuf protected interface? 
  2365. o Declarations for the streambuf protected interface in iostream.h 
  2366. o Examining pointers in the protected interface 
  2367. o Setting pointers in the protected interface 
  2368. o Other nonvirtual members 
  2369. o Virtual member functions 
  2370.  
  2371.  
  2372. ΓòÉΓòÉΓòÉ 4.2.1. What Is the streambuf Protected Interface? ΓòÉΓòÉΓòÉ
  2373.  
  2374. Although streambuf is not defined as a virtual base class, you can think of it 
  2375. as one. You should not create objects of the streambuf class itself, but you 
  2376. can use it in the following ways: 
  2377.  
  2378. o As a base class to implement your own specialized stream buffers. In this 
  2379.   sense you can think of streambuf as a virtual base class. The streambuf class 
  2380.   only provides the basic functions needed to manipulate characters in a stream 
  2381.   buffer. The filebuf, strstreambuf, and stdiobuf classes contain functions 
  2382.   that handle the interface with the standard ultimate consumers and producers. 
  2383.   If you want to perform more sophisticated operations, or if you want to use 
  2384.   other ultimate consumers and ultimate producers, you will have to create your 
  2385.   own class derived from streambuf. This chapter describes the members of the 
  2386.   streambuf class that you need to know about if you want to create a class 
  2387.   derived from streambuf. Collectively, these members are called the protected 
  2388.   interface of the streambuf class. 
  2389.  
  2390. o Through a predefined class derived from streambuf. This use of the streambuf 
  2391.   class is described in streambuf Public Interface. 
  2392.  
  2393. There are two kinds of functions in the protected interface described in this 
  2394. chapter: 
  2395.  
  2396. o Nonvirtual member functions, which manipulate streambuf objects at a level of 
  2397.   detail that would be inappropriate in the public interface. 
  2398.  
  2399. o Virtual member functions, which permit classes that you derive from streambuf 
  2400.   to customize their operations depending on the ultimate producer or ultimate 
  2401.   consumer. When you define the virtual functions in your derived classes, you 
  2402.   must ensure that these definitions fulfill the conditions stated in the 
  2403.   descriptions of the virtual functions. If your definitions of the virtual 
  2404.   functions do not fulfill these conditions, objects of the derived class may 
  2405.   have unspecified behavior. Although most virtual functions are declared as 
  2406.   public members, they are described with the protected interface (with the 
  2407.   exception of the destructor for the streambuf class) because they are meant 
  2408.   to be overridden in the classes that you derive from streambuf. 
  2409.  
  2410.  
  2411. ΓòÉΓòÉΓòÉ 4.2.2. Declarations for the streambuf Protected Interface in iostream.h ΓòÉΓòÉΓòÉ
  2412.  
  2413. You must include the following statement in any file that uses the streambuf 
  2414. protected interface: 
  2415.  
  2416. #include <iostream.h>
  2417.  
  2418. The following is an excerpt from the iostream.h header file that shows the 
  2419. relevant declarations for the protected interface of streambuf. Note that this 
  2420. excerpt only shows the declarations in the streambuf class that are relevant to 
  2421. the protected interface. The declarations for the public interface are listed 
  2422. in Declarations for the streambuf Public Interface in iostream.h. 
  2423.  
  2424. class ios {
  2425. public:
  2426.      enum io_state     { goodbit, eofbit, failbit, badbit,
  2427.                          hardfail};
  2428.      enum open_mode    { in, out, ate, app, trunc,
  2429.                          nocreate, noreplace, bin, binary=bin} ;
  2430.      enum seek_dir     { beg, cur, end } ;
  2431.      // .
  2432.      // .
  2433.      // .
  2434. };
  2435. class streambuf {
  2436. public:
  2437.      void            dbp();
  2438.      virtual int     overflow(int c = EOF);
  2439.      virtual int     underflow();
  2440.      virtual int     pbackfail(int c);
  2441.      virtual int     sync();
  2442.      virtual streambuf* setbuf(char* p, int len);
  2443.      streambuf*      setbuf(unsigned char* p, int len);
  2444.      streambuf*      setbuf(char* p, int len, int count);
  2445.      virtual streampos  seekoff(streamoff, seek_dir,
  2446.                                int=ios::in|ios::out);
  2447.      virtual streampos   seekpos(streampos,
  2448.                                  int = ios::in|ios::out);
  2449.      virtual int     xsgetn(char* s, int n);
  2450.      virtual int     xsputn(const char* s, int n);
  2451. protected:
  2452.      char*           base();
  2453.      char*           pbase();
  2454.      char*           pptr();
  2455.      char*           epptr();
  2456.      char*           gptr();
  2457.      char*           egptr();
  2458.      char*           eback();
  2459.      char*           ebuf();
  2460.      int             blen() const;
  2461.      void            setp(char*  p, char*  ep);
  2462.      void            setg(char*  eb,char*  g, char*  eg);
  2463.      void            pbump(int n);
  2464.      void            gbump(int n);
  2465.      void            setb(char* b, char* eb, int a = 0 );
  2466.      int             unbuffered() const;
  2467.      void            unbuffered(int unb);
  2468.      int             allocate();
  2469.      virtual int     doallocate();
  2470. };
  2471.  
  2472. Note:  xsgetn() and xsputn() are for the internal use of the I/O Stream 
  2473. Library. They are declared in iostream.h and listed above, but you should not 
  2474. use them directly. 
  2475.  
  2476.  
  2477. ΓòÉΓòÉΓòÉ 4.2.3. Functions That Return Pointers in the Protected Interface ΓòÉΓòÉΓòÉ
  2478.  
  2479. This section describes the functions in the protected interface of streambuf 
  2480. that return pointers to boundaries of areas in a stream buffer. 
  2481.  
  2482. Note:  The following descriptions assume that the functions are called as part 
  2483. of an object called dsb which is an object of a class that is derived from 
  2484. streambuf. 
  2485.  
  2486. The following functions are described: 
  2487.  
  2488. o base - Return pointer to beginning of stream buffer 
  2489. o eback - Return pointer to beginning of putback area 
  2490. o ebuf - Return pointer to end of stream buffer 
  2491. o egptr - Return pointer to end of get area 
  2492. o epptr - Return pointer to end of put area 
  2493. o gptr - Return pointer to beginning of get area 
  2494. o pbase - Return pointer to beginning of space available for put area 
  2495. o pptr - Return pointer to beginning of put area 
  2496.  
  2497.  
  2498. ΓòÉΓòÉΓòÉ 4.2.3.1. base - Return Pointer to Beginning of Stream Buffer ΓòÉΓòÉΓòÉ
  2499.  
  2500. Class: streambuf (protected interface) 
  2501.  
  2502. char* base();
  2503.  
  2504. base() returns a pointer to the first byte of the stream buffer. The stream 
  2505. buffer consists of the space between the pointer returned by base() and the 
  2506. pointer returned by ebuf(). 
  2507.  
  2508.  
  2509. ΓòÉΓòÉΓòÉ 4.2.3.2. eback - Return Pointer to Beginning of Putback Area ΓòÉΓòÉΓòÉ
  2510.  
  2511. Class: streambuf (protected interface) 
  2512.  
  2513. char* eback();
  2514.  
  2515. eback() returns a pointer to the lower bound of the space available for the get 
  2516. area of dsb. The space between the pointer returned by eback() and the pointer 
  2517. returned by gptr() is available for putback. 
  2518.  
  2519.  
  2520. ΓòÉΓòÉΓòÉ 4.2.3.3. ebuf - Return Pointer to End of Stream Buffer ΓòÉΓòÉΓòÉ
  2521.  
  2522. Class: streambuf (Protected interface) 
  2523.  
  2524. char* ebuf();
  2525.  
  2526. ebuf() returns a pointer to the byte after the last byte of the stream buffer. 
  2527.  
  2528.  
  2529. ΓòÉΓòÉΓòÉ 4.2.3.4. egptr - Return Pointer to End of Get Area ΓòÉΓòÉΓòÉ
  2530.  
  2531. Class: streambuf (Protected interface) 
  2532.  
  2533. char* egptr();
  2534.  
  2535. egptr() returns a pointer to the byte after the last byte of the get area of 
  2536. dsb. 
  2537.  
  2538.  
  2539. ΓòÉΓòÉΓòÉ 4.2.3.5. epptr - Return Pointer to End of Put Area ΓòÉΓòÉΓòÉ
  2540.  
  2541. Class: streambuf (Protected interface) 
  2542.  
  2543. char* epptr();
  2544.  
  2545. epptr() returns a pointer to the byte after the last byte of the put area of 
  2546. dsb. 
  2547.  
  2548.  
  2549. ΓòÉΓòÉΓòÉ 4.2.3.6. gptr - Return Pointer to Beginning of Get Area ΓòÉΓòÉΓòÉ
  2550.  
  2551. Class: streambuf (Protected interface) 
  2552.  
  2553. char* gptr();
  2554.  
  2555. gptr() returns a pointer to the first byte of the get area of dsb. The get area 
  2556. consists of the space between the pointer returned by gptr() and the pointer 
  2557. returned by egptr(). Characters are extracted from the stream buffer beginning 
  2558. at the character pointed to by gptr(). 
  2559.  
  2560.  
  2561. ΓòÉΓòÉΓòÉ 4.2.3.7. pbase - Return Pointer to Beginning of Space Available for Put Area ΓòÉΓòÉΓòÉ
  2562.  
  2563. Class: streambuf (Protected interface) 
  2564.  
  2565. char* pbase();
  2566.  
  2567. pbase() returns a pointer to the beginning of the space available for the put 
  2568. area of dsb. Characters between the pointer returned by pbase() and the pointer 
  2569. returned by pptr() have been stored in the stream buffer, but they have not 
  2570. been consumed by the ultimate consumer. 
  2571.  
  2572.  
  2573. ΓòÉΓòÉΓòÉ 4.2.3.8. pptr - Return Pointer to Beginning of Put Area ΓòÉΓòÉΓòÉ
  2574.  
  2575. Class: streambuf (Protected interface) 
  2576.  
  2577. char* pptr();
  2578.  
  2579. pptr() returns a pointer to the beginning of the put area of dsb. The put area 
  2580. consists of the space between the pointer returned by pptr() and the pointer 
  2581. returned by epptr(). 
  2582.  
  2583.  
  2584. ΓòÉΓòÉΓòÉ 4.2.4. Functions That Set Pointers in the Protected Interface ΓòÉΓòÉΓòÉ
  2585.  
  2586. This section describes the functions in the protected interface of streambuf 
  2587. that set the boundaries of areas in a stream buffer. The values of these 
  2588. boundaries are returned by the functions described in Functions That Return 
  2589. Pointers in the Protected Interface. 
  2590.  
  2591. Note:  The following descriptions assume that the functions are called as part 
  2592. of an object called dsb which is an object of a class that is derived from 
  2593. streambuf. 
  2594.  
  2595. The following functions are described: 
  2596.  
  2597. o setb - Set boundaries of stream buffer 
  2598. o setp - Set boundaries of put area 
  2599. o setg - Set boundaries of get area 
  2600.  
  2601.  
  2602. ΓòÉΓòÉΓòÉ 4.2.4.1. setb - Set Boundaries of Stream Buffer ΓòÉΓòÉΓòÉ
  2603.  
  2604. Class: streambuf (Protected interface) 
  2605.  
  2606. void setb(char* startbuf, char* endbuf, int delbuf = 0);
  2607.  
  2608. setb() sets the beginning of the stream buffer (the pointer returned by 
  2609. dsb.pbase()) to the position pointed to by startbuf, and sets the end of the 
  2610. stream buffer (the pointer returned by dsb.ebuf()) to the position pointed to 
  2611. by endbuf. If delbuf is a nonzero value, the stream buffer will be deleted when 
  2612. setb() is called again. If startbuf and endbuf are both equal to 0, no stream 
  2613. buffer is established. If startbuf is not equal to 0, a stream buffer is 
  2614. established, even if endbuf is less than startbuf. If this is the case, the 
  2615. stream buffer has length zero. 
  2616.  
  2617.  
  2618. ΓòÉΓòÉΓòÉ 4.2.4.2. setp - Set Boundaries of Put Area ΓòÉΓòÉΓòÉ
  2619.  
  2620. Class: streambuf (Protected interface) 
  2621.  
  2622. void setp(char* startput, char* endput);
  2623.  
  2624. setp() sets the beginning of the put area of dsb (the pointer returned by 
  2625. dsb.pptr()) to the position pointed to by startput, and sets the end of the put 
  2626. area (the pointer returned by dsb.epptr()) to the position pointed to by 
  2627. endput. 
  2628.  
  2629.  
  2630. ΓòÉΓòÉΓòÉ 4.2.4.3. setg - Set Boundaries of Get Area ΓòÉΓòÉΓòÉ
  2631.  
  2632. Class: streambuf (Protected interface) 
  2633.  
  2634. void setg(char* startput, char* startget, char* endget);
  2635.  
  2636. setg() sets the beginning of the get area of dsb (the pointer returned by 
  2637. dsb.gptr()) to startget, and sets the end of the get area (the pointer returned 
  2638. by dsb.egptr()) to endget. setg() also sets the beginning of the area available 
  2639. for putback (the pointer returned by dsb.eback()) to startput. 
  2640.  
  2641.  
  2642. ΓòÉΓòÉΓòÉ 4.2.5. Other NonVirtual Member Functions in the Protected Interface ΓòÉΓòÉΓòÉ
  2643.  
  2644. This section describes the remaining nonvirtual member functions that make up 
  2645. the protected interface of streambuf. 
  2646.  
  2647. Note:  The following descriptions assume that the functions are called as part 
  2648. of an object called dsb which is an object of a class that is derived from 
  2649. streambuf. 
  2650.  
  2651. The following functions are described: 
  2652.  
  2653. o allocate - Set up a stream buffer 
  2654. o blen - Return length of stream buffer 
  2655. o dbp - Record stream buffer status 
  2656. o gbump - Move beginning of get area 
  2657. o pbump - Move beginning of put area 
  2658. o unbuffered - Buffering state 
  2659.  
  2660.  
  2661. ΓòÉΓòÉΓòÉ 4.2.5.1. allocate - Set Up a Stream Buffer ΓòÉΓòÉΓòÉ
  2662.  
  2663. Class: streambuf (Protected interface) 
  2664.  
  2665. int allocate();
  2666.  
  2667. allocate() attempts to set up a stream buffer. allocate() returns the following 
  2668. values: 
  2669.  
  2670. o 0 if dsb already has a stream buffer set up (that is, dsb->base() returns a 
  2671.   nonzero value), or if unbuffered() returns a nonzero value. allocate() does 
  2672.   not do any further processing if it returns 0. 
  2673. o 1 if allocate() does set up a stream buffer. 
  2674. o EOF if the attempt to allocate space for the stream buffer fails. 
  2675.  
  2676. allocate() is not called by any other nonvirtual member function of streambuf. 
  2677.  
  2678.  
  2679. ΓòÉΓòÉΓòÉ 4.2.5.2. blen - Return Length of Stream Buffer ΓòÉΓòÉΓòÉ
  2680.  
  2681. Class: streambuf (Protected interface) 
  2682.  
  2683. int blen() const;
  2684.  
  2685. blen() returns the length (in bytes) of the stream buffer. 
  2686.  
  2687.  
  2688. ΓòÉΓòÉΓòÉ 4.2.5.3. dbp - Record Stream Buffer Status ΓòÉΓòÉΓòÉ
  2689.  
  2690. Class: streambuf (Protected interface) 
  2691.  
  2692. void dbp();
  2693.  
  2694. dbp() writes to standard output the values returned by the following functions: 
  2695.  
  2696. o base() 
  2697. o eback() 
  2698. o ebuf() 
  2699. o egptr() 
  2700. o epptr() 
  2701. o gptr() 
  2702. o pptr() 
  2703.  
  2704. dbp() is intended for debugging. streambuf does not specify anything about the 
  2705. form of the output. dbp() is considered part of the protected interface because 
  2706. the information that it prints can only be understood in relation to that 
  2707. interface. It is declared as a public function so that it can be called 
  2708. anywhere during debugging. 
  2709.  
  2710. The following example shows the output produced by dbp() when it is called as 
  2711. part of a filebuf object: 
  2712.  
  2713. #include <iostream.h>
  2714. void main()
  2715. {
  2716.      cout << "Here is some sample output." << endl;
  2717.      cout.rdbuf()->dbp();
  2718. }
  2719.  
  2720. If you compile and run this example, your output will look like this: 
  2721.  
  2722. Here is some sample output.
  2723. buf at 0x90210, base=0x91010, ebuf=0x91410,
  2724. pptr=0x91010, epptr=0x91410, eback=0, gptr=0, egptr=0
  2725.  
  2726.  
  2727. ΓòÉΓòÉΓòÉ 4.2.5.4. gbump - Move Beginning of Get Area ΓòÉΓòÉΓòÉ
  2728.  
  2729. Class: streambuf (Protected interface) 
  2730.  
  2731. void gbump(int offset);
  2732.  
  2733. gbump() offsets the beginning of the get area by the value of offset. The value 
  2734. of offset can be positive or negative. gbump() does not check to see if the new 
  2735. value returned by gptr() is valid. 
  2736.  
  2737. The beginning of the get area is equal to the value returned by gptr(). 
  2738.  
  2739.  
  2740. ΓòÉΓòÉΓòÉ 4.2.5.5. pbump - Move Beginning of Put Area ΓòÉΓòÉΓòÉ
  2741.  
  2742. Class: streambuf (Protected interface) 
  2743.  
  2744. void pbump(int offset);
  2745.  
  2746. pbump() offsets the beginning of the put area by the value of offset. The value 
  2747. of offset can be positive or negative. pbump() does not check to see if the new 
  2748. value returned by pptr() is valid. 
  2749.  
  2750. The beginning of the put area is equal to the value returned by pptr(). 
  2751.  
  2752.  
  2753. ΓòÉΓòÉΓòÉ 4.2.5.6. unbuffered - Buffering State ΓòÉΓòÉΓòÉ
  2754.  
  2755. Class: streambuf (Protected interface) 
  2756.  
  2757. int unbuffered() const;
  2758. void unbuffered(int buffstate);
  2759.  
  2760. unbuffered() manipulates the private streambuf variable called the buffering 
  2761. state. If the buffering state is nonzero, a call to allocate() does not set up 
  2762. a stream buffer. 
  2763.  
  2764. There are two versions of unbuffered(). The version of unbuffered() that takes 
  2765. no arguments returns the current value of the buffering state. The version that 
  2766. takes an argument, buffstate, changes the value of the buffering state to 
  2767. buffstate. 
  2768.  
  2769.  
  2770. ΓòÉΓòÉΓòÉ 4.2.6. Virtual Member Functions in the Protected Interface ΓòÉΓòÉΓòÉ
  2771.  
  2772. This section describes the virtual functions in the protected interface of 
  2773. streambuf. Although these virtual functions have default definitions in 
  2774. streambuf, they can be overridden in classes that are derived from streambuf. 
  2775. The following descriptions state the default definition of each function and 
  2776. the expected behavior for these functions in classes where they are overridden. 
  2777.  
  2778. Note:  The following descriptions assume that the functions are called as part 
  2779. of an object called dsb, which is an object of a class that is derived from 
  2780. streambuf. 
  2781.  
  2782. The following functions are described: 
  2783.  
  2784. o doallocate - Allocate space for a stream buffer 
  2785. o overflow -  Clear put area 
  2786. o pbackfail - Deal with full putback area 
  2787. o seekoff - Reposition external get or put pointer 
  2788. o seekpos - Reposition external get or put pointer 
  2789. o setbuf - Set up stream buffer 
  2790. o sync - Synchronize stream buffer and ultimate producer or ultimate consumer 
  2791. o underflow - Fill get area. 
  2792.  
  2793.  
  2794. ΓòÉΓòÉΓòÉ 4.2.6.1. doallocate - Allocate Space for a Stream Buffer ΓòÉΓòÉΓòÉ
  2795.  
  2796. Class: streambuf (Protected interface) 
  2797.  
  2798. virtual int doallocate();
  2799.  
  2800. doallocate() is called when allocate() determines that space is needed for a 
  2801. stream buffer. 
  2802.  
  2803. The default definition of doallocate() attempts to allocate space for a stream 
  2804. buffer using the operator new. 
  2805.  
  2806. If you define your own version of doallocate(), it must call setb() to provide 
  2807. space for a stream buffer or return EOF if it cannot allocate space. 
  2808. doallocate() should only be called if unbuffered() and base() return zero. 
  2809.  
  2810.  
  2811. ΓòÉΓòÉΓòÉ 4.2.6.2. overflow -  Clear Put Area ΓòÉΓòÉΓòÉ
  2812.  
  2813. Class: streambuf (Protected interface) 
  2814.  
  2815. virtual int overflow(int c = EOF);
  2816.  
  2817. overflow() is called when the put area is full, and an attempt is made to store 
  2818. another character in it. overflow() may be called at other times. 
  2819.  
  2820. The default definition of overflow() is compatible with the AT&T C++ Language 
  2821. System Release 1.2 version of the stream package, but it is not considered part 
  2822. of the current I/O Stream Library. Thus, the default definition of overflow() 
  2823. should not be used, and every class derived from streambuf should define 
  2824. overflow() itself. 
  2825.  
  2826. The definition of overflow() in your classes derived from streambuf should 
  2827. cause the ultimate consumer to consume the characters in the put area, call 
  2828. setp() to establish a new put area, and store the argument c in the put area if 
  2829. c does not equal EOF. overflow() should return EOF if an error occurs, and it 
  2830. should return a value not equal to EOF otherwise. 
  2831.  
  2832.  
  2833. ΓòÉΓòÉΓòÉ 4.2.6.3. pbackfail - Deal With Full Putback Area ΓòÉΓòÉΓòÉ
  2834.  
  2835. Class: streambuf (Protected interface) 
  2836.  
  2837. virtual int pbackfail(int c);
  2838.  
  2839. pbackfail() is called when both of the following conditions are true: 
  2840.  
  2841. o An attempt has been made to put back a character. 
  2842. o There is no room in the putback area. The pointer returned by eback() equals 
  2843.   the pointer returned by gptr(). 
  2844.  
  2845. The default definition of pbackfail() returns EOF. 
  2846.  
  2847. If you define pbackfail() in your own classes, your definition of pbackfail() 
  2848. should attempt to deal with the full putback area by, for instance, 
  2849. repositioning the get pointer of the ultimate producer. If this is possible, 
  2850. pbackfail() should return the argument c. If not, pbackfail() should return 
  2851. EOF. 
  2852.  
  2853.  
  2854. ΓòÉΓòÉΓòÉ 4.2.6.4. seekoff - Reposition External Get or Put Pointer ΓòÉΓòÉΓòÉ
  2855.  
  2856. Class: streambuf (Protected interface) 
  2857.  
  2858. virtual streampos seekoff(streamoff so, seek_dir dir,
  2859.           int mode = ios::in|ios::out);
  2860.  
  2861. seekoff() repositions the get or put pointer of the ultimate producer or 
  2862. ultimate consumer. seekoff() does not change the values returned by dsb.gptr() 
  2863. or dsb.pptr(). 
  2864.  
  2865. The default  definition of seekoff() returns EOF. 
  2866.  
  2867. If you define your own seekoff() function, it should return EOF if the derived 
  2868. class does not support repositioning. If the class does support repositioning, 
  2869. seekoff() should return the new position of the affected pointer, or EOF if an 
  2870. error occurs. so is an offset from a position in the ultimate producer or 
  2871. ultimate consumer. dir is a position in the ultimate producer or ultimate 
  2872. consumer. dir can have the following values: 
  2873.  
  2874. o ios::beg: the beginning of the ultimate producer or ultimate consumer 
  2875. o ios::cur: the current position in the ultimate producer or ultimate consumer 
  2876. o ios::end: the end of the ultimate producer or ultimate consumer. 
  2877.  
  2878. The new position of the affected pointer is the position specified by dir 
  2879. offset by the value of so. If you derive your own classes from streambuf, 
  2880. certain values of dir may not be valid depending on the nature of the ultimate 
  2881. consumer or producer. 
  2882.  
  2883. If ios::in is set in mode, the seekoff() should modify the get pointer. If 
  2884. ios::out is set in mode, the put pointer should be modified. If both ios::in 
  2885. and ios::out are set, both the get pointer and the put pointer should be 
  2886. modified. 
  2887.  
  2888.  
  2889. ΓòÉΓòÉΓòÉ 4.2.6.5. seekpos - Reposition External Get or Put Pointer ΓòÉΓòÉΓòÉ
  2890.  
  2891. Class: streambuf (Protected interface) 
  2892.  
  2893. virtual streampos seekpos(streampos pos,
  2894.           int mode = ios::in|ios::out);
  2895.  
  2896. seekpos() repositions the get or put pointer of the ultimate producer or 
  2897. ultimate consumer to the position pos. If ios::in is set in mode, the get 
  2898. pointer is repositioned. If ios::out is set in mode, the put pointer is 
  2899. repositioned. If both ios::in and ios::out are set, both the get pointer and 
  2900. the put pointer are affected. seekpos() does not change the values returned by 
  2901. dsb.gptr() or dsb.pptr(). 
  2902.  
  2903. The default definition of seekpos() returns the return value of the function 
  2904. seekoff(streamoff(pos), ios::beg, mode). Thus, if you want to define seeking 
  2905. operations in a class derived from streambuf, you can define seekoff() and use 
  2906. the default definition of seekpos(). 
  2907.  
  2908. If you define seekpos() in a class derived from streambuf, seekpos() should 
  2909. return EOF if the class does not support repositioning or if pos points to a 
  2910. position equal to or greater than the end of the stream. If not, seekpos() 
  2911. should return pos. 
  2912.  
  2913.  
  2914. ΓòÉΓòÉΓòÉ 4.2.6.6. setbuf - Set Up Stream Buffer ΓòÉΓòÉΓòÉ
  2915.  
  2916. Class: streambuf (Protected interface) 
  2917.  
  2918. virtual streambuf* setbuf(char* ptr, int len);
  2919. streambuf* setbuf(unsigned char* ptr, int len);
  2920. streambuf* setbuf(char* ptr, int len, int count);
  2921.  
  2922. There are three versions of setbuf(). The two versions that take two arguments 
  2923. set up a stream buffer consisting of the array of bytes starting at ptr with 
  2924. length len. The version of setbuf() that takes three arguments is obsolete. The 
  2925. I/O Stream Library includes it to be compatible with AT&T C++ Language System 
  2926. Release 1.2. 
  2927.  
  2928. The default definition of setbuf() sets up the stream buffer if the streambuf 
  2929. object does not already have a stream buffer. 
  2930.  
  2931. If you define setbuf() in a class derived from streambuf, setbuf() can either 
  2932. accept or ignore a request for an unbuffered streambuf object. The call to 
  2933. setbuf() is a request for an unbuffered streambuf object if ptr equals 0 or len 
  2934. equals 0. setbuf() should return a pointer to sb if it accepts the request, and 
  2935. 0 otherwise. 
  2936.  
  2937.  
  2938. ΓòÉΓòÉΓòÉ 4.2.6.7. sync - Synchronize Stream Buffer and Ultimate Producer or Ultimate Consumer ΓòÉΓòÉΓòÉ
  2939.  
  2940. Class: streambuf (Protected interface) 
  2941.  
  2942. virtual int sync();
  2943.  
  2944. sync() synchronizes the stream buffer with the ultimate producer or the 
  2945. ultimate consumer. 
  2946.  
  2947. The default definition of sync() returns 0 if either of the following 
  2948. conditions is true: 
  2949.  
  2950. o The get area is empty and there are no characters waiting to go to the 
  2951.   ultimate consumer 
  2952. o No stream buffer has been allocated for sb. 
  2953.  
  2954. Otherwise, sync() returns EOF. 
  2955.  
  2956. If you define sync() in a class derived from streambuf, it should send any 
  2957. characters that are stored in the put area to the ultimate consumer, and (if 
  2958. possible) send any characters that are waiting in the get area back to the 
  2959. ultimate producer. When sync() returns, both the put area and the get area 
  2960. should be empty. sync() should return EOF if an error occurs. 
  2961.  
  2962.  
  2963. ΓòÉΓòÉΓòÉ 4.2.6.8. underflow - Fill Get Area ΓòÉΓòÉΓòÉ
  2964.  
  2965. Class: streambuf (Protected interface) 
  2966.  
  2967. virtual int underflow();
  2968.  
  2969. underflow() takes characters from the ultimate producer and puts them in the 
  2970. get area. 
  2971.  
  2972. The default definition of underflow() is compatible with the AT&T C++ Language 
  2973. System Release 1.2 version version of the stream package, but it is not 
  2974. considered part of the current I/O Stream Library. Thus, the default definition 
  2975. of underflow() should not be used, and every class derived from streambuf 
  2976. should define underflow() itself. 
  2977.  
  2978. If you define underflow() in a class derived from streambuf, it should return 
  2979. the first character in the get area if the get area is not empty. If the get 
  2980. area is empty, underflow() should create a get area that is not empty and 
  2981. return the next character. If no more characters are available in the ultimate 
  2982. producer, underflow() should return EOF and leave the get area empty. 
  2983.  
  2984.  
  2985. ΓòÉΓòÉΓòÉ 4.3. streambuf Public Interface ΓòÉΓòÉΓòÉ
  2986.  
  2987. This chapter describes the public interface of the streambuf class. The public 
  2988. interface consists of the public member functions of streambuf that give you 
  2989. direct access to the predefined classes that are derived from streambuf. 
  2990.  
  2991. The streambuf class implements the concept of stream buffers. A stream buffer 
  2992. acts as a buffer between the source or target of data and the member functions 
  2993. of the classes derived from ios (such as istream and ostream) that format this 
  2994. data. See Stream Buffers for a more detailed description of stream buffers. 
  2995.  
  2996. The following topics are described in this chapter: 
  2997.  
  2998. o What Is the streambuf Public Interface 
  2999. o Declarations for the streambuf public interface in iostream.h 
  3000. o Public members of the streambuf public interface 
  3001.  
  3002.  
  3003. ΓòÉΓòÉΓòÉ 4.3.1. What Is the streambuf Public Interface? ΓòÉΓòÉΓòÉ
  3004.  
  3005. Although streambuf is not defined as a virtual base class, you can think of it 
  3006. as one.  You should not create objects of the streambuf class itself, but you 
  3007. can use it in the following ways: 
  3008.  
  3009. o Through a predefined class derived from streambuf. You can use objects of 
  3010.   filebuf, strstreambuf and stdiobuf (the predefined classes that are derived 
  3011.   from streambuf) directly as implementations of stream buffers. The public 
  3012.   interface consists of the streambuf public member functions that can be 
  3013.   called on objects of these predefined classes. This chapter describes the 
  3014.   members of the streambuf class that you need to know about if you want to use 
  3015.   the predefined classes derived from streambuf to implement stream buffers 
  3016.   directly. streambuf itself does not have any facilities for taking characters 
  3017.   from the ultimate producer or sending them to the ultimate consumer. The 
  3018.   specialized member functions that handle the interface with the ultimate 
  3019.   producer and the ultimate consumer are defined in the  filebuf, strstreambuf, 
  3020.   and stdiobuf classes. 
  3021.  
  3022. o As a base class to implement your own specialized stream buffers. This use of 
  3023.   the streambuf class is described in streambuf Protected Interface. 
  3024.  
  3025. o Through a predefined class derived from streambuf. You can use objects of 
  3026.   filebuf strstreambuf, and stdiobuf (the predefined classes that are derived 
  3027.   from streambuf) directly as implementations of stream buffers. The public 
  3028.   interface consists of the streambuf public member functions that can be 
  3029.   called on objects of these predefined classes. This chapter describes the 
  3030.   members of the streambuf class that you need to know about if you want to use 
  3031.   the predefined classes derived from streambuf to implement stream buffers 
  3032.   directly. streambuf itself does not have any facilities for taking characters 
  3033.   from the ultimate producer or sending them to the ultimate consumer. The 
  3034.   specialized member functions that handle the interface with the ultimate 
  3035.   producer and the ultimate consumer are defined in the  filebuf, strstreambuf, 
  3036.   and stdiobuf classes. 
  3037.  
  3038. Except for the destructor for the streambuf class, the virtual functions are 
  3039. described as part of the protected interface. Although most virtual functions 
  3040. are declared public, they are meant to be overloaded in the classes that you 
  3041. derive from streambuf, and thus they are part of the protected interface. 
  3042.  
  3043.  
  3044. ΓòÉΓòÉΓòÉ 4.3.2. Declarations for the streambuf Public Interface in iostream.h ΓòÉΓòÉΓòÉ
  3045.  
  3046. You must include the following statement in any file that uses member functions 
  3047. of the public interface of streambuf: 
  3048.  
  3049. #include <iostream.h>
  3050.  
  3051. The following is an excerpt from the iostream.h header file that shows the 
  3052. relevant declarations for the members of the public interface. Note that this 
  3053. excerpt only shows the declarations in the streambuf class that are relevant to 
  3054. the public interface. The declarations for the protected interface are listed 
  3055. in Declarations for the streambuf Protected Interface in iostream.h. 
  3056.  
  3057. class ios {
  3058. public:
  3059.      enum io_state     { goodbit, eofbit, failbit, badbit,
  3060.                          hardfail};
  3061.      enum open_mode    { in, out, ate, app, trunc,
  3062.                          nocreate, noreplace, bin, binary=bin} ;
  3063.      enum seek_dir     { beg, cur, end} ;
  3064.      // .
  3065.      // .
  3066.      // .
  3067. };
  3068.  
  3069. class streambuf {
  3070. public :
  3071.                streambuf();
  3072.                streambuf(char*  p, int l);
  3073.                streambuf(char*  p, int l,int c);
  3074.      virtual   ~streambuf();
  3075.      int       in_avail();
  3076.      int       out_waiting();
  3077.      int       sgetc();
  3078.      int       snextc();
  3079.      int       sbumpc();
  3080.      void      stossc();
  3081.      int       sputbackc(char c);
  3082.      int       sputc(int c);
  3083.      int       sputn(const char*  s,int n);
  3084.      int       sgetn(char*  s,int n);
  3085.      virtual   streambuf* setbuf(char*  p, int len);
  3086.      streambuf*  setbuf(unsigned char*  p, int len);
  3087.      streambuf*  setbuf(char*  p, int len, int count);
  3088.      int       optim_in_avail();
  3089.      int       optim_sbumpc();
  3090.      int       pptr_non_null();
  3091. };
  3092.  
  3093. Note: 
  3094.  
  3095.  1. pptr_non_null(), optim_in_avail(), and optim_sbumpc() are internal 
  3096.     implementation functions. They are declared in iostream.h and listed above, 
  3097.     but you should not use them directly. 
  3098.  
  3099.  2. Because setbuf() has both virtual and nonvirtual declarations, it is 
  3100.     described as part of the protected interface. See setbuf - Set Up Stream 
  3101.     Buffer for more details. 
  3102.  
  3103.  
  3104. ΓòÉΓòÉΓòÉ 4.3.3. Public Members of the streambuf Public Interface ΓòÉΓòÉΓòÉ
  3105.  
  3106. Note:  The following descriptions assume that the functions are called as part 
  3107. of an object fb of a class derived from streambuf. fb could, for example, be an 
  3108. object of the class filebuf. It could also be an strstreambuf object or an 
  3109. stdiobuf object. 
  3110.  
  3111. The following member functions are described: 
  3112.  
  3113. o Constructors for streambuf 
  3114. o Destructor for streambuf 
  3115. o in_avail - Return Number of Characters in Get Area 
  3116. o out_waiting - Return Number of Characters in Put Area 
  3117. o sbumpc - Move Get Pointer One Character 
  3118. o sgetc - Return Character After Get Pointer 
  3119. o sgetn - Return Characters Following Get Pointer 
  3120. o snextc - Return Character Following Get Pointer 
  3121. o sputbackc - Move Get Pointer Back One Character 
  3122. o sputc - Store Character After Put Pointer 
  3123. o sputn - Store Characters After Put Pointer 
  3124. o stossc - Move Get Pointer Forward One Character 
  3125.  
  3126.  
  3127. ΓòÉΓòÉΓòÉ 4.3.3.1. Constructors for streambuf ΓòÉΓòÉΓòÉ
  3128.  
  3129. Class: streambuf (Public interface) 
  3130.  
  3131. streambuf();
  3132. streambuf(char* buffer, int len);
  3133. streambuf(char* buffer, int len, int c);
  3134.  
  3135. There are three versions of the constructor for streambuf. The version with no 
  3136. arguments constructs an empty stream buffer corresponding to an empty sequence. 
  3137. The values returned by base(), eback(), ebuf(), egptr(), epptr(), pptr(), 
  3138. gptr(), and pbase() are initially all zero for this stream buffer. 
  3139.  
  3140. The version with two arguments constructs an empty stream buffer of length len 
  3141. starting at the position pointed to by buffer. 
  3142.  
  3143. The version of the constructor with three arguments is obsolete. It is included 
  3144. in the I/O Stream Library for compatibility with the AT&T C++ Language System 
  3145. Release 1.2. 
  3146.  
  3147.  
  3148. ΓòÉΓòÉΓòÉ 4.3.3.2. Destructor for streambuf ΓòÉΓòÉΓòÉ
  3149.  
  3150. Class: streambuf (Public interface) 
  3151.  
  3152. virtual ~streambuf();
  3153.  
  3154. The destructor for streambuf calls sync(). If a stream buffer has been set up 
  3155. and ios::alloc is set, sync() deletes the stream buffer. 
  3156.  
  3157.  
  3158. ΓòÉΓòÉΓòÉ 4.3.3.3. in_avail - Return Number of Characters in Get Area ΓòÉΓòÉΓòÉ
  3159.  
  3160. Class: streambuf (Public interface) 
  3161.  
  3162. int in_avail();
  3163.  
  3164. in_avail() returns the number of characters that are available to be extracted 
  3165. from the get area of fb. You can extract the number of characters equal to the 
  3166. value that in_avail() returns without causing an error. 
  3167.  
  3168.  
  3169. ΓòÉΓòÉΓòÉ 4.3.3.4. out_waiting - Return Number of Characters in Put Area ΓòÉΓòÉΓòÉ
  3170.  
  3171. Class: streambuf (Public interface) 
  3172.  
  3173. int out_waiting();
  3174.  
  3175. out_waiting() returns the number of characters that are in the put area waiting 
  3176. to be sent to the ultimate consumer. 
  3177.  
  3178.  
  3179. ΓòÉΓòÉΓòÉ 4.3.3.5. sbumpc - Move Get Pointer One Character ΓòÉΓòÉΓòÉ
  3180.  
  3181. Class: streambuf (Public interface) 
  3182.  
  3183. int sbumpc();
  3184.  
  3185. sbumpc() moves the get pointer past one character and returns the character 
  3186. that it moved past. sbumpc() returns EOF if the get pointer is already at the 
  3187. end of the get area. 
  3188.  
  3189.  
  3190. ΓòÉΓòÉΓòÉ 4.3.3.6. sgetc - Return Character After Get Pointer ΓòÉΓòÉΓòÉ
  3191.  
  3192. Class: streambuf (Public interface) 
  3193.  
  3194. int sgetc();
  3195.  
  3196. sgetc() returns the character after the get pointer without moving the get 
  3197. pointer itself. If no character is available, sgetc() returns EOF. 
  3198.  
  3199. Note:  sgetc() does not change the position of the get pointer. 
  3200.  
  3201.  
  3202. ΓòÉΓòÉΓòÉ 4.3.3.7. sgetn - Return Characters Following Get Pointer ΓòÉΓòÉΓòÉ
  3203.  
  3204. Class: streambuf (Public interface) 
  3205.  
  3206. int sgetn(char* ptr, int n);
  3207.  
  3208. sgetn() extracts the n characters following the get pointer, and copies them to 
  3209. the area starting at the position pointed to by ptr. If there are fewer than n 
  3210. characters following the get pointer, sgetn() takes the characters that are 
  3211. available and stores them in the position pointed to by ptr. sgetn() 
  3212. repositions the get pointer following the extracted characters and returns the 
  3213. number of extracted characters. 
  3214.  
  3215.  
  3216. ΓòÉΓòÉΓòÉ 4.3.3.8. snextc - Return Character Following Get Pointer ΓòÉΓòÉΓòÉ
  3217.  
  3218. Class: streambuf (Public interface) 
  3219.  
  3220. int snextc();
  3221.  
  3222. snextc() moves the get pointer forward one character and returns the character 
  3223. following the new position of the get pointer. snextc() returns EOF if the get 
  3224. pointer is at the end of the get area either before or after it is moved 
  3225. forward. 
  3226.  
  3227.  
  3228. ΓòÉΓòÉΓòÉ 4.3.3.9. sputbackc - Move Get Pointer Back One Character ΓòÉΓòÉΓòÉ
  3229.  
  3230. Class: streambuf (Public interface) 
  3231.  
  3232. int sputbackc(char c);
  3233.  
  3234. sputbackc() moves the get pointer back one character. The get pointer may 
  3235. simply move, or the ultimate producer may rearrange the internal data 
  3236. structures so that the character c is saved. The argument c must equal the 
  3237. character that precedes the get pointer in the stream buffer. The effect of 
  3238. sputbackc() is undefined if c is not equal to the character before the get 
  3239. pointer. sputbackc() returns EOF if an error occurs. The conditions that cause 
  3240. errors depend on the derived class. 
  3241.  
  3242.  
  3243. ΓòÉΓòÉΓòÉ 4.3.3.10. sputc - Store Character After Put Pointer ΓòÉΓòÉΓòÉ
  3244.  
  3245. Class: streambuf (Public interface) 
  3246.  
  3247. int sputc(int c);
  3248.  
  3249. sputc() stores the argument c after the put pointer and moves the put pointer 
  3250. past the stored character. If there is enough space in the stream buffer, this 
  3251. will extend the size of the put area. sputc() returns EOF if an error occurs. 
  3252. The conditions that cause errors depend on the derived class. 
  3253.  
  3254.  
  3255. ΓòÉΓòÉΓòÉ 4.3.3.11. sputn - Store Characters After Put Pointer ΓòÉΓòÉΓòÉ
  3256.  
  3257. Class: streambuf (Public interface) 
  3258.  
  3259. int sputn(const char* s, int n);
  3260.  
  3261. sputn() stores the n characters starting at s after the put pointer and moves 
  3262. the put pointer to the end of the series. sputn() returns the number of 
  3263. characters successfully stored. If an error occurs, sputn() returns a value 
  3264. less than n. 
  3265.  
  3266.  
  3267. ΓòÉΓòÉΓòÉ 4.3.3.12. stossc - Move Get Pointer Forward One Character ΓòÉΓòÉΓòÉ
  3268.  
  3269. Class: streambuf (Public interface) 
  3270.  
  3271. void stossc();
  3272.  
  3273. stossc() moves the get pointer forward one character. If the get pointer is 
  3274. already at the end of the get area, stossc() does not move it. 
  3275.  
  3276.  
  3277. ΓòÉΓòÉΓòÉ 4.4. ios Class ΓòÉΓòÉΓòÉ
  3278.  
  3279. The ios class maintains the error and format state information for the classes 
  3280. that are derived from it.  See The I/O Stream Library Class Hierarchy for a 
  3281. list of these classes. The derived classes support the movement of formatted 
  3282. and unformatted data to and from the stream buffer. This chapter describes the 
  3283. members of the ios class, and thus describes the operations that are common to 
  3284. all the classes that are derived from ios. 
  3285.  
  3286. The following topics are described in this chapter: 
  3287.  
  3288. o Declarations for ios in the iostream.h header file 
  3289. o Constructors and assignment operator for ios 
  3290. o The format state and the error state 
  3291. o Format state variables 
  3292. o Format state flags 
  3293. o Public members of ios for the format state 
  3294. o Public members of ios for user-defined format flags 
  3295. o Public members of ios for the error state 
  3296. o Other ios member functions 
  3297. o Built-in manipulators 
  3298.  
  3299.  
  3300. ΓòÉΓòÉΓòÉ 4.4.1. Declarations for ios in iostream.h ΓòÉΓòÉΓòÉ
  3301.  
  3302. You must include the following statement in any file that uses members of the 
  3303. ios class: 
  3304.  
  3305. #include <iostream.h>
  3306.  
  3307. The following is an excerpt from the iostream.h header file that shows the 
  3308. relevant declarations for the members of ios. 
  3309.  
  3310. Note:  In the following excerpt, the values of the enumerators have been 
  3311. omitted. Look at the iostream.h header file if you need to use these values 
  3312. explicitly. 
  3313.  
  3314. class ios {
  3315. public:
  3316.      enum io_state     {goodbit, eofbit, failbit, badbit,
  3317.                         hardfail};
  3318.      enum open_mode    { in, out, ate, app, trunc,
  3319.                          nocreate, noreplace, bin, binary=bin} ;
  3320.      enum seek_dir     {beg, cur, end } ;
  3321.      enum              {skipws,
  3322.                        left, right, internal,
  3323.                        dec, oct, hex,
  3324.                        showbase,
  3325.                        showpoint,
  3326.                        uppercase,
  3327.                        showpos,
  3328.                        scientific, fixed,
  3329.                        unitbuf, stdio
  3330.                        };
  3331.                   ios(streambuf*);
  3332.      virtual      ~ios() ;
  3333.      static const long
  3334.                   basefield; /* dec|oct|hex */
  3335.      static const long
  3336.                   adjustfield; /* left|right|internal */
  3337.      static const long
  3338.                   floatfield; /* scientific|fixed */
  3339.      long         flags() const;
  3340.      long         flags(long f);
  3341.      long         setf(long newset, long field);
  3342.      long         setf(long);
  3343.      long         unsetf(long)
  3344.      int          width() const;
  3345.      int          width(int w);
  3346.      ostream*     tie(ostream* s);
  3347.      ostream*     tie();
  3348.      char         fill(char);
  3349.      char         fill() const;
  3350.      int          precision(int);
  3351.      int          precision() const;
  3352.      int          rdstate() const;
  3353.                   operator void*();
  3354.                   operator const void*() const;
  3355.      int          operator!() const;
  3356.      int          eof() const;
  3357.      int          fail() const;
  3358.      int          bad() const;
  3359.      int          good() const;
  3360.      void         clear(int i =0);
  3361.      streambuf*   rdbuf();
  3362.      long &       iword(int);
  3363.      void* &      pword(int);
  3364.      static long  bitalloc()
  3365.      static int   xalloc();
  3366.      static void  sync_with_stdio();
  3367.      int          skip(int i);
  3368. protected:
  3369.                   ios();
  3370.      void         init(streambuf* isb);
  3371.      ostream*     x_tie;
  3372.      short        x_precision;
  3373.      char         x_fill;
  3374.      short        x_width;
  3375. private:
  3376.                   ios(ios& ioa);
  3377.      void         operator=(ios& iob);
  3378. };
  3379.  
  3380. ios& dec(ios&);
  3381. ios& hex(ios&);
  3382. ios& oct(ios&);
  3383. istream& ws(istream&);
  3384. ostream& endl(ostream& i);
  3385. ostream& ends(ostream& i);
  3386. ostream& flush(ostream&);
  3387.  
  3388. Note:  iostream.h contains private declarations for an assignment operator and 
  3389. a copy constructor. iostream.h includes these declaration for compatibility 
  3390. with AT&T C++ Language System Release 1.2. The declarations are private to 
  3391. prevent ios objects from being copied. 
  3392.  
  3393.  
  3394. ΓòÉΓòÉΓòÉ 4.4.2. Constructors and Assignment Operator for ios ΓòÉΓòÉΓòÉ
  3395.  
  3396. public:
  3397.      ios(streambuf* sb);
  3398. protected:
  3399.      ios();
  3400.      init(streambuf* isb);
  3401. private:
  3402.      ios(ios& ioa);
  3403.      void operator=(ios& iob);
  3404.  
  3405. There are three versions of the ios constructor. The version that is declared 
  3406. public takes a single argument that is a pointer to the streambuf object that 
  3407. becomes associated with the constructed ios object. If this pointer is equal to 
  3408. 0, the result is undefined. 
  3409.  
  3410. The version of the ios constructor that is declared protected takes no 
  3411. arguments. This version is needed because ios is used as a virtual base class 
  3412. for iostream, and therefore the ios class must have a constructor that takes no 
  3413. arguments. If you use this constructor in a derived class, you must use the 
  3414. init() function to associate the constructed ios object with the streambuf 
  3415. object pointed to by the argument isb. 
  3416.  
  3417. Copying of ios objects is not well defined, and for this reason, both the 
  3418. assignment operator and the copy constructor are declared private. Assignment 
  3419. between streams is supported by the istream_withassign, ostream_withassign, and 
  3420. iostream_withassign classes. See istream_withassign Assignment Operator and 
  3421. ostream_withassign Assignment Operator for more details. None of the predefined 
  3422. classes derived from ios has a copy constructor or an assignment operator. 
  3423. Unless you define your own copy constructor or assignment operator for a class 
  3424. that you derive from ios, your class will have neither a copy constructor nor 
  3425. an assignment operator. 
  3426.  
  3427.  
  3428. ΓòÉΓòÉΓòÉ 4.4.3. The Format State and the Error State ΓòÉΓòÉΓòÉ
  3429.  
  3430. Each ios object has a format state and an error state. The format state is a 
  3431. collection of flags and variables that can be set to control the details of 
  3432. formatting operations for input and output. The error state is a collection of 
  3433. flags that records whether any errors have taken place in the processing of the 
  3434. ios object. It also records whether the end of an input stream has been 
  3435. reached. You can view and set the format state and the error state using the 
  3436. functions described in this chapter. 
  3437.  
  3438.  
  3439. ΓòÉΓòÉΓòÉ 4.4.4. Format State Variables ΓòÉΓòÉΓòÉ
  3440.  
  3441. The format state is a collection of format flags and format variables that 
  3442. control the details of formatting for input and output operations. This section 
  3443. describes the format variables. 
  3444.  
  3445. The format variables have the following declarations: 
  3446.  
  3447.      short     x_precision;
  3448.      char      x_fill;
  3449.      short     x_width;
  3450.  
  3451. They are used in the following manner: 
  3452.  
  3453. o x_precision is the number of significant digits in the representation of 
  3454.   floating-point values. Its default value is 6. 
  3455. o x_fill is the character that is used to pad values that do not require the 
  3456.   width of an entire field for their representation. Its default value is a 
  3457.   space character. 
  3458. o x_width is the minimum width of a field. Its default value is 0. 
  3459.  
  3460.  
  3461. ΓòÉΓòÉΓòÉ 4.4.5. Format State Flags ΓòÉΓòÉΓòÉ
  3462.  
  3463. The format flags have the following declaration in iostream.h: 
  3464.  
  3465. enum     {     skipws,
  3466.                left, right, internal,
  3467.                dec, oct, hex,
  3468.                showbase,
  3469.                showpoint,
  3470.                uppercase,
  3471.                showpos,
  3472.                scientific, fixed,
  3473.                unitbuf, stdio
  3474.          } ;
  3475.  
  3476. The following list shows the formatting features and the format flags that 
  3477. control them: 
  3478.  
  3479. o Whitespace and padding: ios::skipws, ios::left, ios::right, ios::internal 
  3480. o Base conversion: ios::dec, ios::hex, ios::oct, ios::showbase 
  3481. o Integral formatting: ios::showpos 
  3482. o Floating-point formatting: ios::fixed, ios::scientific, ios::showpoint 
  3483. o Uppercase and lowercase: ios::uppercase 
  3484. o Buffer flushing: ios::stdio, ios::unitbuf 
  3485.  
  3486. The following sections describe these formatting features in detail. Mutually 
  3487. Exclusive Format Flags describes the flags that produce unpredictable results 
  3488. if they are set at the same time. 
  3489.  
  3490.  
  3491. ΓòÉΓòÉΓòÉ 4.4.5.1. Whitespace and Padding ΓòÉΓòÉΓòÉ
  3492.  
  3493. Class: ios 
  3494.  
  3495. The following format state flags control whitespace and padding characters: 
  3496.  
  3497. o skipws: if ios::skipws is set, whitespace will be skipped on input. If it is 
  3498.   not set, whitespace is not skipped. If ios::skipws is not set, the arithmetic 
  3499.   extractors will signal an error if you attempt to read an integer or 
  3500.   floating-point value that is preceded by whitespace. ios::failbit is set, and 
  3501.   extraction ceases until it is cleared. This is done to avoid the looping 
  3502.   problems that could occur otherwise. If the following program is run with an 
  3503.   input file that contains integer values separated by spaces, ios::failbit is 
  3504.   set after the first integer value is read, and the program halts. If the 
  3505.   program did not call fail() at the beginning of the while loop to test if 
  3506.   ios::failbit is set, it would loop indefinitely. 
  3507.  
  3508.     #include <fstream.h>
  3509.  
  3510.          void main()
  3511.          {
  3512.               fstream f("spadina.dat", ios::in);
  3513.               f.unsetf(ios::skipws);
  3514.               int i;
  3515.               while (!f.eof() && !f.fail()) {
  3516.                    f >> i;
  3517.                    cout << i;
  3518.                    }
  3519.          }
  3520.  
  3521. o left: if ios::left is set, the value is left-justified. Fill characters are 
  3522.   added after the value. 
  3523.  
  3524. o right: if ios::right is set, the value is right-justified. Fill characters 
  3525.   are added before the value. 
  3526.  
  3527. o internal: if ios::internal is set, the fill characters are added after any 
  3528.   leading sign or base notation, but before the value itself. 
  3529.  
  3530.  
  3531. ΓòÉΓòÉΓòÉ 4.4.5.2. Base Conversion ΓòÉΓòÉΓòÉ
  3532.  
  3533. Class: ios 
  3534.  
  3535. The manipulators ios::dec, ios::oct, and ios::hex (see Built-In Manipulators 
  3536. for ios for more details) have the same effect as the flags ios::dec, ios::oct, 
  3537. and ios::hex, respectively. 
  3538.  
  3539. o dec: if ios::dec is set, the conversion base is 10. 
  3540.  
  3541. o oct: if ios::oct is set, the conversion base is 8. 
  3542.  
  3543. o hex: if ios::hex is set, the conversion base is 16. 
  3544.  
  3545. o showbase: if ios::showbase is set, the operation that inserts values converts 
  3546.   them to an external form that can be read according to the C++ lexical 
  3547.   conventions for integral constants. By default, ios::showbase is unset. 
  3548.  
  3549.  
  3550. ΓòÉΓòÉΓòÉ 4.4.5.3. Integral Formatting ΓòÉΓòÉΓòÉ
  3551.  
  3552. Class: ios 
  3553.  
  3554. o showpos: if ios::showpos is set, the operation that inserts values places a 
  3555.   positive sign "+" into decimal conversions of positive integral values. 
  3556.  
  3557.  
  3558. ΓòÉΓòÉΓòÉ 4.4.5.4. Floating-Point Formatting ΓòÉΓòÉΓòÉ
  3559.  
  3560. Class: ios 
  3561.  
  3562. The following format flags control the formatting of floating-point values: 
  3563.  
  3564. o showpoint: if ios::showpoint is set, trailing zeros and a decimal point 
  3565.   appear in the result of a floating-point conversion. This flag has no effect 
  3566.   if either ios::scientific or ios::fixed is set. 
  3567.  
  3568. o scientific: if ios::scientific is set, the value is converted using 
  3569.   scientific notation. In scientific notation, there is one digit before the 
  3570.   decimal point and the number of digits following the decimal point depends on 
  3571.   the value of ios::x_precision. The default value for ios::x_precision is 6. 
  3572.   If ios::uppercase is set, an uppercase "E" precedes the exponent. Otherwise, 
  3573.   a lowercase "e" precedes the exponent. 
  3574.  
  3575. o fixed: if ios::fixed is set, floating point values are converted to fixed 
  3576.   notation with the number of digits after the decimal point equal to the value 
  3577.   of ios::x_precision (or 6 by default). 
  3578.  
  3579. If neither ios::fixed nor ios::scientific is set, the representation of 
  3580. floating-point values depends on their values and the number of significant 
  3581. digits in the representation equals ios::x_precision. Floating-point values are 
  3582. converted to scientific notation if the exponent resulting from a conversion to 
  3583. scientific notation is less than -4 or greater than or equal to the value of 
  3584. ios::x_precision. Otherwise, floating-point values are converted to fixed 
  3585. notation. If ios::showpoint is not set, trailing zeros are removed from the 
  3586. result and a decimal point appears only if it is followed by a digit. 
  3587. ios::scientific and ios::fixed are collectively identified by the static member 
  3588. ios::floatfield. 
  3589.  
  3590. The following example shows some of the possibilities for formatting a 
  3591. floating-point value: 
  3592.  
  3593. #include
  3594. <iomanip.h>
  3595.  
  3596. void main()
  3597. {
  3598.      float fp = 3.14f;
  3599.      //
  3600.      // print the value with an uppercase "E" in the exponent
  3601.      //
  3602.      cout << setiosflags(ios::scientific|ios::uppercase);
  3603.      cout << "Here is a floating-point value " << fp << endl;
  3604.      //
  3605.      // print the value with a lowercase "e" in the exponent
  3606.      //
  3607.      cout << resetiosflags(ios::uppercase);
  3608.      cout << "Here is a floating-point value "
  3609.           << fp << endl;
  3610.      //
  3611.      // print the value in fixed format
  3612.      //
  3613.      cout << resetiosflags(ios::scientific)
  3614.           << setiosflags(ios::fixed);
  3615.      cout << "Here is a floating-point value "
  3616.           << fp << endl;
  3617. }
  3618.  
  3619. This program produces the following output: 
  3620.  
  3621. Here is a floating-point value 3.140000E+00
  3622. Here is a floating-point value 3.140000e+00
  3623. Here is a floating-point value 3.140000
  3624.  
  3625.  
  3626. ΓòÉΓòÉΓòÉ 4.4.5.5. Uppercase and Lowercase ΓòÉΓòÉΓòÉ
  3627.  
  3628. Class: ios 
  3629.  
  3630. o uppercase: if ios::uppercase is set, the operation that inserts values uses 
  3631.   an uppercase "E" for floating point values in scientific notation. In 
  3632.   addition, the operation that inserts values stores hexadecimal digits "A" to 
  3633.   "F" in uppercase and places an uppercase "X" before hexadecimal values when 
  3634.   ios::showbase is set. If ios::uppercase is not set, a lowercase "e" 
  3635.   introduces the exponent in floating-point values, hexadecimal digits "a" to 
  3636.   "f" are stored in lowercase, and a lowercase "x" is inserted before 
  3637.   hexadecimal values when ios::showbase is set. 
  3638.  
  3639.  
  3640. ΓòÉΓòÉΓòÉ 4.4.5.6. Buffer Flushing ΓòÉΓòÉΓòÉ
  3641.  
  3642. Class: ios 
  3643.  
  3644. o unitbuf: if ios::unitbuf is set, ostream::osfx() performs a flush after each 
  3645.   insertion. The attached stream buffer is unit buffered. 
  3646.  
  3647. o stdio: this flag is used internally by sync_with_stdio(). You should not use 
  3648.   ios::stdio directly. If you want to combine I/O Stream Library input and 
  3649.   output with stdio.h input and output, you should use sync_with_stdio(). See 
  3650.   sync_with_stdio - Attach stdiobuf Object to Predefined Streams for more 
  3651.   details on sync_with_stdio(). 
  3652.  
  3653.  
  3654. ΓòÉΓòÉΓòÉ 4.4.5.7. Mutually Exclusive Format Flags ΓòÉΓòÉΓòÉ
  3655.  
  3656. Class: ios 
  3657.  
  3658. If you specify conflicting flags the results are unpredictable. For example, 
  3659. the results will be unpredictable if you set both ios::left and ios::right in 
  3660. the format state of iosobj. You should set only one flag in each set of the 
  3661. following three sets: 
  3662.  
  3663. o ios::left, ios::right, ios::internal 
  3664. o ios::dec, ios::oct, ios::hex 
  3665. o ios::scientific, ios::fixed. 
  3666.  
  3667.  
  3668. ΓòÉΓòÉΓòÉ 4.4.6. Public Members of ios for the Format State ΓòÉΓòÉΓòÉ
  3669.  
  3670. You can use the member functions listed below to control the format state of an 
  3671. ios object. 
  3672.  
  3673. Note:  The following descriptions assume that the functions are called as part 
  3674. of an ios object called iosobj. 
  3675.  
  3676. The following functions are described: 
  3677.  
  3678. o fill - Set the fill character 
  3679. o flags - Set format flags 
  3680. o precision - Set the precision 
  3681. o setf - Set specific format flags 
  3682. o skip - Set ios::skipws format flag 
  3683. o unsetf - Turn off format flags 
  3684. o width - Set field width 
  3685.  
  3686.  
  3687. ΓòÉΓòÉΓòÉ 4.4.6.1. fill - Set the Fill Character ΓòÉΓòÉΓòÉ
  3688.  
  3689. Class: ios 
  3690.  
  3691. char fill() const;
  3692. char fill(char fillchar);
  3693.  
  3694. There are two versions of fill(). fill() with no arguments returns the value of 
  3695. ios::x_fill in the format state of iosobj. fill() with an argument fillchar 
  3696. sets ios::x_fill to be equal to fillchar. 
  3697.  
  3698. ios::x_fill is the character used as padding if the field is wider than the 
  3699. representation of a value. The default value for ios::x_fill is a space. The 
  3700. ios::left, ios::right, and ios::internal flags determine the position of the 
  3701. fill character. 
  3702.  
  3703. You can also use the parameterized manipulator setfill to set the value of 
  3704. ios::x_fill. 
  3705.  
  3706.  
  3707. ΓòÉΓòÉΓòÉ 4.4.6.2. flags - Set Format Flags ΓòÉΓòÉΓòÉ
  3708.  
  3709. Class: ios 
  3710.  
  3711. long flags() const;
  3712. long flags(long flagset);
  3713.  
  3714. There are two versions of flags(). The version that takes no arguments returns 
  3715. the value of the flags that make up the current format state. The version that 
  3716. takes an argument sets the flags in the format state to the settings specified 
  3717. in flagset and returns the value of the previous settings of the format flags. 
  3718.  
  3719.  
  3720. ΓòÉΓòÉΓòÉ 4.4.6.3. precision - Set the Precision ΓòÉΓòÉΓòÉ
  3721.  
  3722. Class: ios 
  3723.  
  3724. int precision() const;
  3725. int precision(int prec);
  3726.  
  3727. There are two versions of precision(). The version that takes no arguments 
  3728. returns the value of ios::x_precision. The version that takes one argument sets 
  3729. the value of ios::x_precision to prec and returns the previous value. The value 
  3730. of prec must be greater than 0. If the value is nonpositive, the value of 
  3731. ios::x_precision is set to the default value, 6. ios::x_precision controls the 
  3732. number of significant digits when floating-point values are inserted. 
  3733.  
  3734. If neither ios::scientific nor ios::fixed is set, ios::x_precision specifies 
  3735. the number of significant digits in the floating-point value that is being 
  3736. inserted. If, in addition, ios::showpoint is not set, all trailing zeros are 
  3737. removed and a decimal point only appears if it is followed by digits. 
  3738.  
  3739. If either ios::scientific or ios::fixed is set, ios::x_precision specifies the 
  3740. number of digits following the decimal point. 
  3741.  
  3742. You can also use the parameterized manipulator setprecision to set 
  3743. ios::x_precision. 
  3744.  
  3745.  
  3746. ΓòÉΓòÉΓòÉ 4.4.6.4. setf - Set Specific Format Flags ΓòÉΓòÉΓòÉ
  3747.  
  3748. Class: ios 
  3749.  
  3750. long setf(long newset);
  3751. long setf(long newset, long field);
  3752.  
  3753. There are two versions of setf(). The version that takes one argument is 
  3754. accumulative. It sets the format flags that are marked in newset, without 
  3755. affecting flags that are not marked in newset, and returns the previous value 
  3756. of the format state. You can also use the parameterized manipulator setiosflags 
  3757. to set the format flags to a specific setting. 
  3758.  
  3759. The version of setf() that takes two arguments clears the format flags 
  3760. specified in field, sets the format flags specified in newset, and returns the 
  3761. previous value of the format state. For example, to change the conversion base 
  3762. in the format state to ios::hex, you could use a statement like this: 
  3763.  
  3764. s.setf(ios::hex, ios::basefield);
  3765.  
  3766. In this statement, ios::basefield specifies the conversion base as the format 
  3767. flag that is going to be changed, and ios::hex specifies the new value for the 
  3768. conversion base. If newset equals 0, all of the format flags specified in field 
  3769. are cleared. You can also use the parameterized manipulator resetiosflags to 
  3770. clear format flags. 
  3771.  
  3772. The following example demonstrates the use of flags(), setf(), and unsetf(). 
  3773. The main() function changes the flags as follows: 
  3774.  
  3775.  1. The original settings of the format state flags are determined, using 
  3776.     flags(). 
  3777.  
  3778.  2. ios::fixed is set, and all other flags are cleared, using 
  3779.     flags(ios::fixed). 
  3780.  
  3781.  3. ios::adjustfield is set to ios::right, without affecting other fields, 
  3782.     using setf(ios::right). 
  3783.  
  3784.  4. ios::floatfield is set to ios::scientific without affecting other fields, 
  3785.     using setf(ios::scientific | ios::left,ios::floatfield). ios::left does not 
  3786.     affect the format state here because the second argument to setf() does not 
  3787.     include ios::adjustfield.  There would be no point in coding a call this 
  3788.     way in a real application, but it illustrates the effect of using a mask 
  3789.     for the second argument of setf(). 
  3790.  
  3791.  5. The original format state is restored, by calling flags() with an argument 
  3792.     of originalFlags, which contains the format state determined in step 1. 
  3793.  
  3794. The function showFlags() determines and displays the current flag settings. It 
  3795. obtains the value of the settings using flags(), then excludes ios::oct from 
  3796. the result before displaying the result in octal. This exclusion is done in 
  3797. order to display the result in octal without causing the octal setting for 
  3798. ios::basefield to show up in the program's output. 
  3799.  
  3800. /* Program to demonstrate use of flags(), flags(long), setf(long),
  3801.    and setf(long,long).                                            */
  3802.  
  3803. #include <iostream.h>
  3804. void showFlags() {
  3805. // save altered flag settings, but clear ios::oct from them
  3806.       long flagSettings = cout.flags() & (~ios::oct) ;
  3807. // display those flag settings in octal
  3808.       cout << oct << flagSettings << endl;
  3809. }
  3810.  
  3811.  
  3812. void main () {
  3813. // get and display current flag settings using flags()
  3814.       cout << "flags():                             ";
  3815.       long originalFlags = cout.flags();
  3816.       showFlags();
  3817.  
  3818. // change format state using flags(long)
  3819.       cout << "flags(ios::fixed):                   ";
  3820.       cout.flags(ios::fixed);
  3821.       showFlags();
  3822.  
  3823. // change adjust field using setf(long)
  3824.       cout << "setf(ios::right):                    ";
  3825.       cout.setf(ios::right);
  3826.       showFlags();
  3827.  
  3828. // change floatfield using setf(long, long)
  3829.       cout << "setf(ios::scientific | ios::left,\n"
  3830.            << "ios::floatfield):                    ";
  3831.       cout.setf(ios::scientific | ios::left,ios::floatfield);
  3832.       showFlags();
  3833.  
  3834. // reset to original setting
  3835.       cout << "flags(originalFlags):                ";
  3836.       cout.flags(originalFlags);
  3837.       showFlags();
  3838. }
  3839.  
  3840. This example produces the following output: 
  3841.  
  3842. flags():                             21
  3843. flags(ios::fixed):                   10000
  3844. setf(ios::right):                    10004
  3845. setf(ios::scientific | ios::left,
  3846. ios::floatfield):                    4004
  3847. flags(originalFlags):                21
  3848.  
  3849. Note:  If you set conflicting flags the results are unpredictable. 
  3850.  
  3851.  
  3852. ΓòÉΓòÉΓòÉ 4.4.6.5. skip - Set ios::skipws Format Flag ΓòÉΓòÉΓòÉ
  3853.  
  3854. Class: ios 
  3855.  
  3856. int skip(int i);
  3857.  
  3858. skip() sets the format flag ios::skipws if the value of the argument i does not 
  3859. equal 0. If i does equal 0, ios::skipws is cleared. skip() returns a value of 1 
  3860. if ios::skipws was set prior to the call to skip(), and returns 0 otherwise. 
  3861.  
  3862.  
  3863. ΓòÉΓòÉΓòÉ 4.4.6.6. unsetf - Turn Off Format Flags ΓòÉΓòÉΓòÉ
  3864.  
  3865. Class: ios 
  3866.  
  3867. long unsetf(long oflags);
  3868.  
  3869. unsetf() turns off the format flags specified in oflags and returns the 
  3870. previous format state. 
  3871.  
  3872.  
  3873. ΓòÉΓòÉΓòÉ 4.4.6.7. width - Set Field Width ΓòÉΓòÉΓòÉ
  3874.  
  3875. Class: ios 
  3876.  
  3877. int width() const;
  3878. int width(int fwidth);
  3879.  
  3880. There are two versions of width(). The version that takes no arguments returns 
  3881. the value of the current setting of the format state field width variable, 
  3882. ios::x_width. If the value of ios::x_width is smaller than the space needed for 
  3883. the representation of the value, the full value is still inserted. 
  3884.  
  3885. The version of width() that takes one argument fwidth sets ios::x_width to the 
  3886. value of fwidth and returns the previous value. The default field width is 0. 
  3887. When the value of ios::x_width is 0, the operations that insert values only 
  3888. insert the characters needed to represent a value. 
  3889.  
  3890. If the value of ios::x_width is greater than 0, the characters needed to 
  3891. represent the value are inserted. Then fill characters are inserted, if 
  3892. necessary, so that the representation of the value takes up the entire field. 
  3893. ios::x_width only specifies a minimum width, not a maximum width. If the number 
  3894. of characters needed to represent a value is greater than the field width, none 
  3895. of the characters is truncated. After every insertion of a numeric value or a 
  3896. string, the value of ios::x_width is reset to 0. After every extraction of a 
  3897. value of one of the following types, the value of ios::x_width is reset to 0: 
  3898.  
  3899. o char* 
  3900. o unsigned char* 
  3901. o signed char* 
  3902. o float, double and long double values 
  3903. o integral values:short, int, long 
  3904. o wchar_t * 
  3905.  
  3906. Extractions of any other types do not affect ios::x_width. 
  3907.  
  3908. You can also use the parameterized manipulator setw to set the field width. 
  3909.  
  3910.  
  3911. ΓòÉΓòÉΓòÉ 4.4.7. Public Members of ios for User-Defined Format Flags ΓòÉΓòÉΓòÉ
  3912.  
  3913. In addition to the flags described in Format State Flags, you can also use the 
  3914. ios member functions listed in this section to define additional format flags 
  3915. or variables in classes that you derive from ios. 
  3916.  
  3917. The following member functions are described: 
  3918.  
  3919. o bitalloc - Create bit set 
  3920. o iword - Return reference to user-defined flag 
  3921. o pword - Return reference to user-defined flag 
  3922. o xalloc - Return index to format state variables. 
  3923.  
  3924.  
  3925. ΓòÉΓòÉΓòÉ 4.4.7.1. bitalloc - Create Bit Set ΓòÉΓòÉΓòÉ
  3926.  
  3927. Class: ios 
  3928.  
  3929. static long bitalloc();
  3930.  
  3931. bitalloc() is a static function that returns a long value with a previously 
  3932. unallocated bit set. You can use this long value as an additional flag, and 
  3933. pass it as an argument to the format state member functions. When all the bits 
  3934. are exhausted, bitalloc() returns 0. 
  3935.  
  3936.  
  3937. ΓòÉΓòÉΓòÉ 4.4.7.2. iword - Return Reference to User-Defined Flag ΓòÉΓòÉΓòÉ
  3938.  
  3939. Class: ios 
  3940.  
  3941. long& iword(int i);
  3942.  
  3943. iword() returns a reference to the ith user-defined flag, where i is an index 
  3944. returned by xalloc(). iword() allocates space for the user-defined flag. If the 
  3945. allocation fails, iword() sets ios::failbit. 
  3946.  
  3947.  
  3948. ΓòÉΓòÉΓòÉ 4.4.7.3. pword - Return Reference to User-Defined Flag ΓòÉΓòÉΓòÉ
  3949.  
  3950. Class: ios 
  3951.  
  3952. void* & pword(int i);
  3953.  
  3954. pword() returns a reference to a pointer to the ith user-defined flag, where i 
  3955. is an index returned by xalloc(). pword() allocates space for the user-defined 
  3956. flag. If the allocation fails, pword() sets ios::failbit. pword() is the same 
  3957. as iword() except that the two functions return different types. 
  3958.  
  3959.  
  3960. ΓòÉΓòÉΓòÉ 4.4.7.4. xalloc - Return Index to Format State Variables ΓòÉΓòÉΓòÉ
  3961.  
  3962. Class: ios 
  3963.  
  3964. static int xalloc();
  3965.  
  3966. xalloc() is a static function that returns an unused index into an array of 
  3967. words available for use as format state variables by classes derived from ios. 
  3968.  
  3969. xalloc() simply returns a new index; it does not do any allocation. iword() and 
  3970. pword() do the allocation, and if the allocation fails, they set ios::failbit. 
  3971. You should check ios::failbit after calling iword() or pword(). 
  3972.  
  3973.  
  3974. ΓòÉΓòÉΓòÉ 4.4.8. Public Members of ios for the Error State ΓòÉΓòÉΓòÉ
  3975.  
  3976. The error state is an enumeration that records the errors that take place in 
  3977. the processing of ios objects. It has the following declaration: 
  3978.  
  3979. enum io_state { goodbit, eofbit, failbit, badbit, hardfail };
  3980.  
  3981. The error state is manipulated using the ios member functions described in this 
  3982. section. 
  3983.  
  3984. Note: 
  3985.  
  3986.  1. hardfail is a flag used internally by the I/O Stream Library. Do not use 
  3987.     it. 
  3988.  
  3989.  2. The following descriptions assume that the functions are called as part of 
  3990.     an ios object called iosobj. 
  3991.  
  3992. The following member functions are described: 
  3993.  
  3994. o bad - Check ios::badbit 
  3995. o clear - Clear or Set Error State 
  3996. o eof - Check ios::eofbit 
  3997. o fail - Check ios::failbit and ios::badbit 
  3998. o good - Are Any Bits Set 
  3999. o rdstate - Return Error State 
  4000. o Convert ios Object to Pointer Operator void*() 
  4001. o Check ios::failbit and ios::badbit Operator ! 
  4002.  
  4003.  
  4004. ΓòÉΓòÉΓòÉ 4.4.8.1. bad - Check ios::badbit ΓòÉΓòÉΓòÉ
  4005.  
  4006. Class: ios 
  4007.  
  4008. int bad() const;
  4009.  
  4010. bad() returns a nonzero value if ios::badbit is set in the error state of 
  4011. iosobj. Otherwise, it returns 0. ios::badbit is usually set when some operation 
  4012. on the streambuf object that is associated with the ios object has failed. It 
  4013. will probably not be possible to continue input and output operations on the 
  4014. ios object. 
  4015.  
  4016.  
  4017. ΓòÉΓòÉΓòÉ 4.4.8.2. clear - Clear or Set Error State ΓòÉΓòÉΓòÉ
  4018.  
  4019. Class: ios 
  4020.  
  4021. void clear(int state=0);
  4022.  
  4023. clear() changes the error state of iosobj to state. If state equals 0 (its 
  4024. default), all of the bits in the error state are cleared. If you want to set 
  4025. one of the bits without clearing or setting the other bits in the error state, 
  4026. you can bitwise OR the bit you want to set with the current error state. For 
  4027. example, the following statement sets ios::badbit in iosobj and leaves all the 
  4028. other error state bits unchanged: 
  4029.  
  4030. iosobj.clear(ios::badbit|iosobj.rdstate());
  4031.  
  4032.  
  4033. ΓòÉΓòÉΓòÉ 4.4.8.3. eof - Check ios::eofbit ΓòÉΓòÉΓòÉ
  4034.  
  4035. Class: ios 
  4036.  
  4037. int eof() const;
  4038.  
  4039. eof() returns a nonzero value if ios::eofbit is set in the error state of 
  4040. iosobj. Otherwise, it returns 0. ios::eofbit is usually set when an EOF has 
  4041. been encountered during an extraction operation. 
  4042.  
  4043.  
  4044. ΓòÉΓòÉΓòÉ 4.4.8.4. fail - Check ios::failbit and ios::badbit ΓòÉΓòÉΓòÉ
  4045.  
  4046. Class: ios 
  4047.  
  4048. int fail() const;
  4049.  
  4050. fail() returns a nonzero value if either ios::badbit or ios::failbit is set in 
  4051. the error state. Otherwise, it returns 0. 
  4052.  
  4053.  
  4054. ΓòÉΓòÉΓòÉ 4.4.8.5. good - Are Any Bits Set ΓòÉΓòÉΓòÉ
  4055.  
  4056. Class: ios 
  4057.  
  4058. int good() const;
  4059.  
  4060. good() returns a nonzero value if no bits are set in the error state of iosobj. 
  4061. Otherwise, it returns 0. 
  4062.  
  4063.  
  4064. ΓòÉΓòÉΓòÉ 4.4.8.6. rdstate - Return Error State ΓòÉΓòÉΓòÉ
  4065.  
  4066. Class: ios 
  4067.  
  4068. int rdstate() const;
  4069.  
  4070. rdstate() returns the current value of the error state of iosobj. 
  4071.  
  4072.  
  4073. ΓòÉΓòÉΓòÉ 4.4.8.7. Convert ios Object to Pointer Operator void*() ΓòÉΓòÉΓòÉ
  4074.  
  4075. Class: ios 
  4076.  
  4077. operator void*();
  4078. operator const void*() const;
  4079.  
  4080. The operator void*() converts iosobj to a pointer so that it can be compared to 
  4081. 0. The conversion returns 0 if ios::failbit or ios::badbit is set in the error 
  4082. state of iosobj. Otherwise, a pointer value is returned. This value is not 
  4083. meant to be manipulated as a pointer; the purpose of the operator is to allow 
  4084. you to write statements such as the following: 
  4085.  
  4086. if (cin)
  4087.      cout << "ios::badbit and ios::failbit are not set" << endl;
  4088. if (cin >> x)
  4089.      cout << "ios::badbit and ios::failbit are not set "
  4090.           << x << " was input" << endl;
  4091.  
  4092.  
  4093. ΓòÉΓòÉΓòÉ 4.4.8.8. Check ios::failbit and ios::badbit Operator ! ΓòÉΓòÉΓòÉ
  4094.  
  4095. Class: ios 
  4096.  
  4097. int operator!() const;
  4098.  
  4099. The operator !() returns a nonzero value if ios::failbit or ios::badbit is set 
  4100. in the error state of iosobj. This allows you to write statements like this: 
  4101.  
  4102. if (!cin)
  4103.      cout << "either ios::failbit or ios::badbit is set" << endl;
  4104. else
  4105.      cout << "neither ios::failbit nor ios::badbit is set"
  4106.           << endl;
  4107.  
  4108.  
  4109. ΓòÉΓòÉΓòÉ 4.4.9. Other Members of ios ΓòÉΓòÉΓòÉ
  4110.  
  4111. This section describes the ios member functions that do not deal with the error 
  4112. state or the format state. These descriptions assume that the functions are 
  4113. called as part of an ios object called iosobj. 
  4114.  
  4115. The following functions are described: 
  4116.  
  4117. o rdbuf - Return pointer to associated streambuf object 
  4118. o sync_with_stdio - Attach stdiobuf object to predefined streams 
  4119. o tie - Set the tie variable 
  4120.  
  4121.  
  4122. ΓòÉΓòÉΓòÉ 4.4.9.1. rdbuf - Return Pointer to Associated streambuf Object ΓòÉΓòÉΓòÉ
  4123.  
  4124. Class: ios 
  4125.  
  4126. streambuf* rdbuf();
  4127.  
  4128. rdbuf() returns a pointer to the streambuf object that is associated with 
  4129. iosobj. This is the streambuf object that was passed as an argument to the ios 
  4130. constructor. 
  4131.  
  4132.  
  4133. ΓòÉΓòÉΓòÉ 4.4.9.2. sync_with_stdio - Attach stdiobuf Object to Predefined Streams ΓòÉΓòÉΓòÉ
  4134.  
  4135. Class: ios 
  4136.  
  4137. static void sync_with_stdio();
  4138.  
  4139. sync_with_stdio() is a static function that solves the problems that occur when 
  4140. you call functions declared in stdio.h and I/O Stream Library functions in the 
  4141. same program. The first time that you call sync_with_stdio(), it attaches 
  4142. stdiobuf objects to the predefined streams cin, cout, and cerr. After that, 
  4143. input and output using these predefined streams can be mixed with input and 
  4144. output using the corresponding FILE objects (stdin, stdout, and stderr). This 
  4145. input and output is correctly synchronized. 
  4146.  
  4147. If you switch between the I/O Stream Library formatted extraction functions and 
  4148. stdio.h functions, you may find that a byte is "lost". The reason is that the 
  4149. formatted extraction functions for integers and floating-point values keep 
  4150. extracting characters until a non-digit character is encountered. This 
  4151. non-digit character acts as a delimiter for the value that preceded it. It is 
  4152. not part of the value, so putback() is called to return it to the stream 
  4153. buffer. If a C stdio library function, such as getchar(), performs the next 
  4154. input operation, it will begin input at the character after this non-digit 
  4155. character. Thus, this non-digit character is not part of the value extracted by 
  4156. the formatted extraction function, and it is not the character extracted by the 
  4157. C stdio library function. It is "lost". Therefore, you should avoid switching 
  4158. between the I/O Stream Library formatted extraction functions and C stdio 
  4159. library functions whenever possible. 
  4160.  
  4161. sync_with_stdio() makes cout and cerr unit buffered. After you call 
  4162. sync_with_stdio(), the performance of your program could diminish. The 
  4163. performance of your program is affected adversely if you are dealing mostly 
  4164. with relatively short strings. 
  4165.  
  4166. Note:  You should use I/O Stream Library functions exclusively for all new 
  4167. code. 
  4168.  
  4169.  
  4170. ΓòÉΓòÉΓòÉ 4.4.9.3. tie - Set the tie Variable ΓòÉΓòÉΓòÉ
  4171.  
  4172. Class: ios 
  4173.  
  4174. ostream* tie();
  4175. ostream* tie(ostream* os);
  4176.  
  4177. There are two versions of tie(). The version that takes no arguments returns 
  4178. the value of ios::x_tie, the tie variable. The version that takes one argument 
  4179. os makes the tie variable, ios::x_tie, equal to os and returns the previous 
  4180. value. 
  4181.  
  4182. You can use ios::x_tie to automatically flush the stream buffer attached to an 
  4183. ios object. If ios::x_tie for an ios object is not equal to 0 and the ios 
  4184. object needs more characters or has characters to be consumed, the ostream 
  4185. object pointed to by ios::x_tie is flushed. 
  4186.  
  4187. By default, the tie variables of the predefined streams cin, cerr, and clog all 
  4188. point to the predefined stream cout. For example, the initial return value of 
  4189. cin.tie() is a pointer to cout. 
  4190.  
  4191.  
  4192. ΓòÉΓòÉΓòÉ 4.4.10. Built-In Manipulators for ios ΓòÉΓòÉΓòÉ
  4193.  
  4194. The I/O Stream Library provides you with a set of built-in manipulators for ios 
  4195. and the classes derived from it. These manipulators have a specific effect on a 
  4196. stream other than inserting or extracting a value. Manipulators implicitly 
  4197. invoke functions that modify the state of the stream, and they allow you to 
  4198. modify the state of a stream at the same time as you are doing input and 
  4199. output. The syntax for manipulators is consistent with the syntax for input and 
  4200. output. 
  4201.  
  4202. The following is a list of the manipulators and the classes that they apply to: 
  4203.  
  4204. dec       istream and ostream 
  4205. hex       istream and ostream 
  4206. oct       istream and ostream 
  4207. ws        istream 
  4208. endl      ostream 
  4209. ends      ostream 
  4210. flush     ostream 
  4211.  
  4212. See Built-In Manipulators for istream for more details on the built-in 
  4213. manipulators for istream. See Built-In Manipulators for ostream for more 
  4214. details on the manipulators for ostream. 
  4215.  
  4216.  
  4217. ΓòÉΓòÉΓòÉ 4.5. istream and istream_withassign Class ΓòÉΓòÉΓòÉ
  4218.  
  4219. This chapter describes the istream class and its derived class 
  4220. istream_withassign. The istream member functions allow you to take characters 
  4221. out of the stream buffer that is associated with an istream object. 
  4222. istream_withassign is derived from istream and includes an assignment operator. 
  4223.  
  4224. istream supports two kinds of input: 
  4225.  
  4226. o Unformatted input: characters are taken as a sequence of bytes from the 
  4227.   stream buffer that is associated with the istream object. 
  4228. o Formatted input: characters are taken from the stream buffer associated with 
  4229.   the istream object, converted to a given type, and stored in a variable of 
  4230.   that type. 
  4231.  
  4232. The following topics are described in this chapter: 
  4233.  
  4234. o Declarations for istream and istream_withassign in the iostream.h header file 
  4235. o Constructors for istream 
  4236. o Input prefix function 
  4237. o Public members of istream for formatted input 
  4238. o Public members of istream for unformatted input 
  4239. o Public members of istream for positioning 
  4240. o Other public members of istream 
  4241. o Built-In Manipulators for istream 
  4242. o Public members of istream_withassign 
  4243.  
  4244.  
  4245. ΓòÉΓòÉΓòÉ 4.5.1. Declarations for istream and istream_withassign in iostream.h ΓòÉΓòÉΓòÉ
  4246.  
  4247. You must include the following statement in any file that uses member functions 
  4248. of the istream class: 
  4249.  
  4250. #include <iostream.h>
  4251.  
  4252. The following is an excerpt from the iostream.h header file that shows the 
  4253. relevant declarations for the members of istream: 
  4254.  
  4255. class istream : virtual public ios {
  4256. public:
  4257.                istream(streambuf*);
  4258.                istream(streambuf*, int sk, ostream* t=0);
  4259.                istream(int size ,char*,int sk=1);
  4260.                istream(int fd,int sk=1, ostream* t=0);
  4261.      virtual   ~istream();
  4262.      int       ipfx(int noskipws=0);
  4263.      void      isfx();
  4264.      istream&  seekg(streampos p);
  4265.      istream&  seekg(streamoff o, ios::seek_dir d);
  4266.      streampos tellg();
  4267.      istream&  operator>> (istream& (*f)(istream&));
  4268.      istream&  operator>> (ios& (*f)(ios&) );
  4269.      istream&  operator>>(char*);
  4270.      istream&  operator>>(signed char*);
  4271.      istream&  operator>>(unsigned char*);
  4272.      istream&  operator>>(char& c);
  4273.      istream&  operator>>(signed char& c);
  4274.      istream&  operator>>(unsigned char& c);
  4275.      istream&  operator>>(short&);
  4276.      istream&  operator>>(int&);
  4277.      istream&  operator>>(long&);
  4278.      istream&  operator>>(unsigned short&);
  4279.      istream&  operator>>(unsigned int&);
  4280.      istream&  operator>>(unsigned long&);
  4281.      istream&  operator>>(float&);
  4282.      istream&  operator>>(double&);
  4283.      istream&  operator>>(long double&);
  4284.      istream&  operator>>(streambuf*);
  4285.      istream&  operator>>(wchar_t&);
  4286.      istream&  operator>>(wchar_t *);
  4287.      istream&  rs_complicated(char& c);
  4288.      istream&  rs_complicated(signed char& c);
  4289.      istream&  rs_complicated(unsigned char& c);
  4290.      istream&  get(char* , int lim, char delim='\n');
  4291.      istream&  get(signed char* b,int lim,
  4292.                    char delim='\n');
  4293.      istream&  get(unsigned char* b,int lim,
  4294.                    char delim='\n');
  4295.      istream&  get(streambuf& sb, char delim ='\n');
  4296.  
  4297.      istream&  get_complicated(char& c);
  4298.      istream&  get_complicated(signed char& c);
  4299.      istream&  get_complicated(unsigned char& c);
  4300.      istream&  get(char& c);
  4301.      istream&  get(signed char& c);
  4302.      istream&  get(unsigned char& c);
  4303.      int       get();
  4304.      istream&  getline(char* b, int lim,
  4305.                        char delim='\n');
  4306.      istream&  getline(signed char* b, int lim,
  4307.                        char delim='\n');
  4308.      istream&  getline(unsigned char* b, int lim,
  4309.                        char delim='\n');
  4310.      istream&  get(wchar_t&);
  4311.      int       peek();
  4312.      istream&  ignore(int n=1,int delim=EOF);
  4313.      istream&  read(char*  s,int n);
  4314.      istream&  read(signed char* s,int n);
  4315.      istream&  read(unsigned char* s,int n);
  4316.      int       gcount();
  4317.      istream&  putback(char c);
  4318.      int       sync();
  4319. protected:
  4320.                istream();
  4321.      int       do_ipfx(int noskipws);
  4322.      void      eatwhite();
  4323. };
  4324.  
  4325. class istream_withassign : public istream {
  4326. public:
  4327.                          istream_withassign();
  4328.      virtual             ~istream_withassign();
  4329.      istream_withassign& operator=(istream&);
  4330.      istream_withassign& operator=(streambuf*);
  4331. };
  4332.  
  4333. extern istream_withassign cin;
  4334.  
  4335. ios&          dec(ios&);
  4336. ios&          hex(ios&);
  4337. ios&          oct(ios&);
  4338. istream&      ws(istream&);
  4339.  
  4340. Note:  The following functions have declarations included in the above listing 
  4341. of iostream.h, but they are only for the internal use of the I/O Stream 
  4342. Library. You should not use them directly: 
  4343.  
  4344. o isfx() 
  4345. o rs_complicated() 
  4346. o get_complicated() 
  4347. o do_ipfx() 
  4348. o eatwhite(). 
  4349.  
  4350.  
  4351. ΓòÉΓòÉΓòÉ 4.5.2. Constructors for istream ΓòÉΓòÉΓòÉ
  4352.  
  4353. Class: istream 
  4354.  
  4355. istream(streambuf* sb);
  4356.  
  4357. The istream constructor takes a single argument sb. The constructor creates an 
  4358. istream object that is attached to the streambuf object that is pointed to by 
  4359. sb. The constructor also initializes the format variables to their defaults. 
  4360.  
  4361. The other istream constructor declarations in iostream.h are obsolete; do not 
  4362. use them. 
  4363.  
  4364.  
  4365. ΓòÉΓòÉΓòÉ 4.5.3. Input Prefix Function ΓòÉΓòÉΓòÉ
  4366.  
  4367. int ipfx(int need=0);
  4368.  
  4369. ipfx() checks the stream buffer attached to an istream object to determine if 
  4370. it is capable of satisfying requests for characters. It returns a nonzero value 
  4371. if the stream buffer is ready, and 0 if it is not. 
  4372.  
  4373. The formatted input operator calls ipfx(0), while the unformatted input 
  4374. functions call ipfx(1). 
  4375.  
  4376. If the error state of the istream object is nonzero, ipfx() returns 0. 
  4377. Otherwise, the stream buffer attached to the istream object is flushed if 
  4378. either of the following conditions is true: 
  4379.  
  4380. o need has a value of 0. 
  4381. o The number of characters available in the stream buffer is fewer than the 
  4382.   value of need. 
  4383.  
  4384. If ios::skipws is set in the format state of the istream object and need has a 
  4385. value of 0, leading whitespace characters are extracted from the stream buffer 
  4386. and discarded. If ios::hardfail is set or EOF is encountered, ipfx() returns 0. 
  4387. Otherwise, it returns a nonzero value. 
  4388.  
  4389.  
  4390. ΓòÉΓòÉΓòÉ 4.5.4. Public Members of istream for Formatted Input ΓòÉΓòÉΓòÉ
  4391.  
  4392. The istream class lets you perform formatted input from a stream buffer using 
  4393. the input operator >>. Consider the following statement, where ins is a 
  4394. reference to an istream object and x is a variable of a built-in type: 
  4395.  
  4396. ins >> x;
  4397.  
  4398. The input operator >> calls ipfx(0). If ipfx() returns a nonzero value, the 
  4399. input operator extracts characters from the streambuf object that is associated 
  4400. with ins. It converts these characters to the type of x and stores the result 
  4401. in x. The input operator sets ios::failbit if the characters extracted from the 
  4402. stream buffer cannot be converted to the type of x. If the attempt to extract 
  4403. characters fails because EOF is encountered, the input operator sets 
  4404. ios::eofbit and ios::failbit. If the attempt to extract characters fails for 
  4405. another reason, the input operator sets ios::badbit. Even if an error occurs, 
  4406. the input operator always returns ins. 
  4407.  
  4408. The details of conversion depend on the format state of the istream object and 
  4409. the type of the variable x. The input operator may set the width variable 
  4410. ios::x_width to 0, but it does not change anything else in the format state. 
  4411.  
  4412. The input operator is defined for the following types: 
  4413.  
  4414. o Arrays of character values (including signed char and unsigned char) 
  4415. o Other integral values: short, int, long 
  4416. o float, double, and long double values. 
  4417.  
  4418. In addition, the input operator is defined for streambuf objects. 
  4419.  
  4420. The following sections describe the input operator for these types. 
  4421.  
  4422. Note:  The following descriptions assume that the input operator is called with 
  4423. the istream object ins on the left side of the operator. 
  4424.  
  4425.  
  4426. ΓòÉΓòÉΓòÉ 4.5.4.1. Input Operator for Arrays of Characters ΓòÉΓòÉΓòÉ
  4427.  
  4428. Class: istream 
  4429.  
  4430. istream& operator>>(char* pc);
  4431. istream& operator>>(signed char* pc);
  4432. istream& operator>>(unsigned char* pc);
  4433. istream& operator>>(wchar_t* pwc);
  4434.  
  4435. For pointers to char, signed char, and unsigned char, the input operator stores 
  4436. characters from the stream buffer attached to ins in the array pointed to by 
  4437. pc. The input operator stores characters until a whitespace character is found. 
  4438. This whitespace character is left in the stream buffer, and the extraction 
  4439. stops. If ios::x_width does not equal zero, a maximum of ios::x_width - 1 
  4440. characters are extracted. The input operator calls ins.width(0) to reset 
  4441. ios::x_width to 0. 
  4442.  
  4443. For pointers to wchar_t, the input operator stores characters from the stream 
  4444. buffer attached to ins in the array pointed to by pwc. The input operator 
  4445. stores characters until a whitespace character or a wchar_t blank is found. If 
  4446. the terminating character is a whitespace character, it is left in the stream 
  4447. buffer. If it is a wchar_t blank, it is discarded. This is done to avoid 
  4448. returning two bytes to the input stream. 
  4449.  
  4450. For wchar_t* arrays, if ios::width does not equal zero, a maximum of 
  4451. ios::width-1 characters (at 2 bytes each) are extracted. A 2-character space is 
  4452. reserved for the wchar_t terminating null character. 
  4453.  
  4454. Note:  The input operators for numeric types and for pointers to char 
  4455. (including pointers to signed char, unsigned char and wchar_t) reset 
  4456. ios::x_width to 0. None of the other input operators affects ios::x_width. All 
  4457. of the output operators, on the other hand, reset ios::x_width to 0. 
  4458.  
  4459. The input operator always stores a terminating null character in the array 
  4460. pointed to by pc or pwc, even if an error occurs. For arrays of wchar_t*, this 
  4461. terminating null character is a wchar_t terminating null character. 
  4462.  
  4463.  
  4464. ΓòÉΓòÉΓòÉ 4.5.4.2. Input Operator for char ΓòÉΓòÉΓòÉ
  4465.  
  4466. Class: istream 
  4467.  
  4468. istream& operator>>(char& rc);
  4469. istream& operator>>(signed char& rc);
  4470. istream& operator>>(unsigned char& rc);
  4471. istream& operator>>(wchar_t& rc);
  4472.  
  4473. For char, signed char, and unsigned char, the input operator extracts a 
  4474. character from the stream buffer attached to ins and stores it in rc. 
  4475.  
  4476. For references to wchar_t, the input operator extracts a wchar_t character from 
  4477. the stream buffer and stores it in wc. If ios::skipsw is set, the input 
  4478. operator skips leading wchar_t spaces as well as leading char white spaces. 
  4479.  
  4480.  
  4481. ΓòÉΓòÉΓòÉ 4.5.4.3. Input Operator for Other Integral Values - short, int, long ΓòÉΓòÉΓòÉ
  4482.  
  4483. Class: istream 
  4484.  
  4485. istream& operator>>(short& ir);
  4486. istream& operator>>(unsigned short& ir);
  4487. istream& operator>>(int& ir);
  4488. istream& operator>>(unsigned int& ir);
  4489. istream& operator>>(long& ir);
  4490. istream& operator>>(unsigned long& ir);
  4491.  
  4492. This section describes how the input operator works for references to the 
  4493. integral types: short, unsigned short, int, unsigned int, long, and unsigned 
  4494. long. For these integral types, the input operator extracts characters from the 
  4495. stream buffer associated with ins and converts them according to the format 
  4496. state of ins. The converted characters are then stored in ir. There is no 
  4497. overflow detection on conversion of integral types. 
  4498.  
  4499. The first character extracted from the stream buffer may be a sign (+ or -). 
  4500. The subsequent characters are converted until a non-digit character is 
  4501. encountered. This non-digit character is left in the stream buffer. Which 
  4502. characters are treated as digits depends on the setting of the following format 
  4503. flags: 
  4504.  
  4505. o ios::oct: the characters are converted to an octal value. Characters are 
  4506.   extracted from the stream buffer until a character that is not an octal digit 
  4507.   (a digit from 0 to 7) is encountered. If ios::oct is set and a signed value 
  4508.   is encountered, the value is converted into a decimal value. For example, if 
  4509.   the characters "- 45" are encountered in the input stream and ios::oct is 
  4510.   set, the decimal value - 37 is actually extracted. 
  4511.  
  4512. o ios::dec: the characters are converted to a decimal value. Characters are 
  4513.   extracted from the stream buffer until a character that is not a decimal 
  4514.   digit (a digit from 0 to 9) is encountered. 
  4515.  
  4516. o ios::hex: the characters are converted to a hexadecimal value. Characters are 
  4517.   extracted from the stream buffer until a character that is not a hexadecimal 
  4518.   digit (a digit from 0 to 9 or a letter from "A" to "F", upper or lower case) 
  4519.   is encountered. If ios::hex is set and a signed value is encountered, the 
  4520.   value is converted into a decimal value. For example, if the characters "-12" 
  4521.   are encountered in the input stream and ios::hex is set, the decimal value 
  4522.   -18 is actually extracted. 
  4523.  
  4524. If none of these format flags is set, the characters are converted according to 
  4525. the C++ lexical conventions. This conversion depends on the characters that 
  4526. follow the optional sign: 
  4527.  
  4528. o If these characters are "0x" or "0X", the subsequent characters are converted 
  4529.   to a hexadecimal value. 
  4530. o If the first character is "0" and the second character is not "x" or "X", the 
  4531.   subsequent characters are converted to an octal value. 
  4532. o If neither of these cases is true, the characters are converted to a decimal 
  4533.   value. 
  4534.  
  4535. If no digits are available in the stream buffer (other than the "0" in "0X" or 
  4536. "0x" preceding a hexadecimal value), the input operator sets ios::failbit in 
  4537. the error state of ins. 
  4538.  
  4539.  
  4540. ΓòÉΓòÉΓòÉ 4.5.4.4. Input Operator for float and double Values ΓòÉΓòÉΓòÉ
  4541.  
  4542. Class: istream 
  4543.  
  4544. istream& operator>>(float& ref);
  4545. istream& operator>>(double& ref);
  4546. istream& operator>>(long double& ref);
  4547.  
  4548. For float, double, and long double values, the input operator converts 
  4549. characters from the stream buffer attached to ins according to the C++ lexical 
  4550. conventions. 
  4551.  
  4552. The following conversions occur for certain string values: 
  4553.  
  4554. o If the value consists of the character strings "inf" or "infinity" in any 
  4555.   combination of uppercase and lowercase letters, the string is converted to 
  4556.   the appropriate type's representation of infinity. 
  4557. o If the value consists of the character string "nan" in any combination of 
  4558.   uppercase and lowercase letters, the string is converted to the appropriate 
  4559.   type's representation of a NaN. 
  4560.  
  4561. The resulting value is stored in ref. The input operator sets ios::failbit if 
  4562. no digits are available in the stream buffer or if the digits that are 
  4563. available do not begin a floating-point number. 
  4564.  
  4565.  
  4566. ΓòÉΓòÉΓòÉ 4.5.4.5. Input Operator for streambuf Objects ΓòÉΓòÉΓòÉ
  4567.  
  4568. Class: istream 
  4569.  
  4570. istream& operator>>(streambuf* sb);
  4571.  
  4572. For pointers to streambuf objects, the input operator calls ipfx(0). If ipfx(0) 
  4573. returns a nonzero value, the input operator extracts characters from the stream 
  4574. buffer attached to ins and inserts them in sb. Extraction stops when an EOF 
  4575. character is encountered. The input operator always returns ins. 
  4576.  
  4577.  
  4578. ΓòÉΓòÉΓòÉ 4.5.5. Public Members of istream for Unformatted Input ΓòÉΓòÉΓòÉ
  4579.  
  4580. The functions listed in this section allow you to extract characters from a 
  4581. stream buffer as a sequence of bytes. All of these functions call ipfx(1). They 
  4582. only proceed with their processing if ipfx(1) returns a nonzero value. 
  4583.  
  4584. Note:  The following descriptions assume that the functions are called as part 
  4585. of an  istream object called ins. The following functions are described: 
  4586.  
  4587. o get - Extract characters and store in array 
  4588. o get - Extract characters and store in stream buffer 
  4589. o get - Extract character and store in char 
  4590. o get - Extract character and return it 
  4591. o getline - Extract characters and store in array 
  4592. o ignore - Extract characters and discard them 
  4593. o read - Extract characters and store in array 
  4594.  
  4595.  
  4596. ΓòÉΓòÉΓòÉ 4.5.5.1. get - Extract Characters and Store in Array ΓòÉΓòÉΓòÉ
  4597.  
  4598. Class: istream 
  4599.  
  4600. istream& get(char* ptr, int len, char delim='\n');
  4601. istream& get(signed char* ptr, int len, char delim='\n');
  4602. istream& get(unsigned char* ptr, int len, char delim='\n');
  4603.  
  4604. get() with three arguments extracts characters from the stream buffer attached 
  4605. to ins and stores them in the byte array beginning at the location pointed to 
  4606. by ptr and extending for len bytes. The default value of the delim argument is 
  4607. '\n'. Extraction stops when either of the following conditions is true: 
  4608.  
  4609. o delim or EOF is encountered before len-1 characters have been stored in the 
  4610.   array. delim is left in the stream buffer and not stored in the array. 
  4611. o len-1 characters are extracted without delim or EOF being encountered. 
  4612.  
  4613. get() always stores a terminating null character in the array, even if it does 
  4614. not extract any characters from the stream buffer. get() sets the ios::failbit 
  4615. if it encounters an EOF character before it stores any characters. 
  4616.  
  4617.  
  4618. ΓòÉΓòÉΓòÉ 4.5.5.2. get - Extract Characters and Store in Stream Buffer ΓòÉΓòÉΓòÉ
  4619.  
  4620. Class: istream 
  4621.  
  4622. istream& get(streambuf& sb, char delim='\n');
  4623.  
  4624. get() with two arguments extracts characters from the stream buffer attached to 
  4625. ins and stores them in sb. The default value of the delim argument is "\n". 
  4626. Extraction stops when any of the following conditions is true: 
  4627.  
  4628. o An EOF character is encountered. 
  4629. o An attempt to store a character in sb fails. ios::failbit is set in the error 
  4630.   state of ins. 
  4631. o delim is encountered. delim is left in the stream buffer attached to ins. 
  4632.  
  4633.  
  4634. ΓòÉΓòÉΓòÉ 4.5.5.3. get - Extract Character and Store in char ΓòÉΓòÉΓòÉ
  4635.  
  4636. Class: istream 
  4637.  
  4638. istream& get(char& cref);
  4639. istream& get(signed char& cref);
  4640. istream& get(unsigned char& cref);
  4641. istream& get(wchar_t& cref);
  4642.  
  4643. get() with a single argument extracts a single character or wchar_t from the 
  4644. stream buffer attached to ins and stores this character in cref. 
  4645.  
  4646.  
  4647. ΓòÉΓòÉΓòÉ 4.5.5.4. get - Extract Character and Return It ΓòÉΓòÉΓòÉ
  4648.  
  4649. Class: istream 
  4650.  
  4651. int get();
  4652.  
  4653. get() with no arguments extracts a character from the stream buffer attached to 
  4654. ins and returns it. This version of get() returns EOF if EOF is extracted. 
  4655. ios::failbit is never set. 
  4656.  
  4657.  
  4658. ΓòÉΓòÉΓòÉ 4.5.5.5. getline - Extract Characters and Store in Array ΓòÉΓòÉΓòÉ
  4659.  
  4660. Class: istream 
  4661.  
  4662. istream& getline(char* ptr, int len, char delim='\n');
  4663. istream& getline(signed char* ptr, int len, char delim='\n');
  4664. istream& getline(unsigned char* ptr, int len, char delim='\n');
  4665.  
  4666. getline() extracts characters from the stream buffer attached to ins and stores 
  4667. them in the byte array beginning at the location pointed to by ptr and 
  4668. extending for len bytes. The default value of the delim argument is "\n". 
  4669. Extraction stops when any one of the following conditions is true: 
  4670.  
  4671. o delim or EOF is encountered before len-1 characters have been stored in the 
  4672.   array. getline() extracts delim from the stream buffer, but it does not store 
  4673.   delim in the array. 
  4674. o len-1 characters are extracted before delim or EOF is encountered. 
  4675.  
  4676. getline() always stores a terminating null character in the array, even if it 
  4677. does not extract any characters from the stream buffer. getline() sets the 
  4678. ios::failbit for ins if it encounters an EOF character before it stores any 
  4679. characters. 
  4680.  
  4681. getline() is like get() with three arguments, except that get() does not 
  4682. extract the delim character from the stream buffer, while getline() does. 
  4683.  
  4684.  
  4685. ΓòÉΓòÉΓòÉ 4.5.5.6. ignore - Extract Characters and Discard Them ΓòÉΓòÉΓòÉ
  4686.  
  4687. Class: istream 
  4688.  
  4689. istream& ignore(int num=1, int delim=EOF);
  4690.  
  4691. ignore() extracts up to num character from the stream buffer attached to ins 
  4692. and discards them. ignore() will extract fewer than num characters if it 
  4693. encounters delim or EOF. 
  4694.  
  4695.  
  4696. ΓòÉΓòÉΓòÉ 4.5.5.7. read - Extract Characters and Store in Array ΓòÉΓòÉΓòÉ
  4697.  
  4698. Class: istream 
  4699.  
  4700. istream& read(char* s, int n);
  4701. istream& read(signed char* s, int n);
  4702. istream& read(unsigned char* s, int n);
  4703.  
  4704. read() extracts n characters from the stream buffer attached to ins and stores 
  4705. them in an array beginning at the position pointed to by s. If EOF is 
  4706. encountered before read() extracts n characters, read() sets the ios::failbit 
  4707. in the error state of ins. You can determine the number of characters that 
  4708. read() extracted by calling gcount() immediately after the call to read(). 
  4709.  
  4710.  
  4711. ΓòÉΓòÉΓòÉ 4.5.6. Public Members of istream for Positioning ΓòÉΓòÉΓòÉ
  4712.  
  4713. The following functions are described: 
  4714.  
  4715. o seekg - reposition get pointer of ultimate producer 
  4716. o tellg - return position of get pointer of ultimate producer 
  4717.  
  4718.  
  4719. ΓòÉΓòÉΓòÉ 4.5.6.1. seekg - Reposition Get Pointer of Ultimate Producer ΓòÉΓòÉΓòÉ
  4720.  
  4721. Class: istream 
  4722.  
  4723. istream& seekg(streampos sp);
  4724. istream& seekg(streamoff so, ios::seek_dir dir);
  4725.  
  4726. seekg() repositions the get pointer of the ultimate producer. seekg() with one 
  4727. argument sets the get pointer to the position sp. seekg() with two arguments 
  4728. sets the get pointer to the position specified by dir with the offset so. dir 
  4729. can have the following values: 
  4730.  
  4731. o ios::beg: the beginning of the stream 
  4732. o ios::cur: the current position of the get pointer 
  4733. o ios::end: the end of the stream 
  4734.  
  4735. If you attempt to set the get pointer to a position that is not valid, seekg() 
  4736. sets ios::badbit. 
  4737.  
  4738.  
  4739. ΓòÉΓòÉΓòÉ 4.5.6.2. tellg - Return Position of Get Pointer of Ultimate Producer ΓòÉΓòÉΓòÉ
  4740.  
  4741. Class: istream 
  4742.  
  4743. streampos tellg();
  4744.  
  4745. tellg() returns the current position of the get pointer of the ultimate 
  4746. producer. 
  4747.  
  4748.  
  4749. ΓòÉΓòÉΓòÉ 4.5.7. Other Public Members of istream ΓòÉΓòÉΓòÉ
  4750.  
  4751. Note:  The following descriptions assume that the functions are called as part 
  4752. of an  istream object called ins. 
  4753.  
  4754. The following member functions are described: 
  4755.  
  4756. o gcount - Return number of characters extracted 
  4757. o peek - Return next character without extracting it 
  4758. o putback - Put extracted characters back into stream buffer 
  4759. o sync - Synchronize stream buffer and ultimate producer 
  4760.  
  4761.  
  4762. ΓòÉΓòÉΓòÉ 4.5.7.1. gcount - Return Number of Characters Extracted ΓòÉΓòÉΓòÉ
  4763.  
  4764. Class: istream 
  4765.  
  4766. int gcount();
  4767.  
  4768. gcount() returns the number of characters extracted from the stream buffer 
  4769. attached to ins by the last call to an unformatted input function. 
  4770.  
  4771.  The input operator >> may call unformatted input
  4772. functions, and thus formatted input may affect the value returned by gcount(). 
  4773.  
  4774.  
  4775. ΓòÉΓòÉΓòÉ 4.5.7.2. peek - Return Next Character Without Extracting It ΓòÉΓòÉΓòÉ
  4776.  
  4777. Class: istream 
  4778.  
  4779. int peek();
  4780.  
  4781. peek() calls ipfx(1). If ipfx() returns zero, or if no more input is available 
  4782. from the ultimate producer, peek() returns EOF. Otherwise, it returns the next 
  4783. character in the stream buffer attached to ins without extracting the 
  4784. character. 
  4785.  
  4786.  
  4787. ΓòÉΓòÉΓòÉ 4.5.7.3. putback - Put Extracted Characters Back into Stream Buffer ΓòÉΓòÉΓòÉ
  4788.  
  4789. Class: istream 
  4790.  
  4791. istream& putback(char c);
  4792.  
  4793. putback() attempts to put a character that was extracted from the stream buffer 
  4794. attached to ins back into the stream buffer. c must equal the character before 
  4795. the get pointer of the stream buffer. Unless some other activity is modifying 
  4796. the stream buffer, this is the last character extracted from the stream buffer. 
  4797. If c is not equal to the character before the get pointer, the result of 
  4798. putback() is undefined, and the error state of ins may be set. putback() does 
  4799. not call ipfx(), but if the error state of ins is nonzero, putback() returns 
  4800. without doing anything. 
  4801.  
  4802.  
  4803. ΓòÉΓòÉΓòÉ 4.5.7.4. sync - Synchronize Stream Buffer and Ultimate Producer ΓòÉΓòÉΓòÉ
  4804.  
  4805. Class: istream 
  4806.  
  4807. int sync();
  4808.  
  4809. sync() establishes consistency between the ultimate producer and the stream 
  4810. buffer attached to ins. sync() calls ins.rdbuf()->sync(), which is a virtual 
  4811. function, so the details of its operation depend on the way the function is 
  4812. defined in a given derived class. If an error occurs, sync() returns EOF. 
  4813.  
  4814.  
  4815. ΓòÉΓòÉΓòÉ 4.5.8. Built-In Manipulators for istream ΓòÉΓòÉΓòÉ
  4816.  
  4817. istream&      ws(istream&);
  4818. ios&          dec(ios&);
  4819. ios&          hex(ios&);
  4820. ios&          oct(ios&);
  4821.  
  4822. The I/O Stream Library provides you with a set of built-in manipulators that 
  4823. can be used with the istream class. These manipulators have a specific effect 
  4824. on an istream object beyond extracting their own values. The built-in 
  4825. manipulators are accepted by the following versions of the input operator: 
  4826.  
  4827. istream& operator>> (istream& (*f) (istream&));
  4828. istream& operator>> (ios& (*f) (ios&));
  4829.  
  4830. If ins is a reference to an istream object, then this statement extracts 
  4831. whitespace characters from the stream buffer attached to ins: 
  4832.  
  4833. ins >> ws;
  4834.  
  4835. This statement sets ios::dec: 
  4836.  
  4837. ins >> dec;
  4838.  
  4839. This statement sets ios::hex: 
  4840.  
  4841. ins >> hex;
  4842.  
  4843. This statement sets ios::oct: 
  4844.  
  4845. ins >> oct;
  4846.  
  4847.  
  4848. ΓòÉΓòÉΓòÉ 4.5.9. Public Members of istream_withassign ΓòÉΓòÉΓòÉ
  4849.  
  4850. There are two public member functions of istream_withassign: 
  4851.  
  4852. o istream_withassign constructor 
  4853. o istream_withassign assignment operator 
  4854.  
  4855.  
  4856. ΓòÉΓòÉΓòÉ 4.5.9.1. istream_withassign Constructor ΓòÉΓòÉΓòÉ
  4857.  
  4858. Class: istream 
  4859.  
  4860. istream_withassign();
  4861.  
  4862. The istream_withassign constructor creates an istream_withassign object. It 
  4863. does not do any initialization of this object. 
  4864.  
  4865.  
  4866. ΓòÉΓòÉΓòÉ 4.5.9.2. istream_withassign Assignment Operator ΓòÉΓòÉΓòÉ
  4867.  
  4868. Class: istream 
  4869.  
  4870. istream_withassign& operator=(istream& is);
  4871. istream_withassign& operator=(streambuf* sb);
  4872.  
  4873. There are two versions of the istream_withassign assignment operator. The first 
  4874. version takes a reference to an istream object, is, as its argument. It 
  4875. associates the stream buffer attached to is with the istream_withassign object 
  4876. that is on the left side of the assignment operator. The following example 
  4877. shows how you can use this version of the assignment operator to redirect input 
  4878. that would have come from standard input so that it comes from a file: 
  4879.  
  4880. #include <iostream.h>
  4881. #include <fstream.h>
  4882.  
  4883. void main()
  4884. {
  4885.      ifstream infile("eglinton.dat");
  4886.  
  4887.      char *s=new char[10];
  4888.      float f;
  4889.      int i;
  4890.      cin = infile;
  4891.      //
  4892.      // from this point on, all input from cin really
  4893.      // comes from infile
  4894.      //
  4895.      cin >> s >> f >> i;
  4896.      cout <<"Here are the values from the input file: " << endl;
  4897.      cout << s << endl << f << endl << i << endl;
  4898.      infile.close();
  4899.      exit(0);
  4900. }
  4901.  
  4902. If the file eglinton.dat contains the following: 
  4903.  
  4904. Bloor
  4905. 5.5
  4906. 3
  4907.  
  4908. The example produces the following output: 
  4909.  
  4910. Here are the values from the input file:
  4911. Bloor
  4912. 5.5
  4913. 3
  4914.  
  4915. Note that this assignment operator gives the istream_withassign object cin the 
  4916. same stream buffer as the ifstream object, but the two objects maintain 
  4917. separate ios objects. Thus, if the error state is set through the common stream 
  4918. buffer, it must be reset twice, once for each of the two ios objects that are 
  4919. attached to the stream buffer. 
  4920.  
  4921. The second version of the assignment operator takes a pointer to a streambuf 
  4922. object, sb, as its argument. It associates this streambuf object with the 
  4923. istream_withassign object that is on the left side of the assignment operator. 
  4924. The following example shows how you can use this version of the assignment 
  4925. operator to associate cin with the input file eglinton.dat: 
  4926.  
  4927. #include <iostream.h>
  4928. #include <fstream.h>
  4929. #include <stdlib.h>
  4930.  
  4931. void main()
  4932. {
  4933.  
  4934.      ifstream infile("eglinton.dat");
  4935.  
  4936.      char *s=new char[10];
  4937.      float f;
  4938.      int i;
  4939.  
  4940.      //
  4941.      // from this point on, all input from cin really
  4942.      // comes from infile
  4943.      //
  4944.      cin = infile.rdbuf();
  4945.      cin >> s >> f >> i;
  4946.      cout <<"Here are the values from the input file: " << endl;
  4947.      cout << s << endl << f << endl << i << endl;
  4948.      infile.close();
  4949.      exit(0);
  4950. }
  4951.  
  4952. Given the same input, this example produces the same output as the preceding 
  4953. example. 
  4954.  
  4955.  
  4956. ΓòÉΓòÉΓòÉ 4.6. ostream and ostream_withassign Classes ΓòÉΓòÉΓòÉ
  4957.  
  4958. This chapter describes the ostream class and its derived class 
  4959. ostream_withassign. The ostream member functions allow you to put characters 
  4960. into the streambuf object that is associated with an ostream object. 
  4961. ostream_withassign is derived from ostream and includes an assignment operator. 
  4962.  
  4963. The following topics are described in this chapter: 
  4964.  
  4965. o Declarations for ostream and ostream_withassign in the iostream.h header file 
  4966. o Constructors for ostream 
  4967. o Output suffix and prefix functions 
  4968. o Public members of ostream for formatted output 
  4969. o Public members of ostream for unformatted output 
  4970. o Public members of ostream for positioning 
  4971. o Other public members of ostream 
  4972. o Built-in manipulators for ostream 
  4973. o Public members of ostream_withassign 
  4974.  
  4975.  
  4976. ΓòÉΓòÉΓòÉ 4.6.1. Declarations for ostream and ostream_withassign in iostream.h ΓòÉΓòÉΓòÉ
  4977.  
  4978. You must include the following statement in any file that uses member functions 
  4979. of the ostream class: 
  4980.  
  4981. #include <iostream.h>
  4982.  
  4983. The following is an excerpt from the iostream.h header file that shows the 
  4984. relevant declarations for the members of ostream: 
  4985.  
  4986. class ostream : virtual public ios {
  4987. public:
  4988.                   ostream(streambuf*) ;
  4989.                   ostream(int fd) ;
  4990.                   ostream(int size ,char*) ;
  4991.      virtual      ~ostream();
  4992.      int          opfx();
  4993.      void         osfx();
  4994.      ostream&     flush();
  4995.      ostream&     seekp(streampos p);
  4996.      ostream&     seekp(streamoff o,ios::seek_dir d);
  4997.      streampos    tellp();
  4998.      ostream&     put(char c);
  4999.      ostream&     complicated_put(char c);
  5000.      ostream&     operator<<(char c);
  5001.      ostream&     operator<<(signed char c);
  5002.      ostream&     operator<<(unsigned char c);
  5003.      ostream&     operator<<(wchar_t);
  5004.      ostream&     operator<<(const wchar_t *);
  5005.      ostream&     operator<<(const char*);
  5006.      ostream&     operator<<(const signed char*);
  5007.      ostream&     operator<<(const unsigned char*);
  5008.      ostream&     operator<<(int a);
  5009.      ostream&     operator<<(long);
  5010.      ostream&     operator<<(double);
  5011.      ostream&     operator<<(long double);
  5012.      ostream&     operator<<(float);
  5013.      ostream&     operator<<(unsigned int a);
  5014.      ostream&     operator<<(unsigned long);
  5015.      ostream&     operator<<(void*);
  5016.      ostream&     operator<<(streambuf*);
  5017.      ostream&     operator<<(short i);
  5018.      ostream&     operator<<(unsigned short i);
  5019.      ostream&     operator<<(ostream& (*f)(ostream&));
  5020.      ostream&     operator<<(ios& (*f)(ios&) );
  5021.      ostream&     ls_complicated(char);
  5022.      ostream&     ls_complicated(signed char);
  5023.      ostream&     ls_complicated(unsigned char);
  5024.      ostream&     write(const char*  s,int n);
  5025.      ostream&     write(const signed char* s, int n);
  5026.      ostream&     write(const unsigned char* s, int n);
  5027. protected:
  5028.      int          do_opfx();
  5029.      void         do_osfx();
  5030.                   ostream();
  5031. };
  5032.  
  5033. class ostream_withassign : public ostream {
  5034. public:
  5035.                          ostream_withassign();
  5036.      virtual             ~ostream_withassign();
  5037.      ostream_withassign& operator=(ostream&);
  5038.      ostream_withassign& operator=(streambuf*);
  5039. };
  5040.  
  5041. extern ostream_withassign cout;
  5042. extern ostream_withassign cerr;
  5043. extern ostream_withassign clog;
  5044.  
  5045. ostream&     endl(ostream& i);
  5046. ostream&     ends(ostream& i);
  5047. ostream&     flush(ostream&);
  5048. ios&         dec(ios&);
  5049. ios&         hex(ios&);
  5050. ios&         oct(ios&);
  5051.  
  5052. Note:  The following functions have declarations included in the above listing 
  5053. of iostream.h, but they are only for the internal use of the I/O Stream 
  5054. Library. Do not use them directly: 
  5055.  
  5056. o complicated_put() 
  5057. o ls_complicated() 
  5058. o do_opfx() 
  5059. o do_osfx() 
  5060.  
  5061.  
  5062. ΓòÉΓòÉΓòÉ 4.6.2. Constructors for ostream ΓòÉΓòÉΓòÉ
  5063.  
  5064. Class: ostream 
  5065.  
  5066. ostream(streambuf* sb);
  5067.  
  5068. The ostream constructor takes a single argument sb, which is a pointer to a 
  5069. streambuf object. The constructor creates an ostream object that is attached to 
  5070. the streambuf object pointed to by sb. The constructor also initializes the 
  5071. format variables to their defaults. 
  5072.  
  5073. The other declarations for the ostream constructor in iostream.h are obsolete; 
  5074. do not use them. 
  5075.  
  5076.  
  5077. ΓòÉΓòÉΓòÉ 4.6.3. Output Suffix and Prefix Functions ΓòÉΓòÉΓòÉ
  5078.  
  5079. The output operator calls the output prefix function opfx() before inserting 
  5080. characters into a stream buffer, and calls the output suffix function osfx() 
  5081. after inserting characters. The following descriptions assume the functions are 
  5082. called as part of an ostream object called os. 
  5083.  
  5084.  
  5085. ΓòÉΓòÉΓòÉ 4.6.3.1. osfx - Output Suffix Function ΓòÉΓòÉΓòÉ
  5086.  
  5087. Class: ostream 
  5088.  
  5089. void osfx();
  5090.  
  5091. osfx() is called before a formatted output function returns. osfx() flushes the 
  5092. streambuf object attached to os if ios::unitbuf is set. 
  5093.  
  5094. osfx() is called by the output operator. If you overload the output operator to 
  5095. handle your own classes, you should ensure that osfx() is called after any 
  5096. direct manipulation of a streambuf object. Binary output functions do not call 
  5097. osfx(). 
  5098.  
  5099.  
  5100. ΓòÉΓòÉΓòÉ 4.6.3.2. opfx - Output Prefix Function ΓòÉΓòÉΓòÉ
  5101.  
  5102. Class: ostream 
  5103.  
  5104. int opfx();
  5105.  
  5106. opfx() checks the error state of os. If the internal flag ios::hardfail is set, 
  5107. opfx() returns 0. Otherwise, opfx() flushes the stream buffer attached to the 
  5108. ios object pointed to by os.tie(), if one exists, and returns the value 
  5109. returned by ios::good(). ios::good() returns 0 if ios::failbit, ios::badbit, or 
  5110. ios::eofbit is set. Otherwise, ios::good() returns a nonzero value. 
  5111.  
  5112.  
  5113. ΓòÉΓòÉΓòÉ 4.6.4. Public Members of ostream for Formatted Output ΓòÉΓòÉΓòÉ
  5114.  
  5115. The ostream class lets you use the output operator << to perform formatted 
  5116. output (or insertion) to a stream buffer. Consider the following statement, 
  5117. where outs is a reference to an ostream object and x is a variable of a 
  5118. built-in type: 
  5119.  
  5120. outs << x;
  5121.  
  5122. The output operator << calls opfx() before beginning insertion. If opfx() 
  5123. returns a nonzero value, the output operator converts x into a series of 
  5124. characters and inserts these characters into the stream buffer attached to 
  5125. outs. If an error occurs, the output operator sets ios::failbit. 
  5126.  
  5127. The details of the conversion of x depend on the format state of the ostream 
  5128. object and the type of x. For numeric and string values, the output operator 
  5129. resets the width variable ios::x_width of the format state of an ostream object 
  5130. to 0, but it does not affect anything else in the format state. 
  5131.  
  5132. Note:  Only the definitions of the output operator for numeric types and for 
  5133. pointers to char (including signed char and unsigned char) reset ios::x_width. 
  5134.  
  5135. The output operator is defined for the following types: 
  5136.  
  5137. o Arrays of characters and char values, including arrays of wchar_t and wchar_t 
  5138.   values. 
  5139. o Other integral values: short, int, long 
  5140. o float, double and long double values 
  5141. o Pointers to void 
  5142.  
  5143. In addition, the output operator is defined for streambuf objects. The 
  5144. following sections describe the output operators for these types. 
  5145.  
  5146. Note:  The following descriptions assume that the input operator is called with 
  5147. the ostream object outs on the left side of the operator. 
  5148.  
  5149.  
  5150. ΓòÉΓòÉΓòÉ 4.6.4.1. Inserting Fill Characters ΓòÉΓòÉΓòÉ
  5151.  
  5152. Once the representation of a value has been determined, the output operator may 
  5153. insert fill characters. If ios::x_width is greater than 0 and the 
  5154. representation of the value to be inserted is less than ios::x_width, the 
  5155. output operator inserts enough fill characters to ensure that the 
  5156. representation occupies an entire field in the stream buffer. The position of 
  5157. the fill characters depends on the format state of outs. 
  5158.  
  5159.  
  5160. ΓòÉΓòÉΓòÉ 4.6.4.2. Output Operator for Arrays of Characters and char Values ΓòÉΓòÉΓòÉ
  5161.  
  5162. Class: ostream 
  5163.  
  5164. ostream& operator<<(const char* cp);
  5165. ostream& operator<<(const signed char* cp);
  5166. ostream& operator<<(const unsigned char* cp);
  5167. ostream& operator<<(wchar_t);
  5168. ostream& operator<<(char ch);
  5169. ostream& operator<<(signed char ch);
  5170. ostream& operator<<(unsigned char ch);
  5171. ostream& operator<<(const wchar_t *);
  5172.  
  5173. For a pointer to a char, signed char, or unsigned char value, the output 
  5174. operator inserts all the characters in the string into the stream buffer with 
  5175. the exception of the null character that terminates the string. For a pointer 
  5176. to a wchar_t, the output operator converts the wchar_t string to its equivalent 
  5177. multibyte character string, then inserts it into the stream buffer with the 
  5178. exception of the null character that terminates the string. 
  5179.  
  5180. If ios::x_width is greater than zero and the representation of the value to be 
  5181. inserted is less than ios::x_width, the output operator inserts enough fill 
  5182. characters to ensure that the representation occupies an entire field in the 
  5183. stream buffer. 
  5184.  
  5185. The output operator does not perform any conversion on char, signed char, 
  5186. unsigned char, or wchar_t values. 
  5187.  
  5188.  
  5189. ΓòÉΓòÉΓòÉ 4.6.4.3. Output Operator for Other Integral Values ΓòÉΓòÉΓòÉ
  5190.  
  5191. Class: ostream 
  5192.  
  5193. ostream& operator<<(short iv);
  5194. ostream& operator<<(unsigned short iv);
  5195. ostream& operator<<(int iv);
  5196. ostream& operator<<(unsigned int iv);
  5197. ostream& operator<<(long iv);
  5198. ostream& operator<<(unsigned long iv);
  5199.  
  5200. For the integral types (short, unsigned short, int, unsigned int, long, and 
  5201. unsigned long), the output operator converts the integral value iv according to 
  5202. the format state of outs and inserts characters into the stream buffer 
  5203. associated with outs. There is no overflow detection on conversion of integral 
  5204. types. 
  5205.  
  5206. The conversion that takes place on iv depends, in part, on the settings of the 
  5207. following format flags: 
  5208.  
  5209. o If ios::oct is set, iv is converted to a series of octal digits. If 
  5210.   ios::showbase is set, "0" is inserted into the stream buffer before the octal 
  5211.   digits. If the value being inserted is equal to 0, a single "0" is inserted, 
  5212.   not "00". 
  5213. o If ios::dec is set, iv is converted to a series of decimal digits. 
  5214. o If ios::hex is set, iv is converted to a series of hexadecimal digits. If 
  5215.   ios::showbase is set, "0x" (or "0X" if ios::uppercase is set) is inserted 
  5216.   into the stream buffer before the hexadecimal digits. 
  5217.  
  5218. If none of these format flags is set, iv is converted to a series of decimal 
  5219. digits. If iv is converted to a series of decimal digits, its sign also affects 
  5220. the conversion: 
  5221.  
  5222. o If iv is negative, a negative sign "-" is inserted before the decimal digits. 
  5223. o If iv is equal to 0, the single digit 0 is inserted. 
  5224. o If iv is positive and ios::showpos is set, a positive sign "+" is inserted 
  5225.   before the decimal digits. 
  5226.  
  5227.  
  5228. ΓòÉΓòÉΓòÉ 4.6.4.4. Output Operator for float and double Values ΓòÉΓòÉΓòÉ
  5229.  
  5230. Class: ostream 
  5231.  
  5232. ostream& operator<<(float val);
  5233. ostream& operator<<(double val);
  5234. ostream& operator<<(long double val);
  5235.  
  5236. The output operator performs a conversion operation on the value val and 
  5237. inserts it into the stream buffer attached to outs. The conversion depends on 
  5238. the values returned by the following functions: 
  5239.  
  5240. o outs.precision(): returns the number of significant digits that appear after 
  5241.   the decimal. The default value is 6. 
  5242. o outs.width(): if this returns 0, val is inserted without any fill characters. 
  5243.   If the return value is greater than the number of characters needed to 
  5244.   represent val, extra fill characters are inserted so that the total number of 
  5245.   characters inserted is equal to the return value. 
  5246.  
  5247. The conversion also depends on the values of the following format flags: 
  5248.  
  5249. o If ios::scientific is set, val is converted to scientific notation, with one 
  5250.   digit before the decimal, and the number of digits after the decimal equal to 
  5251.   the value returned by outs.precision(). The exponent begins with a lowercase 
  5252.   "e" unless ios::uppercase is set, in which case the exponent begins with an 
  5253.   uppercase "E". 
  5254. o If ios::fixed is set, val is converted to fixed notation, with the number of 
  5255.   digits after the decimal point equal to the value returned by 
  5256.   outs.precision(). If neither ios::fixed nor ios::scientific is set, the 
  5257.   conversion depends upon the value of val. See Floating-Point Formatting for 
  5258.   more details. 
  5259. o If ios::uppercase is set, the exponents of values in scientific notation 
  5260.   begin with an uppercase "E". 
  5261.  
  5262. See Format State Flags for more details on the format state. 
  5263.  
  5264.  
  5265. ΓòÉΓòÉΓòÉ 4.6.4.5. Output Operator for Pointers to void ΓòÉΓòÉΓòÉ
  5266.  
  5267. Class: ostream 
  5268.  
  5269. ostream& operator<<(void* vp);
  5270.  
  5271. The output operator converts pointers to void to integral values and then 
  5272. converts them to hexadecimal values as if ios::showbase were set. This version 
  5273. of the output operator is used to print out the values of pointers. 
  5274.  
  5275.  
  5276. ΓòÉΓòÉΓòÉ 4.6.4.6. Output Operator for streambuf Objects ΓòÉΓòÉΓòÉ
  5277.  
  5278. Class: ostream 
  5279.  
  5280. ostream& operator<<(streambuf* sb);
  5281.  
  5282. If opfx() returns a nonzero value, the output operator inserts all of the 
  5283. characters that can be taken from sb into the stream buffer attached to outs. 
  5284. Insertion stops when no more characters can be fetched from sb. No padding is 
  5285. performed. The return value is outs. 
  5286.  
  5287.  
  5288. ΓòÉΓòÉΓòÉ 4.6.5. Public Members of ostream for Unformatted Output ΓòÉΓòÉΓòÉ
  5289.  
  5290. The functions listed in this section allow you to insert characters into a 
  5291. stream buffer without regard to the type of the values that the characters 
  5292. represent. 
  5293.  
  5294. Note:  The following descriptions assume that the functions are called as part 
  5295. of an ostream object called outs. 
  5296.  
  5297. The following functions are described: 
  5298.  
  5299. o put - insert a single character 
  5300. o write - insert an array of characters 
  5301.  
  5302.  
  5303. ΓòÉΓòÉΓòÉ 4.6.5.1. put - Insert a Single Character ΓòÉΓòÉΓòÉ
  5304.  
  5305. Class: ostream 
  5306.  
  5307. ostream& put(char c);
  5308.  
  5309. put() inserts c in the stream buffer attached to outs. put() sets the error 
  5310. state of outs if the insertion fails. 
  5311.  
  5312.  
  5313. ΓòÉΓòÉΓòÉ 4.6.5.2. write - Insert an Array of Characters ΓòÉΓòÉΓòÉ
  5314.  
  5315. Class: ostream 
  5316.  
  5317. ostream& write(const char* cp, int n);
  5318. ostream& write(const signed char* cp, int n);
  5319. ostream& write(const unsigned char* cp, int n);
  5320.  
  5321. write() inserts the n characters that begin at the position pointed to by cp. 
  5322. This array of characters does not need to end with a null character. 
  5323.  
  5324.  
  5325. ΓòÉΓòÉΓòÉ 4.6.6. Public Members of ostream for Positioning ΓòÉΓòÉΓòÉ
  5326.  
  5327. Note:  The following descriptions assume that the functions are called as part 
  5328. of an ostream object called outs. 
  5329.  
  5330. The following functions are described: 
  5331.  
  5332. o seekp - reposition put pointer of ultimate consumer 
  5333. o tellp - return position of put pointer of associated stream buffer 
  5334.  
  5335.  
  5336. ΓòÉΓòÉΓòÉ 4.6.6.1. seekp - Reposition Put Pointer of Ultimate Consumer ΓòÉΓòÉΓòÉ
  5337.  
  5338. Class: ostream 
  5339.  
  5340. ostream& seekp(streampos sp);
  5341. ostream& seekp(streamoff so, ios::seek_dir dir);
  5342.  
  5343. seekp() repositions the put pointer of the ultimate consumer. seekp() with one 
  5344. argument sets the put pointer to the position sp. seekp() with two arguments 
  5345. sets the put pointer to the position specified by dir with the offset so. dir 
  5346. can have the following values: 
  5347.  
  5348. o ios::beg: the beginning of the stream 
  5349. o ios::cur: the current position of the put pointer 
  5350. o ios::end: the end of the stream. 
  5351.  
  5352. The new position of the put pointer is equal to the position specified by dir 
  5353. offset by the value of so. If you attempt to move the put pointer to a position 
  5354. that is not valid, seekp() sets ios::badbit. 
  5355.  
  5356.  
  5357. ΓòÉΓòÉΓòÉ 4.6.6.2. tellp - Return Position of Put Pointer of Associated Stream Buffer ΓòÉΓòÉΓòÉ
  5358.  
  5359. Class: ostream 
  5360.  
  5361. streampos tellp();
  5362.  
  5363. tellp() returns the current position of the put pointer of the stream buffer 
  5364. that is attached to outs. 
  5365.  
  5366.  
  5367. ΓòÉΓòÉΓòÉ 4.6.7. Other Public Members of ostream ΓòÉΓòÉΓòÉ
  5368.  
  5369. In this section, the following function is described: 
  5370.  
  5371. flush - clear associated stream buffer 
  5372.  
  5373.  
  5374. ΓòÉΓòÉΓòÉ 4.6.7.1. clear.flush - Clear Associated Stream Buffer ΓòÉΓòÉΓòÉ
  5375.  
  5376. Class: ostream 
  5377.  
  5378. ostream& flush();
  5379.  
  5380. The ultimate consumer of characters that are stored in a stream buffer may not 
  5381. necessarily consume them immediately. flush() causes any characters that are 
  5382. stored in the stream buffer attached to outs to be consumed. It calls 
  5383. outs.rdbuf()->sync() to accomplish this action. 
  5384.  
  5385.  
  5386. ΓòÉΓòÉΓòÉ 4.6.8. Built-In Manipulators for ostream ΓòÉΓòÉΓòÉ
  5387.  
  5388. ostream&     endl(ostream& i);
  5389. ostream&     ends(ostream& i);
  5390. ostream&     flush(ostream&);
  5391. ios&         dec(ios&);
  5392. ios&         hex(ios&);
  5393. ios&         oct(ios&);
  5394.  
  5395. The I/O Stream Library provides you with a set of built-in manipulators that 
  5396. can be used with the ostream class. These manipulators have a specific effect 
  5397. on an ostream object beyond extracting their own values. The built-in 
  5398. manipulators are accepted by the following versions of the output operators: 
  5399.  
  5400. ostream&     operator<<(ostream& (*f)(ostream&));
  5401. ostream&     operator<<(ios& (*f)(ios&) );
  5402.  
  5403. If outs is a reference to an ostream object, then this statement inserts a 
  5404. newline character and calls flush(). 
  5405.  
  5406. outs << endl;
  5407.  
  5408. This statement inserts a null character: 
  5409.  
  5410. outs << ends;
  5411.  
  5412. This statement flushes the stream buffer attached to outs. It is equivalent to 
  5413. flush() 
  5414.  
  5415. outs << flush;
  5416.  
  5417. This statement sets ios::dec: 
  5418.  
  5419. outs << dec;
  5420.  
  5421. This statement sets ios::hex: 
  5422.  
  5423. outs << hex;
  5424.  
  5425. This statement sets ios::oct: 
  5426.  
  5427. outs << oct;
  5428.  
  5429.  
  5430. ΓòÉΓòÉΓòÉ 4.6.9. Public Members of ostream_withassign ΓòÉΓòÉΓòÉ
  5431.  
  5432. There are two public members of ostream_withassign: 
  5433.  
  5434. o ostream_withassign constructor 
  5435. o ostream_withassign assignment operator 
  5436.  
  5437.  
  5438. ΓòÉΓòÉΓòÉ 4.6.9.1. ostream_withassign Constructor ΓòÉΓòÉΓòÉ
  5439.  
  5440. Class: ostream 
  5441.  
  5442. ostream_withassign();
  5443.  
  5444. The ostream_withassign constructor creates an ostream_withassign object. It 
  5445. does not do any initialization on the object. 
  5446.  
  5447.  
  5448. ΓòÉΓòÉΓòÉ 4.6.9.2. ostream_withassign Assignment Operator ΓòÉΓòÉΓòÉ
  5449.  
  5450. Class: ostream 
  5451.  
  5452. ostream_withassign& operator=(ostream& os);
  5453. ostream_withassign& operator=(streambuf* sb);
  5454.  
  5455. There are two versions of the ostream_withassign assignment operator. The first 
  5456. version takes a reference to an ostream object, os, as its argument. It 
  5457. associates the streambuf attached to os with the ostream_withassign object that 
  5458. is on the left side of the assignment operator. The following example shows how 
  5459. this version of the assignment operator is used: 
  5460.  
  5461. #include <iostream.h>
  5462. #include <fstream.h>
  5463. #include <stdlib.h>
  5464. void main()
  5465. {
  5466.      ofstream outfile("sheppard.dat");
  5467.      // from this point on, output sent to cout will
  5468.      // actually be sent to outfile
  5469.      cout = outfile;
  5470.      cout << "This is going to the output file." << endl;
  5471.      outfile.close();
  5472.      exit(0);
  5473. }
  5474.  
  5475. After this program is run, the file sheppard.dat contains the following: 
  5476.  
  5477. This is going to the output file.
  5478.  
  5479. The second version of the assignment operator takes a pointer to a streambuf 
  5480. object, sb, as its argument. It associates sb with the ostream_withassign 
  5481. object that is on the left side of the assignment operator. The following 
  5482. example shows how this version of the assignment operator is used: 
  5483.  
  5484. #include <iostream.h>
  5485. #include <fstream.h>
  5486. #include <stdlib.h>
  5487. void main()
  5488. {
  5489.      ofstream outfile("sheppard.dat");
  5490.      // from this point on, output sent to cout will
  5491.      // actually be sent to outfile
  5492.      cout = outfile.rdbuf();
  5493.      cout << "This is going to the output file." << endl;
  5494.      outfile.close();
  5495.      exit(0);}
  5496.  
  5497. This example produces the same results as the preceding example. 
  5498.  
  5499.  
  5500. ΓòÉΓòÉΓòÉ 4.7. iostream and iostream_withassign Classes ΓòÉΓòÉΓòÉ
  5501.  
  5502. This chapter describes the iostream class and its derived class 
  5503. iostream_withassign. iostream is derived from istream and ostream 
  5504. iostream_withassign is derived from iostream and includes an assignment 
  5505. operator. Derivation Relationships in the I/O Stream Library shows how iostream 
  5506. and iostream_withassign are derived from their base classes. 
  5507.  
  5508. The following topics are described in this chapter: 
  5509.  
  5510. o Declarations for iostream and iostream_withassign in iostream.h 
  5511. o Public members of iostream and iostream_withassign 
  5512.  
  5513.  
  5514. ΓòÉΓòÉΓòÉ 4.7.1. Declarations for iostream and iostream_withassign in iostream.h ΓòÉΓòÉΓòÉ
  5515.  
  5516. You must include the following statement in any file that uses member functions 
  5517. of the iostream or iostream_withassign classes: 
  5518.  
  5519. #include <iostream.h>
  5520.  
  5521. The following is an excerpt from the iostream.h header file that shows the 
  5522. relevant declarations for the members of iostream and iostream_withassign: 
  5523.  
  5524. class iostream : public istream, public ostream {
  5525. public:
  5526.                  iostream(streambuf*);
  5527.      virtual     ~iostream();
  5528.  
  5529. protected:
  5530.                  iostream();
  5531.      };
  5532.  
  5533.  
  5534. class iostream_withassign : public iostream {
  5535. public:
  5536.                               iostream_withassign();
  5537.      virtual                  ~iostream_withassign();
  5538.      iostream_withassign&     operator=(ios&);
  5539.      iostream_withassign&     operator=(streambuf*);
  5540. };
  5541.  
  5542.  
  5543. ΓòÉΓòÉΓòÉ 4.7.2. Public Members of iostream and iostream_withassign ΓòÉΓòÉΓòÉ
  5544.  
  5545. The following public member functions are described: 
  5546.  
  5547. o iostream constructor 
  5548. o iostream_withassign constructor 
  5549. o iostream_withassign assignment operator 
  5550.  
  5551.  
  5552. ΓòÉΓòÉΓòÉ 4.7.2.1. Constructor for iostream ΓòÉΓòÉΓòÉ
  5553.  
  5554. Class: iostream 
  5555.  
  5556. iostream(streambuf* sb);
  5557.  
  5558. The iostream constructor takes a single argument sb. The constructor creates an 
  5559. iostream object that is attached to the streambuf object that is pointed to by 
  5560. sb. The constructor also initializes the format variables to their defaults. 
  5561.  
  5562.  
  5563. ΓòÉΓòÉΓòÉ 4.7.2.2. iostream_withassign Constructor ΓòÉΓòÉΓòÉ
  5564.  
  5565. Class: iostream_withassign 
  5566.  
  5567. iostream_withassign();
  5568.  
  5569. The iostream_withassign constructor creates an iostream_withassign object. It 
  5570. does not do any initialization of this object. 
  5571.  
  5572.  
  5573. ΓòÉΓòÉΓòÉ 4.7.2.3. iostream_withassign Assignment Operator ΓòÉΓòÉΓòÉ
  5574.  
  5575. Class: iostream_withassign 
  5576.  
  5577. iostream_withassign& operator=(ios& is);
  5578. iostream_withassign& operator=(streambuf* sb);
  5579.  
  5580. There are two versions of the iostream_withassign assignment operator. The 
  5581. first version takes a reference to an ios object, is, as its argument. It 
  5582. associates the stream buffer attached to is with the iostream_withassign object 
  5583. that is on the left side of the assignment operator. 
  5584.  
  5585. The second version of the iostream_withassign assignment operator takes a 
  5586. pointer to a streambuf object, sb, as its argument. It associates this 
  5587. streambuf object with the iostream_withassign object that is on the left side 
  5588. of the assignment operator. 
  5589.  
  5590.  
  5591. ΓòÉΓòÉΓòÉ 4.8. filebuf Class ΓòÉΓòÉΓòÉ
  5592.  
  5593. This chapter describes the filebuf class. This class is derived from streambuf 
  5594. and specializes it for using files as the ultimate producer or the ultimate 
  5595. consumer. 
  5596.  
  5597. The following topics are described in this chapter: 
  5598.  
  5599. o Introduction to the filebuf class 
  5600. o Declarations for filebuf in the fstream.h header file 
  5601. o Public members of filebuf 
  5602.  
  5603.  
  5604. ΓòÉΓòÉΓòÉ 4.8.1. Introduction to the filebuf Class ΓòÉΓòÉΓòÉ
  5605.  
  5606. In a filebuf object, characters are cleared out of the put area by doing write 
  5607. operations to the file, and characters are put into the get area by doing read 
  5608. operations from that file. The filebuf class supports seek operations on files 
  5609. that allow seek operations. A filebuf object that is attached to a file 
  5610. descriptor is said to be open. 
  5611.  
  5612. The stream buffer is allocated automatically if one is not specified explicitly 
  5613. with a constructor or a call to setbuf(). You can also create an unbuffered 
  5614. filebuf object by calling the constructor or setbuf() with the appropriate 
  5615. arguments. If the filebuf object is unbuffered, a system call is made for each 
  5616. character that is read or written. 
  5617.  
  5618. The get and put pointers for a filebuf object behave as a single pointer. This 
  5619. single pointer is referred to as the get/put pointer. The file that is attached 
  5620. to the filebuf object also has a single pointer that indicates the current 
  5621. position where information is being read or written. In this chapter, this 
  5622. pointer is called the file get/put pointer. 
  5623.  
  5624.  
  5625. ΓòÉΓòÉΓòÉ 4.8.2. Declarations for filebuf in fstream.h ΓòÉΓòÉΓòÉ
  5626.  
  5627. You must include the following statement in any file that uses members of the 
  5628. filebuf class: 
  5629.  
  5630. #include <fstream.h>
  5631.  
  5632. The following is an excerpt from the fstream.h header file that shows the 
  5633. relevant declarations for the members of filebuf: 
  5634.  
  5635. class  filebuf : public streambuf {
  5636. public:
  5637.                    filebuf();
  5638.                    filebuf(int fd);
  5639.                    filebuf(int fd, char*  p, int l);
  5640.                    ~filebuf();
  5641.      int           detach();
  5642.      int           fd() ;
  5643.      int           is_open();
  5644.      filebuf*      open(const char *name, int om, int
  5645.                         prot=openprot);
  5646.      filebuf*      attach(int fd);
  5647.      filebuf*      close();
  5648.      virtual int   overflow(int=EOF);
  5649.      virtual int   underflow();
  5650.      virtual int   sync();
  5651.      virtual streampos
  5652.                seekoff(streamoff,ios::seek_dir,int);
  5653.      virtual streambuf*
  5654.                setbuf(char*  p, int len);
  5655.      static const int openprot ;
  5656. };
  5657.  
  5658. Note:  filebuf::openprot is the default protection for open(). It is equivalent 
  5659. to S_IREAD | S_IWRITE. 
  5660.  
  5661.  
  5662. ΓòÉΓòÉΓòÉ 4.8.3. Public Members of filebuf ΓòÉΓòÉΓòÉ
  5663.  
  5664. Note:  The following descriptions assume that the functions are called as part 
  5665. of a filebuf object called fb. 
  5666.  
  5667. The following member functions are described: 
  5668.  
  5669. o Constructors for filebuf 
  5670. o Destructor for filebuf 
  5671. o attach - attach filebuf object to file 
  5672. o close - disconnect filebuf 
  5673. o detach - detach filebuf object from file 
  5674. o fd - return file descriptor 
  5675. o is_open - return status of filebuf 
  5676. o open - open file and attach to filebuf 
  5677. o seekoff - move the file get/put pointer 
  5678. o seekpos - move the file get/put pointer 
  5679. o setbuf - set up stream buffer 
  5680. o sync - synchronize file and stream buffer 
  5681.  
  5682.  
  5683. ΓòÉΓòÉΓòÉ 4.8.3.1. Constructors for filebuf ΓòÉΓòÉΓòÉ
  5684.  
  5685. Class: filebuf 
  5686.  
  5687. filebuf();
  5688. filebuf(int d);
  5689. filebuf(int d, char* p, int len);
  5690.  
  5691. filebuf() constructor with no arguments constructs an initially closed filebuf 
  5692. object. 
  5693.  
  5694. filebuf() constructor with one argument constructs a filebuf object that is 
  5695. attached to file descriptor d. 
  5696.  
  5697. filebuf() constructor with three arguments constructs a filebuf object that is 
  5698. attached to file descriptor d. The object is initialized to use the stream 
  5699. buffer starting at the position pointed to by p with length equal to len. 
  5700.  
  5701.  
  5702. ΓòÉΓòÉΓòÉ 4.8.3.2. Destructor for filebuf ΓòÉΓòÉΓòÉ
  5703.  
  5704. Class: filebuf 
  5705.  
  5706. ~filebuf();
  5707.  
  5708. The filebuf destructor calls fb.close(). 
  5709.  
  5710.  
  5711. ΓòÉΓòÉΓòÉ 4.8.3.3. attach - Attach filebuf Object to File ΓòÉΓòÉΓòÉ
  5712.  
  5713. Class: filebuf 
  5714.  
  5715. filebuf* attach(int d);
  5716.  
  5717. attach() attaches fb to the file descriptor d. If fb is already open or if d is 
  5718. not open, attach() returns 0. Otherwise, attach() returns a pointer to fb. 
  5719.  
  5720.  
  5721. ΓòÉΓòÉΓòÉ 4.8.3.4. detach - Detach filebuf Object from File ΓòÉΓòÉΓòÉ
  5722.  
  5723. Class: filebuf 
  5724.  
  5725. int detach();
  5726.  
  5727. fb.detach() disconnects fb from the file without closing the file.  If fb is 
  5728. not open detach() returns -1.  Otherwise detach() flushes any output that is 
  5729. waiting in fb to be sent to the file, disconnects fb from the file, and returns 
  5730. the file descriptor. 
  5731.  
  5732.  
  5733. ΓòÉΓòÉΓòÉ 4.8.3.5. close - Disconnect filebuf ΓòÉΓòÉΓòÉ
  5734.  
  5735. Class: filebuf 
  5736.  
  5737. filebuf* close();
  5738.  
  5739. close() does the following: 
  5740.  
  5741.  1. Flushes any output that is waiting in fb to be sent to the file 
  5742.  2. Disconnects fb from the file 
  5743.  3. Closes the file that was attached to fb 
  5744.  
  5745. If an error occurs, close() returns 0. Otherwise, close() returns a pointer to 
  5746. fb. Even if an error occurs, close() performs the second and third steps listed 
  5747. above. 
  5748.  
  5749.  
  5750. ΓòÉΓòÉΓòÉ 4.8.3.6. fd - Return File Descriptor ΓòÉΓòÉΓòÉ
  5751.  
  5752. Class: filebuf 
  5753.  
  5754. int fd();
  5755.  
  5756. fd() returns the file descriptor that is attached to fb. If fb is closed, fd() 
  5757. returns EOF. 
  5758.  
  5759.  
  5760. ΓòÉΓòÉΓòÉ 4.8.3.7. is_open - Return Status of filebuf ΓòÉΓòÉΓòÉ
  5761.  
  5762. Class: filebuf 
  5763.  
  5764. int is_open();
  5765.  
  5766. is_open() returns a nonzero value if fb is attached to a file descriptor. 
  5767. Otherwise, is_open() returns zero. 
  5768.  
  5769.  
  5770. ΓòÉΓòÉΓòÉ 4.8.3.8. open - Open File and Attach to filebuf ΓòÉΓòÉΓòÉ
  5771.  
  5772. Class: filebuf 
  5773.  
  5774. filebuf* open(char* fname, int omode, int prot=openprot);
  5775.  
  5776. open() opens the file with the name fname and attaches fb to it. If fname does 
  5777. not already exist and omode does not equal ios::nocreate, open() tries to 
  5778. create it with protection mode equal to prot. The default value of prot is 
  5779. filebuf::openprot. An error occurs if fb is already open. If an error occurs, 
  5780. open() returns 0. Otherwise, open() returns a pointer to fb. 
  5781.  
  5782. The default protection mode for the filebuf class is S_IREAD | S_IWRITE. If you 
  5783. create a file with both S_IREAD and S_IWRITE set, the file is created with both 
  5784. read and write permission. If you create a file with only S_IREAD set, the file 
  5785. is created with read-only permission, and cannot be deleted later with the 
  5786. stdio.h library function remove(). S_IREAD and S_IWRITE are defined in 
  5787. sys\stat.h. 
  5788.  
  5789.  
  5790. ΓòÉΓòÉΓòÉ 4.8.3.9. seekoff - Move the  File Get/Put Pointer ΓòÉΓòÉΓòÉ
  5791.  
  5792. Class: filebuf 
  5793.  
  5794. streampos seekoff(streamoff so, seek_dir sd, int omode);
  5795.  
  5796. seekoff() moves the file get/put pointer to the position specified by sd with 
  5797. the offset so. sd can have the following values: 
  5798.  
  5799. o ios::beg: the beginning of the file 
  5800. o ios::cur: the current position of the file get/put pointer 
  5801. o ios::end: the end of the file 
  5802.  
  5803. seekoff() changes the position of the file get/put pointer to the position 
  5804. specified by the value sd + so. The offset so can be either positive or 
  5805. negative. seekoff() ignores the value of omode. 
  5806.  
  5807. If fb is attached to a file that does not support seeking, or if the value sd + 
  5808. so specifies a position before the beginning of the file, seekoff() returns EOF 
  5809. and the position of the file get/put pointer is undefined. Otherwise, seekoff() 
  5810. returns the new position of the file get/put pointer. 
  5811.  
  5812. The following example shows some of the ways that you can call seekoff(). The 
  5813. file dundas.dat is opened as an fstream object. Assume that this file contains 
  5814. at least 101 bytes of data. rdbuf() returns the filebuf object that is 
  5815. associated with this fstream object, and seekoff() is called with a variety of 
  5816. arguments. 
  5817.  
  5818.  
  5819. #include <fstream.h>
  5820.  
  5821. #define MODE ios::in
  5822.  
  5823. void main()
  5824. {
  5825.      streampos pos;
  5826.      fstream myfile("dundas.dat", MODE);
  5827.      filebuf *fb = myfile.rdbuf();
  5828.  
  5829.      // valid: so = 0, dir = ios::end
  5830.  
  5831.      streamoff offset = 0;
  5832.      pos= fb->seekoff(offset, ios::end, MODE);
  5833.  
  5834.      // valid: so = 100, dir = ios::beg
  5835.  
  5836.      offset = 100;
  5837.      pos= fb->seekoff(offset, ios::beg, MODE);
  5838.  
  5839.      // valid: so = -1, dir = ios::end
  5840.  
  5841.      offset = -1;
  5842.      pos= fb->seekoff(offset, ios::end, MODE);
  5843.  
  5844.      // valid: so = 1, dir = ios::cur
  5845.  
  5846.      offset = 1;
  5847.      pos= fb->seekoff(offset, ios::cur, MODE);
  5848.  
  5849.      // invalid: so = -1, dir = ios::beg
  5850.  
  5851.      offset = -1;
  5852.      pos= fb->seekoff(offset, ios::beg, MODE);
  5853. }
  5854.  
  5855.  
  5856. ΓòÉΓòÉΓòÉ 4.8.3.10. seekpos - Move the File Get/Put Pointer ΓòÉΓòÉΓòÉ
  5857.  
  5858. Class: filebuf 
  5859.  
  5860. The filebuf class inherits the default definition of seekpos() from the 
  5861. streambuf class. The default definition defines seekpos() as a call to 
  5862. seekoff(). Thus, the following call to seekpos(): 
  5863.  
  5864. seekpos(pos, mode);
  5865.  
  5866. is converted to a call to seekoff(): 
  5867.  
  5868. seekoff(streamoff(pos), ios::beg, mode);
  5869.  
  5870.  
  5871. ΓòÉΓòÉΓòÉ 4.8.3.11. setbuf - Set Up Stream Buffer ΓòÉΓòÉΓòÉ
  5872.  
  5873. Class: filebuf 
  5874.  
  5875. streambuf* setbuf(char* pbegin, int len);
  5876.  
  5877. setbuf() sets up a stream buffer with length in bytes equal to len beginning at 
  5878. the position pointed to by pbegin. setbuf() does the following: 
  5879.  
  5880. o If pbegin is 0 or len is nonpositive, setbuf() makes fb unbuffered. 
  5881. o If fb is open and a stream buffer has been allocated, no changes are made to 
  5882.   this stream buffer, and setbuf() returns 0. 
  5883. o If neither of these cases is true, setbuf() returns a pointer to fb. 
  5884.  
  5885.  
  5886. ΓòÉΓòÉΓòÉ 4.8.3.12. sync - Synchronize File and Stream Buffer ΓòÉΓòÉΓòÉ
  5887.  
  5888. Class: filebuf 
  5889.  
  5890. int sync();
  5891.  
  5892. sync() attempts to synchronize the get/put pointer and the file get/put 
  5893. pointer. sync() may cause bytes that are waiting in the stream buffer to be 
  5894. written to the file, or it may reposition the file get/put pointer if 
  5895. characters that have been read from the file are waiting in the stream buffer. 
  5896. If it is not possible to synchronize the get/put pointer and the file get/put 
  5897. pointer, sync() returns EOF. If it is possible to synchronize them, sync() 
  5898. returns zero. 
  5899.  
  5900.  
  5901. ΓòÉΓòÉΓòÉ 4.9. fstream, ifstream, and ofstream Classes ΓòÉΓòÉΓòÉ
  5902.  
  5903. This chapter describes fstream, ifstream, and ofstream, the classes that 
  5904. specialize iostream, istream, and ostream, respectively, for files. Another 
  5905. class, fstreambase, is declared in the fstream.h header file. fstreambase is 
  5906. the base class for fstream, ifstream, and ofstream, but it is not meant to be 
  5907. used directly. fstreambase is only documented here to show the functions that 
  5908. the other three classes inherit from it: attach(), close(), and setbuf(). 
  5909.  
  5910. The following topics are described in this chapter: 
  5911.  
  5912. o Declarations in the fstream.h header file 
  5913. o Public members of fstreambase 
  5914. o Public members of fstream 
  5915. o Public members of ifstream 
  5916. o Public members of ofstream 
  5917. o Example of using fstream, ifstream and ofstream 
  5918.  
  5919.  
  5920. ΓòÉΓòÉΓòÉ 4.9.1. Declarations for fstream, ifstream and ofstream in fstream.h ΓòÉΓòÉΓòÉ
  5921.  
  5922. You must include the following statement in any file that uses members of the 
  5923. fstream, ifstream, or ofstream classes: 
  5924.  
  5925. #include <fstream.h>
  5926.  
  5927. The following is an excerpt from the fstream.h header file that shows the 
  5928. relevant declarations for these three classes: 
  5929.  
  5930. class fstreambase : virtual public ios {
  5931. public:
  5932.                fstreambase();
  5933.                fstreambase(const char* name,
  5934.                            int mode,
  5935.                            int prot=filebuf::openprot);
  5936.                fstreambase(int fd);
  5937.                fstreambase(int fd, char*  p, int l);
  5938.                ~fstreambase();
  5939.      void      open(const char* name, int mode,
  5940.                     int prot=filebuf::openprot);
  5941.      void      attach(int fd);
  5942.      void      close();
  5943.      int       detach();
  5944.      void      setbuf(char* p, int l);
  5945.      filebuf*  rdbuf();
  5946. };
  5947.  
  5948. class ifstream : public fstreambase, public istream {
  5949. public:
  5950.                ifstream();
  5951.                ifstream(const char* name,
  5952.                         int mode=ios::in,
  5953.                         int prot=filebuf::openprot);
  5954.                ifstream(int fd);
  5955.                ifstream(int fd, char*  p, int l);
  5956.                ~ifstream();
  5957.      filebuf*  rdbuf();
  5958.      void      open(const char* name, int mode=ios::in,
  5959.                     int prot=filebuf::openprot);
  5960. };
  5961. class ofstream : public fstreambase, public ostream {
  5962. public:
  5963.                ofstream();
  5964.                ofstream(const char* name,
  5965.                         int mode=ios::out,
  5966.                         int prot=filebuf::openprot);
  5967.  
  5968.                ofstream(int fd);
  5969.                ofstream(int fd, char*  p, int l);
  5970.                ~ofstream();
  5971.      filebuf*  rdbuf();
  5972.      void      open(const char* name, int mode=ios::out,
  5973.                     int prot=filebuf::openprot);
  5974. };
  5975.  
  5976. class fstream : public fstreambase, public iostream {
  5977. public:
  5978.                fstream();
  5979.                fstream(const char* name,
  5980.                        int mode,
  5981.                        int prot=filebuf::openprot);
  5982.                fstream(int fd);
  5983.                fstream(int fd, char*  p, int l);
  5984.                ~fstream();
  5985.      filebuf*  rdbuf();
  5986.      void      open(const char* name, int mode,
  5987.                     int prot=filebuf::openprot)
  5988. ;
  5989. };
  5990.  
  5991.  
  5992. ΓòÉΓòÉΓòÉ 4.9.2. Public Members of fstreambase ΓòÉΓòÉΓòÉ
  5993.  
  5994. Notes 
  5995.  
  5996. Note: 
  5997.  
  5998.  1. The fstreambase class is an internal class that provides common functions 
  5999.     for the classes that are derived from it. Do not use the fstreambase class 
  6000.     directly. The following descriptions are provided so that you can use the 
  6001.     functions as part of fstream, ifstream, and ofstream objects. 
  6002.  
  6003.  2. The following descriptions assume that the functions are called as part of 
  6004.     an fstream, ifstream, or ofstream object called fb. 
  6005.  
  6006. The following functions are described: 
  6007.  
  6008. o attach - connect fstream object to file descriptor 
  6009. o close - close associated filebuf object 
  6010. o setbuf - set up stream buffer 
  6011.  
  6012.  
  6013. ΓòÉΓòÉΓòÉ 4.9.2.1. attach - Connect fstream Object to File Descriptor ΓòÉΓòÉΓòÉ
  6014.  
  6015. Class: fstreambase 
  6016.  
  6017. void attach(int filedesc);
  6018.  
  6019. attach() attaches fb to the file descriptor filedesc. If fb is already attached 
  6020. to a file descriptor, an error occurs and ios::failbit is set in the format 
  6021. state of fb. attach() returns zero if the file descriptor is not already open 
  6022. or if fb is already attached to a file descriptor. 
  6023.  
  6024.  
  6025. ΓòÉΓòÉΓòÉ 4.9.2.2. close - Close Associated filebuf Object ΓòÉΓòÉΓòÉ
  6026.  
  6027. Class: fstreambase 
  6028.  
  6029. void close();
  6030.  
  6031. close() closes the filebuf object, breaking the connection between fb and the 
  6032. file descriptor. close() calls fb.rdbuf()->close(). If this call fails, the 
  6033. error state of fb is not cleared. 
  6034.  
  6035.  
  6036. ΓòÉΓòÉΓòÉ 4.9.2.3. detach - Detach Associated filebuf Object ΓòÉΓòÉΓòÉ
  6037.  
  6038. Class: fstreambase 
  6039.  
  6040. int detach();
  6041.  
  6042. detach detaches the filebuf object by calling fb.rdbuf->detach(), and returns 
  6043. the value returned by fb.rdbuf->detach(). 
  6044.  
  6045.  
  6046. ΓòÉΓòÉΓòÉ 4.9.2.4. setbuf - Set Up Stream Buffer ΓòÉΓòÉΓòÉ
  6047.  
  6048. Class: fstreambase 
  6049.  
  6050. void setbuf(char* pbegin, int len);
  6051.  
  6052. setbuf() sets up a stream buffer with length in bytes equal to len beginning at 
  6053. the position pointed to by pbegin. If pbegin is equal to 0 or len is 
  6054. nonpositive, fb will be unbuffered. If fb is open, or the call to 
  6055. fb.rdbuf()->setbuf() fails, setbuf() returns 0. Otherwise, setbuf() returns a 
  6056. pointer to the stream buffer that was established. 
  6057.  
  6058.  
  6059. ΓòÉΓòÉΓòÉ 4.9.3. Public Members of fstream ΓòÉΓòÉΓòÉ
  6060.  
  6061. Note:  The following descriptions assume that the functions are called as part 
  6062. of an fstream object called fs. 
  6063.  
  6064. The following functions are described: 
  6065.  
  6066. o Constructors for fstream 
  6067. o open - open file and connect to fstream object 
  6068. o rdbuf - return pointer to filebuf object 
  6069.  
  6070.  
  6071. ΓòÉΓòÉΓòÉ 4.9.3.1. Constructors for fstream ΓòÉΓòÉΓòÉ
  6072.  
  6073. Class: fstream 
  6074.  
  6075. fstream();
  6076. fstream(int filedesc);
  6077. fstream(const char* fname, int mode, int prot=filebuf::openprot);
  6078. fstream(int filedesc, char* bufpos, int len);
  6079.  
  6080. There are four versions of the fstream constructor. The first version takes no 
  6081. arguments and constructs an unopened fstream object. The second version takes 
  6082. two arguments and constructs an fstream object that is attached to the file 
  6083. descriptor filedesc. If filedesc is not open, ios::failbit is set in the format 
  6084. state of fs. 
  6085.  
  6086. The third and fourth versions of the fstream() constructor take three 
  6087. arguments. The third version constructs an fstream object and opens the file 
  6088. fname with open mode equal to mode and protection mode equal to prot. The 
  6089. default value for the argument prot is filebuf::openprot. If the file cannot be 
  6090. opened, the error state of the constructed fstream object is set. 
  6091.  
  6092. The fourth version constructs an fstream object that is attached to the file 
  6093. descriptor filedesc. If filedesc is not open, ios::failbit is set in the format 
  6094. state of fs. This constructor also sets up an associated filebuf object with a 
  6095. stream buffer that has length len bytes and begins at the position pointed to 
  6096. by bufpos. If bufpos is equal to 0 or len is equal to 0, the associated filebuf 
  6097. object is unbuffered. 
  6098.  
  6099.  
  6100. ΓòÉΓòÉΓòÉ 4.9.3.2. open - Open File and Connect to fstream Object ΓòÉΓòÉΓòÉ
  6101.  
  6102. Class: fstream 
  6103.  
  6104. void open(const char* fname, int mode, int prot=filebuf::openprot);
  6105.  
  6106. open() opens the file with the name fname and attaches it to fs. If fname does 
  6107. not already exist, open() tries to create it with protection mode equal to 
  6108. prot, unless ios::nocreate is set. 
  6109.  
  6110. The default value for prot is filebuf::openprot. If fs is already attached to a 
  6111. file or if the call to fs.rdbuf()->open() fails, ios::failbit is set in the 
  6112. error state for fs. 
  6113.  
  6114. The members of the ios::open_mode enumeration are bits that can be ORed 
  6115. together. The value of mode is the result of such an OR operation. This result 
  6116. is an int value, and for this reason, mode has type int rather than open_mode. 
  6117.  
  6118. The elements of the open_mode enumeration have the following meanings: 
  6119.  
  6120. ios::app       open() performs a seek to the end of the file. Data that is 
  6121.                written is appended to the end of the file. This value implies 
  6122.                that the file is open for output. 
  6123. ios::ate       open() performs a seek to the end of the file. Setting ios::ate 
  6124.                does not open the file for input or output. If you set ios::ate, 
  6125.                you should explicitly set ios::in, ios::out, or both. 
  6126. ios::bin       See ios::binary below. 
  6127. ios::binary    The file is opened in binary mode. In the default (text) mode, 
  6128.                carriage returns are discarded on input, as is an end-of-file 
  6129.                (0x1a) character if it is the last character in the file. This 
  6130.                means that a carriage return without an accompanying line feed 
  6131.                causes the characters on either side of the carriage return to 
  6132.                become adjacent. On output, a line feed is expanded to a 
  6133.                carriage return and line feed. If you specify ios::binary, 
  6134.                carriage returns and terminating end-of-file characters are not 
  6135.                removed on input, and a line feed is not expanded to a carriage 
  6136.                return and line feed on output. ios::binary and ios::bin provide 
  6137.                identical functionality. 
  6138. ios::in        The file is opened for input. If the file that is being opened 
  6139.                for input does not exist, the open operation will fail. 
  6140.                ios::noreplace is ignored if ios::in is set. 
  6141. ios::out       The file is opened for output. 
  6142. ios::trunc     If the file already exists, its contents will be discarded. If 
  6143.                you specify ios::out and neither ios::ate nor ios::app, you are 
  6144.                implicitly specifying ios::trunc. If you set ios::trunc, you 
  6145.                should explicitly set ios::in, ios::out, or both. 
  6146. ios::nocreate  If the file does not exist, the call to open() fails. 
  6147. ios::noreplace If the file already exists and ios::out is set, the call to 
  6148.                open() fails. If ios::out is not set, ios::noreplace is ignored. 
  6149.  
  6150.  
  6151. ΓòÉΓòÉΓòÉ 4.9.3.3. rdbuf - Return Pointer to filebuf Object ΓòÉΓòÉΓòÉ
  6152.  
  6153. Class: fstream 
  6154.  
  6155. filebuf* rdbuf();
  6156.  
  6157. rdbuf() returns a pointer to the filebuf object that is attached to fs. 
  6158.  
  6159.  
  6160. ΓòÉΓòÉΓòÉ 4.9.4. Public Members of ifstream ΓòÉΓòÉΓòÉ
  6161.  
  6162. Note:  The following descriptions assume that the functions are called as part 
  6163. of an ifstream object called ifs. 
  6164.  
  6165. o Constructors for ifstream 
  6166. o open - open file and connect to ifstream object 
  6167. o rdbuf - return pointer to filebuf object 
  6168.  
  6169.  
  6170. ΓòÉΓòÉΓòÉ 4.9.4.1. Constructors for ifstream ΓòÉΓòÉΓòÉ
  6171.  
  6172. Class: ifstream 
  6173.  
  6174. ifstream();
  6175. ifstream(int filedesc);
  6176. ifstream(const char* fname,
  6177.          int mode=ios::in,
  6178.          int prot=filebuf::openprot);
  6179. ifstream(int filedesc, char* bufpos, int len);
  6180.  
  6181. There are four versions of the ifstream constructor. The first version takes no 
  6182. arguments and constructs an unopened ifstream object. The second version takes 
  6183. one argument and constructs an ifstream object that is attached to the file 
  6184. descriptor filedesc. If filedesc is not open, ios::failbit is set in the format 
  6185. state of ifs. 
  6186.  
  6187. The third and fourth versions of the ifstream() constructor take three 
  6188. arguments. The third version constructs an ifstream object and opens the file 
  6189. fname with open mode equal to mode and protection mode equal to prot. The 
  6190. default value for mode is ios::in, and the default value for prot is 
  6191. filebuf::openprot. If the file cannot be opened, the error state of the 
  6192. constructed ifstream object is set. 
  6193.  
  6194. The fourth version constructs an ifstream object that is attached to the file 
  6195. descriptor filedesc. If filedesc is not open, ios::failbit is set in the format 
  6196. state of ifs. This constructor also sets up an associated filebuf object with a 
  6197. stream buffer that has length len bytes and begins at the position pointed to 
  6198. by bufpos. If bufpos is equal to 0 or len is equal to 0, the associated filebuf 
  6199. object is unbuffered. 
  6200.  
  6201.  
  6202. ΓòÉΓòÉΓòÉ 4.9.4.2. open - Open File and Connect to ifstream Object ΓòÉΓòÉΓòÉ
  6203.  
  6204. Class: ifstream 
  6205.  
  6206. void open(const char* fname,
  6207.           int mode=ios::in,
  6208.           int prot=filebuf::openprot);
  6209.  
  6210. open() opens the file with the name fname and attaches it to ifs. If fname does 
  6211. not already exist, open() tries to create it with protection mode equal to 
  6212. prot, unless ios::nocreate is set in mode. 
  6213.  
  6214. The default value for mode is ios::in. The default value for prot is 
  6215. filebuf::openprot. If ifs is already attached to a file, or if the call to 
  6216. ifs.rdbuf()->open() fails, ios::failbit is set in the error status for ifs. 
  6217.  
  6218. The members of the ios::open_mode enumeration are bits that can be ORed 
  6219. together. The value of mode is the result of such an OR operation. This result 
  6220. is an int value, and for this reason mode has type int rather than type 
  6221. open_mode. See open - Open File and Connect to fstream Object for a list of the 
  6222. possible values for mode. 
  6223.  
  6224.  
  6225. ΓòÉΓòÉΓòÉ 4.9.4.3. rdbuf - Return Pointer to filebuf Object ΓòÉΓòÉΓòÉ
  6226.  
  6227. Class: ifstream 
  6228.  
  6229. filebuf* rdbuf();
  6230.  
  6231. rdbuf() returns a pointer to the filebuf object that is attached to ifs. 
  6232.  
  6233.  
  6234. ΓòÉΓòÉΓòÉ 4.9.5. Public Members of ofstream ΓòÉΓòÉΓòÉ
  6235.  
  6236. Note:  The following descriptions assume that the functions are called as part 
  6237. of an ofstream object called ofs. 
  6238.  
  6239. o Constructors for ofstream 
  6240. o open - open file and connect to ofstream object 
  6241. o rdbuf - return pointer to filebuf object 
  6242.  
  6243.  
  6244. ΓòÉΓòÉΓòÉ 4.9.5.1. Constructors for ofstream ΓòÉΓòÉΓòÉ
  6245.  
  6246. Class: ofstream 
  6247.  
  6248. ofstream();
  6249. ofstream(int filedesc);
  6250. ofstream(const char* fname,
  6251.          int mode=ios::out,
  6252.          int prot=filebuf::openprot);
  6253. ofstream(int filedesc, char* bufpos, int len);
  6254.  
  6255. There are four versions of the ofstream constructor. The first version takes no 
  6256. arguments and constructs an unopened ofstream object. The second version takes 
  6257. one argument and constructs an ofstream object that is attached to the file 
  6258. descriptor filedesc. If filedesc is not open, ios::failbit is set in the format 
  6259. state of ofs. 
  6260.  
  6261. The third and fourth versions of the ofstream() constructor take three 
  6262. arguments. The third version constructs an ofstream object and opens the file 
  6263. fname with open mode equal to mode and protection mode equal to prot. The 
  6264. default value for mode is ios::out, and the default value for prot is 
  6265. filebuf::openprot. If the file cannot be opened, the error state of the 
  6266. constructed ofstream object is set. 
  6267.  
  6268. The fourth version constructs an ofstream object that is attached to the file 
  6269. descriptor filedesc. If filedesc is not open, ios::failbit is set in the format 
  6270. state of ofs. This constructor also sets up an associated filebuf object with a 
  6271. stream buffer that has length len bytes and begins at the position pointed to 
  6272. by bufpos. If p is equal to 0 or len is equal to 0, the associated filebuf 
  6273. object is unbuffered. 
  6274.  
  6275.  
  6276. ΓòÉΓòÉΓòÉ 4.9.5.2. open - Open File and Connect to ofstream Object ΓòÉΓòÉΓòÉ
  6277.  
  6278. Class: ofstream 
  6279.  
  6280. void open(const char* fname, int mode, int prot=filebuf::openprot);
  6281.  
  6282. open() opens the file with the name fname and attaches it to ofs. If fname does 
  6283. not already exist, open() tries to create it with protection mode equal to 
  6284. prot, unless ios::nocreate is set. 
  6285.  
  6286. The default value for mode is ios::out. The default value for the argument prot 
  6287. is filebuf::openprot. If ofs is already attached to a file, or if the call to 
  6288. the function ofs.rdbuf()->open() fails, ios::failbit is set in the error state 
  6289. for ofs. 
  6290.  
  6291. The members of the ios::open_mode enumeration are bits that can be ORed 
  6292. together. The value of mode is the result of such an OR operation. This result 
  6293. is an int value, and for this reason, mode has type int rather than open_mode. 
  6294. See open - Open File and Connect to fstream Object for a list of the possible 
  6295. values for mode. 
  6296.  
  6297.  
  6298. ΓòÉΓòÉΓòÉ 4.9.5.3. rdbuf - Return Pointer to filebuf Object ΓòÉΓòÉΓòÉ
  6299.  
  6300. Class: ofstream 
  6301.  
  6302. filebuf* rdbuf();
  6303.  
  6304. rdbuf() returns a pointer to the filebuf object that is attached to ofs. 
  6305.  
  6306.  
  6307. ΓòÉΓòÉΓòÉ 4.9.6. Example of Using fstream, ifstream, and ofstream ΓòÉΓòÉΓòÉ
  6308.  
  6309. In the following example, the ifstream object source is associated with the 
  6310. input file source.dat, and the ofstream object dest is associated with the 
  6311. output file dest.dat. After both files have been opened, the contents of 
  6312. source.dat are written to dest.dat through the associated ifstream and ofstream 
  6313. objects. 
  6314.  
  6315.  
  6316. #include <fstream.h>
  6317. #include <stdlib.h>
  6318.  
  6319. void main() {
  6320.      ifstream source("source.dat");
  6321.      ofstream dest("dest.dat");
  6322.      //
  6323.      // Check that the source file was opened correctly
  6324.      if (!source)
  6325.           {
  6326.           cerr << "Cannot open file 'source.dat'" << endl;
  6327.           exit(1);
  6328.           }
  6329.      //
  6330.      // Check that the destination file was opened correctly
  6331.      if (!dest)
  6332.           {
  6333.           cerr << "Cannot open file 'dest.dat'" << endl;
  6334.           exit(1);
  6335.           }
  6336.      // Read source file and write it to the dest file.
  6337.      char c = 0;
  6338.      while (dest && source.get(c))
  6339.           dest.put(c);
  6340.      if (!source.eof())
  6341.           {
  6342.           cerr << "Unexpected result" << endl;
  6343.           exit(1);
  6344.           }
  6345.      //
  6346.      // Close files.
  6347.      source.close();
  6348.      dest.close();
  6349.      exit(0);
  6350. }
  6351.  
  6352.  
  6353. ΓòÉΓòÉΓòÉ 4.10. strstreambuf Class ΓòÉΓòÉΓòÉ
  6354.  
  6355. This chapter describes the strstreambuf class, the class that specializes 
  6356. streambuf to use an array of bytes in memory as the ultimate producer or 
  6357. ultimate consumer. 
  6358.  
  6359. The following topics are described in this chapter: 
  6360.  
  6361. o Declarations for strstreambuf in strstrea.h header file 
  6362. o Public members of strstreambuf 
  6363.  
  6364.  
  6365. ΓòÉΓòÉΓòÉ 4.10.1. Declarations for strstreambuf in strstrea.h ΓòÉΓòÉΓòÉ
  6366.  
  6367. You must include the following statement in any file that uses the strstreambuf 
  6368. class: 
  6369.  
  6370. #include <strstrea.h>
  6371.  
  6372. The following is an excerpt from the strstrea.h header file that shows the 
  6373. relevant declarations for the public, nonvirtual members of strstreambuf: 
  6374.  
  6375. class strstreambuf : public streambuf
  6376. {
  6377.  
  6378. public:
  6379.                strstreambuf();
  6380.                strstreambuf(int)
  6381.                strstreambuf(void* (*a)(long),
  6382.                             void (*f)(void*));
  6383.                strstreambuf(char* b, int size,
  6384.                             char* pstart = 0 );
  6385.                strstreambuf(signed char* b, int size,
  6386.                             signed char* pstart = 0 );
  6387.                strstreambuf(unsigned char* b, int size,
  6388.                             unsigned char* pstart = 0 );
  6389.                ~strstreambuf();
  6390.      int       pcount();
  6391.      void      freeze(int n=1);
  6392.      char*     str();
  6393.      virtual int     doallocate();
  6394.      virtual int     overflow(int);
  6395.      virtual int     underflow();
  6396.      virtual streampos seekoff(streamoff, ios::seek_dir, int);
  6397.      virtual streambuf* setbuf(char* p, int n); };
  6398.  
  6399. Note:  Do not use pcount(). It is declared as a public member function of the 
  6400. strstreambuf class, but pcount() is meant to be used internally by the I/O 
  6401. Stream Library. 
  6402.  
  6403.  
  6404. ΓòÉΓòÉΓòÉ 4.10.2. Public Members of strstreambuf ΓòÉΓòÉΓòÉ
  6405.  
  6406. The following functions are described: 
  6407.  
  6408. o Constructors for strstreambuf 
  6409. o Destructor for strstreambuf 
  6410. o doallocate - allocate space for a stream buffer 
  6411. o freeze - control deletion of stream buffer 
  6412. o overflow - clear put area 
  6413. o str - return pointer to beginning of stream buffer and freeze strstreambuf 
  6414.   Object 
  6415. o seekoff - reposition get or put pointer in memory array 
  6416. o setbuf - set minimum stream buffer size 
  6417. o underflow - fill get area. 
  6418.  
  6419.  
  6420. ΓòÉΓòÉΓòÉ 4.10.2.1. Constructors for strstreambuf ΓòÉΓòÉΓòÉ
  6421.  
  6422. Class: strstreambuf 
  6423.  
  6424. strstreambuf();
  6425. strstreambuf(int bufsize);
  6426. strstreambuf(void* (*alloc) (long), void(*free)(void*));
  6427. strstreambuf(char* sp, int len, char* tp);
  6428. strstreambuf(signed char* sp, int len, signed char* tp);
  6429. strstreambuf(unsigned char* sp, int len, unsigned char* tp);
  6430.  
  6431. The first version of the strstreambuf constructor takes no arguments and 
  6432. constructs an empty strstreambuf object in dynamic mode. Space will be 
  6433. allocated automatically to accommodate the characters that are put into the 
  6434. strstreambuf object. This space will be allocated using the operator new and 
  6435. deallocated using the operator delete. The characters that are already stored 
  6436. by the strstreambuf object may have to be copied when new space is allocated. 
  6437. If you know you are going to insert many characters into an strstreambuf 
  6438. object, you can give the I/O Stream Library an estimate of the size of the 
  6439. object before you create it by calling strstreambuf::setbuf(). 
  6440.  
  6441. The second version of the strstreambuf constructor takes one argument and 
  6442. constructs an empty strstreambuf object in dynamic mode. The initial size of 
  6443. the stream buffer will be at least bufsize bytes. 
  6444.  
  6445. The third version of the strstreambuf constructor takes two arguments and 
  6446. creates an empty strstreambuf object in dynamic mode. alloc is a pointer to the 
  6447. function that is used to allocate space. alloc is passed a long value that 
  6448. equals the number of bytes that it is supposed to allocate. If the value of 
  6449. alloc is 0, the operator new is used to allocate space. free is a pointer to 
  6450. the function that is used to free space. free is passed an argument that is a 
  6451. pointer to the array of bytes that alloc allocated. If free has a value of 0, 
  6452. the operator delete is used to free space. 
  6453.  
  6454. The fourth, fifth, and sixth versions of the strstreambuf constructor take 
  6455. three arguments and construct a strstreambuf object with a stream buffer that 
  6456. begins at the position pointed to by sp. The nature of the stream buffer 
  6457. depends on the value of len: 
  6458.  
  6459. o If len is positive, the len bytes following the position pointed to by sp 
  6460.   make up the stream buffer. 
  6461.  
  6462. o If len equals 0, sp points to the beginning of a null-terminated string, and 
  6463.   the bytes of that string, excluding the terminating null character, will make 
  6464.   up the stream buffer. 
  6465.  
  6466. o If len is negative, the stream buffer has an indefinite length. The get 
  6467.   pointer of the stream buffer is initialized to sp, and the put pointer is 
  6468.   initialized to tp. 
  6469.  
  6470. Regardless of the value of len, if the value of tp is 0, the get area will 
  6471. include the entire stream buffer, and insertions will cause errors. 
  6472.  
  6473.  
  6474. ΓòÉΓòÉΓòÉ 4.10.2.2. Destructor for strstreambuf ΓòÉΓòÉΓòÉ
  6475.  
  6476. Class: strstreambuf 
  6477.  
  6478. ~strstreambuf();
  6479.  
  6480. If freeze() has not been called for the strstreambuf object, and a stream 
  6481. buffer is associated with the strstreambuf object, the strstreambuf destructor 
  6482. frees the space allocated by the strstreambuf constructor. The effect of the 
  6483. destructor depends on the constructor used to create the strstreambuf object: 
  6484.  
  6485. o If you created the strstreambuf object using the constructor that takes two 
  6486.   pointers to functions as arguments (see Constructors for strstreambuf for 
  6487.   more details), the destructor frees the space allocated by the destructor by 
  6488.   calling the function pointed to by the second argument to the constructor. 
  6489.  
  6490. o If you created the strstreambuf object using any of the other constructors, 
  6491.   the destructor calls the delete operator to free the space allocated by the 
  6492.   constructor. 
  6493.  
  6494.  
  6495. ΓòÉΓòÉΓòÉ 4.10.2.3. doallocate - Allocate Space for a Stream Buffer ΓòÉΓòÉΓòÉ
  6496.  
  6497. Class: strstreambuf 
  6498.  
  6499. virtual int doallocate();
  6500.  
  6501. doallocate() attempts to allocate space for a stream buffer. If you created the 
  6502. strstreambuf object using the constructor that takes two pointers to functions 
  6503. as arguments (see Constructors for strstreambuf for more details), doallocate() 
  6504. allocates space for the stream buffer by calling the function pointed to by the 
  6505. first argument to the constructor. Otherwise, doallocate() calls the operator 
  6506. new to allocate space for the stream buffer. 
  6507.  
  6508.  
  6509. ΓòÉΓòÉΓòÉ 4.10.2.4. freeze - Control Deletion of Stream Buffer ΓòÉΓòÉΓòÉ
  6510.  
  6511. Class: strstreambuf 
  6512.  
  6513. void freeze(int n=1);
  6514.  
  6515. freeze() controls whether the array that makes up a stream buffer can be 
  6516. deleted automatically. If n has a nonzero value, the array is not deleted 
  6517. automatically. If n equals 0, the array is deleted automatically when more 
  6518. space is needed or when the strstreambuf object is deleted. If you call 
  6519. freeze() with a nonzero argument for a strstreambuf object that was allocated 
  6520. in dynamic mode, any attempts to put characters in the stream buffer may result 
  6521. in errors. Therefore, you should avoid insertions to such stream buffers 
  6522. because the results are unpredictable. However, if you have a "frozen" stream 
  6523. buffer and you call freeze() with an argument equal to 0, you can put 
  6524. characters in the stream buffer again. 
  6525.  
  6526. Only space that is acquired through dynamic allocation is ever freed. 
  6527.  
  6528.  
  6529. ΓòÉΓòÉΓòÉ 4.10.2.5. overflow - Clear Put Area ΓòÉΓòÉΓòÉ
  6530.  
  6531. Class: strstreambuf 
  6532.  
  6533. virtual int overflow(int c);
  6534.  
  6535. overflow() causes the ultimate consumer to consume the characters in the put 
  6536. area and calls setp() to establish a new put area. The argument c is stored in 
  6537. the new put area if c is not equal to EOF. 
  6538.  
  6539.  
  6540. ΓòÉΓòÉΓòÉ 4.10.2.6. str - Return Pointer to Beginning of Stream Buffer and Freeze strstreambuf Object ΓòÉΓòÉΓòÉ
  6541.  
  6542. Class: strstreambuf 
  6543.  
  6544. char* str();
  6545.  
  6546. str() returns a pointer to the first character in the stream buffer and calls 
  6547. freeze() with a nonzero argument. Any attempts to put characters in the stream 
  6548. buffer may result in errors. If the strstreambuf object was created with an 
  6549. explicit array (that is, the strstreambuf constructor with three arguments was 
  6550. used), str() returns a pointer to that array. If the strstreambuf object was 
  6551. created in dynamic mode and nothing is stored in the array, str() may return 0. 
  6552.  
  6553.  
  6554. ΓòÉΓòÉΓòÉ 4.10.2.7. seekoff - Reposition Get or Put Pointer in Memory Array ΓòÉΓòÉΓòÉ
  6555.  
  6556. Class: strstreambuf 
  6557.  
  6558. virtual streampos seekoff(
  6559.       streamoff so, ios::seek_dir dir, int mode);
  6560.  
  6561. seekoff() repositions the get or put pointer in the array of bytes in memory 
  6562. that serves as the ultimate producer or the ultimate consumer. For a text mode 
  6563. file, seekoff() returns the actual physical file position, because carriage 
  6564. return characters and end-of-file characters are discarded on input. Thus, 
  6565. there may not be a one-to-one correspondence between the characters in a stream 
  6566. and those in its external representation. For further details, see "Low-Level 
  6567. I/O" in the IBM C/C++ Tools C Library Reference. 
  6568.  
  6569. If you constructed the strstreambuf in dynamic mode (see Constructors for 
  6570. strstreambuf), the results of seekoff() are unpredictable. Therefore, do not 
  6571. use seekoff() with an strstreambuf object that you created in dynamic mode. 
  6572.  
  6573. If you did not construct the strstreambuf object in dynamic mode, seekoff() 
  6574. attempts to reposition the get pointer or the put pointer, depending on the 
  6575. value of mode. If ios::in is set in mode, seekoff() repositions the get 
  6576. pointer. If ios::out is set in mode, seekoff() repositions the put pointer. If 
  6577. both ios::in and ios::out are set, seekoff() repositions both pointers. 
  6578.  
  6579. seekoff() attempts to reposition the affected pointer to the value of dir + so. 
  6580. dir can have the following values: 
  6581.  
  6582. o ios::beg: the beginning of the array in memory 
  6583. o ios::cur: the current position in the array in memory 
  6584. o ios::end: the end of the array in memory. 
  6585.  
  6586. If the value of dir + so is equal to or greater than the end of the array, the 
  6587. value is not valid and seekoff() returns EOF. Otherwise, seekoff() sets the 
  6588. affected pointer to this value and returns this value. 
  6589.  
  6590. The following example shows some of the settings of dir and so that can result 
  6591. in a value that is not valid: 
  6592.  
  6593.  
  6594. #include <string.h>
  6595. #include <strstrea.h>
  6596.  
  6597. char *DATA = "Hello";
  6598.  
  6599. #define MODE ios::in
  6600.  
  6601. void main()
  6602. {
  6603.      streampos pos;
  6604.      strstream temp (DATA, (int)strlen(DATA)+1, ios::in);
  6605.      strstreambuf *ss = temp.rdbuf();
  6606.      //
  6607.      // invalid: so = 0, dir = ios::end
  6608.      streamoff offset = 0;
  6609.      pos= ss->seekoff(offset, ios::end, MODE);
  6610.      if (pos == streampos(EOF)) {
  6611.           cout << "seekoff returned EOF" << endl;
  6612.           cout << "offset equals " << offset << endl;
  6613.           cout << "position equals " << ios::end << endl;
  6614.           }
  6615.      //
  6616.      // invalid: so = strlen(DATA)+1, dir = ios::beg
  6617.      offset = strlen(DATA)+1;
  6618.      pos= ss->seekoff(offset, ios::beg, MODE);
  6619.      if (pos == streampos(EOF)) {
  6620.           cout << "seekoff returned EOF" << endl;
  6621.           cout << "offset equals " << offset << endl;
  6622.           cout << "position equals " << ios::beg << endl;
  6623.           }
  6624.      //
  6625.      // valid: so = -1, dir = ios::end
  6626.      offset = -1;
  6627.      pos= ss->seekoff(offset, ios::end, MODE);
  6628.      if (pos == streampos(EOF)) {
  6629.           cout << "seekoff returned EOF" << endl;
  6630.           cout << "offset equals " << offset << endl;
  6631.           cout << "position equals " << ios::end << endl;
  6632.           }
  6633.      //
  6634.      // invalid: so = 1, dir = ios::cur
  6635.      offset = 1;
  6636.      pos= ss->seekoff(offset, ios::cur, MODE);
  6637.      if (pos == streampos(EOF)) {
  6638.           cout << "seekoff returned EOF" << endl;
  6639.           cout << "offset equals " << offset << endl;
  6640.           cout << "position equals " << ios::cur << endl;
  6641.           }
  6642. }
  6643.  
  6644. The attempts to position the get pointer to the end of the array or beyond the 
  6645. end of the array are not valid. seekoff() returns EOF in these cases. The 
  6646. following is the expected output of this example: 
  6647.  
  6648. seekoff returned EOF
  6649. offset equals 0
  6650. position equals 2
  6651.  
  6652. seekoff returned EOF
  6653. offset equals 6
  6654. position equals 0
  6655.  
  6656. seekoff returned EOF
  6657. offset equals 1
  6658. position equals 1
  6659.  
  6660.  
  6661. ΓòÉΓòÉΓòÉ 4.10.2.8. setbuf - Set Minimum Stream Buffer Size ΓòÉΓòÉΓòÉ
  6662.  
  6663. Class: strstreambuf 
  6664.  
  6665. virtual streambuf* setbuf(0, int bufsize);
  6666.  
  6667. setbuf() records bufsize. The next time that the strstreambuf object 
  6668. dynamically allocates a stream buffer, the stream buffer is at least bufsize 
  6669. bytes long. 
  6670.  
  6671. Note:  If you call setbuf() for an strstreambuf object, you must call it with 
  6672. the first argument equal to 0. 
  6673.  
  6674.  
  6675. ΓòÉΓòÉΓòÉ 4.10.2.9. underflow - Fill Get Area ΓòÉΓòÉΓòÉ
  6676.  
  6677. Class: strstreambuf 
  6678.  
  6679. virtual int underflow();
  6680.  
  6681. If the get area is not empty, underflow() returns the first character in the 
  6682. get area. If the get area is empty, underflow() creates a new get area that is 
  6683. not empty and returns the first character. If no more characters are available 
  6684. in the ultimate producer, underflow() returns EOF and leaves the get area 
  6685. empty. 
  6686.  
  6687.  
  6688. ΓòÉΓòÉΓòÉ 4.11. strstream, istrstream, and ostrstream Classes ΓòÉΓòÉΓòÉ
  6689.  
  6690. This chapter describes istrstream, ostrsteam, and strstream, the classes that 
  6691. specialize istream, ostream, and iostream (respectively) to use strstreambuf 
  6692. objects for stream buffers. These classes are called the array stream buffer 
  6693. classes because their stream buffers are arrays of bytes in memory. You can use 
  6694. these classes to perform input and output with strings in memory. 
  6695.  
  6696. This chapter also describes strstreambase, the class from which the array 
  6697. stream buffer classes are derived. 
  6698.  
  6699. The following topics are described in this chapter: 
  6700.  
  6701. o Declarations for array stream buffer classes in strstrea.h 
  6702. o Public members of strstreambase 
  6703. o Public members of strstream 
  6704. o Public members of istrstream 
  6705. o Public members of ostrstream 
  6706.  
  6707.  
  6708. ΓòÉΓòÉΓòÉ 4.11.1. Declarations for strstream, istrstream and ostrstream in strstrea.h ΓòÉΓòÉΓòÉ
  6709.  
  6710. You must include the following statement in any file that uses istrstream, 
  6711. ostrsteam, or strstream: 
  6712.  
  6713. #include <strstrea.h>
  6714.  
  6715. The following is an excerpt from the strstrea.h header file that shows the 
  6716. relevant declarations for the members of the istrstream, ostrsteam, and 
  6717. strstream classes: 
  6718.  
  6719. class strstreambase : public virtual ios {
  6720. public:
  6721.      strstreambuf*     rdbuf();
  6722. };
  6723. class istrstream : public strstreambase, public istream
  6724. {
  6725. public:
  6726.                istrstream(char* str);
  6727.                istrstream(signed char* str);
  6728.                istrstream(unsigned char* str);
  6729.                istrstream(char* str, int size );
  6730.                istrstream(signed char* str, int size);
  6731.                istrstream(unsigned char* str, int size);
  6732.                istrstream(const char* str);
  6733.                istrstream(const signed char* str);
  6734.                istrstream(const unsigned char* str);
  6735.                istrstream(const char* str, int size );
  6736.                istrstream(const signed char* str, int size);
  6737.                istrstream(const unsigned char* str, int size);
  6738.                ~istrstream();
  6739. };
  6740.  
  6741. class ostrstream : public strstreambase, public ostream {
  6742. public:
  6743.                ostrstream(char* str, int size,
  6744.                     int=ios::out);
  6745.                ostrstream(signed char* str,
  6746.                     int size, int=ios::out);
  6747.                ostrstream(unsigned char* str,
  6748.                     int size, int=ios::out);
  6749.                ostrstream();
  6750.                ~ostrstream();
  6751.      char*     str();
  6752.      int       pcount();
  6753. };
  6754.  
  6755. class strstream : public strstreambase, public iostream
  6756. {
  6757. public:
  6758.                strstream();
  6759.                strstream(char* str, int size, int mode);
  6760.                strstream(signed char* str, int size,
  6761.                     int mode);
  6762.                strstream(unsigned char* str, int size,
  6763.                     int mode);
  6764.                ~strstream();
  6765.      char*     str();
  6766. };
  6767.  
  6768.  
  6769. ΓòÉΓòÉΓòÉ 4.11.2. Public Members of strstreambase ΓòÉΓòÉΓòÉ
  6770.  
  6771. Note:  The strstreambase class is an internal class that provides common 
  6772. functions for the classes that are derived from it. Do not use the 
  6773. strstreambase class directly. The following description is provided so that you 
  6774. can use the function as part of istrstream, ostrsteam, and strstream objects. 
  6775.  
  6776.  
  6777. ΓòÉΓòÉΓòÉ 4.11.2.1. rdbuf - Return Pointer to Associated Stream Buffer ΓòÉΓòÉΓòÉ
  6778.  
  6779. Class: ostrsteam 
  6780.  
  6781. strstreambuf* rdbuf();
  6782.  
  6783. rdbuf() returns a pointer to the stream buffer that the strstreambase object is 
  6784. attached to. 
  6785.  
  6786.  
  6787. ΓòÉΓòÉΓòÉ 4.11.3. Public Members of strstream ΓòÉΓòÉΓòÉ
  6788.  
  6789. The following functions are described: 
  6790.  
  6791. o Constructor for strstream 
  6792. o Destructor for strstream 
  6793. o str - return pointer to stream buffer array 
  6794.  
  6795. An example of using the strstream class is also provided. 
  6796.  
  6797.  
  6798. ΓòÉΓòÉΓòÉ 4.11.3.1. Constructor for strstream ΓòÉΓòÉΓòÉ
  6799.  
  6800. Class: strstream 
  6801.  
  6802. strstream();
  6803. strstream(char* cp, int len, int mode);
  6804. strstream(signed char* cp, int len, int mode);
  6805. strstream(unsigned char* cp, int len, int mode);
  6806.  
  6807. There are two versions of the strstream constructor. The version that takes no 
  6808. arguments specifies that space is allocated dynamically for the stream buffer 
  6809. that is attached to the strstream object. 
  6810.  
  6811. The version of the strstream constructor that takes three arguments specifies 
  6812. that characters should be extracted and inserted into the array of bytes that 
  6813. starts at the position pointed to by cp with a length of len bytes. If ios::ate 
  6814. or ios::app is set in mode, cp points to a null-terminated string and 
  6815. insertions begin at the null character. Otherwise, insertions begin at the 
  6816. position pointed to by cp. You can use the istream::seekg() function to 
  6817. reposition the get pointer anywhere in this array. 
  6818.  
  6819.  
  6820. ΓòÉΓòÉΓòÉ 4.11.3.2. Destructor for strstream ΓòÉΓòÉΓòÉ
  6821.  
  6822. Class: ostrsteam 
  6823.  
  6824. ~strstream();
  6825.  
  6826. The strstream destructor frees the space allocated by the strstream 
  6827. constructor. 
  6828.  
  6829.  
  6830. ΓòÉΓòÉΓòÉ 4.11.3.3. str - Return Pointer to Stream Buffer Array ΓòÉΓòÉΓòÉ
  6831.  
  6832. Class: ostrsteam 
  6833.  
  6834. char* str();
  6835.  
  6836. str() returns a pointer to the stream buffer attached to the strstream and 
  6837. calls freeze() with a nonzero value to prevent the stream buffer from being 
  6838. deleted. If the stream buffer was constructed with an explicit array, the value 
  6839. returned is a pointer to that array. If the stream buffer was constructed in 
  6840. dynamic mode, cp points to the dynamically allocated area. 
  6841.  
  6842. Until you call str(), deleting the dynamically allocated stream buffer is the 
  6843. responsibility of the strstream object. After str() has been called, the 
  6844. calling application has responsibility for the dynamically allocated stream 
  6845. buffer. 
  6846.  
  6847. Note:  If your application calls str() without calling freeze() with a nonzero 
  6848. argument (to unfreeze the strstream), or without explicitly deleting the array 
  6849. of characters returned by the call to str(), the array of characters will not 
  6850. be deallocated by the strstream when it gets destroyed. This situation is a 
  6851. potential source of a memory leak. 
  6852.  
  6853.  
  6854. ΓòÉΓòÉΓòÉ 4.11.3.4. Example of Using the strstream Class ΓòÉΓòÉΓòÉ
  6855.  
  6856. Class: ostrsteam 
  6857.  
  6858. The following example shows how you can use the strstream class. An strstream 
  6859. object s is created, and a line of data is written into it. This data is sent 
  6860. to cout using str(). str() calls freeze() with a nonzero argument and "freezes" 
  6861. the stream buffer attached to s. Therefore, the next step is to "thaw" the 
  6862. stream buffer by calling freeze() with an argument equal to 0. Once this has 
  6863. been done, the program continues by writing each character in s to cout using 
  6864. get(). 
  6865.  
  6866. #include <strstrea.h>
  6867.  
  6868. void main()
  6869. {
  6870.      strstream s;
  6871.      char c;
  6872.      s << "A line of data is written to the strstream.";
  6873.      //
  6874.      // Get access to the stream buffer via str().
  6875.      // The call to str() freezes the stream buffer.
  6876.      cout << "The string is: " << s.str() << endl;
  6877.      //
  6878.      // Now thaw it so that we can proceed.
  6879.      s.rdbuf()->freeze(0);
  6880.      cout << "The string is: ";
  6881.      while (s)
  6882.      {
  6883.           c = s.get();
  6884.           cout << c;
  6885.      }
  6886.      cout << endl;
  6887. }
  6888.  
  6889. The example produces the following output: 
  6890.  
  6891. The string is: A line of data is written to the strstream.
  6892. The string is: A line of data is written to the strstream.
  6893.  
  6894.  
  6895. ΓòÉΓòÉΓòÉ 4.11.4. Public Members of istrstream ΓòÉΓòÉΓòÉ
  6896.  
  6897. The following functions are described: 
  6898.  
  6899. o Constructors for istrstream 
  6900. o Destructor for istrstream 
  6901.  
  6902.  
  6903. ΓòÉΓòÉΓòÉ 4.11.4.1. Constructors for istrstream ΓòÉΓòÉΓòÉ
  6904.  
  6905. Class: istrstream 
  6906.  
  6907. istrstream(char* cp);
  6908. istrstream(signed char* cp);
  6909. istrstream(unsigned char* cp);
  6910. istrstream(const char* cp);
  6911. istrstream(const signed char* cp);
  6912. istrstream(const unsigned char* cp);
  6913. istrstream(char* cp, int len);
  6914. istrstream(signed char* cp, int len);
  6915. istrstream(unsigned char* cp, int len);
  6916. istrstream(const char* cp, int len);
  6917. istrstream(const signed char* cp, int len);
  6918. istrstream(const unsigned char* cp, int len);
  6919.  
  6920. The versions of the istrstream constructor that take one argument specify that 
  6921. characters should be extracted from the null-terminated string that is pointed 
  6922. to by cp. You can use the istream::seekg() function to reposition the get 
  6923. pointer in this string. 
  6924.  
  6925. The versions of the istrstream constructor that take two arguments specify that 
  6926. characters should be extracted from the array of bytes that starts at the 
  6927. position pointed to by cp and has a length of len bytes. You can use 
  6928. istream::seekg() to reposition the get pointer anywhere in this array. 
  6929.  
  6930.  
  6931. ΓòÉΓòÉΓòÉ 4.11.4.2. istrstream Destructor ΓòÉΓòÉΓòÉ
  6932.  
  6933. Class: ostrsteam 
  6934.  
  6935. ~istrstream();
  6936.  
  6937. The istrstream destructor frees space that was allocated by the istrstream 
  6938. constructor. 
  6939.  
  6940.  
  6941. ΓòÉΓòÉΓòÉ 4.11.5. Public Members of ostrstream ΓòÉΓòÉΓòÉ
  6942.  
  6943. The following functions are described: 
  6944.  
  6945. o Constructors for ostrstream 
  6946. o Destructor for ostrstream 
  6947. o str - return pointer to stream buffer array 
  6948. o pcount - return number of characters in stream buffer 
  6949.  
  6950.  
  6951. ΓòÉΓòÉΓòÉ 4.11.5.1. Constructors for ostrstream ΓòÉΓòÉΓòÉ
  6952.  
  6953. Class: ostrsteam 
  6954.  
  6955. ostrstream();
  6956. ostrstream(char* cp, int len, int mode = ios::out);
  6957. ostrstream(signed char* cp, int len, int mode = ios::out);
  6958. ostrstream(unsigned char* cp, int len, int mode = ios::out);
  6959.  
  6960. The version of the ostrsteam constructor that takes no arguments specifies that 
  6961. space is allocated dynamically for the stream buffer that is attached to the 
  6962. ostrsteam object. 
  6963.  
  6964. The versions of the ostrsteam constructor that take three arguments specify 
  6965. that the stream buffer that is attached to the ostrsteam object consists of an 
  6966. array that starts at the position pointed to by cp with a length of len bytes. 
  6967. If ios::ate or ios::app is set in mode, cp points to a null-terminated string 
  6968. and insertions begin at the null character. Otherwise, insertions begin at the 
  6969. position pointed to by cp. You can use the ostream::seekp() function to 
  6970. reposition the put pointer. 
  6971.  
  6972.  
  6973. ΓòÉΓòÉΓòÉ 4.11.5.2. Destructor for ostrstream ΓòÉΓòÉΓòÉ
  6974.  
  6975. Class: ostrsteam 
  6976.  
  6977. ~ostrstream();
  6978.  
  6979. The ostrsteam destructor frees space allocated by the ostrsteam constructor. 
  6980. The destructor also writes a null byte to the stream buffer to terminate the 
  6981. stream. 
  6982.  
  6983.  
  6984. ΓòÉΓòÉΓòÉ 4.11.5.3. str - Return Pointer to Stream Buffer Array ΓòÉΓòÉΓòÉ
  6985.  
  6986. Class: ostrsteam 
  6987.  
  6988. char* str();
  6989.  
  6990. str() returns a pointer to the stream buffer attached to the ostrsteam and 
  6991. calls freeze() with a nonzero value to prevent the stream buffer from being 
  6992. deleted. If the stream buffer was constructed with an explicit array, the value 
  6993. returned is a pointer to that array. If the stream buffer was constructed in 
  6994. dynamic mode, cp points to the dynamically allocated area. 
  6995.  
  6996. Until you call str(), deleting the dynamically allocated stream buffer is the 
  6997. responsibility of the ostrsteam object. After str() has been called, the 
  6998. calling application has responsibility for the dynamically allocated stream 
  6999. buffer. 
  7000.  
  7001.  
  7002. ΓòÉΓòÉΓòÉ 4.11.5.4. pcount - Return Number of Characters in Stream Buffer ΓòÉΓòÉΓòÉ
  7003.  
  7004. Class: ostrsteam 
  7005.  
  7006. int pcount();
  7007.  
  7008. pcount() returns the number of bytes that have been stored in the stream 
  7009. buffer. pcount() is mainly useful when binary data has been stored and the 
  7010. stream buffer attached to the ostrsteam object is not a null-terminated string. 
  7011. pcount() returns the total number of bytes, not just the number of bytes up to 
  7012. the first null character. 
  7013.  
  7014.  
  7015. ΓòÉΓòÉΓòÉ 4.12. stdiobuf and stdiostream Classes ΓòÉΓòÉΓòÉ
  7016.  
  7017. This chapter describes the stdiobuf class and stdiostream, the class that uses 
  7018. stdiobuf objects as stream buffers. Operations on an stdiobuf are mirrored on 
  7019. the associated FILE structure (defined in the C header file stdio.h). 
  7020.  
  7021. Note:  The classes described in this chapter are meant to be used when you have 
  7022. to mix C code with C++ code. If you are writing new C++ code, use filebuf 
  7023. fstream, ifstream, and ofstream instead of stdiobuf and stdiostream. 
  7024.  
  7025. The following topics are described in this chapter: 
  7026.  
  7027. o Declarations for stdiobuf and stdiostream in stdiostr.h 
  7028. o Public members of stdiobuf 
  7029. o Public members of stdiostream 
  7030.  
  7031.  
  7032. ΓòÉΓòÉΓòÉ 4.12.1. Declarations for stdiobuf and stdiostream in stdiostr.h ΓòÉΓòÉΓòÉ
  7033.  
  7034. You must include the following statement in any file that uses the stdiobuf 
  7035. class: 
  7036.  
  7037. #include <stdiostr.h>
  7038.  
  7039. The following is an excerpt from the stdiostr.h header file that shows the 
  7040. relevant declarations for the members of stdiobuf: 
  7041.  
  7042. class stdiobuf : public streambuf {
  7043. public:
  7044.                  stdiobuf(FILE* f);
  7045.      virtual     ~stdiobuf();
  7046.      virtual int overflow(int=EOF);
  7047.      virtual int underflow();
  7048.      virtual int sync() ;
  7049.      virtual streampos
  7050.                  seekoff(streamoff,ios::seek_dir,int);
  7051.      virtual int pbackfail(int c);
  7052.      FILE*       stdiofile();
  7053. };
  7054.  
  7055. class stdiostream : public ios {
  7056. public:
  7057.                  stdiostream(FILE*);
  7058.                  ~stdiostream();
  7059.      stdiobuf*   rdbuf();
  7060. };
  7061.  
  7062.  
  7063. ΓòÉΓòÉΓòÉ 4.12.2. Public Members of stdiobuf ΓòÉΓòÉΓòÉ
  7064.  
  7065. The following functions are described: 
  7066.  
  7067. o Constructor for stdiobuf 
  7068. o Destructor for stdiobuf 
  7069. o stdiofile - return associated FILE 
  7070.  
  7071.  
  7072. ΓòÉΓòÉΓòÉ 4.12.2.1. Constructor for stdiobuf ΓòÉΓòÉΓòÉ
  7073.  
  7074. Class: stdiobuf 
  7075.  
  7076. stdiobuf(FILE* f);
  7077.  
  7078. The stdiobuf constructor creates an stdiobuf object that is associated with the 
  7079. FILE pointed to by f. Changes that are made to the stream buffer in an stdiobuf 
  7080. object are also made to the associated FILE pointed to by f. 
  7081.  
  7082. Note:  If ios::stdio is set in the format state of an ostream object, a call to 
  7083. osfx() flushes stdout and stderr. 
  7084.  
  7085.  
  7086. ΓòÉΓòÉΓòÉ 4.12.2.2. Destructor for stdiobuf ΓòÉΓòÉΓòÉ
  7087.  
  7088. Class: stdiobuf 
  7089.  
  7090. ~stdiobuf();
  7091.  
  7092. The stdiobuf destructor frees space allocated by the stdiobuf constructor and 
  7093. flushes the file that this stdiobuf object is associated with. 
  7094.  
  7095.  
  7096. ΓòÉΓòÉΓòÉ 4.12.2.3. stdiofile - Return Associated FILE ΓòÉΓòÉΓòÉ
  7097.  
  7098. Class: stdiobuf 
  7099.  
  7100. FILE* stdiofile();
  7101.  
  7102. stdiofile() returns a pointer to the FILE object that the stdiobuf object is 
  7103. associated with. 
  7104.  
  7105.  
  7106. ΓòÉΓòÉΓòÉ 4.12.3. Public Members of stdiostream ΓòÉΓòÉΓòÉ
  7107.  
  7108. The following member functions are described: 
  7109.  
  7110. o Constructor for stdiostream 
  7111. o rdbuf - return pointer to stream buffer 
  7112.  
  7113. An example of using stdiostream is also provided. 
  7114.  
  7115.  
  7116. ΓòÉΓòÉΓòÉ 4.12.3.1. Constructor for stdiostream ΓòÉΓòÉΓòÉ
  7117.  
  7118. Class: stdiostream 
  7119.  
  7120. stdiostream(FILE* file);
  7121.  
  7122. The stdiostream constructor creates a stdiostream object that is attached to 
  7123. the FILE pointed to by file. 
  7124.  
  7125.  
  7126. ΓòÉΓòÉΓòÉ 4.12.3.2. rdbuf - Return Pointer to Stream Buffer ΓòÉΓòÉΓòÉ
  7127.  
  7128. Class: stdiostream 
  7129.  
  7130. stdiobuf* rdbuf();
  7131.  
  7132. rdbuf() returns a pointer to the stdiobuf object that is attached to the 
  7133. stdiostream object. 
  7134.  
  7135.  
  7136. ΓòÉΓòÉΓòÉ 4.12.3.3. Example of How to Use stdiostream ΓòÉΓòÉΓòÉ
  7137.  
  7138. The following example shows how you can use the stdiostream class. Two files 
  7139. are opened using fopen(). The pointers to the FILE structures are then used to 
  7140. create stdiostream objects. Finally, the contents of one of these stdiostream 
  7141. objects are copied into the other stdiostream object. 
  7142.  
  7143. #include <stdiostr.h>
  7144. #include <stdio.h>
  7145. #include <stdlib.h>
  7146.  
  7147. void main()
  7148. {
  7149.      FILE *in = fopen("in.dat", "r");
  7150.      FILE *out = fopen("out.dat", "w");
  7151.      int c;
  7152.      if (in == NULL)
  7153.      {
  7154.           cerr << "Cannot open file 'in.dat' for reading."
  7155.                << endl;
  7156.           exit(1);
  7157.      }
  7158.      if (out == NULL)
  7159.      {
  7160.           cerr << "Cannot open file 'out.dat' for writing."
  7161.                << endl;
  7162.           exit(1);
  7163.      }
  7164.      //
  7165.      // Create a stdiostream object attached to "f"
  7166.      //
  7167.      stdiostream sin(in);
  7168.      stdiostream sout(out);
  7169.      cout << "The data contained in the file is: " << endl;
  7170.      //
  7171.      // Now read data from "sin" and copy it to
  7172.      // "cout" and "sout"
  7173.      //
  7174.      while ((c = sin.rdbuf()->sbumpc()) != EOF)
  7175.      {
  7176.           cout << char(c);
  7177.           sout.rdbuf()->sputc(c);
  7178.      }
  7179.      cout << endl;
  7180. }
  7181.  
  7182. If you run this example with an input file containing the following: 
  7183.  
  7184. input input input input
  7185.  
  7186. The following output is produced: 
  7187.  
  7188. The data contained in the file is:
  7189. input input input input
  7190.  
  7191.  
  7192. ΓòÉΓòÉΓòÉ 4.13. Manipulators ΓòÉΓòÉΓòÉ
  7193.  
  7194. This chapter describes the parameterized manipulators provided by the I/O 
  7195. Stream Library and the facilities you can use to declare your own manipulators. 
  7196.  
  7197. The following topics are described in this chapter: 
  7198.  
  7199. o Introduction to manipulators 
  7200. o Declarations for parameterized manipulators in iomanip.h 
  7201. o Simple manipulators and parameterized manipulators 
  7202. o Creating simple manipulators for your own types 
  7203. o Creating parameterized manipulators for your own types 
  7204. o Parameterized manipulators for the format state 
  7205.  
  7206.  
  7207. ΓòÉΓòÉΓòÉ 4.13.1. Introduction to Manipulators ΓòÉΓòÉΓòÉ
  7208.  
  7209. Manipulators provide a convenient way of changing the characteristics of an 
  7210. input or output stream, using the same syntax that is used to insert or extract 
  7211. values. Manipulators allow you to embed a function call in an expression that 
  7212. contains a series of insertions or extractions. Manipulators usually provide 
  7213. shortcuts for sequences of iostream library operations. See Simple Manipulators 
  7214. and Parameterized Manipulators for a description of the two kinds of 
  7215. manipulators. 
  7216.  
  7217. The iomanip.h header file contains a definition for a macro IOMANIPdeclare(). 
  7218. IOMANIPdeclare() takes a type name as an argument and creates a series of 
  7219. classes that can be used to define manipulators for a given kind of stream. 
  7220. Calling the macro IOMANIPdeclare() with a type as an argument creates a series 
  7221. of classes that let you define manipulators for your own classes. You will get 
  7222. a syntax error if you call IOMANIPdeclare() with the same argument more than 
  7223. once in a file. 
  7224.  
  7225.  
  7226. ΓòÉΓòÉΓòÉ 4.13.2. Declarations for Parameterized Manipulators in iomanip.h ΓòÉΓòÉΓòÉ
  7227.  
  7228. You must include the following statement in any file that uses members of the 
  7229. parameterized manipulator classes: 
  7230.  
  7231. #include <iomanip.h>
  7232.  
  7233. The following is an excerpt from the iomanip.h header file that shows the 
  7234. relevant declarations for the members of the parameterized manipulator classes. 
  7235. This header file supplies macro definitions that you can use to define new 
  7236. parameterized manipulators. 
  7237.  
  7238. #include <generic.h>
  7239. #include <iostream.h>
  7240.  
  7241. #define SMANIP(T)name2(smanip_,T)
  7242. #define SAPP(T)name2(sapply_,T)
  7243. #define IMANIP(T)name2(imanip_,T)
  7244. #define OMANIP(T)name2(omanip_,T)
  7245. #define IOMANIP(T)name2(iomanip_,T)
  7246. #define IAPP(T)name2(iapply_,T)
  7247. #define OAPP(T)name2(oapply_,T)
  7248. #define IOAPP(T)name2(ioapply_,T)
  7249. #define IOMANIPdeclare(T)
  7250. class SMANIP(T) {
  7251.      ios& (*fct)(ios&,T);
  7252.      T arg ;
  7253. public:
  7254.      SMANIP(T)(ios& (*f)(ios&, T), T a);
  7255.      friend istream& operator>>(istream& i,const SMANIP(T)& m);
  7256.      friend ostream& operator<<(ostream& o, const SMANIP(T)& m);
  7257. };
  7258.  
  7259. class SAPP(T) {
  7260.      ios& (*fct)(ios&, T);
  7261. public:
  7262.      SAPP(T)(ios& (*f)(ios&,T));
  7263.      SMANIP(T) operator()(T a);
  7264. };
  7265.  
  7266. class IMANIP(T) {
  7267.      istream& (*fct)(istream&,T);
  7268.      T arg;
  7269. public:
  7270.      IMANIP(T)(istream& (*f)(istream&, T), T a );
  7271.      friend istream& operator>>(istream& s, const IMANIP(T)& m);
  7272. };
  7273.  
  7274. class IAPP(T) {
  7275.      istream& (*fct)(istream&, T);
  7276. public:
  7277.      IAPP(T)(istream& (*f)(istream&,T));
  7278.      IMANIP(T) operator()(T a);
  7279. };
  7280.  
  7281. class OMANIP(T) {
  7282.      ostream& (*fct)(ostream&,T);
  7283.      T arg;
  7284. public:
  7285.      OMANIP(T)(ostream& (*f)(ostream&, T), T a );
  7286.      friend ostream& operator<<(ostream& s, const OMANIP(T)& m);
  7287. };
  7288.  
  7289. class OAPP(T) {
  7290.      ostream& (*fct)(ostream&, T);
  7291. public:
  7292.      OAPP(T)(ostream& (*f)(ostream&,T));
  7293.      OMANIP(T) operator()(T a);
  7294. };
  7295.  
  7296. class IOMANIP(T) {
  7297.      iostream& (*fct)(iostream&,T);
  7298.      T arg ;
  7299. public:
  7300.      IOMANIP(T)(iostream& (*f)(iostream&, T), T a );
  7301.      friend istream& operator>>(iostream& s,
  7302.           const IOMANIP(T)& m);
  7303.      friend ostream& operator<<(iostream& s,
  7304.           const IOMANIP(T)& m);
  7305. };
  7306.  
  7307. class IOAPP(T) {
  7308.      iostream& (*fct)(iostream&, T);
  7309. public:
  7310.      IOAPP(T)(iostream& (*f)(iostream&,T));
  7311.      IOMANIP(T) operator()(T a);
  7312. };
  7313.  
  7314. IOMANIPdeclare(int);
  7315. IOMANIPdeclare(long);
  7316.  
  7317. SMANIP(int)     setbase(int b);
  7318. SMANIP(long)    resetiosflags(long b);
  7319. SMANIP(long)    setiosflags(long b);
  7320. SMANIP(int)     setfill(int f);
  7321. SMANIP(int)     setprecision(int p);
  7322. SMANIP(int)     setw(int w);
  7323.  
  7324. Note: 
  7325.  
  7326.  1. The following classes are defined after the macro IOMANIPdeclare(T) 
  7327.     expands: 
  7328.  
  7329.    o SMANIP(T) 
  7330.    o SAPP(T) 
  7331.    o IMANIP(T) 
  7332.    o IAPP(T) 
  7333.    o OMANIP(T) 
  7334.    o OAPP(T) 
  7335.    o IOMANIP(T) 
  7336.    o IOAPP(T) 
  7337.  
  7338.  2. These classes could have been implemented as template classes with the 
  7339.     argument T rather than as macro expansions. 
  7340.  
  7341.  3. The macro IOMANIPdeclare(T) is defined on a single, continued line in the 
  7342.     iomanip.h header file. The continuation delimiters have been omitted from 
  7343.     the above excerpt to make it easier to read. 
  7344.  
  7345.  
  7346. ΓòÉΓòÉΓòÉ 4.13.3. Simple Manipulators and Parameterized Manipulators ΓòÉΓòÉΓòÉ
  7347.  
  7348. There are two kinds of manipulators: 
  7349.  
  7350. o Simple manipulators do not take any arguments. The following classes have 
  7351.   built-in simple manipulators: 
  7352.  
  7353.    - ios 
  7354.    - istream 
  7355.    - ostream. 
  7356.  
  7357. o Parameterized manipulators require one or more arguments. setfill is an 
  7358.   example of a parameterized manipulator. You can create your own parameterized 
  7359.   manipulators and your own simple manipulators. 
  7360.  
  7361. The following example shows the uses of both simple and parameterized 
  7362. manipulators. 
  7363.  
  7364. #include <iostream.h>
  7365. #include <iomanip.h>
  7366. ostream& SimpleGap (ostream& os)
  7367.    { return os << "____"; }
  7368. ostream& ParmGap(ostream& os, int n)
  7369.    {
  7370.    for (int i=n;i;i--) os << '.';
  7371.    return os;
  7372.    }
  7373. OMANIP(int) ParmGap(int n) { return OMANIP(int)(ParmGap,n);}
  7374.  
  7375. void main()
  7376.    {
  7377.    cout << "Here is a" << SimpleGap << "and some text" << endl
  7378.         << "Here is a" << ParmGap(10) << "of ten dots" << endl;
  7379.    }
  7380.  
  7381. The simple manipulator SimpleGap allows you to write a fixed-length string of 
  7382. underscores to an ostream object. ParmGap allows you to write a variable-length 
  7383. string of dots to an ostream object. The length of the string of dots is 
  7384. specified as the argument to ParmGap. This program produces the following 
  7385. output: 
  7386.  
  7387. Here is a____and some text
  7388. Here is a..........of ten dots
  7389.  
  7390.  
  7391. ΓòÉΓòÉΓòÉ 4.13.4. Creating Simple Manipulators for Your Own Types ΓòÉΓòÉΓòÉ
  7392.  
  7393. The I/O Stream Library gives you the facilities to create simple manipulators 
  7394. for your own types. Simple manipulators that manipulate istream objects are 
  7395. accepted by the following input operators: 
  7396.  
  7397. istream istream::operator>> (istream&, istream& (*f) (istream&));
  7398. istream istream::operator>> (istream&, ios&(*f) (ios&));
  7399.  
  7400. Simple manipulators that manipulate ostream objects are accepted by the 
  7401. following output operators: 
  7402.  
  7403. ostream ostream::operator<< (ostream&, ostream&(*f) (ostream&));
  7404. ostream ostream::operator<< (ostream&, ios&(*f) (ios&));
  7405.  
  7406. The definition of a simple manipulator depends on the type of object that it is 
  7407. supposed to modify. The following table shows sample function definitions to 
  7408. modify istream, ostream, and ios objects. 
  7409.  
  7410. ΓöîΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓö¼ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÉ
  7411. Γöé Class of object    Γöé Sample function definition        Γöé
  7412. Γö£ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓö╝ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöñ
  7413. Γöé istream        Γöé "istream &fi(istream&){ /*...*/ }"    Γöé
  7414. Γö£ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓö╝ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöñ
  7415. Γöé ostream        Γöé "ostream &fo(ostream&){ /*...*/ }"    Γöé
  7416. Γö£ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓö╝ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöñ
  7417. Γöé ios          Γöé "ios &fios(ios&){ /*...*/ }"       Γöé
  7418. ΓööΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓö┤ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÿ
  7419.  
  7420. For example, if you want to define a simple manipulator tab that inserts a tab 
  7421. character into an ostream object, the definition would look like this: 
  7422.  
  7423. ostream     (ostream& os){ return os << '\t'; }
  7424.  
  7425. Thus defined, the tab manipulator could be used like this: 
  7426.  
  7427. cout << "The value is" << tab << "10" << endl;
  7428.  
  7429.  
  7430. ΓòÉΓòÉΓòÉ 4.13.5. Creating Parameterized Manipulators for Your Own Types ΓòÉΓòÉΓòÉ
  7431.  
  7432. The I/O Stream Library gives you the facilities to create parameterized 
  7433. manipulators for your own types. Follow these steps to create a parameterized 
  7434. manipulator that takes an argument of a particular type tp: 
  7435.  
  7436.  1. Call the macro IOMANIPdeclare(tp). Note that tp must be a single 
  7437.     identifier. For example, if you want tp to be a reference to a long double 
  7438.     value, use typedef to make a single identifier to replace the two 
  7439.     identifiers that make up the type label long double: 
  7440.  
  7441.              typedef long double& LONGREF
  7442.  
  7443.  2. Determine the class of your manipulator. If you want to define the 
  7444.     manipulator as shown in Example of Defining an APP Parameterized 
  7445.     Manipulator, choose a class that has APP in its name (an APP class, also 
  7446.     known as an applicator). If you want to define the manipulator as shown in 
  7447.     Example of Defining a MANIP Parameterized Manipulator, choose a class that 
  7448.     has MANIP in its name (a MANIP class). Once you have determined whether to 
  7449.     use an APP class or a MANIP class, the particular class that you choose 
  7450.     depends on the type of object that the manipulator is going to manipulate. 
  7451.     The following table shows the class of objects to be modified, and the 
  7452.     corresponding manipulator classes. 
  7453.  
  7454.         ΓöîΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓö¼ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÉ
  7455.         Γöé Class to be modified        Γöé Manipulator class         Γöé
  7456.         Γö£ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓö╝ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöñ
  7457.         Γöé istream              Γöé "IMANIP(tp)" or "IAPP(tp)"     Γöé
  7458.         Γö£ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓö╝ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöñ
  7459.         Γöé ostream              Γöé "OMANIP(tp)" or "OAPP(tp)"     Γöé
  7460.         Γö£ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓö╝ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöñ
  7461.         Γöé iostream              Γöé "IOMANIP(tp)" or "IOAPP(tp)"    Γöé
  7462.         Γö£ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓö╝ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöñ
  7463.         Γöé The ios part of istream objects  Γöé "SMANIP(tp)" or "SAPP(tp)"     Γöé
  7464.         Γöé or ostream objects         Γöé                  Γöé
  7465.         ΓööΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓö┤ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÿ
  7466.  
  7467.  3. Define a function f that takes an object of the class tp as an argument. 
  7468.     The definition of this function depends on the class you chose in step 2, 
  7469.     and is shown in the following table: 
  7470.  
  7471.         ΓöîΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓö¼ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÉ
  7472.         Γöé Class chosen       Γöé Sample definition           Γöé
  7473.         Γö£ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓö╝ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöñ
  7474.         Γöé "IMANIP(tp)" or      Γöé "istream &f(istream&, tp){/ *... */  Γöé
  7475.         Γöé "IAPP(tp)"        Γöé }"                   Γöé
  7476.         Γö£ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓö╝ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöñ
  7477.         Γöé "OMANIP(tp)" or      Γöé "ostream &f(ostream&, tp){/* ... */  Γöé
  7478.         Γöé "OAPP(tp)"        Γöé }"                   Γöé
  7479.         Γö£ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓö╝ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöñ
  7480.         Γöé "IOMANIP(tp)" or     Γöé "iostream &f(iostream&, tp){/* ... */ Γöé
  7481.         Γöé "IOAPP(tp)"        Γöé }"                   Γöé
  7482.         Γö£ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓö╝ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöñ
  7483.         Γöé "SMANIP(tp)" or      Γöé "ios &f(ios&, tp){/* ... */ }"     Γöé
  7484.         Γöé "SAPP(tp)"        Γöé                    Γöé
  7485.         ΓööΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓö┤ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÿ
  7486.  
  7487.  4. If you chose one of the APP classes in step 2, define the manipulator as 
  7488.     shown in Example of Defining an APP Parameterized Manipulator If you chose 
  7489.     one of the MANIP classes in step 2, define the manipulator as shown in 
  7490.     Example of Defining a MANIP Parameterized Manipulator. These two methods 
  7491.     produce equivalent manipulators. 
  7492.  
  7493.     Note:  Parameterized manipulators defined with IOMANIP or IOAPP are not 
  7494.     associative. See Examples of Nonassociative Parameterized Manipulators for 
  7495.     more details. 
  7496.  
  7497.  
  7498. ΓòÉΓòÉΓòÉ 4.13.5.1. Example of Defining an APP Parameterized Manipulator ΓòÉΓòÉΓòÉ
  7499.  
  7500. In the following example, the macro IOMANIPdeclare is called with the 
  7501. user-defined class my_class as an argument. One of the classes that is 
  7502. produced, OAPP(my_class), is used to define the manipulator pre_print. 
  7503.  
  7504. #include
  7505. <iomanip.h>
  7506.  
  7507. // declare class
  7508.  
  7509. class my_class {
  7510. public:
  7511.      char * s1;
  7512.      const char c;
  7513.      unsigned short ctr;
  7514.      my_class(char *theme, const char suffix,
  7515.           unsigned short times):
  7516.           s1(theme), c(suffix), ctr(times) {}
  7517. };
  7518.  
  7519. // print a character an indicated number of times
  7520. // followed by a string
  7521.  
  7522. ostream& produce_prefix(ostream& o, my_class mc) {
  7523.      for (register i=mc.ctr; i; --i) o << mc.c ;
  7524.      o << mc.s1;
  7525.      return o;
  7526. }
  7527.  
  7528. IOMANIPdeclare(my_class);
  7529.  
  7530. // define a manipulator for the class my_class
  7531.  
  7532. OAPP(my_class) pre_print=produce_prefix;
  7533.  
  7534. void main()
  7535. {
  7536.         my_class obj("Hello",'-',10);
  7537.         cout << pre_print(obj) << endl;
  7538. }
  7539.  
  7540. This program produces the following output: 
  7541.  
  7542. ----------Hello
  7543.  
  7544.  
  7545. ΓòÉΓòÉΓòÉ 4.13.5.2. Example of Defining a MANIP Parameterized Manipulator ΓòÉΓòÉΓòÉ
  7546.  
  7547. In the following example, the macro IOMANIPdeclare is called with the 
  7548. user-defined class my_class as an argument. One of the classes that is 
  7549. produced, OMANIP(my_class), is used to define the manipulator pre_print(). 
  7550.  
  7551. #include <iostream.h>
  7552. #include <iomanip.h>
  7553.  
  7554. class my_class {
  7555. public:
  7556.      char * s1;
  7557.      const char c;
  7558.      unsigned short ctr;
  7559.      my_class(char *theme, const char suffix,
  7560.           unsigned short times):
  7561.           s1(theme), c(suffix), ctr(times) {};
  7562. };
  7563.  
  7564. // print a character an indicated number of times
  7565. // followed by a string
  7566.  
  7567. ostream& produce_prefix(ostream& o, my_class mc) {
  7568.      for (register int i=mc.ctr; i; --i) o << mc.c ;
  7569.      o << mc.s1;
  7570.      return o;
  7571. }
  7572.  
  7573. IOMANIPdeclare(my_class);
  7574.  
  7575. // define a manipulator for the class my_class
  7576.  
  7577. OMANIP(my_class) pre_print(my_class mc) {
  7578.      return OMANIP(my_class) (produce_prefix,mc);
  7579. }
  7580.  
  7581. void main()
  7582. {
  7583.          my_class obj("Hello",'-',10);
  7584.          cout << pre_print(obj) << "\0" << endl;
  7585. }
  7586.  
  7587. This example produces the same output as the previous example. 
  7588.  
  7589.  
  7590. ΓòÉΓòÉΓòÉ 4.13.5.3. Examples of Nonassociative Parameterized Manipulators ΓòÉΓòÉΓòÉ
  7591.  
  7592. The following example demonstrates that parameterized manipulators defined with 
  7593. IOMANIP or IOAPP are not associative. The parameterized manipulator mysetw() is 
  7594. defined with IOMANIP. mysetw() can be applied once in any statement, but if it 
  7595. is applied more than once, it causes a compile-time error. You can avoid such 
  7596. an error by putting each application of mysetw into a separate statement. 
  7597.  
  7598. #include <iomanip.h>
  7599.  
  7600. iostream&  f(iostream & io, int i) {
  7601.      io.width(i);
  7602.      return io;
  7603. }
  7604.  
  7605. IOMANIP (int) mysetw(int i) {
  7606.      return IOMANIP(int) (f,i);
  7607. }
  7608.  
  7609. iostream_withassign ioswa;
  7610.  
  7611. void main() {
  7612.      ioswa = cout ;
  7613.      int i1 = 8, i2 = 14;
  7614.      //
  7615.      // The following statement does not cause a compile-time
  7616.      // error.
  7617.      //
  7618.      ioswa << mysetw(3) << i1 << endl ;
  7619.      //
  7620.      // The following statement causes a compile-time error
  7621.      // because the manipulator mysetw is applied twice.
  7622.      //
  7623.      ioswa << mysetw(3) << i1 <<mysetw(5) << i2 << endl;
  7624.      //
  7625.      // The following statements are equivalent to the previous
  7626.      // statement, but they do not cause a compile-time error.
  7627.      //
  7628.      ioswa << mysetw(3) << i1 ;
  7629.      ioswa << mysetw(5) << i2 << endl;
  7630. }
  7631.  
  7632.  
  7633. ΓòÉΓòÉΓòÉ 4.13.6. Parameterized Manipulators for the Format State ΓòÉΓòÉΓòÉ
  7634.  
  7635. The iomanip.h header file also contains calls to the IOMANIPdeclare() macro for 
  7636. types int and long. These calls create classes that are used to create the 
  7637. parameterized manipulators that control the format state of ios objects. 
  7638.  
  7639. The call to IOMANIPdeclare(int) creates classes with names that are expanded 
  7640. from the following macros: 
  7641.  
  7642. o SMANIP(int) 
  7643. o SAPP(int) 
  7644. o IMANIP(int) 
  7645. o IAPP(int) 
  7646. o OMANIP(int) 
  7647. o OAPP(int) 
  7648. o IOMANIP(int) 
  7649. o IOAPP(int). 
  7650.  
  7651. All of these macros expand to names that include the string "int". Similarly, 
  7652. IOMANIPdeclare(long) creates eight classes whose names include the string 
  7653. "long". 
  7654.  
  7655. The following manipulators are declared using the classes created by the calls 
  7656. to IOMANIPdeclare(int) and IOMANIPdeclare(long). 
  7657.  
  7658. Note:  All of the parameterized manipulators described below are defined for 
  7659. both istream and ostream objects. In the following descriptions, os is a 
  7660. reference to an ostream object and is is a reference to an istream object. 
  7661.  
  7662. The following manipulators are described: 
  7663.  
  7664. o resetiosflags - clear format flags 
  7665. o setbase - set conversion base 
  7666. o setfill - set fill character 
  7667. o setiosflags - set format flags 
  7668. o setprecision - set precision 
  7669. o setw - set field width 
  7670.  
  7671.  
  7672. ΓòÉΓòÉΓòÉ 4.13.6.1. resetiosflags - Clear Format Flags ΓòÉΓòÉΓòÉ
  7673.  
  7674. Manipulators 
  7675.  
  7676. SMANIP(long) resetiosflags(long flags);
  7677.  
  7678. resetiosflags() clears the format flags specified in flags. It can appear in an 
  7679. input stream: 
  7680.  
  7681. is >> resetiosflags(flags);
  7682.  
  7683. In this case, resetiosflags() calls is.setf(0,flags). 
  7684.  
  7685. resetiosflags() can also appear in an output stream: 
  7686.  
  7687. os << resetiosflags(flags);
  7688.  
  7689. In this case, resetiosflags calls os.setf(0,flags). 
  7690.  
  7691.  
  7692. ΓòÉΓòÉΓòÉ 4.13.6.2. setbase - Set Conversion Base ΓòÉΓòÉΓòÉ
  7693.  
  7694. Manipulators 
  7695.  
  7696. SMANIP(int) setbase(int base);
  7697.  
  7698. setbase() sets the conversion base to be equal to the value of the argument 
  7699. base. If base equals 10, the conversion base is set to 10. If base equals 8, 
  7700. the conversion base is set to 8. If base equals 16, the conversion base is set 
  7701. to 16. Otherwise, the conversion base is set to 0. If the conversion base is 0, 
  7702. output is treated the same as if the base were 10, but input is interpreted 
  7703. according to the C++ lexical conventions. This means that input values that 
  7704. begin with "0" are interpreted as octal values, and values that begin with "0x" 
  7705. or "0X" are interpreted as hexadecimal values. 
  7706.  
  7707.  
  7708. ΓòÉΓòÉΓòÉ 4.13.6.3. setfill - Set Fill Character ΓòÉΓòÉΓòÉ
  7709.  
  7710. Manipulators 
  7711.  
  7712. SMANIP(int) setfill(int fill);
  7713.  
  7714. setfill() sets the fill character, ios::x_fill, to fill. The fill character is 
  7715. the character that appears in values that need to be padded to fill the field 
  7716. width. setfill() can appear in either an input stream or an output stream: 
  7717.  
  7718. is >> setfill(fill);
  7719. os << setfill(fill);
  7720.  
  7721. setfill() performs the same task as the function fill(). 
  7722.  
  7723.  
  7724. ΓòÉΓòÉΓòÉ 4.13.6.4. setiosflags - Set Format Flags ΓòÉΓòÉΓòÉ
  7725.  
  7726. Manipulators 
  7727.  
  7728. SMANIP(long) setiosflags(long flags);
  7729.  
  7730. setiosflags() sets the format flags specified in flags. setiosflags() can 
  7731. appear in an input stream: 
  7732.  
  7733. is >> setiosflags(flags);
  7734.  
  7735. If it appears in an input stream, setiosflags() calls is.setf.(flags) 
  7736.  
  7737. If it appears in an output stream, setiosflags() calls os.setf(flags): 
  7738.  
  7739. os << setiosflags(flags);
  7740.  
  7741.  
  7742. ΓòÉΓòÉΓòÉ 4.13.6.5. setprecision - Set Precision ΓòÉΓòÉΓòÉ
  7743.  
  7744. Manipulators 
  7745.  
  7746. SMANIP(int) setprecision(int prec);
  7747.  
  7748. setprecision() sets the precision format state variable, ios::x_prec, to the 
  7749. value of prec. The value of prec must be greater than zero. If the value of 
  7750. prec is negative, the precision format state variable is set to 6. 
  7751.  
  7752. setprecision() can appear in either an input stream or an output stream: 
  7753.  
  7754. is >> setprecision(prec);
  7755. os << setprecision(prec);
  7756.  
  7757.  
  7758. ΓòÉΓòÉΓòÉ 4.13.6.6. setw - Set Field Width ΓòÉΓòÉΓòÉ
  7759.  
  7760. Manipulators 
  7761.  
  7762. SMANIP(int) setw(int width);
  7763.  
  7764. setw() sets the width format state variable, ios::x_width, to the value of 
  7765. width. 
  7766.  
  7767. setw() can appear in either an input stream or an output stream: 
  7768.  
  7769. is >> setw(width);
  7770. os << setw(width);
  7771.  
  7772.  
  7773. ΓòÉΓòÉΓòÉ 5. The Task Library ΓòÉΓòÉΓòÉ
  7774.  
  7775. Introduction to the Task Library 
  7776.   Introduces the Task Library and lists the runtime error messages it 
  7777.   generates. 
  7778. Task Handling Classes 
  7779.   Describes object, sched, task, and timer, the task handling classes. These 
  7780.   classes give you the facilities to create and manage tasks. 
  7781. Queue Management Classes 
  7782.   Describes qhead and qtail the queue management classes. These classes let you 
  7783.   implement a wide range of message-passing and data-buffering schemes. 
  7784. Interrupt_handler Class 
  7785.   Describes Interrupt_handler, the class that allows your tasks to handle 
  7786.   external events in the form of signals from the operating system. 
  7787. Simulation Classes 
  7788.   Describes histogrm, randint, urand, and erand, the simulation classes. These 
  7789.   classes provide utilities for writing program simulations and gathering 
  7790.   statistics about them. 
  7791.  
  7792.  
  7793. ΓòÉΓòÉΓòÉ 5.1. Introduction to the Task Library ΓòÉΓòÉΓòÉ
  7794.  
  7795. This chapter introduces the classes that make up the Task Library. This library 
  7796. provides you with facilities to write programs with routines that appear to run 
  7797. in parallel. These routines, or tasks, can communicate with each other and 
  7798. start new tasks, but they do not actually run concurrently. The Task Library 
  7799. lets you simulate concurrent execution and gives you the facilities to write 
  7800. simulations. 
  7801.  
  7802. Extended Task Library Examples contains examples of how you might use the Task 
  7803. Library to implement a parallel algorithm and event-driven simulations. 
  7804.  
  7805. Note:  The Task Library is also referred to as the C++ Coroutine Library in 
  7806. some non-IBM documentation. 
  7807.  
  7808.  
  7809. ΓòÉΓòÉΓòÉ 5.1.1. What Are Tasks? ΓòÉΓòÉΓòÉ
  7810.  
  7811. Each task described in the context of the Task Library is an instance of a 
  7812. class that you derive from the class task. The constructor for the task class 
  7813. is declared protected, so you do not create instances of the class task, only 
  7814. instances of the classes that you derive from task. 
  7815.  
  7816. Tasks are said to be nonpreemptive and lightweight. They are nonpreemptive 
  7817. because only a single task is executing at any one time, and that task 
  7818. continues to execute until it suspends itself or is terminated. Tasks are 
  7819. lightweight because less time and space are required to create a task than to 
  7820. create a true OS/2 process. 
  7821.  
  7822. Tasks are more similar to OS/2 threads than to OS/2 processes. 
  7823.  
  7824. Each task object has a pointer associated with it called thistask. thistask is 
  7825. a pointer to the task that is executing. For example, if you use thistask in 
  7826. the main() function, you are using a pointer to the main() task, the task whose 
  7827. code appears in the main() function. 
  7828.  
  7829. Note:  Tasks are commonly used in program simulations. They are not meant to be 
  7830. replacements for multitasking facilities provided by OS/2 such as OS/2 
  7831. processes and threads. For further information see Using the Task Library with 
  7832. OS/2. 
  7833.  
  7834.  
  7835. ΓòÉΓòÉΓòÉ 5.1.2. What Do Tasks Do? ΓòÉΓòÉΓòÉ
  7836.  
  7837. Here are some of the things that tasks can do: 
  7838.  
  7839. o Communicate with other tasks using the facilities provided by the classes 
  7840.   qhead and qtail. See Queue Management Classes for more details on the qhead 
  7841.   and qtail classes. 
  7842.  
  7843. o Create new tasks. These tasks are called child tasks, and the task that 
  7844.   creates them is called the parent task. 
  7845.  
  7846. o Wait for external events. The Interrupt_handler class allows tasks to wait 
  7847.   for external events in the form of signals from the operating system. See 
  7848.   Interrupt_handler Class for more details on the Interrupt_handler class. 
  7849.  
  7850. o Consume simulated time. The concept of simulated time is implemented by the 
  7851.   timer class. 
  7852.  
  7853.  
  7854. ΓòÉΓòÉΓòÉ 5.1.3. The Task Library Class Hierarchy ΓòÉΓòÉΓòÉ
  7855.  
  7856. The Task Library has the following class hierarchy: 
  7857.  
  7858. o randint: each object of this class provides an independent sequence of 
  7859.   pseudo-random numbers. It is the base class for the following classes: 
  7860.  
  7861.    - urand 
  7862.    - erand 
  7863.  
  7864. o object is the base class for the following classes: 
  7865.  
  7866.    - Interrupt_handler 
  7867.    - qtail 
  7868.    - qhead 
  7869.    - sched 
  7870.  
  7871. o sched is itself the base class for the following classes: 
  7872.  
  7873.    - timer 
  7874.    - task 
  7875.  
  7876. o task is itself the base class for the internal class Interrupt_alerter 
  7877.  
  7878. The classes task and sched are used strictly as base classes. The constructors 
  7879. for both of these classes are declared protected. When you use the Task 
  7880. Library, you create objects of classes derived from these two classes rather 
  7881. than creating objects of these classes directly. 
  7882.  
  7883. Derivation Relationships in the Task Library shows the relationships between 
  7884. the classes in the task library. In the figure, two classes are connected by an 
  7885. arrow if the class at the pointed end of the arrow is derived from the class at 
  7886. the other end of the arrow. object and randint are the two base classes in the 
  7887. class hierarchy of the task library. histogram is a stand-alone class (that is, 
  7888. it is neither a base class nor a derived class), but it is commonly used in 
  7889. conjunction with randint. 
  7890.  
  7891.          object
  7892.    ΓöîΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÿΓöéΓöéΓööΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÉ
  7893.    Γöé    ΓöîΓöÇΓöÇΓöÇΓöÇΓöÿΓööΓöÇΓöÇΓöÇΓöÇΓöÉ    Γöé
  7894.  Interrupt_  qtail  qhead   sched
  7895.  handler            Γöé Γöé
  7896.               ΓöîΓöÇΓöÇΓöÇΓöÿ ΓööΓöÇΓöÇΓöÇΓöÉ
  7897.              timer    task
  7898.                    Γöé
  7899.                    Γöé
  7900.                  Interrupt_
  7901.                   alerter
  7902. histogram     randint
  7903.        ΓöîΓöÇΓöÇΓöÇΓöÇΓöÇΓöÿ ΓööΓöÇΓöÇΓöÇΓöÇΓöÇΓöÉ
  7904.       urand     erand
  7905.  
  7906. Derivation Relationships in the Task Library 
  7907.  
  7908.  
  7909. ΓòÉΓòÉΓòÉ 5.1.4. Using the Task Library with OS/2 ΓòÉΓòÉΓòÉ
  7910.  
  7911. The Task Library was originally coded to run in a UNIX** operating system 
  7912. environment.  Compromises were made in porting the library to OS/2 because of 
  7913. architectural differences between operating systems.  Use the following 
  7914. guidelines to ensure that your programs' use of the Task Library is appropriate 
  7915. and reliable. 
  7916.  
  7917.  
  7918. ΓòÉΓòÉΓòÉ 5.1.4.1. Linking to the Task Library ΓòÉΓòÉΓòÉ
  7919.  
  7920. You must specify the TASK.LIB library when compiling or linking programs that 
  7921. use the Task Library. There is no multitasking version of the library. You 
  7922. should not use this library in a multitasking program. 
  7923.  
  7924. No dynamically linkable version of this library is provided. 
  7925.  
  7926.  
  7927. ΓòÉΓòÉΓòÉ 5.1.4.2. Compiler Options for Programs that Use the Task Library ΓòÉΓòÉΓòÉ
  7928.  
  7929. You must compile any modules that use the Task Library with the following 
  7930. options to ensure that the correct stack tracebacks are generated for the Task 
  7931. Library: 
  7932.  
  7933. /Op-    Disable all stack pointer optimization 
  7934. /O-     Disable optimization (Note: this also sets /Os-) 
  7935. /Gh-    Disable profiler hook (This is the default. See Tasks and the Execution 
  7936.         Trace Analyzer for further details.) 
  7937.  
  7938.  
  7939. ΓòÉΓòÉΓòÉ 5.1.4.3. Tasks and Threads ΓòÉΓòÉΓòÉ
  7940.  
  7941. All tasks run in the user process address space and therefore do not benefit 
  7942. from OS/2's interprocess memory protection.  In addition, because threads are 
  7943. preemptive and tasks are not, threads may be an attractive alternative to tasks 
  7944. for many OS/2 processes. 
  7945.  
  7946. The Task Library should never be used in a multithreaded environment, because 
  7947. it is a single task simulating a multithreaded environment. Unpredictable 
  7948. results will occur if you use the Task Library in a true multithreaded 
  7949. environment. See "Creating Multithread Programs" in the IBM C/C++ Tools: 
  7950. Programming Guide for details on multithreaded programs. 
  7951.  
  7952.  
  7953. ΓòÉΓòÉΓòÉ 5.1.4.4. Tasks and the Execution Trace Analyzer ΓòÉΓòÉΓòÉ
  7954.  
  7955. Do not attempt to use the execution trace analyzer with programs that make use 
  7956. of the Task Library.  Your programs may compile successfully when you specify 
  7957. the /gh compiler option, but the results will be unpredictable and your program 
  7958. may fail without warning. 
  7959.  
  7960.  
  7961. ΓòÉΓòÉΓòÉ 5.1.4.5. Other Restrictions ΓòÉΓòÉΓòÉ
  7962.  
  7963. Tasks are simulated on OS/2 by using the heap for all tasks. Because of 
  7964. differences in the way the Task Library and the operating system access the 
  7965. stack, you should follow the guidelines below: 
  7966.  
  7967.  1. You can register an exception handler for a task with OS/2, either through 
  7968.     a call to DosSetExceptionHandler or through #pragma handler(). However, you 
  7969.     should not register an exception in one task and use it in another task. 
  7970.  
  7971.  2. Do not call longjmp from one task to another. 
  7972.  
  7973.  
  7974. ΓòÉΓòÉΓòÉ 5.1.4.6. The /noe Option and the Task Library ΓòÉΓòÉΓòÉ
  7975.  
  7976. If you experience problems linking your programs that use the Task Library with 
  7977. dde4sbs.lib, use the /noe option. 
  7978.  
  7979.  
  7980. ΓòÉΓòÉΓòÉ 5.1.5. Task Library Error Messages ΓòÉΓòÉΓòÉ
  7981.  
  7982. The following list shows the errors that can be generated by member functions 
  7983. of the classes that make up the Task Library. The first line in each entry 
  7984. shows the name of the error and the text of the error message that is produced. 
  7985. For some errors, additional information may be provided to help you determine 
  7986. the cause of the error.  This information is not included here. The second line 
  7987. explains the error. 
  7988.  
  7989. Note:  You should always use the error name macros, rather than using error 
  7990. numbers directly. Error numbers may change in subsequent releases of the 
  7991. compiler, or may vary between different compilers. For example, IBM C Set ++ 
  7992. for AIX*/6000 uses error numbers starting at 1, while IBM C/C++ Tools for OS/2 
  7993. uses error numbers starting at 7001. 
  7994.  
  7995. Error Name and Message Explanation 
  7996.  
  7997.  
  7998. E_OLINK object::delete(): has chain 
  7999.   Attempt to delete an object that remembers a task. 
  8000.  
  8001.  
  8002. E_ONEXT object::delete(): on chain 
  8003.   Attempt to delete an object that is still on some chain. 
  8004.  
  8005.  
  8006. E_GETEMPTY qhead::get(): empty 
  8007.   Attempt to get from an empty queue that has mode E_MODE. 
  8008.  
  8009.  
  8010. E_PUTOBJ qtail::put(): object on another queue 
  8011.   Attempt to put an object that is already on another queue. 
  8012.  
  8013.  
  8014. E_PUTFULL qtail::put(): full 
  8015.   Attempt to put to a full queue that has mode E_MODE. 
  8016.  
  8017.  
  8018. E_BACKOBJ qhead::putback(): object on another queue 
  8019.   Attempt to put back an object that is already on another queue. 
  8020.  
  8021.  
  8022. E_BACKFULL qhead::putback(): full 
  8023.   Attempt to put back to a full queue that has mode E_MODE. 
  8024.  
  8025.  
  8026. E_SETCLOCK sched::setclock(): clock!=0 
  8027.   Clock was nonzero when setclock() was called. 
  8028.  
  8029.  
  8030. E_CLOCKIDLE sched::schedule(): clock_task not idle 
  8031.   The clock_task was not IDLE when setclock() was called. 
  8032.  
  8033.  
  8034. E_RESTERM sched::schedule: terminated 
  8035.   Attempt to resume a task object with state equal to TERMINATED. 
  8036.  
  8037.  
  8038. E_RESRUN sched::schedule: running 
  8039.   Attempt to resume a task object with state equal to RUNNING. 
  8040.  
  8041.  
  8042. E_NEGTIME sched::schedule: clock<0 
  8043.   Negative argument to delay(). 
  8044.  
  8045.  
  8046. E_RESOBJ sched::schedule: task or timer on another queue 
  8047.   Attempt to resume a task or timer object that is already on some queue. 
  8048.  
  8049.  
  8050. E_HISTO histogram::histogram: bad arguments 
  8051.   Bad arguments for the histogram constructor. 
  8052.  
  8053.  
  8054. E_STACK task::restore(): stack overflow 
  8055.   Task runtime stack overflow. 
  8056.  
  8057.  
  8058. E_STORE new: free store exhausted 
  8059.   No more free store is available;  the call to new() failed. 
  8060.  
  8061.  
  8062. E_TASKMODE task::task(): bad mode 
  8063.   Mode argument not permitted for the task constructor. 
  8064.  
  8065.  
  8066. E_TASKDEL task::~task(): not terminated 
  8067.   Attempt to delete a non-TERMINATED task object. 
  8068.  
  8069.  
  8070. E_TASKPRE task::preempt(): not running 
  8071.   Attempt to preempt a non-RUNNING task. 
  8072.  
  8073.  
  8074. E_TIMERDEL timer::~timer(): not terminated 
  8075.   Attempt to delete a non-TERMINATED timer object. 
  8076.  
  8077.  
  8078. E_SCHTIME schedule: bad time 
  8079.   Scheduler run chain is corrupted;  the time of the task at the beginning of 
  8080.   the run chain is not valid. 
  8081.  
  8082.  
  8083. E_SCHOBJ sched object used directly (not as base) 
  8084.   sched object used directly instead of as a base class. 
  8085.  
  8086.  
  8087. E_QDEL queue::~queue(): not empty 
  8088.   Attempt to delete a nonempty queue. 
  8089.  
  8090.  
  8091. E_RESULT task::result(): thistask->result() 
  8092.   A task object attempted to obtain its own result(). 
  8093.  
  8094.  
  8095. E_WAIT task::wait(): wait for self 
  8096.   A task object called wait() with a pointer to itself as the argument. 
  8097.  
  8098.  
  8099. E_FUNCS FrameLayout:: FrameLayout(): function start 
  8100.   Internal error; cannot determine the call frame layout. 
  8101.  
  8102.  
  8103. E_FRAMES FrameLayout:: FrameLayout(): frame size 
  8104.   Internal error; cannot determine frame size. 
  8105.  
  8106.  
  8107. E_REGMASK task::fudge_return(): unexpected register mask 
  8108.   Internal error; unexpected register mask. 
  8109.  
  8110.  
  8111. E_FUDGE_SIZE task::fudge_return(): frame too big 
  8112.   Internal error;  the frame size returned by fudge_return is too big. 
  8113.  
  8114.  
  8115. E_NO_HNDLR sigFunc - no handler for signal 
  8116.   No handler for the generated signal. 
  8117.  
  8118.  
  8119. E_BADSIG illegal signal number 
  8120.   Attempt to use a signal number that is out of range. 
  8121.  
  8122.  
  8123. E_LOSTHNDLR Interrupt_handler:: ~Interrupt_handler(): signal handler not on 
  8124. chain 
  8125.   Attempt to call the destructor for a signal handler that has already been 
  8126.   destroyed. 
  8127.  
  8128.  
  8129. E_RETURN task returns without calling sched::resultis(int) 
  8130.   Unrecoverable error caused by an attempt to exit a task by a means other than 
  8131.   calling resultis(). 
  8132.  
  8133.  
  8134. E_BADMODE Illegal mode passed to qhead::qhead(), qtail::qtail(), 
  8135. qhead::setmode() or qtail::setmode() 
  8136.   Unrecoverable error caused by an attempt to set a mode that is not valid for 
  8137.   a queue. 
  8138.  
  8139.  
  8140. E_QZERO Attempt to set the maximum size of a queue to a value less than or 
  8141. equal to 0 
  8142.   Unrecoverable error caused by an attempt to set maximum queue size to a value 
  8143.   less than or equal to 0. 
  8144.  
  8145.  
  8146. E_URAND urand::urand(int, int) bad arguments 
  8147.   Attempt to pass arguments that are not valid to urand::urand(). 
  8148.  
  8149.  
  8150. ΓòÉΓòÉΓòÉ 5.2. Task Handling Classes ΓòÉΓòÉΓòÉ
  8151.  
  8152. This chapter describes the task handling classes: task, object, sched, and 
  8153. timer. You can use these classes to create and manage tasks. 
  8154.  
  8155. The fact that the Task Library has a class called object can cause some 
  8156. confusion. In this book, object (in normal typeface) refers to the C++ concept 
  8157. of an instance of a class, while object (in special typeface) refers to the 
  8158. class in the Task Library. In the rest of this book, an object of a given 
  8159. class, such as MyClass, is referred to as a MyClass object. For the sake of 
  8160. simplicity, an object of class object is called an object rather than an object 
  8161. object. 
  8162.  
  8163. Similarly, task (in normal typeface) refers to the concept of a lightweight, 
  8164. nonpreemptive process that can be used to create program simulations, while 
  8165. task (in special typeface) refers to the class in the Task Library. 
  8166.  
  8167. The following topics are described in this chapter: 
  8168.  
  8169. o Deriving classes from the task class 
  8170. o Shared and dedicated tasks 
  8171. o Declarations of task handling classes in task.h 
  8172. o Public members of object 
  8173. o Public members of sched 
  8174. o Members of task 
  8175. o Public members of timer 
  8176.  
  8177.  
  8178. ΓòÉΓòÉΓòÉ 5.2.1. Object States ΓòÉΓòÉΓòÉ
  8179.  
  8180. The object class is designed to be used as a base class. Objects of all of the 
  8181. classes that are derived from the object class (sched, timer, task, and any 
  8182. classes that you derive from these classes) can be in one of the two following 
  8183. states: 
  8184.  
  8185. o If a task object has to wait for an object, the object that is waited for has 
  8186.   a state of pending. If a task object performs a blocking operation (that is, 
  8187.   an operation that may cause the task object to give up control of the 
  8188.   processor while it waits for some event to take place) on a pending object, 
  8189.   it will wait until the state of the object changes to ready. 
  8190.  
  8191. o An object that does not need to be waited for is ready. 
  8192.  
  8193. You can determine the state of an object by calling the object class member 
  8194. function pending(). pending() returns a nonzero value if the state of an object 
  8195. is pending and returns 0 if the state of an object is ready. 
  8196.  
  8197. Any class that is derived from the class object that may be waited for must 
  8198. have rules for when it is pending or ready (that is, it must define the virtual 
  8199. function pending()). Each class derived from the class object can have, at 
  8200. most, one definition for pending(). 
  8201.  
  8202. The following list contains some examples of pending objects: 
  8203.  
  8204. o A qhead object that is associated with an empty queue is pending. If a task 
  8205.   calls qhead::get() for this object, that task must wait until some other task 
  8206.   calls qtail::put() to put something in the queue. See Queue Management 
  8207.   Classes for a description of queues. 
  8208.  
  8209. o A qtail object that is associated with a full queue is pending. If a task 
  8210.   calls qtail::put() for this object, that task must wait until another task 
  8211.   calls qhead::get() to remove something from the queue. 
  8212.  
  8213. o A task object that does not have a state equal to TERMINATED is pending. If 
  8214.   another task object calls sched::result() for this task object, it must wait 
  8215.   until the state of this task object changes to TERMINATED. Once the state of 
  8216.   the task object has changed to TERMINATED, the task object is no longer 
  8217.   pending. 
  8218.  
  8219.  
  8220. ΓòÉΓòÉΓòÉ 5.2.2. Task States ΓòÉΓòÉΓòÉ
  8221.  
  8222. A task can be in one of the three following states: 
  8223.  
  8224. o RUNNING: the task is running or it will be scheduled to run. 
  8225. o IDLE: the task is waiting (possibly for a pending object). Each task object 
  8226.   in the IDLE state has certain conditions that will change its state to 
  8227.   RUNNING. 
  8228. o TERMINATED: the task is complete. It cannot return to the RUNNING state, but 
  8229.   you can retrieve its result. 
  8230.  
  8231.  
  8232. ΓòÉΓòÉΓòÉ 5.2.3. Deriving Classes from the task Class ΓòÉΓòÉΓòÉ
  8233.  
  8234. The task constructor is declared protected. This means that you cannot create 
  8235. objects of the class task itself. You must derive a class from task and create 
  8236. objects of this class. This section describes some of the things that you 
  8237. should keep in mind when you derive such a class. 
  8238.  
  8239. The following topics are discussed: 
  8240.  
  8241. o Parent and child tasks 
  8242. o Only a single level of derivation is allowed from the task class 
  8243. o Constructors for classes derived from task cannot be inline 
  8244. o Using the task constructor 
  8245. o The default stack size 
  8246. o Tasks must explicitly call destructors for objects they create 
  8247.  
  8248.  
  8249. ΓòÉΓòÉΓòÉ 5.2.3.1. Parent and Child Tasks ΓòÉΓòÉΓòÉ
  8250.  
  8251. If a task is created by another task, then the created task is called a child 
  8252. task and the creating task is called a parent task. If the parent creates the 
  8253. child using dynamic allocation (that is, using the operator new), the child is 
  8254. created dynamically. Otherwise, the child is created on the local stack of the 
  8255. parent. 
  8256.  
  8257.  
  8258. ΓòÉΓòÉΓòÉ 5.2.3.2. Only a Single Level of Derivation Is Allowed ΓòÉΓòÉΓòÉ
  8259.  
  8260. Only one level of derivation is allowed from the class task. C/C++ Tools will 
  8261. not produce an error message if you attempt to derive a class from a class that 
  8262. is derived from task, but the behavior of the class will be unpredictable. For 
  8263. example, the following declaration of d_task2 is not valid: 
  8264.  
  8265.  
  8266. class d_task1 : public task { /* valid declaration */ };
  8267. class d_task2 : public d_task1 { /* unpredictable results */ };
  8268.  
  8269. A class can be derived from a set of classes that includes the class task, but 
  8270. the derived class cannot be used as the base class for any other classes. 
  8271.  
  8272.  
  8273. ΓòÉΓòÉΓòÉ 5.2.3.3. Constructors for Classes Derived from task Cannot Be Inline ΓòÉΓòÉΓòÉ
  8274.  
  8275. You should not define as inline the constructor for a class derived from the 
  8276. class task. You will not get a compile-time error if you attempt to do this, 
  8277. but the results will be unpredictable. For example: 
  8278.  
  8279. class d_task : public task {
  8280. public:
  8281.      d_task() { /* inline constructor - results unpredictable */ }
  8282. // .
  8283. // .
  8284. // .
  8285. };
  8286.  
  8287.  
  8288. ΓòÉΓòÉΓòÉ 5.2.3.4. Using the task Constructor ΓòÉΓòÉΓòÉ
  8289.  
  8290. Consider a class Addint that is derived from the class task. The body of the 
  8291. constructor for the Addint class constitutes the code that will be executed by 
  8292. tasks of the Addint class. Suppose that a task of the Addint class adds two 
  8293. numbers together. The definition for Addint could look something like this: 
  8294.  
  8295.  
  8296. #include <task.h>
  8297. #include <iostream.h>
  8298.  
  8299. class Addint : public task
  8300. {
  8301. public:
  8302.      Addint(int, int);
  8303. };
  8304.  
  8305. The constructor for the class Addint looks like this: 
  8306.  
  8307. Addint::Addint(int a, int b)
  8308. {
  8309.      resultis(a+b);
  8310. }
  8311.  
  8312. A task object of the class Addint could be created and used like this: 
  8313.  
  8314. void main(){
  8315.      Addint ai(3,4);
  8316.      cout << "The sum is: " << ai.result() << endl;
  8317.      thistask->resultis(0);
  8318. }
  8319.  
  8320. This program produces the following output: 
  8321.  
  8322. The sum is: 7
  8323.  
  8324.  
  8325. ΓòÉΓòÉΓòÉ 5.2.3.5. The Default Stack Size ΓòÉΓòÉΓòÉ
  8326.  
  8327. The default maximum stack size (in words) for tasks is defined as SIZE in the 
  8328. task.h header file. If you do not specify a maximum stack size when you call 
  8329. the task constructor, the maximum stack size will be set to this value. If you 
  8330. are creating a task that will need to put a large amount of data on the stack, 
  8331. you may need to use a maximum stack size that is bigger than the default. 
  8332.  
  8333. In the following example, the class BigStack is derived from the class task. 
  8334. The BigStack constructor specifies that the maximum stack size for objects of 
  8335. BigStack is bigger than SIZE: 
  8336.  
  8337. #include <task.h>
  8338. #define BIGSIZE SIZE + 256
  8339.  
  8340. class BigStack : public task {
  8341. public:
  8342.      BigStack();
  8343. };
  8344.  
  8345. //
  8346. // request a stack that has a size bigger than the default
  8347. BigStack::BigStack() : task("Big",DEDICATED,BIGSIZE)
  8348. {
  8349.      resultis(1);
  8350. }
  8351.  
  8352. void main() {
  8353.      BigStack bs;
  8354.      thistask->resultis(0);
  8355. }
  8356.  
  8357.  
  8358. ΓòÉΓòÉΓòÉ 5.2.3.6. Tasks Must Explicitly Call Destructors for Objects They Create ΓòÉΓòÉΓòÉ
  8359.  
  8360. Usually, the destructors for all of the objects that were created on the stack 
  8361. within a block are automatically called when that block is exited. When a task 
  8362. calls task::resultis() however, it does not return, and the destructors for 
  8363. objects that were created by that task are not called automatically. Thus, 
  8364. before a task calls task::resultis(), it should call the destructors for all of 
  8365. the objects that have destructors and were created by the task on its stack. 
  8366.  
  8367. In the following example, the dedicated task mtask creates an object r1 on its 
  8368. local stack. The constructor for mtask must explicitly call the destructor for 
  8369. the object r1 to remove it from the run chain. 
  8370.  
  8371. class Resource
  8372.  
  8373. {
  8374. public:
  8375.      Resource();     // get resource
  8376.      ~Resource();    // give up resource
  8377. };
  8378. //     ...
  8379. class MyTask : public task {
  8380. public:
  8381.      MyTask();
  8382. };
  8383.  
  8384. MyTask::MyTask() {
  8385.      Resource r1;
  8386. //     ...
  8387.      r1.Resource::~Resource();
  8388.      resultis(0);
  8389. }
  8390.  
  8391. void main() {
  8392.      MyTask mtask;
  8393. //     ...
  8394. }
  8395.  
  8396.  
  8397. ΓòÉΓòÉΓòÉ 5.2.4. Shared and Dedicated Tasks ΓòÉΓòÉΓòÉ
  8398.  
  8399. When a task creates another task, it can create it as a shared task or as a 
  8400. dedicated task. You specify whether a task is shared or dedicated in the second 
  8401. argument to the protected constructor of the class task. See Constructor for 
  8402. task for a description of the task class constructor. 
  8403.  
  8404. Each dedicated task has its own stack that is dynamically allocated when the 
  8405. task is created. A shared task, on the other hand, shares the memory space for 
  8406. its stack with its parent. 
  8407.  
  8408. The following topics are described in this section: 
  8409.  
  8410. o Advantages of dedicated tasks 
  8411. o Advantages of shared tasks 
  8412. o Shared child tasks should be dynamically allocated 
  8413. o Passing arguments to shared child tasks 
  8414.  
  8415.  
  8416. ΓòÉΓòÉΓòÉ 5.2.4.1. Advantages of Dedicated Tasks ΓòÉΓòÉΓòÉ
  8417.  
  8418. The advantage of using dedicated tasks is that it takes less time to switch 
  8419. between dedicated tasks than it takes to switch between shared tasks. Suppose 
  8420. that there are two tasks, task1 and task2, that share the same stack, and 
  8421. suppose that you want to switch from task1 to task2. Space must be allocated 
  8422. and the contents of the shared stack (that is, the contents of the stack of 
  8423. task1) must be copied into this space. This space is called the dynamic save 
  8424. area of task1. task2 then copies the contents of its stack (currently saved in 
  8425. its dynamic save area) into the shared stack. If you want to switch back to 
  8426. task1, the contents of the shared stack must be copied into the dynamic save 
  8427. area of task2, and the contents of the stack of task1 must be copied from the 
  8428. dynamic save area of task1 into the shared stack. 
  8429.  
  8430. When you switch between two dedicated tasks, the stacks do not have to be saved 
  8431. and restored because both tasks have their own separate stack spaces. The 
  8432. result is that it is faster to switch between dedicated tasks. 
  8433.  
  8434.  
  8435. ΓòÉΓòÉΓòÉ 5.2.4.2. Advantages of Shared Tasks ΓòÉΓòÉΓòÉ
  8436.  
  8437. The advantage of using shared tasks is that you can save some memory. Each time 
  8438. that a dedicated task is created, a set amount of memory is allocated for its 
  8439. stack. Even though memory must be allocated for the dynamic save area before 
  8440. you switch between shared tasks, the system only needs to allocate the memory 
  8441. that is needed to save the portion of the stack that the shared task is using. 
  8442. This amount of memory may be considerably less than the set amount of memory 
  8443. that must be allocated for a dedicated task. 
  8444.  
  8445. Note:  The following two sections describe some restrictions that are placed on 
  8446. the use of shared tasks. If you do not adhere to these restrictions when you 
  8447. use shared tasks, the results will be unpredictable. Because these restrictions 
  8448. do not affect dedicated tasks, you should use dedicated tasks whenever 
  8449. possible. 
  8450.  
  8451.  
  8452. ΓòÉΓòÉΓòÉ 5.2.4.3. Shared Child Tasks Should Be Dynamically Allocated ΓòÉΓòÉΓòÉ
  8453.  
  8454. If a parent task creates a child that is a shared task, the parent should 
  8455. dynamically allocate the child task. The following example shows what can 
  8456. happen when child tasks are not dynamically allocated. In the following 
  8457. program, the child tasks are dynamically allocated if the macro WRONG is not 
  8458. defined, and they are not dynamically allocated if WRONG is defined. 
  8459.  
  8460. #include <task.h>
  8461. #include <iostream.h>
  8462.  
  8463. class Semaphore: public object {
  8464.      int signal;
  8465. public:
  8466.      Semaphore(int s) : signal(s) {}
  8467.      int pending() { return signal <= 0; }
  8468.      void p();
  8469.      void v();
  8470. };
  8471.  
  8472. void Semaphore::p(){
  8473.      while (1)
  8474.      {
  8475.           if (--signal >= 0)
  8476.                return;
  8477.           signal++;
  8478.           thistask->sleep(this);
  8479.      }
  8480. }
  8481.  
  8482. void Semaphore::v(){
  8483.      if (signal++ == 0)
  8484.           alert();
  8485. }
  8486.  
  8487. class Child : public task {
  8488. public:
  8489.      Child(char *name, Semaphore &);
  8490. };
  8491.  
  8492. Child::Child(char *name, Semaphore &s) : task("Child", SHARED){
  8493.      cout << "Child " << name <<" entered." << endl;
  8494.      s.p();
  8495.      cout << "Child " << name <<" after p()." << endl;
  8496.      resultis(0);
  8497. }
  8498.  
  8499. class Parent : public task {
  8500. public:
  8501.      Parent(Semaphore &);
  8502. };
  8503.  
  8504.  
  8505. Parent::Parent(Semaphore &s) : task("Parent", SHARED){
  8506.      #ifdef WRONG
  8507.           Child c1("c1",s);
  8508.           Child c2("c2",s);
  8509.      #else
  8510.           Child &c1 = *new Child("c1",s);
  8511.           Child &c2 = *new Child("c2",s);
  8512.      #endif
  8513.      cout << "Parent task after creating c1 and c2." << endl;
  8514.      c1.result();
  8515.      c2.result();
  8516.      cout << "Parent task after waiting "
  8517.           << "for c1 and c2 to terminate." << endl;
  8518.      resultis(0);
  8519. }
  8520.  
  8521. void main(){
  8522.      #ifdef WRONG
  8523.           Semaphore s(0);
  8524.           Parent p1(s);
  8525.      #else
  8526.           Semaphore &s = *new Semaphore(0);
  8527.           Parent &p1 = *new Parent(s);
  8528.      #endif
  8529.      cout << "Main task after creating parent task." << endl;
  8530.      s.v();
  8531.      s.v();
  8532.      cout << "Main task after signalling semaphores." << endl;
  8533.      p1.result();
  8534.      cout << "Main task after parent task has terminated."
  8535.           << endl;
  8536.      thistask->resultis(0);
  8537. }
  8538.  
  8539. If WRONG is not defined, the following objects are dynamically allocated: 
  8540.  
  8541. o c1 and c2, the Child objects that are created in the Parent constructor. 
  8542. o s, the Semaphore object that is created in main(). 
  8543. o p1, the Parent object that is created in main(). 
  8544.  
  8545. Because these are the only child tasks in the program, all of the child tasks 
  8546. are dynamically allocated. The program produces the following output if WRONG 
  8547. is not defined: 
  8548.  
  8549. Child c1 entered.
  8550. Main task after creating parent task.
  8551. Main task after signalling semaphores.
  8552. Child c2 entered.
  8553. Child c2 after p().
  8554. Child c1 after p().
  8555. Parent task after creating c1 and c2.
  8556. Parent task after waiting for c1 and c2 to terminate.
  8557. Main task after parent task has terminated.
  8558.  
  8559. However, if WRONG is defined, none of c1, c2, s, and p1 is dynamically 
  8560. allocated. If you include the following statement: 
  8561.  
  8562. #define WRONG
  8563. after the include statement for iostream.h, the program fails. The following 
  8564. output is produced: 
  8565.  
  8566. Child c1 entered.
  8567. Main task after creating parent task.
  8568. Main task after signalling semaphores.
  8569. SYS1808:
  8570. The process has stopped.  The software diagnostic
  8571. code (exception code) is  0005.
  8572.  
  8573.  
  8574. ΓòÉΓòÉΓòÉ 5.2.4.4. Passing Arguments to Shared Child Tasks ΓòÉΓòÉΓòÉ
  8575.  
  8576. If you want to pass a reference or a pointer to an object from a parent task to 
  8577. a shared child task, you should ensure that the object that you are passing is 
  8578. either dynamically allocated or declared in global scope. 
  8579.  
  8580. An argument that is either a reference or a pointer to an object that is 
  8581. created on the local stack of the parent task will not be passed properly. When 
  8582. a shared child task is created, its stack uses the same space as the local 
  8583. stack of the parent task. If you pass the child a pointer to a location in the 
  8584. parent's stack, this pointer will not be valid in the scope of the child task 
  8585. because it now points to a location occupied by the child's stack. Note that 
  8586. the parent and child tasks are sharing the same stack space, and thus the 
  8587. current contents of the stack space depend on which task has control of the 
  8588. stack. In the following example, q1 is not a valid argument to pass to the 
  8589. child task, but q2 and q3 are valid arguments: 
  8590.  
  8591. #include <task.h>
  8592.  
  8593. qtail q3;          // argument with global scope
  8594.  
  8595. class Parent : public task {
  8596.      public:
  8597.           Parent();
  8598. };
  8599.  
  8600. class Child : public task {
  8601. public:
  8602.      Child(qtail*);
  8603. };
  8604.  
  8605. Child::Child(qtail* qt) : task("Child",SHARED)
  8606. {
  8607.      qt->print(0);
  8608.      resultis(0);
  8609. }
  8610.  
  8611.  
  8612. Parent::Parent() {
  8613.      qtail q1;                   // object declared on local
  8614.                                  // stack of parent
  8615.                                  //
  8616.      Child *c1 = new Child(&q1); // incorrect - argument was
  8617.                                  // created on local stack
  8618.                                  // of parent task
  8619.                                  //
  8620.      qtail *q2 = new qtail;      // dynamically allocated
  8621.                                  // object
  8622.                                  //
  8623.      Child *c2 = new Child(q2);  // correct - argument
  8624.                                  // dynamically allocated
  8625.                                  //
  8626.      Child *c3 = new Child(&q3); // correct - argument
  8627.                                  // declared in global scope
  8628.      resultis(0);
  8629. };
  8630.  
  8631. void main() {
  8632.      Parent pp;
  8633.      thistask->resultis(0);
  8634. }
  8635.  
  8636.  
  8637. ΓòÉΓòÉΓòÉ 5.2.5. Declarations for the Task Handling Classes in task.h ΓòÉΓòÉΓòÉ
  8638.  
  8639. You must include the following statement in any file that uses the task 
  8640. handling classes: 
  8641.  
  8642. #include <task.h>
  8643.  
  8644. The following is an excerpt from the task.h header file that show the relevant 
  8645. declarations for the members of the task handling classes. 
  8646.  
  8647. typedef int (*PFIO) (int,object*);
  8648. typedef void (*PFV) ();
  8649. typedef void SIG_FUNC_TYP(int);
  8650. typedef void (*SIG_PF)(int);
  8651.  
  8652. #define SIZE 1024
  8653. #define thistask (object::this_task())
  8654. #define run_chain (sched::get_run_chain());
  8655. #define task_chain (task::get_task_chain())
  8656.  
  8657. extern _hwm;
  8658.  
  8659. class object{
  8660. friend class sched;
  8661. friend class task;
  8662. private:
  8663.      static sched*     runchain;
  8664. public:
  8665.      enum objtype     { OBJECT, TIMER, TASK, QHEAD,
  8666.                          QTAIL, INTHANDLER };
  8667.      object*          o_next;
  8668.                       object();
  8669.      virtual          ~object();
  8670.      virtual objtype  o_type();
  8671.      void             remember(task*);
  8672.      void             forget(task*);
  8673.      void             alert();
  8674.      virtual int      pending();
  8675.      virtual void     print(int, int =0);
  8676.      static int       task_error(int, object*);
  8677.      int              task_error(int);
  8678.      static task*     this_task();
  8679.      static PFIO      error_fct;
  8680. };
  8681.  
  8682. class sched : public object {
  8683. friend class timer;
  8684. friend class task;
  8685. friend class object;
  8686. friend void _print_error(int n);
  8687. friend SIG_FUNC_TYP sigFunc;
  8688. public:
  8689.      enum statetype { IDLE, RUNNING, TERMINATED };
  8690.      static void      setclock(long);
  8691.      static long      get_clock();
  8692.      static sched*    get_run_chain();
  8693.      static int       get_exit_status();
  8694.      static void      set_exit_status( int i );
  8695.      sched*           get_priority_sched();
  8696.      static  task*    clock_task;
  8697.      long             rdtime();
  8698.      statetype        rdstate();
  8699.      int              pending();
  8700.      int              keep_waiting();
  8701.      int              dont_wait();
  8702.      void             cancel(int);
  8703.      int              result();
  8704.      virtual void     setwho(object* t);
  8705.      void             print(int, int =0);
  8706.      static PFV       exit_fct;
  8707. };
  8708.  
  8709. class timer : public sched {
  8710.      void     resume();
  8711. public:
  8712.                timer(long);
  8713.                ~timer();
  8714.      void             reset(long);
  8715.      object::objtype  o_type();
  8716.      void             setwho(object*);
  8717.      void             print(int, int =0);
  8718. };
  8719.  
  8720. class task : public sched {
  8721. friend class sched;
  8722. private:
  8723.      int*          t_framep;
  8724.      int*          t_basep;
  8725.      object*       t_alert;
  8726. protected:
  8727.      task(char* name=0, modetype mode=DEFAULT_MODE,
  8728.                int stacksize=SIZE);:
  8729. public:
  8730.      enum modetype { DEDICATED, SHARED };
  8731.                      ~task();
  8732.      object::objtype o_type();
  8733.      task*           t_next;
  8734.      char*           t_name;
  8735.      static task*    get_task_chain();
  8736.      int             waitvec(object**);
  8737.      int             waitlist(object* ...);
  8738.      void            wait(object* ob);
  8739.      void            delay(long);
  8740.      long            preempt();
  8741.      void            sleep(object* t =0);
  8742.      void            resultis(int);
  8743.      void            cancel(int);
  8744.      void            setwho(object* t);
  8745.      void            print(int, int =0);
  8746.      object*         who_alerted_me();
  8747. };
  8748.  
  8749. inline void          setclock(long i);
  8750. void                 _print_error(int);
  8751.  
  8752.    typedef int (*PFIO)(int,object*);
  8753.    typedef void (*PFV)();
  8754.    typedef void SIG_FUNC_TYP(int);
  8755.    typedef void (*SIG_PF)(int);
  8756.  
  8757.    #define SIZE 1024
  8758.    #define thistask (object::this_task())
  8759.    #define run_chain (sched::get_run_chain())
  8760.    #define task_chain (task::get_task_chain())
  8761.  
  8762.    extern _hwm;
  8763.  
  8764.    class object
  8765.    {
  8766.    friend class sched;
  8767.    friend class task;
  8768.    private:
  8769.            static task*    thxstxsk;
  8770.    public:
  8771.         enum objtype { OBJECT, TIMER, TASK, QHEAD,
  8772.                        QTAIL, INTHANDLER };
  8773.            object* o_next;
  8774.                    object();
  8775.            virtual ~object();
  8776.            virtual objtype o_type();
  8777.  
  8778.            void    remember(task*);
  8779.            void    forget(task*);
  8780.            void    alert();
  8781.            virtual int     pending();
  8782.            virtual void    print(int, int =0);
  8783.            static int      task_error(int, object*);
  8784.            int     task_error(int);
  8785.            static task*    this_task();
  8786.            static  PFIO    error_fct;
  8787.    };
  8788.  
  8789.    class sched : public object {
  8790.    friend class timer;
  8791.    friend class task;
  8792.    friend class object;
  8793.    friend void _print_error(int n);
  8794.    friend SIG_FUNC_TYP sigFunc;
  8795.    public:
  8796.            enum statetype { IDLE=1, RUNNING=2, TERMINATED=4 };
  8797.            static void     setclock(long);
  8798.            static long     get_clock();
  8799.            static sched*   get_run_chain();
  8800.            static int      get_exit_status();
  8801.            static void     set_exit_status( int i );
  8802.            sched*  get_priority_sched();
  8803.            static  task*   clock_task;
  8804.            long    rdtime();
  8805.            statetype       rdstate();
  8806.            int     pending();
  8807.            int     keep_waiting();
  8808.            int     dont_wait();
  8809.            void    cancel(int);
  8810.            int     result();
  8811.            virtual void    setwho(object* t);
  8812.            void    print(int, int =0);
  8813.            static  PFV     exit_fct;
  8814.    };
  8815.  
  8816.    class timer : public sched {
  8817.            void    resume();
  8818.    public:
  8819.                    timer(long);
  8820.                    ~timer();
  8821.            void    reset(long);
  8822.            object::objtype o_type();
  8823.            void    setwho(object*);
  8824.            void    print(int, int =0);
  8825.    };
  8826.  
  8827.    class task : public sched {
  8828.    friend class sched;
  8829.    private:
  8830.            int*    t_framep;
  8831.            int*    t_basep;
  8832.            object* t_alert;
  8833.    protected:
  8834.                    task(char* name=0, modetype mode=DEFAULT_MODE,
  8835.                         int stacksize=SIZE);
  8836.    public:
  8837.            enum    modetype { DEDICATED, SHARED };
  8838.                    ~task();
  8839.            object::objtype o_type();
  8840.            task*   t_next;
  8841.            char*   t_name;
  8842.            static task*    get_task_chain();
  8843.            int     waitvec(object**);
  8844.            int     waitlist(object* ...);
  8845.            void    wait(object* ob);
  8846.            void    delay(long);
  8847.            long    preempt();
  8848.            void    sleep(object* t =0);
  8849.            void    resultis(int);
  8850.            void    cancel(int);
  8851.            void    setwho(object* t);
  8852.            void    print(int, int =0);
  8853.            object* who_alerted_me();
  8854.    };
  8855.    inline void     setclock(long i) { sched::setclock(i); }
  8856.    void            _print_error(int);
  8857.  
  8858.  
  8859. ΓòÉΓòÉΓòÉ 5.2.6. Public Members of object ΓòÉΓòÉΓòÉ
  8860.  
  8861. Each object of the class object has a list of tasks called a remember chain. 
  8862. Tasks that are on the remember chain have a state of IDLE. These tasks will 
  8863. have their state changed from IDLE to RUNNING and be moved to the run chain 
  8864. when the state of the object changes from pending to ready. 
  8865.  
  8866. For every object of the task, timer, qhead, and qtail classes, a call to 
  8867. object::alert() is automatically made when the state of the object changes from 
  8868. pending to ready. If you derive a class from object, you must ensure that 
  8869. object::alert() is explicitly called by objects of that class for the tasks 
  8870. that are on their remember chains. 
  8871.  
  8872. Each object also has a pointer to a list of other objects. This pointer is used 
  8873. to chain together objects that are placed on a queue and task objects on the 
  8874. run chain of the sched object. 
  8875.  
  8876. Note:  The following descriptions assume that the functions are called as part 
  8877. of an object called obj. 
  8878.  
  8879. The following functions are described: 
  8880.  
  8881. o Constructor for object 
  8882. o alert - change state of tasks 
  8883. o error_fct - user-defined error-handling function 
  8884. o forget - remove task from remember chain 
  8885. o o_type - return class object type 
  8886. o pending - return object state 
  8887. o print - print contents of object 
  8888. o remember - add task to remember chain 
  8889. o task_error - error-handling 
  8890. o this_task - return pointer to current task 
  8891.  
  8892.  
  8893. ΓòÉΓòÉΓòÉ 5.2.6.1. Constructor for object ΓòÉΓòÉΓòÉ
  8894.  
  8895. Class: object 
  8896.  
  8897. object();
  8898.  
  8899. The object() constructor constructs an object that is not in any list of 
  8900. objects. 
  8901.  
  8902.  
  8903. ΓòÉΓòÉΓòÉ 5.2.6.2. alert - Change State of Tasks ΓòÉΓòÉΓòÉ
  8904.  
  8905. Class: object 
  8906.  
  8907. void alert();
  8908.  
  8909. alert() changes the state of all of the tasks on the remember chain for obj 
  8910. from IDLE to RUNNING. 
  8911.  
  8912.  
  8913. ΓòÉΓòÉΓòÉ 5.2.6.3. error_fct - User-Defined Error-Handling Function ΓòÉΓòÉΓòÉ
  8914.  
  8915. Class: object 
  8916.  
  8917. typedef int (*PFIO) (int, object*);
  8918. static PFIO error_fct;
  8919.  
  8920. When an error occurs, task_error() calls the function pointed to by error_fct 
  8921. with two arguments: the error number, and a pointer to the object that caused 
  8922. the error. To assign the address of a function to error_fct, use an expression 
  8923. similar to the following: 
  8924.  
  8925. error_fct = e_function;
  8926.  
  8927. where e_function is a user-defined error-handling function that has the 
  8928. following format: 
  8929.  
  8930. int e_function(int errno, object* obj);
  8931.  
  8932. The first argument, errno, is the error number, and obj is either 0 or a 
  8933. pointer to the object that called task_error(). If error_fct is set to point to 
  8934. such a function, task_error() calls this function with the error number and a 
  8935. pointer to the object as arguments. The pointer to the object is equal to 0 if 
  8936. task_error() was not called by an object. If the user-defined error function 
  8937. does not return 0, task_error calls exit() with an argument equal to errno. If 
  8938. the user-defined error function returns 0, task_error() retries the operation 
  8939. that caused the error. 
  8940.  
  8941.  
  8942. ΓòÉΓòÉΓòÉ 5.2.6.4. forget - Remove Task from Remember Chain ΓòÉΓòÉΓòÉ
  8943.  
  8944. Class: object 
  8945.  
  8946. void forget(task* tsk);
  8947.  
  8948. forget() removes the task object pointed to by tsk from the remember chain for 
  8949. obj. 
  8950.  
  8951.  
  8952. ΓòÉΓòÉΓòÉ 5.2.6.5. o_type - Return Class Object Type ΓòÉΓòÉΓòÉ
  8953.  
  8954. Class: object 
  8955.  
  8956. virtual objtype o_type();
  8957.  
  8958. o_type() returns the class type of obj, namely object::OBJECT. 
  8959.  
  8960.  
  8961. ΓòÉΓòÉΓòÉ 5.2.6.6. pending - Return Object State ΓòÉΓòÉΓòÉ
  8962.  
  8963. Class: object 
  8964.  
  8965. virtual int pending();
  8966.  
  8967. pending() is a virtual function that is meant to return 0 if obj is ready and a 
  8968. nonzero value if obj is pending. The default definition of pending() always 
  8969. returns 1. If you want to define a class that is derived from object, and if 
  8970. you want to be able to wait for objects of that class, you must explicitly 
  8971. define pending() for that class. 
  8972.  
  8973.  
  8974. ΓòÉΓòÉΓòÉ 5.2.6.7. print - Print Contents of Object ΓòÉΓòÉΓòÉ
  8975.  
  8976. Class: object 
  8977.  
  8978. virtual void print(int x, int y=0);
  8979.  
  8980. print() prints information about obj on standard output. object::print() is 
  8981. called by the print() member functions of the classes derived from object. 
  8982.  
  8983. The argument x specifies the amount of information to be printed. The argument 
  8984. y is used internally by the Task Library. Its default value is 0. print() 
  8985. prints the type of the object if x equals 0. If x equals VERBOSE, print() 
  8986. prints the type of the object along with the value of the this pointer and the 
  8987. list of tasks on the remember chain for this object. 
  8988.  
  8989.  
  8990. ΓòÉΓòÉΓòÉ 5.2.6.8. remember - Add Task to Remember Chain ΓòÉΓòÉΓòÉ
  8991.  
  8992. Class: object 
  8993.  
  8994. void remember(task* tk);
  8995.  
  8996. remember() adds the task pointed to by tk to the remember chain for obj. Tasks 
  8997. that are on the remember chain will be alerted when the state of obj becomes 
  8998. ready. 
  8999.  
  9000.  
  9001. ΓòÉΓòÉΓòÉ 5.2.6.9. task_error - Error-Handling ΓòÉΓòÉΓòÉ
  9002.  
  9003. Class: object 
  9004.  
  9005. static int task_error(int errno, object* op);
  9006. int task_error(int errno);
  9007.  
  9008. task_error() is called by the task system when a runtime error occurs. errno is 
  9009. the error number. op is a pointer to the object that called task_error(). If 
  9010. the member error_fct points to a function, task_error() calls that function 
  9011. with errno and op as arguments Otherwise, print_error() is called with the 
  9012. argument errno. print_error() prints an error message on stderr and terminates 
  9013. the program. 
  9014.  
  9015. The nonstatic, single-argument form of task_error() is obsolete, but it is kept 
  9016. for compatibility with the AT&T** C++ Language System Release 1.2 Task Library. 
  9017.  
  9018. The runtime errors are listed in Task Library Error Messages. These errors can 
  9019. be divided into two groups: 
  9020.  
  9021. o Recoverable: the condition that caused the error should be corrected by the 
  9022.   program in the function pointed to by error_fct. error_fct should return 0, 
  9023.   and thus the function that caused the error can be tried again. 
  9024.  
  9025. o Unrecoverable: the condition that caused the error cannot be corrected by the 
  9026.   program. The function that you define for error_fct must not return 0 for 
  9027.   unrecoverable errors. If this function does return 0 for unrecoverable 
  9028.   errors, the results will be unpredictable. 
  9029.  
  9030. The following runtime errors are recoverable: 
  9031.  
  9032. E_BACKOBJ         E_FUNCS
  9033. E_BACKFULL        E_GETEMPTY
  9034. E_FRAMES          E_PUTOBJ
  9035. E_FUDGE_SIZE      E_PUTFULL
  9036.  
  9037. The following runtime errors are unrecoverable: 
  9038.  
  9039. E_BADMODE         E_QDEL
  9040. E_BADSIG          E_QZERO
  9041. E_CLOCKIDLE       E_REGMASK
  9042. E_HISTO           E_RESOBJ
  9043. E_LOSTHNDLR       E_RESRUN
  9044. E_NEGTIME         E_RESTERM
  9045. E_NO_HNDLR        E_RESULT
  9046. E_OLINK           E_RETURN
  9047. E_ONEXT           E_SCHOBJ
  9048.  
  9049.  
  9050. ΓòÉΓòÉΓòÉ 5.2.6.10. this_task - Return Pointer to Current Task ΓòÉΓòÉΓòÉ
  9051.  
  9052. Class: object 
  9053.  
  9054. static task* this_task();
  9055. #define thistask object::this_task()
  9056.  
  9057. this_task() returns a pointer to the task that is currently running. 
  9058.  
  9059. The macro form, thistask, is kept for compatibility with the AT&T** C++ 
  9060. Language System Release 1.2 Task Library. 
  9061.  
  9062.  
  9063. ΓòÉΓòÉΓòÉ 5.2.7. Public Members of sched ΓòÉΓòÉΓòÉ
  9064.  
  9065. The constructor for the sched class is declared protected, and thus you cannot 
  9066. create your own objects of the class sched. The sched class is important for 
  9067. the Task Library because it: 
  9068.  
  9069. o Provides common functions for task and timer, the classes that are derived 
  9070.   from sched. 
  9071.  
  9072. o Provides functions for measuring simulated time. 
  9073.  
  9074. o Implements the scheduler, the construct of the Task Library that determines 
  9075.   the task that is supposed to run next. 
  9076.  
  9077. The sched class includes a static data member called runchain. This is a list 
  9078. of all the tasks that are ready to run. 
  9079.  
  9080. Note:  The following descriptions assume that the functions are called as part 
  9081. of schd, an object of a class derived from sched. 
  9082.  
  9083. The following functions are described: 
  9084.  
  9085. o cancel - change state to TERMINATED 
  9086. o clock_task - system clock task variable 
  9087. o dont_wait - return number of objects waiting for external events 
  9088. o exit_fct - user defined exit function 
  9089. o get_clock - return value of the task system clock 
  9090. o get_exit_status - return exit status of task program 
  9091. o get_priority_sched - return pointer to interrupt_alerter 
  9092. o get_run_chain - return pointer to run chain 
  9093. o keep_waiting - keep scheduler waiting 
  9094. o pending - return state 
  9095. o print - print the contents of sched object 
  9096. o rdstate - return the state of sched object 
  9097. o rdtime - return clock time 
  9098. o result - return result of sched object 
  9099. o setclock - initialize the system clock 
  9100. o setwho - record alerting object 
  9101. o set_exit_status - set exit status of task 
  9102.  
  9103.  
  9104. ΓòÉΓòÉΓòÉ 5.2.7.1. cancel - Change State to TERMINATED ΓòÉΓòÉΓòÉ
  9105.  
  9106. Class: sched 
  9107.  
  9108. void cancel(int ret_val);
  9109.  
  9110. cancel() changes the state of schd to TERMINATED without invoking the scheduler 
  9111. and sets the result of schd to ret_val. Because the scheduler is not invoked, a 
  9112. task object should not call cancel() on itself. 
  9113.  
  9114.  
  9115. ΓòÉΓòÉΓòÉ 5.2.7.2. clock_task - System Clock Task Variable ΓòÉΓòÉΓòÉ
  9116.  
  9117. Class: sched 
  9118.  
  9119. static task* clock_task;
  9120.  
  9121. clock_task is a pointer to the task that is scheduled each time the system 
  9122. clock advances, before any other tasks. The state of the task pointed to by 
  9123. clock_task must be IDLE when it is scheduled. The task that is pointed to by 
  9124. clock_task can ensure that it will have a state of IDLE by calling 
  9125. task::sleep() on itself. 
  9126.  
  9127.  
  9128. ΓòÉΓòÉΓòÉ 5.2.7.3. dont_wait - Return Number of Objects Waiting for External Events ΓòÉΓòÉΓòÉ
  9129.  
  9130. Class: sched 
  9131.  
  9132. int dont_wait();
  9133.  
  9134. dont_wait() returns the number of times that keep_waiting() has been called, 
  9135. minus the number of times that dont_wait() has been called (excluding the 
  9136. current call). The return value should equal the number of objects that were 
  9137. waiting for external events before the current call. 
  9138.  
  9139.  
  9140. ΓòÉΓòÉΓòÉ 5.2.7.4. exit_fct - User Defined Exit Function ΓòÉΓòÉΓòÉ
  9141.  
  9142. Class: sched 
  9143.  
  9144. typedef void (*PFV) ();
  9145. static PFV exit_fct;
  9146.  
  9147. exit_fct is a pointer to a user-defined exit function that has the following 
  9148. format: 
  9149.  
  9150. void e_function();
  9151.  
  9152. If exit_fct is set to point to such a function, the task system scheduler will 
  9153. call it before the program exits. To assign the address of a function to 
  9154. exit_fct, use an expression similar to the following: 
  9155.  
  9156. exit_fct = e_function;
  9157.  
  9158. where e_function is a user-defined exit function with a return type of void and 
  9159. no arguments. 
  9160.  
  9161.  
  9162. ΓòÉΓòÉΓòÉ 5.2.7.5. get_clock - Return Value of the Task System Clock ΓòÉΓòÉΓòÉ
  9163.  
  9164. Class: sched 
  9165.  
  9166. static long get_clock();
  9167.  
  9168. get_clock() returns the value of the task system clock. 
  9169.  
  9170.  
  9171. ΓòÉΓòÉΓòÉ 5.2.7.6. get_exit_status - Return Exit Status of Task Program ΓòÉΓòÉΓòÉ
  9172.  
  9173. Class: sched 
  9174.  
  9175. static int get_exit_status();
  9176.  
  9177. get_exit_status() returns the exit status of the task program. When a task 
  9178. program terminates normally (task_error() is not called), the program will call 
  9179. exit() with the argument i, where i is the value of the argument in the last 
  9180. call of set_exit_status(). 
  9181.  
  9182.  
  9183. ΓòÉΓòÉΓòÉ 5.2.7.7. get_priority_sched - Return Pointer to interrupt_alerter ΓòÉΓòÉΓòÉ
  9184.  
  9185. Class: sched 
  9186.  
  9187. sched* get_priority_sched();
  9188.  
  9189. get_priority_sched() returns a pointer to the system task interrupt_alerter if 
  9190. the following two conditions are true: 
  9191.  
  9192. o A signal occurred that an Interrupt_handler object was waiting for. 
  9193. o The Interrupt_handler object has not been scheduled yet. 
  9194.  
  9195. get_priority_sched() returns 0 otherwise. 
  9196.  
  9197.  
  9198. ΓòÉΓòÉΓòÉ 5.2.7.8. get_run_chain - Return Pointer to Run Chain ΓòÉΓòÉΓòÉ
  9199.  
  9200. Class: sched 
  9201.  
  9202. #define run_chain (sched::get_run_chain())
  9203.  
  9204. private:
  9205.      static sched* runchain;
  9206. public:
  9207.      static sched* get_run_chain();
  9208.  
  9209. get_run_chain() returns a pointer to the static sched member runchain. runchain 
  9210. is a linked list of task and timer objects that are ready to run. The macro 
  9211. definition run_chain is obsolete, but it is included for compatibility with the 
  9212. AT&T** C++ Language System Release 1.2. 
  9213.  
  9214.  
  9215. ΓòÉΓòÉΓòÉ 5.2.7.9. keep_waiting - Keep Scheduler Waiting ΓòÉΓòÉΓòÉ
  9216.  
  9217. Class: sched 
  9218.  
  9219. int keep_waiting();
  9220.  
  9221. keep_waiting() returns the number of times keep_waiting() has been called (not 
  9222. counting the current call) minus the number of times that dont_wait() has been 
  9223. called. keep_waiting() is meant to be called when an object that will wait for 
  9224. an external event is created. For example, the Interrupt_handler constructor 
  9225. calls keep_waiting() when it creates an Interrupt_handler object. dont_wait() 
  9226. should be called when such an object is deleted. 
  9227.  
  9228. keep_waiting() prevents the scheduler from exiting when there are no tasks that 
  9229. can run. In such a situation, an external event could change the state of a 
  9230. task from IDLE, and then the scheduler would have to act. 
  9231.  
  9232.  
  9233. ΓòÉΓòÉΓòÉ 5.2.7.10. pending - Return State ΓòÉΓòÉΓòÉ
  9234.  
  9235. Class: sched 
  9236.  
  9237. int pending();
  9238.  
  9239. pending() returns 0 if the state of schd equals TERMINATED. Otherwise, 
  9240. pending() returns a nonzero value. 
  9241.  
  9242.  
  9243. ΓòÉΓòÉΓòÉ 5.2.7.11. print - Print the Contents of sched Object ΓòÉΓòÉΓòÉ
  9244.  
  9245. Class: sched 
  9246.  
  9247. void print(int x, int y=0);
  9248.  
  9249. print() prints information about schd on standard output. For more details on 
  9250. print(), see the description of task::print() in print - Print Task 
  9251. Information. 
  9252.  
  9253.  
  9254. ΓòÉΓòÉΓòÉ 5.2.7.12. rdstate - Return the State of sched Object ΓòÉΓòÉΓòÉ
  9255.  
  9256. Class: sched 
  9257.  
  9258. statetype rdstate();
  9259.  
  9260. rdstate() returns the state of schd. The state can be RUNNING, IDLE, or 
  9261. TERMINATED. 
  9262.  
  9263.  
  9264. ΓòÉΓòÉΓòÉ 5.2.7.13. rdtime - Return Clock Time ΓòÉΓòÉΓòÉ
  9265.  
  9266. Class: sched 
  9267.  
  9268. long rdtime();
  9269.  
  9270. rdtime() returns the clock time at which schd is supposed to run. If the state 
  9271. of the task for which rdtime() is called is either TERMINATED or IDLE, the 
  9272. value returned by rdtime() is undefined. If rdtime() is called for a timer 
  9273. task, rdtime() returns the time at which the timer task will terminate. 
  9274.  
  9275. rdtime() is meant to be used in conjunction with the function task::delay() to 
  9276. determine when a task will stop delaying. 
  9277.  
  9278.  
  9279. ΓòÉΓòÉΓòÉ 5.2.7.14. result - Return Result of sched Object ΓòÉΓòÉΓòÉ
  9280.  
  9281. Class: sched 
  9282.  
  9283. int result();
  9284.  
  9285. result() returns the result of schd. If the state of schd is not TERMINATED, 
  9286. the calling task will be suspended to wait for schd to terminate. A task that 
  9287. calls result() for itself will cause an E_RESULT runtime error. 
  9288.  
  9289.  
  9290. ΓòÉΓòÉΓòÉ 5.2.7.15. setclock - Initialize the System Clock ΓòÉΓòÉΓòÉ
  9291.  
  9292. Class: sched 
  9293.  
  9294. static void setclock(long init_time);
  9295.  
  9296. setclock() is a static function that initializes the system clock to the time 
  9297. init_time. You will get an E_SETCLOCK runtime error if you call setclock() more 
  9298. than once in a program. 
  9299.  
  9300.  
  9301. ΓòÉΓòÉΓòÉ 5.2.7.16. setwho - Record Alerting Object ΓòÉΓòÉΓòÉ
  9302.  
  9303. Class: sched 
  9304.  
  9305. virtual void setwho(object* alt_obj);
  9306.  
  9307. setwho() is a virtual function that is defined for task and timer objects. 
  9308. alt_obj is meant to be a pointer to the object that caused schd to be alerted. 
  9309.  
  9310.  
  9311. ΓòÉΓòÉΓòÉ 5.2.7.17. set_exit_status - Set Exit Status of Task ΓòÉΓòÉΓòÉ
  9312.  
  9313. Class: sched 
  9314.  
  9315. static void set_exit_status(int i);
  9316.  
  9317. set_exit_status() sets the exit status of the task program. When a task program 
  9318. terminates normally (task_error() is not called), the program calls exit() with 
  9319. the argument i. 
  9320.  
  9321.  
  9322. ΓòÉΓòÉΓòÉ 5.2.8. Members of task ΓòÉΓòÉΓòÉ
  9323.  
  9324. Note:  The following descriptions assume that the functions are called as part 
  9325. of a task object called tsk. 
  9326.  
  9327. The following functions are described: 
  9328.  
  9329. o Constructor for task 
  9330. o cancel - change state to TERMINATED 
  9331. o delay - suspend task 
  9332. o get_task_chain - return first task in list 
  9333. o _hwm - high water mark variable 
  9334. o o_type - return class object type 
  9335. o preempt - make running task idle 
  9336. o print - print task information 
  9337. o resultis - set return value 
  9338. o setwho - record alerting object 
  9339. o sleep - suspend task unconditionally 
  9340. o wait - suspend task 
  9341. o waitlist - suspend task, wait for list of objects 
  9342. o waitvec - suspend task, wait for vector of objects 
  9343. o who_alerted_me - return object that put task back on run chain 
  9344.  
  9345.  
  9346. ΓòÉΓòÉΓòÉ 5.2.8.1. Constructor for task ΓòÉΓòÉΓòÉ
  9347.  
  9348. Class: task 
  9349.  
  9350. task(char* name = 0, modetype mode = DEFAULT_MODE,
  9351.           int stacksize = SIZE);
  9352.  
  9353. The task constructor is declared protected. You cannot call the task 
  9354. constructor to create a task object directly, but you can use the task 
  9355. constructor to create objects of the classes that you derive from task. 
  9356.  
  9357. The first argument of the task constructor specifies the name of the task. The 
  9358. default value for name is 0. mode specifies whether the task is SHARED or 
  9359. DEDICATED. The default value for mode is DEFAULT_MODE. stacksize specifies the 
  9360. size of the stack for the task. The default value for stacksize is SIZE. The 
  9361. default values DEFAULT_MODE and SIZE are defined in task.h. 
  9362.  
  9363. Notes 
  9364.  
  9365.  1. At the end of every constructor of a task object, including the main() 
  9366.     function in any program that uses the task library, a call to resultis() 
  9367.     should appear, unless you want the constructor to execute infinitely. 
  9368.  
  9369.  2. The results of ending a task abnormally using the standard C library 
  9370.     function exit() are undefined.  Unpredictable results will occur. 
  9371.  
  9372.  
  9373. ΓòÉΓòÉΓòÉ 5.2.8.2. cancel - Change State to TERMINATED ΓòÉΓòÉΓòÉ
  9374.  
  9375. Class: task 
  9376.  
  9377. void cancel(int ret_val);
  9378.  
  9379. cancel() changes the state of tsk to TERMINATED and sets the return value of 
  9380. tsk to ret_val. cancel() does not invoke the scheduler. 
  9381.  
  9382.  
  9383. ΓòÉΓòÉΓòÉ 5.2.8.3. delay - Suspend Task ΓòÉΓòÉΓòÉ
  9384.  
  9385. Class: task 
  9386.  
  9387. void delay(long interval);
  9388.  
  9389. delay() suspends tsk for the time specified by interval. A task that calls 
  9390. delay() has a state of RUNNING, and the call to delay() does not change the 
  9391. state of the task. If the current time on the task system clock is t{0}, tsk 
  9392. resumes at time t{0} + interval. A call to delay() causes the clock to advance. 
  9393.  
  9394. delay() is usually called by a task on itself. If such a task has a state of 
  9395. IDLE, a call to delay() results in an E_CLOCKIDLE runtime error. 
  9396.  
  9397.  
  9398. ΓòÉΓòÉΓòÉ 5.2.8.4. get_task_chain - Return First Task in List ΓòÉΓòÉΓòÉ
  9399.  
  9400. Class: task 
  9401.  
  9402. static task* get_task_chain();
  9403. #define task_chain (task::get_task_chain())
  9404.  
  9405. get_task_chain() returns a pointer to the first task object in the list of task 
  9406. objects. Each task object is linked together by the member t_next. 
  9407.  
  9408. The macro task_chain() is included for compatibility with the AT&T** C++ 
  9409. Language System Release 1.2. 
  9410.  
  9411.  
  9412. ΓòÉΓòÉΓòÉ 5.2.8.5. _hwm - High Water Mark Variable ΓòÉΓòÉΓòÉ
  9413.  
  9414. Class: task 
  9415.  
  9416. extern _hwm;
  9417.  
  9418. The external variable _hwm is the high water mark variable for the stack 
  9419. associated with a task. If it is initially set to a nonzero value, _hwm causes 
  9420. the task system to record the maximum size of the stack. _hwm also affects what 
  9421. is printed by task::print() with an argument of STACK. If _hwm is initially set 
  9422. to a nonzero value, a call to task::print() with an argument of STACK will 
  9423. print out high water mark information. This information is meant for debugging. 
  9424. Note that gathering this information could reduce the performance of your 
  9425. application, and thus you should only give _hwm an initial nonzero value when 
  9426. you are debugging. 
  9427.  
  9428. You can set _hwm in the main() function, but if you want the value of _hwm to 
  9429. be nonzero for the main() task and the interrupt_alerter task you must set the 
  9430. value of _hwm in the constructor of a static object. The following example 
  9431. shows how you might set the value of _hwm to accomplish this: 
  9432.  
  9433. extern _hwm;
  9434.  
  9435. class hwm_init {
  9436. public:
  9437.      hwm_init() { _hwm=1 };
  9438. };
  9439.  
  9440. static hwm_init hwmi;
  9441. // ...
  9442. #include <task.h>
  9443.  
  9444. Note:  The interrupt_alerter task runs before the main() task. Thus, if you set 
  9445. the value of _hwm in the main() function, no high water mark information will 
  9446. be recorded for either the main() task or the interrupt_alerter task. 
  9447.  
  9448.  
  9449. ΓòÉΓòÉΓòÉ 5.2.8.6. o_type - Return Class Object Type ΓòÉΓòÉΓòÉ
  9450.  
  9451. Class: task 
  9452.  
  9453. object::objtype o_type();
  9454.  
  9455. o_type() returns the class object type (object::objtype) of tsk, namely 
  9456. object::TASK. 
  9457.  
  9458.  
  9459. ΓòÉΓòÉΓòÉ 5.2.8.7. preempt - Make Running Task Idle ΓòÉΓòÉΓòÉ
  9460.  
  9461. Class: task 
  9462.  
  9463. long preempt();
  9464.  
  9465. preempt() changes the state of tsk from RUNNING to IDLE. preempt() returns the 
  9466. number of time units left in the delay for tsk. preempt() produces an E_TASKPRE 
  9467. runtime error if tsk has state equal to IDLE or TERMINATED. 
  9468.  
  9469.  
  9470. ΓòÉΓòÉΓòÉ 5.2.8.8. print - Print Task Information ΓòÉΓòÉΓòÉ
  9471.  
  9472. Class: task 
  9473.  
  9474. void print(int x, int y =0);
  9475.  
  9476. print() prints information about tsk on standard output. The argument x 
  9477. specifies the amount of information to be printed. The argument y is used 
  9478. internally by the Task Library. Its default value is 0. 
  9479.  
  9480. If x equals 0, the following information is printed: 
  9481.  
  9482. o The type of the object, namely task 
  9483. o The task state (RUNNING, IDLE, or TERMINATED) 
  9484. o The value of the this pointer 
  9485.  
  9486. If x equals CHAIN, the information listed above is printed, along with the same 
  9487. information for every task in the chain attached to tsk. 
  9488.  
  9489. If x equals VERBOSE, the information that is printed when x equals 0 is 
  9490. printed, along with the following information: 
  9491.  
  9492. o The mode of the task (DEDICATED or SHARED) 
  9493. o t_alert, a pointer to the object that alerted tsk 
  9494. o t_next, a pointer to the next task object in the chain 
  9495. o The value returned by result() for this task 
  9496. o The value of the this pointer for the sched and object objects associated 
  9497.   with this task 
  9498. o The list of tasks on the remember chain for this task 
  9499.  
  9500. If x equals STACK and the state of tsk is either RUNNING or IDLE, all of the 
  9501. information that is printed when x equals 0 is printed in addition to the 
  9502. information listed below: 
  9503.  
  9504. o The maximum size of the stack for this task 
  9505. o The current size of the stack 
  9506. o The size of _hwm (if _hwm is initially set to a nonzero value) 
  9507. o t_basep, the address of the base of the stack when the state of the task is 
  9508.   RUNNING 
  9509. o t_framep, the frame pointer for the task when its state is IDLE 
  9510. o The current address of the top of the stack 
  9511. o The maximum address for the top of the stack 
  9512.  
  9513. If x equals STACK and the state of tsk is TERMINATED, all of the information 
  9514. that is printed when x equals 0 is printed in addition to the information 
  9515. listed below: 
  9516.  
  9517. o The status of the stack, namely deleted 
  9518. o The size and address of _hwm (if _hwm is initially set to a nonzero value) 
  9519.  
  9520.  
  9521. ΓòÉΓòÉΓòÉ 5.2.8.9. resultis - Set Return Value ΓòÉΓòÉΓòÉ
  9522.  
  9523. Class: task 
  9524.  
  9525. void resultis(int res);
  9526.  
  9527. resultis() sets the return value of tsk to res and sets the state of tsk to 
  9528. TERMINATED. The result() member function of the class sched returns the value 
  9529. of this result. An object of a class derived from task object cannot return a 
  9530. value using the usual function return mechanism. A call to resultis() should 
  9531. appear at the end of every constructor for every class that is derived from 
  9532. task. 
  9533.  
  9534.  
  9535. ΓòÉΓòÉΓòÉ 5.2.8.10. setwho - Record Alerting Object ΓòÉΓòÉΓòÉ
  9536.  
  9537. Class: task 
  9538.  
  9539. void setwho(object* alt_obj);
  9540.  
  9541. setwho() is a virtual function that is defined for task and timer objects. 
  9542. alt_obj is a pointer to the object that caused the scheduler to be alerted. 
  9543. setwho() records the object pointed to by alt_obj as the one that caused the 
  9544. state of tsk to change from IDLE to RUNNING. alt_obj points to an object that 
  9545. had a change of state from pending to ready that caused tsk to be put back in 
  9546. the run chain. who_alerted_me() returns this pointer. 
  9547.  
  9548.  
  9549. ΓòÉΓòÉΓòÉ 5.2.8.11. sleep - Suspend Task Unconditionally ΓòÉΓòÉΓòÉ
  9550.  
  9551. Class: task 
  9552.  
  9553. void sleep(object* ob=0);
  9554.  
  9555. sleep() changes the state of tsk to IDLE. If ob points to a pending object, tsk 
  9556. will be put back on the run chain when the state of ob changes to ready. If ob 
  9557. points to a ready object, or if no argument is supplied, tsk will never be put 
  9558. back on the run chain; the event that would cause tsk to be resumed is 
  9559. unspecified. In this case, tsk can only be eliminated by a call to cancel(). 
  9560.  
  9561. Only objects of the predefined classes automatically alert the tasks that are 
  9562. waiting for them. Objects of classes that you define must explicitly call 
  9563. object::alert() when their state changes from pending to ready. 
  9564.  
  9565. See Object States for a general description of the object states. 
  9566.  
  9567.  
  9568. ΓòÉΓòÉΓòÉ 5.2.8.12. wait - Suspend Task ΓòÉΓòÉΓòÉ
  9569.  
  9570. Class: task 
  9571.  
  9572. void wait(object* ob);
  9573.  
  9574. If the state of the object pointed to by ob is pending, wait() changes the 
  9575. state of tsk to IDLE until the object is ready. If the state of the object 
  9576. pointed to by ob is ready, the state of tsk remains unchanged. If wait() is 
  9577. called with an argument equal to 0, tsk will never be put back on the run 
  9578. chain, and tsk can only be eliminated by a call to cancel(). If the argument of 
  9579. wait() is a pointer to tsk, wait() produces an E_WAIT runtime error. 
  9580.  
  9581. Unlike sleep(), wait() checks the state of its argument before it changes the 
  9582. state of tsk. Suppose obr is a pointer to a ready object. tsk.wait(obr) does 
  9583. not change the state of tsk, but tsk.sleep(obr) does change the state of tsk. 
  9584.  
  9585. Note:  A task should only call wait() on itself. 
  9586.  
  9587.  
  9588. ΓòÉΓòÉΓòÉ 5.2.8.13. waitlist - Suspend Task, Wait for List of Objects ΓòÉΓòÉΓòÉ
  9589.  
  9590. Class: task 
  9591.  
  9592. int waitlist(object* ob1...);
  9593.  
  9594. waitlist() takes as arguments a list of pointers to objects. The last element 
  9595. in this list must be equal to 0. The result of waitlist() depends on the state 
  9596. of the objects pointed to by the arguments: 
  9597.  
  9598. o If all of the arguments point to objects that have a state of pending, 
  9599.   waitlist() changes the state of tsk to IDLE. As soon as the state of one of 
  9600.   the objects pointed to by the arguments becomes ready, the state of tsk is 
  9601.   changed back to RUNNING and waitlist() returns the position of this argument 
  9602.   in the list. For example, if the object pointed to by the third argument is 
  9603.   the first one to become ready, waitlist() has a return value of 2 (because 
  9604.   the first argument is at position 0). 
  9605.  
  9606. o If any argument obi ( i = 1 . . . n) in the list of arguments is ready (that 
  9607.   is, the function obi->pending() returns 0), the state of tsk is not changed 
  9608.   and waitlist() returns the position of obi in the argument list. Other 
  9609.   arguments in the list may also point to objects that are ready, but 
  9610.   waitlist() only returns the position of the first one. 
  9611.  
  9612.  
  9613. ΓòÉΓòÉΓòÉ 5.2.8.14. waitvec - Suspend Task, Wait for Vector of Objects ΓòÉΓòÉΓòÉ
  9614.  
  9615. Class: task 
  9616.  
  9617. int waitvec(object** objvec);
  9618.  
  9619. waitvec() is the same as waitlist() except that its argument objvec is a 
  9620. pointer to a vector that holds a list of object pointers. The last pointer in 
  9621. this vector must be equal to 0. 
  9622.  
  9623.  
  9624. ΓòÉΓòÉΓòÉ 5.2.8.15. who_alerted_me - Return Object that Put Task Back on Run Chain ΓòÉΓòÉΓòÉ
  9625.  
  9626. Class: task 
  9627.  
  9628. object* who_alerted_me();
  9629.  
  9630. who_alerted_me() returns a pointer to the object whose state change from 
  9631. pending to ready caused tsk to change state from IDLE to RUNNING. 
  9632.  
  9633.  
  9634. ΓòÉΓòÉΓòÉ 5.2.9. Public Members of timer ΓòÉΓòÉΓòÉ
  9635.  
  9636. Note:  The following descriptions assume that the functions are called as part 
  9637. of a timer object called tm. 
  9638.  
  9639. The following functions are defined: 
  9640.  
  9641. o Constructor for timer 
  9642. o o_type - return class object type 
  9643. o print - print contents of timer object 
  9644. o reset - reset the delay of the timer object 
  9645. o setwho - record alerting object 
  9646.  
  9647. An example of using the timer class is also provided. 
  9648.  
  9649.  
  9650. ΓòÉΓòÉΓòÉ 5.2.9.1. Constructor for timer ΓòÉΓòÉΓòÉ
  9651.  
  9652. Class: timer 
  9653.  
  9654. timer(long wait_time);
  9655.  
  9656. timer() constructs a timer object and inserts it on the scheduler's run chain. 
  9657. wait_time is the number of time units that tm, the newly created timer object, 
  9658. runs. 
  9659.  
  9660. timer objects are inserted into the run chain based on the value of wait_time 
  9661. when they were created. The scheduler resets the current time to the wait_time 
  9662. of the task object at the front of the run chain. 
  9663.  
  9664.  
  9665. ΓòÉΓòÉΓòÉ 5.2.9.2. o_type - Return Class Object Type ΓòÉΓòÉΓòÉ
  9666.  
  9667. Class: timer 
  9668.  
  9669. object::objtype o_type();
  9670.  
  9671. o_type() returns the class object type (object::objtype) of tm, namely 
  9672. object::TIMER. 
  9673.  
  9674.  
  9675. ΓòÉΓòÉΓòÉ 5.2.9.3. print - Print Contents of timer Object ΓòÉΓòÉΓòÉ
  9676.  
  9677. Class: timer 
  9678.  
  9679. void print(int x, int y);
  9680.  
  9681. print() prints information about tm. The argument x specifies the amount of 
  9682. information to be printed. The argument y is used internally by the Task 
  9683. Library. Its default value is 0. 
  9684.  
  9685. If x equals 0, print() displays the following information on standard output: 
  9686.  
  9687. o The type of the object, namely timer 
  9688. o The amount of time that tm has to wait 
  9689. o The offset from the current setting of the task system clock 
  9690.  
  9691. If x equals VERBOSE, print() displays the information listed above, along with 
  9692. the following information: 
  9693.  
  9694. o The value of the this pointer in the sched object 
  9695. o The value of the this pointer in the object 
  9696. o The list of remembered task objects 
  9697.  
  9698.  
  9699. ΓòÉΓòÉΓòÉ 5.2.9.4. reset - Reset the Delay of the timer Object ΓòÉΓòÉΓòÉ
  9700.  
  9701. Class: timer 
  9702.  
  9703. void reset(long wait_time);
  9704.  
  9705. reset() resets the time units that tm has to wait to wait_time. A timer object 
  9706. can be reset even if its state equals TERMINATED. 
  9707.  
  9708.  
  9709. ΓòÉΓòÉΓòÉ 5.2.9.5. setwho - Record Alerting Object ΓòÉΓòÉΓòÉ
  9710.  
  9711. Class: timer 
  9712.  
  9713. void setwho(object* alt_obj);
  9714.  
  9715. In the timer object, setwho() is defined to do nothing. 
  9716.  
  9717.  
  9718. ΓòÉΓòÉΓòÉ 5.2.9.6. Example of Using the timer Class ΓòÉΓòÉΓòÉ
  9719.  
  9720. Class: timer 
  9721.  
  9722. The following example shows the different ways that you can use the timer class 
  9723. and advance the clock: 
  9724.  
  9725.  
  9726. #include <task.h>
  9727. #include <iostream.h>
  9728.  
  9729. class ct : public task {
  9730.      int cnt ;
  9731. public:
  9732.      ct();
  9733. };
  9734.  
  9735. ct::ct():cnt(0) {
  9736.      while(++cnt) {sleep(); }
  9737. }
  9738.  
  9739. void main() {
  9740.      timer *t;
  9741.      object *ov[2];
  9742.      //
  9743.      ct ct1;
  9744.      sched::clock_task = &ct1;
  9745.      //
  9746.      // Advance the task system clock 50 times
  9747.      // by 50 time units each time.  Use various
  9748.      // different ways for clock advancement.
  9749.      //
  9750.      for (register i=0; i<10; ++i) {
  9751.           thistask->delay(50);
  9752.      }
  9753.  
  9754.      for (i=0; i<10; ++i) {
  9755.           t= new timer (50);
  9756.           thistask->wait(t);
  9757.      }
  9758.      t->print(0);
  9759.      for (i=0; i<10; ++i) {
  9760.           t= new timer (50);
  9761.           thistask->sleep(t);
  9762.      }
  9763.      t->print(0);
  9764.      for (i=0; i<10; ++i) {
  9765.           t= new timer (50);
  9766.           thistask->waitlist(t,0);
  9767.      }
  9768.      t->print(VERBOSE);
  9769.      for (i=0; i<10; ++i) {
  9770.           t= new timer (50);
  9771.           ov[0]=t;
  9772.           ov[1]=0;
  9773.           thistask->waitvec(ov);
  9774.      }
  9775.  
  9776.      t->print(0);
  9777.      thistask->resultis(0);
  9778. }
  9779.  
  9780. This program produces output similar to the following: 
  9781.  
  9782. timer 1000 == clock+0
  9783.  
  9784. timer 1500 == clock+0
  9785.  
  9786. timer 1500 == clock+0
  9787.  
  9788. timer 2000 == clock+0
  9789.         sched:  this=b1190
  9790.         object:  this=b1190
  9791.         No tasks remembered
  9792.  
  9793. timer 2500 == clock+0
  9794.  
  9795.  
  9796. ΓòÉΓòÉΓòÉ 5.3. Queue Management Classes ΓòÉΓòÉΓòÉ
  9797.  
  9798. This chapter describes the queue management classes qhead and qtail. You can 
  9799. use these classes to implement a wide range of message-passing and 
  9800. data-buffering schemes. Both classes are derived from the class object. 
  9801.  
  9802. The following topics are described in this chapter: 
  9803.  
  9804. o What is a queue? 
  9805. o Queue attributes 
  9806. o Cutting and splicing queues 
  9807. o Restrictions on putting objects in queues 
  9808. o Declarations for the Queue Management Classes in task.h 
  9809. o Public members of qhead 
  9810. o Public members of qtail 
  9811.  
  9812.  
  9813. ΓòÉΓòÉΓòÉ 5.3.1. What Is a Queue? ΓòÉΓòÉΓòÉ
  9814.  
  9815. A queue is a data structure with an associated list of objects that are 
  9816. extracted from the queue in first-in, first-out order. No public functions deal 
  9817. with queues directly; you manage a queue using the qhead and qtail objects that 
  9818. are associated with each queue. You create a queue by creating either a qhead 
  9819. object or a qtail object. 
  9820.  
  9821. If you create a qhead object, you can obtain the qtail object that is 
  9822. associated with that qhead object by calling qhead::tail(). Similarly, if you 
  9823. create a qtail object, you can obtain the qhead object that is associated with 
  9824. that qtail object by calling qtail::head(). 
  9825.  
  9826. Once a queue is established, you use qtail::put() to add objects to it and 
  9827. qhead::get() to remove objects from it. 
  9828.  
  9829. Because both qhead and qtail are derived from the class object, they both have 
  9830. definitions for when they are ready and when they are pending. qhead objects 
  9831. are ready when the queue is not empty and pending when the queue is empty. 
  9832. qtail objects are ready when the queue is not full and pending when the queue 
  9833. is full. 
  9834.  
  9835.  
  9836. ΓòÉΓòÉΓòÉ 5.3.2. Queue Attributes ΓòÉΓòÉΓòÉ
  9837.  
  9838. Queues have three attributes that control the operation of the queue: 
  9839.  
  9840. o mode: controls what happens when a qhead or qtail object is pending. The mode 
  9841.   attribute can have different settings for the qhead and qtail objects that 
  9842.   make up a queue. 
  9843. o maximum size: controls the maximum number of objects that can be in the 
  9844.   queue. 
  9845. o count: records the number of objects that are in the queue. 
  9846.  
  9847.  
  9848. ΓòÉΓòÉΓòÉ 5.3.2.1. Mode Attribute ΓòÉΓòÉΓòÉ
  9849.  
  9850. The mode controls what happens when a qhead or qtail object is pending. The 
  9851. qhead and qtail objects that are attached to the same queue do not necessarily 
  9852. have the same mode. The mode can have the following values: 
  9853.  
  9854. o WMODE (wait mode): a task that calls qhead::get() on an empty queue is 
  9855.   suspended until the queue is no longer empty. A task that calls qtail::put() 
  9856.   on a full queue is suspended until the queue is no longer full. 
  9857. o EMODE (error mode): calling qhead::get() for an empty queue or qtail::put() 
  9858.   for a full queue causes a runtime error. 
  9859. o ZMODE (zero mode): a call to qtail::put() for a full queue or a call to 
  9860.   qhead::get() for an empty queue returns 0. Otherwise, qtail::put() and 
  9861.   qhead::get() return 1. 
  9862.  
  9863.  
  9864. ΓòÉΓòÉΓòÉ 5.3.2.2. Maximum Size Attribute ΓòÉΓòÉΓòÉ
  9865.  
  9866. The maximum size is set to 10 000 by default. A queue can hold up to 10 000 
  9867. pointers to objects, but it does not preallocate space. You can use either 
  9868. qhead::setmax() or qtail::setmax() to reset the maximum queue size. You can use 
  9869. use either qhead::rdmax() or qtail::rdmax() to get the current setting of the 
  9870. maximum size attribute. 
  9871.  
  9872.  
  9873. ΓòÉΓòÉΓòÉ 5.3.2.3. Count Attribute ΓòÉΓòÉΓòÉ
  9874.  
  9875. The count is the number of objects in the queue. You can use qhead::rdcount() 
  9876. to get the current value of the count attribute. 
  9877.  
  9878.  
  9879. ΓòÉΓòÉΓòÉ 5.3.3. Cutting and Splicing Queues ΓòÉΓòÉΓòÉ
  9880.  
  9881. You can split a queue into two pieces using the member functions qhead::cut() 
  9882. and qtail::cut(). See cut - Split Queue for more details on qhead::cut(). You 
  9883. can join these two pieces together again into a single queue using the member 
  9884. functions qhead::splice() and qtail::splice(). 
  9885.  
  9886. You can use cutting and splicing to insert a filter into a data stream. A 
  9887. filter is a task that takes input from one queue, performs some kind of 
  9888. transformation on it, and puts the transformed data on another queue. If you 
  9889. want to insert a filter into an existing queue, you can use cut() to split the 
  9890. queue in two pieces. Then the filter can get input from the head of one queue 
  9891. and put output to the tail of the other queue. When the filter has finished its 
  9892. processing, you can use splice() to make the two pieces into a single queue 
  9893. again. 
  9894.  
  9895. Note:  Because you do not deal directly with queues, but rather with qhead and 
  9896. qtail objects, you never use the name of a queue in your code. In this chapter, 
  9897. queues are given names so that they can be discussed conveniently. These names 
  9898. would not appear in any actual code. 
  9899.  
  9900. A queue called oldqueue is attached to the qtail object qt. oldqueue is split, 
  9901. and qt is now attached to a new queue called newqueue. Finally, oldqueue and 
  9902. newqueue are reunited with a call to qtail::splice(). The operations 
  9903. illustrated in Cutting and Splicing a qtail Object could be accomplished by 
  9904. code that looks like this: 
  9905.  
  9906. qtail *qt = new qtail;
  9907. qhead *qh = qt->head();
  9908. qtail *qtp = qt->cut();
  9909. qhead *qhp = qt->head();
  9910. qtp->splice(qhp);
  9911. The following figure illustrates what happens to a qtail object when it is cut 
  9912. and spliced. 
  9913.  
  9914. before qt->cut();
  9915.              ΓöîΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÉ
  9916.         qh     Γöé      Γöé     qt
  9917.         ΓöîΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöñ      Γö£ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÉ
  9918.         ΓööΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöñ oldqueue Γö£ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÿ
  9919.              Γöé      Γöé
  9920.              ΓööΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÿ
  9921. after qt->cut();
  9922.  
  9923.      ΓöîΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÉ           ΓöîΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÉ
  9924. qh    Γöé      Γöé    qtp  qhp    Γöé      Γöé     qt
  9925. ΓöîΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöñ      Γö£ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÉ  ΓöîΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöñ      Γö£ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÉ
  9926. ΓööΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöñ oldqueue Γö£ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÿ  ΓööΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöñ newqueue Γö£ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÿ
  9927.      Γöé      Γöé           Γöé      Γöé
  9928.      ΓööΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÿ           ΓööΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÿ
  9929.  
  9930. after qtp->splice(qhp);
  9931.              ΓöîΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÉ
  9932.         qh     Γöé      Γöé     qt
  9933.         ΓöîΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöñ      Γö£ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÉ
  9934.         ΓööΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöñ oldqueue Γö£ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÿ
  9935.              Γöé      Γöé
  9936.              ΓööΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÿ
  9937.  
  9938. Cutting and Splicing a qtail Object 
  9939.  
  9940. Cutting and Splicing a qhead Object illustrates what happens to a qhead object 
  9941. when it is cut and spliced. A queue called oldqueue is attached to the qhead 
  9942. object qh. oldqueue is split, and qh is now attached to a new queue called 
  9943. newqueue. Finally, oldqueue and newqueue are reunited with a call to 
  9944. qhead::splice(). The operations illustrated in Cutting and Splicing a qhead 
  9945. Object could be accomplished by code that looks like this: 
  9946.  
  9947. qhead *qh = new qhead;
  9948. qtail *qt = qh->tail();
  9949. qhead *qhp = qh->cut();
  9950. qtail *qtp = qh->tail();
  9951. qhp->splice(qtp);
  9952.  
  9953. before qh->cut();
  9954.              ΓöîΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÉ
  9955.         qh     Γöé      Γöé     qt
  9956.         ΓöîΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöñ      Γö£ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÉ
  9957.         ΓööΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöñ oldqueue Γö£ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÿ
  9958.              Γöé      Γöé
  9959.              ΓööΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÿ
  9960. after qh->cut();
  9961.  
  9962.      ΓöîΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÉ           ΓöîΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÉ
  9963. qh    Γöé      Γöé    qtp  qhp    Γöé      Γöé     qt
  9964. ΓöîΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöñ      Γö£ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÉ  ΓöîΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöñ      Γö£ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÉ
  9965. ΓööΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöñ newqueue Γö£ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÿ  ΓööΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöñ oldqueue Γö£ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÿ
  9966.      Γöé      Γöé           Γöé      Γöé
  9967.      ΓööΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÿ           ΓööΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÿ
  9968.  
  9969. after qhp->splice(qtp);
  9970.              ΓöîΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÉ
  9971.         qh     Γöé      Γöé     qt
  9972.         ΓöîΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöñ      Γö£ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÉ
  9973.         ΓööΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöñ oldqueue Γö£ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÿ
  9974.              Γöé      Γöé
  9975.              ΓööΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÿ
  9976.  
  9977. Cutting and Splicing a qhead Object 
  9978.  
  9979.  
  9980. ΓòÉΓòÉΓòÉ 5.3.3.1. Example of Cutting and Splicing a Queue ΓòÉΓòÉΓòÉ
  9981.  
  9982. The following example shows how you can use cutting and splicing to insert a 
  9983. filter into a data stream. This example includes the following classes: 
  9984.  
  9985. o Producer: creates a series of random numbers and puts them into the tail of a 
  9986.   queue. 
  9987. o Consumer: gets numbers from the head of a queue and prints them. 
  9988. o Filter: gets numbers from the head of a queue, sorts them, and puts them into 
  9989.   the tail of a queue. 
  9990.  
  9991. In the main() function of this example, the following operations are performed: 
  9992.  
  9993. o The Producer object p1 and the Consumer object c1 are created with the same 
  9994.   queue (called oldqueue in this discussion). Thus, c1 prints the numbers 
  9995.   produced by p1. 
  9996.  
  9997. o oldqueue is cut to produce a new queue (called newqueue in this discussion). 
  9998.   Producer object p2 is created with oldqueue as its argument, and Consumer 
  9999.   object c2 is created with newqueue as its argument. In between p2 and c2, 
  10000.   Filter object f is created with oldqueue and newqueue as arguments. Thus f 
  10001.   takes the numbers produced by p2, sorts them, and sends them to c2 to be 
  10002.   printed. 
  10003.  
  10004. o oldqueue and newqueue are spliced together to produce a restored oldqueue. 
  10005.   Producer object p3 and Consumer object c3 are created with the same queue, 
  10006.   oldqueue. Thus, c3 prints the numbers produced by p3. 
  10007.  
  10008. The output produced by this example includes an unsorted list of numbers, 
  10009. followed by a sorted list of numbers, followed by an unsorted list of numbers. 
  10010.  
  10011. Note:  You must set the language level to "compat" in order to compile this 
  10012. program successfully. You can use the /Sc switch to do this. 
  10013.  
  10014. #include <iostream.h>
  10015. #include <task.h>
  10016. #include <stdlib.h>
  10017.  
  10018. const int NumberOfIterations = 10;
  10019.  
  10020. class Message : public object {
  10021. private:
  10022.      int value;
  10023. public:
  10024.      Message(int v) : value(v) {}
  10025.      int getValue() { return value; }
  10026. };
  10027.  
  10028. class Producer : public task {
  10029. public:
  10030.      Producer(qtail *qt);
  10031. };
  10032.  
  10033. class Consumer : public task {
  10034. public:
  10035.      Consumer(qhead *qh);
  10036. };
  10037.  
  10038. class Filter : public task {
  10039.      static int Comparison(const void *, const void *);
  10040. public:
  10041.      Filter(qhead *qhp, qtail *qtp);
  10042.      void sortArray(int *arr);
  10043. };
  10044.  
  10045. Producer::Producer(qtail *qt)
  10046. {
  10047.      urand u(1,100);
  10048.      for (int lcv=0; lcv < NumberOfIterations; lcv++)
  10049.           qt->put(new Message(u.draw()));
  10050.      resultis(0);
  10051. }
  10052.  
  10053. Consumer::Consumer(qhead *qh)
  10054. {
  10055.      for (int lcv=0; lcv < NumberOfIterations; lcv++)
  10056.      {
  10057.           Message *mess = (Message *)qh->get();
  10058.           cout << "Consumer got "
  10059.                << mess->getValue() << endl;
  10060.           delete mess;
  10061.      }
  10062.      resultis(0);
  10063. }
  10064.  
  10065. Filter::Filter(qhead *qhp, qtail *qtp)
  10066. {
  10067.      int arr[NumberOfIterations];
  10068.      for (int lcv=0; lcv < NumberOfIterations; lcv++)
  10069.      {
  10070.           Message *mess = (Message *)qhp->get();
  10071.           arr[lcv] = mess->getValue();
  10072.           delete mess;
  10073.      }
  10074.      sortArray(arr);
  10075.      for (lcv=0; lcv < NumberOfIterations; lcv++)
  10076.           qtp->put(new Message(arr[lcv]));
  10077.      resultis(0);
  10078. }
  10079.  
  10080. int Filter::Comparison(const void *e1, const
  10081. void *e2)
  10082. {
  10083.      if (*(int *)e1 < *(int *)e2)
  10084.           return -1;
  10085.      if (*(int *)e1 == *(int *)e2)
  10086.           return 0;
  10087.      return 1;
  10088. }
  10089.  
  10090. void Filter::sortArray(int *arr)
  10091. {
  10092.      qsort(arr, NumberOfIterations, sizeof(int),
  10093.            &Filter::Comparison);
  10094. }
  10095.  
  10096. void main()
  10097. {
  10098.      qhead *qh = new qhead;
  10099.      qtail *qt = qh->tail();
  10100.      Producer p1(qt);
  10101.      Consumer c1(qh);
  10102.      cout << "Now insert a filter that will sort the numbers: " << endl;
  10103.      qhead *qhp = qh->cut();
  10104.      qtail *qtp = qh->tail();
  10105.      Producer p2(qt);
  10106.      Filter f(qhp,qtp);
  10107.      Consumer c2(qh);
  10108.      cout << "Now remove the filter: " << endl;
  10109.      qhp->splice(qtp);
  10110.      Producer p3(qt);
  10111.      Consumer c3(qh);
  10112.      cout << "All done" << endl;
  10113.      thistask->resultis(0);
  10114. }
  10115.  
  10116. This program produces output that looks like this: 
  10117.  
  10118. Consumer got 1
  10119. Consumer got 66
  10120. Consumer got 31
  10121. Consumer got 68
  10122. Consumer got 11
  10123. Consumer got 52
  10124. Consumer got 49
  10125. Consumer got 61
  10126. Consumer got 37
  10127. Consumer got 26
  10128. Now insert a filter that will sort the numbers:
  10129. Consumer got 1
  10130. Consumer got 11
  10131. Consumer got 26
  10132. Consumer got 31
  10133. Consumer got 37
  10134. Consumer got 49
  10135. Consumer got 52
  10136. Consumer got 61
  10137. Consumer got 66
  10138. Consumer got 68
  10139. Now remove the filter:
  10140. Consumer got 1
  10141. Consumer got 66
  10142. Consumer got 31
  10143. Consumer got 68
  10144. Consumer got 11
  10145. Consumer got 52
  10146. Consumer got 49
  10147. Consumer got 61
  10148. Consumer got 37
  10149. Consumer got 26
  10150. All done
  10151.  
  10152.  
  10153. ΓòÉΓòÉΓòÉ 5.3.4. Restrictions on Putting Objects in Queues ΓòÉΓòÉΓòÉ
  10154.  
  10155. An object should not be in more than one queue at the same time. An object can 
  10156. end up in two queues in a subtle fashion. For example, the constructor for the 
  10157. timer class places the object that it constructs on the run chain, a queue of 
  10158. task objects that have a state equal to RUNNING. 
  10159.  
  10160. In the following example, a timer object t1 is created and automatically put on 
  10161. the run chain queue. t1 is then placed on another queue, q1. Placing t1 on a 
  10162. second queue causes the program to be suspended: 
  10163.  
  10164. #include <task.h>
  10165.  
  10166. class MyTask : public task {
  10167. public:
  10168.      MyTask();
  10169. };
  10170.  
  10171. MyTask::MyTask() {
  10172.      qtail q1;
  10173.      timer *t1 = new timer(60);     // t1 is put on the run
  10174.                                     // chain queue by the timer
  10175.                                     // constructor
  10176.                                     //
  10177.      q1.put(t1);                    // t1 is now on 2 queues and
  10178.                                     // the program will hang
  10179.      resultis(0);
  10180. }
  10181.  
  10182. void main(){
  10183.      MyTask mt;
  10184.      thistask->resultis(0);
  10185. }
  10186.  
  10187.  
  10188. ΓòÉΓòÉΓòÉ 5.3.5. Declarations for the Queue Management Classes in task.h ΓòÉΓòÉΓòÉ
  10189.  
  10190. You must include the following statement in any file that uses members of qhead 
  10191. or qtail classes: 
  10192.  
  10193. #include <task.h>
  10194.  
  10195. The following excerpt from the task.h header file shows member function 
  10196. declarations for the queue management classes: 
  10197.  
  10198. enum qmodetype { EMODE, WMODE, ZMODE };
  10199.  
  10200. class qhead : public object {
  10201. friend class qtail;
  10202. public:
  10203.                 qhead(qmodetype = WMODE, int = 10000);
  10204.                 ~qhead();
  10205.      object::objtype   o_type();
  10206.      object*    get();
  10207.      int        putback(object*);
  10208.      int        rdcount();
  10209.      int        rdmax();
  10210.      qmodetype  rdmode();
  10211.      qtail*     tail();
  10212.      qhead*     cut();
  10213.      void       splice(qtail*);
  10214.      void       setmode(qmodetype m);
  10215.      void       setmax(int m);
  10216.      int        pending();
  10217.      void       print(int, int =0);
  10218. };
  10219.  
  10220. class qtail : public object {
  10221. friend class qhead;
  10222. public:
  10223.                 qtail(qmodetype = WMODE, int = 10000);
  10224.                 ~qtail();
  10225.      object::objtype   o_type();
  10226.      int        put(object*);
  10227.      int        rdspace();
  10228.      int        rdmax();
  10229.      qmodetype  rdmode();
  10230.      qtail*     cut();
  10231.      void       splice(qhead*);
  10232.      qhead*     head();
  10233.      void       setmode(qmodetype m);
  10234.      void       setmax(int m);
  10235.      int        pending();
  10236.      void       print(int, int =0);
  10237. };
  10238.  
  10239.  
  10240. ΓòÉΓòÉΓòÉ 5.3.6. Public Members of qhead ΓòÉΓòÉΓòÉ
  10241.  
  10242. Note:  The following descriptions assume that the functions are called as part 
  10243. of a qhead object called qh, unless otherwise noted. 
  10244.  
  10245. The following functions are described: 
  10246.  
  10247. o Constructor for qhead 
  10248. o cut - split queue 
  10249. o get - extract object from queue 
  10250. o o_type - return class type 
  10251. o pending - is queue empty? 
  10252. o print - print queue contents 
  10253. o putback - put objects back in queue 
  10254. o rdcount - return number of objects in queue 
  10255. o rdmax - return maximum queue size 
  10256. o rdmode - return mode 
  10257. o setmode - set mode 
  10258. o setmax - set maximum queue size 
  10259. o splice - merge queues 
  10260. o tail - create qtail object 
  10261.  
  10262.  
  10263. ΓòÉΓòÉΓòÉ 5.3.6.1. Constructor for qhead ΓòÉΓòÉΓòÉ
  10264.  
  10265. Class: qhead 
  10266.  
  10267. qhead(qmodetype qm= WMODE, int size=10000);
  10268.  
  10269. The qhead() constructor takes two optional arguments. The qmodetype, qm, can 
  10270. have a value of WMODE, EMODE, or ZMODE. WMODE is the default. The argument 
  10271. size, the maximum number of objects that can be placed in the queue, has a 
  10272. default value of 10 000. 
  10273.  
  10274.  
  10275. ΓòÉΓòÉΓòÉ 5.3.6.2. cut - Split Queue ΓòÉΓòÉΓòÉ
  10276.  
  10277. Class: qhead 
  10278.  
  10279. qhead* cut();
  10280.  
  10281. cut() splits the queue attached to qh and returns a pointer to a new qhead 
  10282. object. In this description, this new qhead object is called qhp. qhp is 
  10283. attached to the original queue. qh now points to a new, empty queue. You must 
  10284. establish a new qtail for qh. If you want to get objects from the original 
  10285. queue, you must call qhp.get(). 
  10286.  
  10287. You can use the member function splice() to restore a split queue to its 
  10288. original configuration. 
  10289.  
  10290. Cutting and Splicing a qhead Object illustrates the cutting and splicing of a 
  10291. qhead object. 
  10292.  
  10293.  
  10294. ΓòÉΓòÉΓòÉ 5.3.6.3. get - Extract Object from Queue ΓòÉΓòÉΓòÉ
  10295.  
  10296. Class: qhead 
  10297.  
  10298. object* get();
  10299.  
  10300. If the queue is not empty, get() returns a pointer to the object at the head of 
  10301. the queue. If the queue is empty, the result depends on the mode: 
  10302.  
  10303. o WMODE: the task that executes get() is suspended until the queue is no longer 
  10304.   empty. 
  10305. o EMODE: the E_GETEMPTY runtime error is produced. 
  10306. o ZMODE: get() returns 0. 
  10307.  
  10308.  
  10309. ΓòÉΓòÉΓòÉ 5.3.6.4. o_type - Return Class Type ΓòÉΓòÉΓòÉ
  10310.  
  10311. Class: qhead 
  10312.  
  10313. object::objtype o_type();
  10314.  
  10315. o_type() returns the class type of qh, namely object::QHEAD. 
  10316.  
  10317.  
  10318. ΓòÉΓòÉΓòÉ 5.3.6.5. pending - Is Queue Empty? ΓòÉΓòÉΓòÉ
  10319.  
  10320. Class: qhead 
  10321.  
  10322. int pending();
  10323.  
  10324. pending() returns a nonzero value if the queue is empty and returns zero 
  10325. otherwise. 
  10326.  
  10327.  
  10328. ΓòÉΓòÉΓòÉ 5.3.6.6. print - Print Queue Contents ΓòÉΓòÉΓòÉ
  10329.  
  10330. Class: qhead 
  10331.  
  10332. void print(int detail, int y=0);
  10333.  
  10334. print() prints information about the queue attached to qh on stdout. It calls 
  10335. the print() member function of the object base class. Argument detail specifies 
  10336. the amount of information to be printed. Argument y is used internally by the 
  10337. Task Library and defaults to 0. 
  10338.  
  10339. If detail equals 0, the following information is printed: 
  10340.  
  10341. o The type of object, namely qhead 
  10342. o The integer value of the mode of the queue 
  10343. o The maximum size of the queue 
  10344. o The value of the count attribute for the queue 
  10345. o The value of the qtail pointer 
  10346.  
  10347. If detail equals VERBOSE, the information listed above is printed along with 
  10348. information about the qtail object that is attached to the queue and 
  10349. information about all of the tasks that are waiting for qh. The following 
  10350. values for the qtail object are printed: 
  10351.  
  10352. o The type of object, namely qtail 
  10353. o The integer value of the mode of the queue 
  10354. o The maximum size of the queue 
  10355. o The number of objects that the queue still has room for 
  10356. o The value of the qhead pointer 
  10357.  
  10358. For each task that is waiting for qh, the following information is printed: 
  10359.  
  10360. o The value of the t_alert pointer, a private pointer to the object that 
  10361.   alerted the task 
  10362. o The value of the t_next pointer, a pointer to the next task on the chain 
  10363. o The value of the s_time pointer, a private pointer to the time at which the 
  10364.   task is scheduled to run 
  10365. o The value of the this pointer for the sched and object this task is derived 
  10366.   from 
  10367.  
  10368.  
  10369. ΓòÉΓòÉΓòÉ 5.3.6.7. putback - Put Objects Back in Queue ΓòÉΓòÉΓòÉ
  10370.  
  10371. Class: qhead 
  10372.  
  10373. int putback(object* objptr);
  10374.  
  10375. putback() puts the object pointed to by objptr back on the head of the queue 
  10376. attached to qh, allowing qh to operate as a stack. A task that calls putback() 
  10377. competes for queue space with tasks that call qtail::put(). If the call to 
  10378. putback() succeeds, putback() returns 1. If the queue is full, a runtime error 
  10379. occurs if mode equals EMODE or WMODE. 0 is returned if mode equals ZMODE. 
  10380.  
  10381.  
  10382. ΓòÉΓòÉΓòÉ 5.3.6.8. rdcount - Return Number of Objects in Queue ΓòÉΓòÉΓòÉ
  10383.  
  10384. Class: qhead 
  10385.  
  10386. int rdcount();
  10387.  
  10388. rdcount() returns the number of objects in the queue attached to qh. 
  10389.  
  10390.  
  10391. ΓòÉΓòÉΓòÉ 5.3.6.9. rdmax - Return Maximum Queue Size ΓòÉΓòÉΓòÉ
  10392.  
  10393. Class: qhead 
  10394.  
  10395. int rdmax();
  10396.  
  10397. rdmax() returns the maximum size of the queue attached to qh. 
  10398.  
  10399.  
  10400. ΓòÉΓòÉΓòÉ 5.3.6.10. rdmode - Return Mode ΓòÉΓòÉΓòÉ
  10401.  
  10402. Class: qhead 
  10403.  
  10404. qmodetype rdmode();
  10405.  
  10406. rdmode() returns the mode of qh. 
  10407.  
  10408.  
  10409. ΓòÉΓòÉΓòÉ 5.3.6.11. setmode - Set Mode ΓòÉΓòÉΓòÉ
  10410.  
  10411. Class: qhead 
  10412.  
  10413. void setmode(qmodetype qm);
  10414.  
  10415. setmode() resets the mode of qh to qm. 
  10416.  
  10417.  
  10418. ΓòÉΓòÉΓòÉ 5.3.6.12. setmax - Set Maximum Queue Size ΓòÉΓòÉΓòÉ
  10419.  
  10420. Class: qhead 
  10421.  
  10422. void setmax(int newmax);
  10423.  
  10424. setmax() sets the maximum size of the queue attached to qh to newmax. You can 
  10425. set the maximum size to be less than the current size of the queue. If you do, 
  10426. you cannot put objects in the queue until the number of objects in the queue is 
  10427. less than the new maximum size. If newmax is less than or equal to 0, the 
  10428. E_QZERO runtime error is produced. 
  10429.  
  10430.  
  10431. ΓòÉΓòÉΓòÉ 5.3.6.13. splice - Merge Queues ΓòÉΓòÉΓòÉ
  10432.  
  10433. Class: qhead 
  10434.  
  10435. void splice(qtail* qt);
  10436.  
  10437. splice() reconnects two queues that were split by a call to cut(). 
  10438. qhp->splice(qtp) merges the queue attached to the qhead pointer qhp with the 
  10439. queue attached to the qtail pointer qtp, where: 
  10440.  
  10441. o A call qh.cut() was made that returned the qhead pointer qhp. After this 
  10442.   call, qh is attached to a new queue and qhp is attached to the original 
  10443.   queue. 
  10444.  
  10445. o After the call to cut(), a call qh.tail() was made that returned the qtail 
  10446.   pointer qtp. 
  10447.  
  10448. In the new queue, the objects that were in the queue attached to qtp precede 
  10449. the objects that were in the queue attached to qhp. splice() deletes both qhp 
  10450. and qtp. 
  10451.  
  10452. If the call to splice() makes an empty queue no longer empty or makes a full 
  10453. queue no longer full, it will alert all of the tasks that are waiting for 
  10454. either of these state changes and add these tasks to the scheduler's run chain. 
  10455.  
  10456. Cutting and Splicing a qhead Object illustrates the cutting and splicing of a 
  10457. qhead object. 
  10458.  
  10459.  
  10460. ΓòÉΓòÉΓòÉ 5.3.6.14. tail - Create qtail Object ΓòÉΓòÉΓòÉ
  10461.  
  10462. Class: qtail 
  10463.  
  10464. qtail* tail();
  10465.  
  10466. tail() creates a qtail object for the queue attached to qh (if none exists) and 
  10467. returns a pointer to the new qtail object. If a qtail object already exists for 
  10468. the queue attached to qh, tail() returns a pointer to that qtail object. 
  10469.  
  10470.  
  10471. ΓòÉΓòÉΓòÉ 5.3.7. Public Members of qtail ΓòÉΓòÉΓòÉ
  10472.  
  10473. Note:  The following descriptions assume that the functions are called as part 
  10474. of a qtail object called qt. 
  10475.  
  10476. The following functions are described: 
  10477.  
  10478. o Constructor for qtail 
  10479. o cut - split queue 
  10480. o head - create qhead object 
  10481. o o_type - return class type 
  10482. o pending - is queue full? 
  10483. o print - print queue contents 
  10484. o put - add object to queue 
  10485. o rdspace - space remaining 
  10486. o rdmax - return maximum queue size 
  10487. o rdmode - return mode 
  10488. o setmode - set mode 
  10489. o setmax - set maximum queue size 
  10490. o splice - merge queues 
  10491.  
  10492.  
  10493. ΓòÉΓòÉΓòÉ 5.3.7.1. Constructor for qtail ΓòÉΓòÉΓòÉ
  10494.  
  10495. Class: qtail 
  10496.  
  10497. qtail(qmodetype qm  = WMODE, int size=10000);
  10498.  
  10499. The qtail() constructor takes two optional arguments. The qmodetype, qm, can 
  10500. have a value of WMODE, EMODE, or ZMODE. WMODE is the default. The argument 
  10501. size, the maximum number of objects that can be placed in the queue, has a 
  10502. default value of 10 000. 
  10503.  
  10504.  
  10505. ΓòÉΓòÉΓòÉ 5.3.7.2. cut - Split Queue ΓòÉΓòÉΓòÉ
  10506.  
  10507. Class: qtail 
  10508.  
  10509. qtail* cut();
  10510.  
  10511. cut() splits the queue attached to qt and returns a pointer to a new qtail 
  10512. object, qtp. qtp is attached to the original queue. qt now points to a new, 
  10513. empty queue. You must establish a new qhead for qt. If you want to put objects 
  10514. into the original queue, you must call qtp.put(). 
  10515.  
  10516. You can use the member function splice() to restore a split queue to its 
  10517. original configuration. 
  10518.  
  10519. Cutting and Splicing a qtail Object illustrates the cutting and splicing of a 
  10520. qtail object. 
  10521.  
  10522.  
  10523. ΓòÉΓòÉΓòÉ 5.3.7.3. head - Create qhead Object ΓòÉΓòÉΓòÉ
  10524.  
  10525. Class: qtail 
  10526.  
  10527. qhead* head();
  10528.  
  10529. head() creates a qhead object for the queue attached to qt (if none exists) and 
  10530. returns a pointer to the new qhead object. If a qhead object already exists for 
  10531. the queue attached to qt, head() returns a pointer to that qhead object. 
  10532.  
  10533.  
  10534. ΓòÉΓòÉΓòÉ 5.3.7.4. o_type - Return Class Type ΓòÉΓòÉΓòÉ
  10535.  
  10536. Class: qtail 
  10537.  
  10538. object::objtype o_type();
  10539.  
  10540. o_type() returns the class type of qt, namely object::QTAIL. 
  10541.  
  10542.  
  10543. ΓòÉΓòÉΓòÉ 5.3.7.5. pending - Is Queue Full? ΓòÉΓòÉΓòÉ
  10544.  
  10545. Class: qtail 
  10546.  
  10547. int pending();
  10548.  
  10549. pending() returns a nonzero value if the queue attached to qt is full and 0 
  10550. otherwise. 
  10551.  
  10552.  
  10553. ΓòÉΓòÉΓòÉ 5.3.7.6. print - Print Queue Contents ΓòÉΓòÉΓòÉ
  10554.  
  10555. Class: qtail 
  10556.  
  10557. void print(int detail, int y=0);
  10558.  
  10559. print() prints information about the queue attached to qt on stdout. It calls 
  10560. the print() member function of the object base class. detail specifies the 
  10561. amount of information to be printed. The second argument, y, is used internally 
  10562. by the Task Library and has a default value of 0. 
  10563.  
  10564. If detail equals 0, the following information is printed: 
  10565.  
  10566. o The type of object, namely qtail 
  10567. o The integer value of the mode of the queue 
  10568. o The maximum size of the queue 
  10569. o The number of objects that the queue still has room for 
  10570. o The value of the qhead pointer 
  10571.  
  10572. If detail equals VERBOSE, the information listed above is printed along with 
  10573. information about the qhead object that is attached to the queue and 
  10574. information about all of the tasks that are waiting for qt. The following 
  10575. values for the qhead object are printed: 
  10576.  
  10577. o The type of object, namely qhead 
  10578. o The integer value of the mode of the queue 
  10579. o The maximum size of the queue 
  10580. o The number of elements in the queue 
  10581. o The value of the qtail pointer 
  10582.  
  10583. For each task that is waiting for qt, the following information is printed: 
  10584.  
  10585. o The value of the t_alert pointer, a private pointer to the object that 
  10586.   alerted the task 
  10587. o The value of the t_next pointer, a pointer to the next task on the chain 
  10588. o The value of the s_time pointer, a private pointer to the time at which the 
  10589.   task is scheduled to run 
  10590. o The value of the this pointer for the sched and object this task is derived 
  10591.   from 
  10592.  
  10593.  
  10594. ΓòÉΓòÉΓòÉ 5.3.7.7. put - Add Object to Queue ΓòÉΓòÉΓòÉ
  10595.  
  10596. Class: qtail 
  10597.  
  10598. int put(object* obj);
  10599.  
  10600. put() adds the object obj to the tail of the queue that is attached to qt. 
  10601. put() returns 1 if it succeeds. If the queue is full, the result depends on the 
  10602. mode: 
  10603.  
  10604. o WMODE: the task that executes put() is suspended until the queue is no longer 
  10605.   full 
  10606. o EMODE: the E_PUTFAIL runtime error is produced 
  10607. o ZMODE: put() returns 0 
  10608.  
  10609.  
  10610. ΓòÉΓòÉΓòÉ 5.3.7.8. rdspace - Space Remaining ΓòÉΓòÉΓòÉ
  10611.  
  10612. Class: qtail 
  10613.  
  10614. int rdspace();
  10615.  
  10616. rdspace() returns the number of objects that can be inserted into the queue 
  10617. attached to qt before it becomes full. The number of objects in a queue can be 
  10618. greater than the value specified by the maximum size of the queue if one of the 
  10619. two following conditions is true: 
  10620.  
  10621. o You call splice() to reunite two pieces of a queue that together contain more 
  10622.   objects than the value specified by the maximum size for the queue. 
  10623. o You call setmax() with an argument that is less than the number of objects 
  10624.   currently in the queue. 
  10625.  
  10626. If the number of objects in the queue is greater than the value specified by 
  10627. the maximum size of the queue, rdspace() returns a negative value. The absolute 
  10628. value of this value equals the number of objects that are in the queue over and 
  10629. above the maximum size of the queue. 
  10630.  
  10631.  
  10632. ΓòÉΓòÉΓòÉ 5.3.7.9. rdmax - Return Maximum Queue Size ΓòÉΓòÉΓòÉ
  10633.  
  10634. Class: qtail 
  10635.  
  10636. int rdmax();
  10637.  
  10638. rdmax() returns the maximum size of the queue attached to qt. 
  10639.  
  10640.  
  10641. ΓòÉΓòÉΓòÉ 5.3.7.10. rdmode - Return Mode ΓòÉΓòÉΓòÉ
  10642.  
  10643. Class: qtail 
  10644.  
  10645. qmodetype rdmode();
  10646.  
  10647. rdmode() returns the mode of qt. 
  10648.  
  10649.  
  10650. ΓòÉΓòÉΓòÉ 5.3.7.11. setmode - Set Mode ΓòÉΓòÉΓòÉ
  10651.  
  10652. Class: qtail 
  10653.  
  10654. void setmode(qmodetype qm);
  10655.  
  10656. setmode() sets the mode of qt to qm. 
  10657.  
  10658.  
  10659. ΓòÉΓòÉΓòÉ 5.3.7.12. setmax - Set Maximum Queue Size ΓòÉΓòÉΓòÉ
  10660.  
  10661. Class: qtail 
  10662.  
  10663. void setmax(int newmax);
  10664.  
  10665. setmax() sets the maximum size of the queue attached to qt to newmax. You can 
  10666. set the maximum size to be less than the current size of the queue. If you do, 
  10667. you cannot put objects on the queue until the number of objects in the queue is 
  10668. less than the new maximum size. If newmax is less than or equal to 0, the 
  10669. E_QZERO runtime error is produced. 
  10670.  
  10671.  
  10672. ΓòÉΓòÉΓòÉ 5.3.7.13. splice - Merge Queues ΓòÉΓòÉΓòÉ
  10673.  
  10674. Class: qtail 
  10675.  
  10676. void splice(qhead* qh);
  10677.  
  10678. splice() reconnects two queues that were split by a call to cut(). 
  10679. qtp->splice(qhp) merges the queue attached to the qtail pointer qtp with the 
  10680. queue attached to the qhead pointer qhp, where: 
  10681.  
  10682. o A call qt.cut() was made that returned the qtail pointer qtp. After this 
  10683.   call, qt is attached to a new queue and qtp is attached to the original 
  10684.   queue. 
  10685. o After the call to cut(), a call qt.head() was made that returned the qhead 
  10686.   pointer qhp. 
  10687.  
  10688. In the new queue, the objects that were in the queue attached to qtp precede 
  10689. the objects that were in the queue attached to qhp. splice() deletes both qtp 
  10690. and qhp. 
  10691.  
  10692. If the call to splice() makes an empty queue no longer empty or makes a full 
  10693. queue no longer full, it will alert all of the tasks that are waiting for 
  10694. either of these state changes and add these tasks to the scheduler's run chain. 
  10695.  
  10696. Cutting and Splicing a qtail Object illustrates the cutting and splicing of a 
  10697. qtail object. 
  10698.  
  10699.  
  10700. ΓòÉΓòÉΓòÉ 5.4. Interrupt_handler Class ΓòÉΓòÉΓòÉ
  10701.  
  10702. This chapter describes the Interrupt_handler class. This class allows tasks to 
  10703. wait for external events in the form of signals from the operating system. The 
  10704. header file task.h includes the declaration of a related class, 
  10705. Interrupt_alerter, but this class is not meant to be used directly. 
  10706. Interrupt_alerter is only documented here to show how it interacts with 
  10707. Interrupt_handler. 
  10708.  
  10709. The following topics are described in this chapter: 
  10710.  
  10711. o Declarations for Interrupt_handler in task.h 
  10712. o Public members of Interrupt_handler 
  10713. o Public members of Interrupt_alerter 
  10714. o Example of using Interrupt_handler 
  10715.  
  10716.  
  10717. ΓòÉΓòÉΓòÉ 5.4.1. Declarations for Interrupt_handler in task.h ΓòÉΓòÉΓòÉ
  10718.  
  10719. You must include the following statement in any file that uses members of the 
  10720. Interrupt_handler class: 
  10721.  
  10722. #include <task.h>
  10723.  
  10724. The following is an excerpt from task.h  that shows the relevant declarations 
  10725. for the members of Interrupt_handler: 
  10726.  
  10727. class Interrupt_handler : public object{
  10728. friend     class Interrupt_alerter;
  10729. friend SIG_FUNC_TYP sigFunc;
  10730.      int          id;
  10731.      int          got_interrupt;
  10732.      Interrupt_handler     *old;
  10733.      virtual void interrupt();
  10734. public:
  10735.                   Interrupt_handler(int sig_num);
  10736.                   ~Interrupt_handler();
  10737.      object::objtype     o_type();
  10738.      int          pending();
  10739.      void         print(int, int =0);
  10740. };
  10741. class Interrupt_alerter : public task {
  10742. public:
  10743.                   Interrupt_alerter();
  10744.                   ~Interrupt_alerter();
  10745. };
  10746. extern Interrupt_alerter     interrupt_alerter;
  10747.  
  10748.  
  10749. ΓòÉΓòÉΓòÉ 5.4.2. Public Members of Interrupt_handler ΓòÉΓòÉΓòÉ
  10750.  
  10751. The following functions are described: 
  10752.  
  10753. o Constructor for Interrupt_handler 
  10754. o Destructor for Interrupt_handler 
  10755. o interrupt - what to do when an interrupt occurs 
  10756. o o_type - return class 
  10757. o pending - did a signal occur 
  10758. o print - print Interrupt_handler information 
  10759.  
  10760.  
  10761. ΓòÉΓòÉΓòÉ 5.4.2.1. Constructor for Interrupt_handler ΓòÉΓòÉΓòÉ
  10762.  
  10763. Class: Interrupt_handler 
  10764.  
  10765. Interrupt_handler(int sig);
  10766.  
  10767. The Interrupt_handler constructor constructs a new Interrupt_handler object 
  10768. that is meant to wait for the signal number sig. When the signal with this 
  10769. signal number occurs, the private virtual function interrupt() is called. 
  10770.  
  10771. If several tasks are waiting for one Interrupt_handler task, when the interrupt 
  10772. occurs and that Interrupt_handler is ready, only one of the waiting tasks will 
  10773. run, and only this task has its state changed to RUNNING. Subsequent interrupts 
  10774. will allow the other waiting tasks to run, one task per interrupt. 
  10775.  
  10776. When an interrupt occurs, the current task is suspended and a special, built-in 
  10777. task, interrupt_alerter, is scheduled. When interrupt() returns, control 
  10778. resumes at the point where the current task was interrupted. As long as an 
  10779. Interrupt_handler object exists, the scheduler will not exit when the run chain 
  10780. becomes empty. It will wait for an interrupt instead. This happens because the 
  10781. Interrupt_handler constructor calls sched::keep_waiting(). The 
  10782. Interrupt_handler destructor calls  sched::dont_wait(). 
  10783.  
  10784.  
  10785. ΓòÉΓòÉΓòÉ 5.4.2.2. Destructor for Interrupt_handler ΓòÉΓòÉΓòÉ
  10786.  
  10787. Class: Interrupt_handler 
  10788.  
  10789. ~Interrupt_handler();
  10790.  
  10791. The Interrupt_handler destructor calls sched::dont_wait() to decrement the 
  10792. count of waiting Interrupt_handler() objects. The destructor also restores the 
  10793. handler for the signal for which the Interrupt_handler object was created. If 
  10794. the destructor for an Interrupt_handler has been called previously, the 
  10795. E_LOSTHNDLR runtime error is produced. Otherwise, the destructor verifies that 
  10796. it did indeed set up the Interrupt_handler object, and exits. 
  10797.  
  10798.  
  10799. ΓòÉΓòÉΓòÉ 5.4.2.3. interrupt - What to Do When an Interrupt Occurs ΓòÉΓòÉΓòÉ
  10800.  
  10801. Class: Interrupt_handler 
  10802.  
  10803. virtual void interrupt();
  10804.  
  10805. interrupt() is a virtual function that you can define in a class that is 
  10806. derived from Interrupt_handler to specify the action to be taken when an 
  10807. interrupt occurs. 
  10808.  
  10809.  
  10810. ΓòÉΓòÉΓòÉ 5.4.2.4. o_type - Return Class ΓòÉΓòÉΓòÉ
  10811.  
  10812. Class: Interrupt_handler 
  10813.  
  10814. object::objtype o_type();
  10815.  
  10816. o_type() is a virtual function that returns the type of the object, namely 
  10817. object::INTHANDLER. 
  10818.  
  10819.  
  10820. ΓòÉΓòÉΓòÉ 5.4.2.5. pending - Did a Signal Occur ΓòÉΓòÉΓòÉ
  10821.  
  10822. Class: Interrupt_handler 
  10823.  
  10824. int pending();
  10825.  
  10826. pending() returns a nonzero value every time it is called except for the first 
  10827. time it is called after a signal occurs. 
  10828.  
  10829.  
  10830. ΓòÉΓòÉΓòÉ 5.4.2.6. print - Print Interrupt_handler Information ΓòÉΓòÉΓòÉ
  10831.  
  10832. Class: Interrupt_handler 
  10833.  
  10834. void     print(int detail, int y=0);
  10835.  
  10836. print() prints information about the Interrupt_handler object. It calls the 
  10837. print() member function of the object base class. detail specifies how much 
  10838. information is printed. The second argument, y, is used internally by the Task 
  10839. Library and has a default value of 0. 
  10840.  
  10841. If detail equals 0, the following information is printed: 
  10842.  
  10843. o The type of object, namely Interrupt_handler. 
  10844. o The number of the signal that the Interrupt_handler object is waiting for. 
  10845. o The value of the private member got_interrupt. got_interrupt equals 1 if the 
  10846.   signal that the Interrupt_handler object is waiting for has been raised. It 
  10847.   equals 0 otherwise. 
  10848. o The value of the private member old. old is a pointer to the previous 
  10849.   Interrupt_handler object for the signal that the current Interrupt_handler 
  10850.   object is waiting for. 
  10851.  
  10852. If detail equals VERBOSE, the information listed above is printed along with 
  10853. the following information: 
  10854.  
  10855. o The objects in the list of previous Interrupt_handler objects for the current 
  10856.   signal 
  10857. o The value of the this pointer 
  10858. o The list of remembered tasks 
  10859.  
  10860.  
  10861. ΓòÉΓòÉΓòÉ 5.4.3. Public Members of Interrupt_alerter ΓòÉΓòÉΓòÉ
  10862.  
  10863. Note:  Interrupt_alerter is an internal class; do not use it directly. The task 
  10864. system creates an object of the Interrupt_alerter class when you create an 
  10865. object of Interrupt_handler. 
  10866.  
  10867. The following functions are described: 
  10868.  
  10869. o Constructor for Interrupt_alerter 
  10870. o Destructor for Interrupt_alerter 
  10871.  
  10872.  
  10873. ΓòÉΓòÉΓòÉ 5.4.3.1. Constructor for Interrupt_alerter ΓòÉΓòÉΓòÉ
  10874.  
  10875. Class: Interrupt_alerter 
  10876.  
  10877. Interrupt_alerter();
  10878.  
  10879. The Interrupt_alerter constructor creates an Interrupt_alerter object. This 
  10880. constructor is only called once, and this call is in the declaration of the 
  10881. interrupt_alerter object in task.h. The interrupt_alerter object alerts the 
  10882. Interrupt_handler objects and any others tasks that have received interrupts 
  10883. since the Interrupt_alerter constructor last ran. This constructor makes any 
  10884. tasks that are waiting for those Interrupt_handler objects eligible for 
  10885. execution. 
  10886.  
  10887. Note: 
  10888.  
  10889.  1. Interrupt_alerter is a class. interrupt_alerter is a predefined object of 
  10890.     the Interrupt_alerter class. 
  10891.  
  10892.  2. Any program that includes task.h will always have at least two tasks: the 
  10893.     interrupt_alerter task and the main() task. 
  10894.  
  10895.  
  10896. ΓòÉΓòÉΓòÉ 5.4.3.2. Destructor for Interrupt_alerter ΓòÉΓòÉΓòÉ
  10897.  
  10898. Class: Interrupt_alerter 
  10899.  
  10900. ~Interrupt_alerter();
  10901.  
  10902. The Interrupt_alerter() destructor cancels the Interrupt_alerter task. 
  10903.  
  10904.  
  10905. ΓòÉΓòÉΓòÉ 5.4.4. Example of Using Interrupt_handler ΓòÉΓòÉΓòÉ
  10906.  
  10907. The following example shows how the Interrupt_handler class can be used. In 
  10908. this example, the class MyHandler is derived from Interrupt_handler. Two 
  10909. objects of the MyHandler class are created (h1 and h2), and a signal is raised 
  10910. for each of them. 
  10911.  
  10912. #include <iostream.h>
  10913. #include <task.h>
  10914. #include <signal.h>
  10915.  
  10916. class MyHandler : public Interrupt_handler {
  10917. private:
  10918.      int sigNo;
  10919.      void interrupt();
  10920. public:
  10921.      MyHandler(int sig);
  10922. };
  10923.  
  10924. void MyHandler::interrupt()
  10925. {
  10926.      cout << "Interrupt " << sigNo << " has occurred." << endl;
  10927. }
  10928.  
  10929. MyHandler::MyHandler(int sig) :  Interrupt_handler(sig), sigNo(sig)
  10930. {
  10931.      cout << "Created an interrupt handler for interrupt "
  10932.           << sig << endl;
  10933. }
  10934.  
  10935. void main()
  10936. {
  10937.      MyHandler h1(SIGUSR1);
  10938.      MyHandler h2(SIGUSR2);
  10939.  
  10940.      //
  10941.      // Raise an interrupt for both MyHandler objects.
  10942.      //
  10943.      raise(SIGUSR2);
  10944.      raise(SIGUSR1);
  10945.      //
  10946.      // Explicitly destroy the handlers.
  10947.      //
  10948.      h1.Interrupt_handler::~Interrupt_handler();
  10949.      h2.Interrupt_handler::~Interrupt_handler();
  10950.      thistask->resultis(0);
  10951. }
  10952.  
  10953. This program produces the following output: 
  10954.  
  10955. Created an interrupt handler for interrupt 7
  10956. Created an interrupt handler for interrupt 8
  10957. Interrupt 8 has occurred.
  10958. Interrupt 7 has occurred.
  10959.  
  10960.  
  10961. ΓòÉΓòÉΓòÉ 5.5. Simulation Classes ΓòÉΓòÉΓòÉ
  10962.  
  10963. This chapter describes the simulation classes: histogrm, randint, urand, and 
  10964. erand. You can use these classes to create program simulations and gather 
  10965. statistics on them. 
  10966.  
  10967. The following topics are described in this chapter: 
  10968.  
  10969. o Declarations for the Simulation Classes in task.h 
  10970. o Public members of histogram 
  10971. o Public members of randint 
  10972. o Public members of urand 
  10973. o Public members of erand 
  10974.  
  10975.  
  10976. ΓòÉΓòÉΓòÉ 5.5.1. Declarations for the Simulation Classes in task.h ΓòÉΓòÉΓòÉ
  10977.  
  10978. You must include the following statement in any files that use the simulation 
  10979. classes: 
  10980.  
  10981. #include <task.h>
  10982.  
  10983. The following is an excerpt from the task.h header file that shows the relevant 
  10984. declarations for the members of the simulation classes: 
  10985.  
  10986. struct histogram
  10987. {
  10988.      int      l, r;
  10989.      int      binsize;
  10990.      int      nbin;
  10991.      int*     h;
  10992.      long     sum;
  10993.      long     sqsum;
  10994.               histogram(int=16, int=0, int=16);
  10995.      void     add(int);
  10996.      void     print();
  10997. };
  10998.  
  10999. class randint
  11000. {
  11001.      long     randx;
  11002. public:
  11003.               randint(long s = 0);
  11004.      void     seed(long s);
  11005.      int      draw();
  11006.      float    fdraw();
  11007. };
  11008.  
  11009. class urand : public randint
  11010. {
  11011. public:
  11012.      int      low, high;
  11013.               urand(int l, int h);
  11014.      int      draw();
  11015. };
  11016.  
  11017. class erand : public randint
  11018. public:
  11019. {    int      mean;
  11020.               erand(int m);
  11021.      int      draw();
  11022. };
  11023.  
  11024.  
  11025. ΓòÉΓòÉΓòÉ 5.5.2. Public Members of histogram ΓòÉΓòÉΓòÉ
  11026.  
  11027. Note:  The following descriptions assume that the functions are called as part 
  11028. of a histogrm object called hist. 
  11029.  
  11030. The following functions are described: 
  11031.  
  11032. o Constructor for histogram 
  11033. o add - add to a bin 
  11034. o print - print information about histogram object 
  11035.  
  11036. An example of using histogrm is also included. 
  11037.  
  11038.  
  11039. ΓòÉΓòÉΓòÉ 5.5.2.1. Constructor for histogram ΓòÉΓòÉΓòÉ
  11040.  
  11041. Class: histogrm 
  11042.  
  11043. histogram (int nb=16, int left=0, int right=16);
  11044.  
  11045. The histogrm constructor creates a histogrm object. A histogram object is a 
  11046. collection of bins, each of which encompasses a range of numbers. Elements can 
  11047. be added to these bins, and the range of the bins can be changed as elements 
  11048. are added to them. The histogrm object has the following data members: 
  11049.  
  11050. o l: the left end of the range of bins. 
  11051. o r: the right end of the range of bins. 
  11052. o binsize: the size of each bin. 
  11053. o nbin: the number of bins in the histogram. 
  11054. o h: a pointer to the array of bins. This array is dynamically allocated by the 
  11055.   constructor. 
  11056. o sum: the sum of the values added to the histogram using the add()  member 
  11057.   function. The constructor initializes sum to 0. 
  11058. o sqsum:  the sum of the squares of the values added to the histogram using the 
  11059.   add()  member function. The constructor initializes sqsum to 0. 
  11060.  
  11061. If the nbin member of a histogram object has a value of x, the histogram will 
  11062. consist of x bins, each covering a range of integers from left to right. The 
  11063. first argument of the constructor, nb, is the number of bins in the histogram. 
  11064. If the value of nb is odd, nb is changed to nb + 1. The second argument of the 
  11065. constructor, left, is the left end of the range of bins. The third argument of 
  11066. the constructor, right, is the right end of the range of bins. The default 
  11067. number of bins is 16, the default left end is 0, and the default right end is 
  11068. 16. 
  11069.  
  11070. If nbin is less than or equal to zero, or if left is greater than or equal to 
  11071. right, an E_HISTO runtime error is produced. The size of each bin equals the 
  11072. value binsize=(right-left)/nb. If binsize is not an integer, the number of bins 
  11073. is equal to the integer value that immediately follows this value, and the 
  11074. value of right is changed to 1 + nb *binsize. 
  11075.  
  11076.  
  11077. ΓòÉΓòÉΓòÉ 5.5.2.2. add - Add to a Bin ΓòÉΓòÉΓòÉ
  11078.  
  11079. Class: histogrm 
  11080.  
  11081. void add(int i);
  11082.  
  11083. add() adds one to the i th bin of hist. In other words, it performs the 
  11084. following operations: 
  11085.  
  11086. h[(i-1)/hist.binsize] = h[(i-1)/hist.binsize] + 1
  11087.  
  11088. If i is greater than or equal to hist.l and less than hist .r, hist.l, hist.r, 
  11089. and hist.binsize are unchanged. However, if i is less than hist.l, these steps 
  11090. are followed: 
  11091.  
  11092.  1. The value of hist .binsize is doubled. 
  11093.  2. The value (hist .r - hist.nbin*hist.binsize) is calculated. If this value 
  11094.     is less than or equal to i, it becomes the new value of hist.l. 
  11095.  3. If this value is still greater than i, return to step 1 and repeat the 
  11096.     process. 
  11097.  
  11098. If, on the other hand, i is greater than hist .r, these steps are followed: 
  11099.  
  11100.  1. The value of hist .binsize is doubled. 
  11101.  2. The value (hist .l + hist .nbin*hist .binsize) is calculated. If this value 
  11102.     is greater than i, it becomes the new value of hist .r. 
  11103.  3. If this value is less than or equal to i, return to step 1 and repeat the 
  11104.     process. 
  11105.  
  11106.  
  11107. ΓòÉΓòÉΓòÉ 5.5.2.3. print - Print Information about histogram Object ΓòÉΓòÉΓòÉ
  11108.  
  11109. Class: histogrm 
  11110.  
  11111. void print();
  11112.  
  11113. print() displays on standard output the following information for each bin in 
  11114. hist that is not empty: 
  11115.  
  11116. o The range of the bin 
  11117. o The number of elements in the bin 
  11118.  
  11119.  
  11120. ΓòÉΓòÉΓòÉ 5.5.2.4. Example of Using histogram Class ΓòÉΓòÉΓòÉ
  11121.  
  11122. Class: histogrm 
  11123.  
  11124. The following program shows how you can use the histogrm class. In this 
  11125. example, a histogrm object is created with the following characteristics: 
  11126.  
  11127. o The number of bins is 16 
  11128. o The size of each bin is 2 
  11129. o The left end of the range of bins is 0 
  11130. o The right end of the range of bins is 32 
  11131.  
  11132. The first call to add() has an argument, 34, that is greater than the right end 
  11133. of the range of bins. Therefore, the following steps are followed: 
  11134.  
  11135. o The size of the bins is doubled to 4 
  11136. o The right end of the range of bins is changed to left + (numbins * binsize) = 
  11137.   64 
  11138.  
  11139. At this point, the histogram has the following characteristics: 
  11140.  
  11141. o The number of bins is 16 
  11142. o The size of each bin is 4 
  11143. o The left end of the range is 0 
  11144. o The right end of the range is 64 
  11145.  
  11146. The arguments for the second and third calls to add() are both within the range 
  11147. of the bins, so the number and the range of the bins remain unchanged. The 
  11148. argument of the fourth call to add(), - 4, is less than the left end of the 
  11149. range of bins, so the following steps are followed: 
  11150.  
  11151. o The size of the bins is doubled to 8 
  11152. o The left end of the range of bins is changed to right - (numbins * binsize) = 
  11153.   - 64 
  11154.  
  11155. Thus, after the fourth call to add(), the histogram has the following 
  11156. characteristics: 
  11157.  
  11158. o The number of bins is 16 
  11159. o The size of each bin is 8 
  11160. o The left end of the range is -64 
  11161. o The right end of the range is 64 
  11162.  
  11163. The following is the code for this example: 
  11164.  
  11165. #include <task.h>
  11166.  
  11167. void main ()
  11168. {
  11169.      int a = 34;    // Some integers.
  11170.      int b = 4;
  11171.      int c = 9;
  11172.      int d = -4;
  11173.      //
  11174.      // create a histogram
  11175.      //
  11176.      histogram h (16,0,32);
  11177.      //
  11178.      // add values to the histogram
  11179.      //
  11180.      h.add(a);
  11181.      h.add(b);
  11182.      h.add(c);
  11183.      h.add(d);
  11184.      //
  11185.      // print the contents of the histogram
  11186.      //
  11187.      h.print();
  11188.       thistask->resultis(0);
  11189. }
  11190.  
  11191. This program produces the following output: 
  11192.  
  11193. [-8:0] : 1
  11194. [0:8] : 1
  11195. [8:16] : 1
  11196. [32:40] : 1
  11197.  
  11198.  
  11199. ΓòÉΓòÉΓòÉ 5.5.3. Public Members of randint ΓòÉΓòÉΓòÉ
  11200.  
  11201. The following functions are described: 
  11202.  
  11203. o Constructor for randint 
  11204. o draw - return a random integer 
  11205. o fdraw - return a random floating-point value 
  11206. o seed - initialize random number generator 
  11207.  
  11208.  
  11209. ΓòÉΓòÉΓòÉ 5.5.3.1. randint Constructor ΓòÉΓòÉΓòÉ
  11210.  
  11211. Class: randint 
  11212.  
  11213. randint(long seed=0);
  11214.  
  11215. randint() constructs a randint object. seed is used as a seed for the random 
  11216. number generator. The default value of seed is 0. 
  11217.  
  11218.  
  11219. ΓòÉΓòÉΓòÉ 5.5.3.2. draw - Return a Random Integer ΓòÉΓòÉΓòÉ
  11220.  
  11221. Class: randint 
  11222.  
  11223. int draw();
  11224.  
  11225. draw() returns a random integer value in the range from 0 to UINT_MAX (defined 
  11226. in limits.h). Integers returned by draw() are uniformly distributed in this 
  11227. range. 
  11228.  
  11229.  
  11230. ΓòÉΓòÉΓòÉ 5.5.3.3. fdraw - Return a Random Floating-Point Value ΓòÉΓòÉΓòÉ
  11231.  
  11232. Class: randint 
  11233.  
  11234. float fdraw();
  11235.  
  11236. fdraw() returns a random floating-point value in the range from 0 to 1. 
  11237. Floating-point values returned by fdraw() are uniformly distributed in this 
  11238. range. 
  11239.  
  11240.  
  11241. ΓòÉΓòÉΓòÉ 5.5.3.4. seed - Initialize Random Number Generator ΓòÉΓòÉΓòÉ
  11242.  
  11243. Class: randint 
  11244.  
  11245. void seed(long seednum);
  11246.  
  11247. seed() reinitializes the random number generator with the seed seednum. 
  11248.  
  11249.  
  11250. ΓòÉΓòÉΓòÉ 5.5.4. Public Members of urand ΓòÉΓòÉΓòÉ
  11251.  
  11252. The following functions are described: 
  11253.  
  11254. o Constructor for urand 
  11255. o draw - return a random number from specified range 
  11256.  
  11257.  
  11258. ΓòÉΓòÉΓòÉ 5.5.4.1. urand Constructor ΓòÉΓòÉΓòÉ
  11259.  
  11260. Class: urand 
  11261.  
  11262. urand(int low, int high);
  11263.  
  11264. urand() constructs a urand object. low and high define the lower and upper 
  11265. bounds for the uniform distribution of the random numbers generated by this 
  11266. object. If low is greater than high, urand() produces the runtime error 
  11267. E_URAND. 
  11268.  
  11269.  
  11270. ΓòÉΓòÉΓòÉ 5.5.4.2. draw - Return a Random Number from Specified Range ΓòÉΓòÉΓòÉ
  11271.  
  11272. Class: urand 
  11273.  
  11274. int draw();
  11275.  
  11276. draw() returns a random integer value from the range specified in the call to 
  11277. the urand constructor. Integer values returned by draw() are uniformly 
  11278. distributed in this range. 
  11279.  
  11280.  
  11281. ΓòÉΓòÉΓòÉ 5.5.5. Public Members of erand ΓòÉΓòÉΓòÉ
  11282.  
  11283. The following functions are described: 
  11284.  
  11285. o Constructor for erand 
  11286. o draw - return a random number from specified range 
  11287.  
  11288.  
  11289. ΓòÉΓòÉΓòÉ 5.5.5.1. erand Constructor ΓòÉΓòÉΓòÉ
  11290.  
  11291. Class: erand 
  11292.  
  11293. erand(int mean);
  11294.  
  11295. erand() constructs an erand object. mean is the mean for the exponential 
  11296. distribution of the random numbers generated by this object. 
  11297.  
  11298.  
  11299. ΓòÉΓòÉΓòÉ 5.5.5.2. draw - Return a Random Number from Specified Range ΓòÉΓòÉΓòÉ
  11300.  
  11301. Class: erand 
  11302.  
  11303. int draw();
  11304.  
  11305. draw() returns a random integer value. draw() returns values that are 
  11306. exponentially distributed around the mean value specified in the call to the 
  11307. erand constructor. 
  11308.  
  11309.  
  11310. ΓòÉΓòÉΓòÉ 6. Appendixes and Glossary ΓòÉΓòÉΓòÉ
  11311.  
  11312. Extended Complex Mathematics Library Examples 
  11313.   Contains examples of how to determine the roots of a complex number, and how 
  11314.   to define your own complex_error function. 
  11315. Extended Task Library Examples 
  11316.   Contains examples of how to use the Task Library to implement a parallel 
  11317.   algorithm and an event-driven simulation. 
  11318.  
  11319.  
  11320. ΓòÉΓòÉΓòÉ 6.1. Extended Complex Mathematics Library Examples ΓòÉΓòÉΓòÉ
  11321.  
  11322. This appendix contains two examples that show some uses of the Complex 
  11323. Mathematics Library. 
  11324.  
  11325. The two examples are: 
  11326.  
  11327. o Finding the roots of a complex number 
  11328. o Defining your own complex_error function 
  11329.  
  11330.  
  11331. ΓòÉΓòÉΓòÉ 6.1.1. Finding the Roots of a Complex Number ΓòÉΓòÉΓòÉ
  11332.  
  11333. Note:  In the following text, exponents appear in italicized square brackets. 
  11334.  
  11335. The following example shows how you can use the Complex Mathematics Library to 
  11336. calculate the roots of a complex number. For every positive integer n, each 
  11337. complex number z has exactly n distinct nth roots. Suppose that in the complex 
  11338. plane the angle between the real axis and point z is theta , and the distance 
  11339. between the origin and the point z is r. Then z has the polar form (r, theta ), 
  11340. and the n roots of z have the values: 
  11341.  
  11342.      sigma
  11343.      sigma ┬╖omega
  11344.      sigma ┬╖omega [2]
  11345.      sigma ┬╖omega [3]
  11346.      .
  11347.      .
  11348.      .
  11349.      sigma ┬╖omega [n-1]
  11350.  
  11351. where omega  is a complex number with the value: 
  11352.  
  11353.      omega  = ( cos(2pi /n), sin(2pi /n) )
  11354.  
  11355. and sigma  is a complex number with the value: 
  11356.  
  11357.      sigma  = r[1/n ]( cos(theta /n), sin(theta /n) )
  11358.  
  11359. The following code includes the functions get_omega() to calculate the value of 
  11360. omega , and get_sigma() to calculate the value of sigma . The user is prompted 
  11361. for the complex value z and the value of n. After the values of omega  and 
  11362. sigma  have been calculated, the n roots of z are calculated and printed. 
  11363.  
  11364. #include <iostream.h>
  11365. #include <complex.h>
  11366. #include <math.h>
  11367.  
  11368. //
  11369. // function to calculate the value of omega for a given value
  11370. // of n
  11371. //
  11372. complex get_omega(double n)
  11373.      {
  11374.      complex omega = complex(cos((2.0*M_PI)/n), sin((2.0*M_PI)/n));
  11375.      return omega;
  11376.      }
  11377.  
  11378. //
  11379. // function to calculate the value of sigma for a given value of
  11380. // n and a given complex value
  11381. //
  11382. complex get_sigma(complex comp_val, double n)
  11383. {
  11384.      double rn, r, theta;
  11385.      complex sigma;
  11386.      r = abs(comp_val);
  11387.      theta = arg(comp_val);
  11388.      rn = pow(r,(1.0/n));
  11389.      sigma = rn * complex(cos(theta/n),sin(theta/n));
  11390.      return sigma;
  11391. }
  11392.  
  11393. void main()
  11394. {
  11395.      double n;
  11396.      complex input, omega, sigma;
  11397.      //
  11398.      // prompt the user for a complex number
  11399.      //
  11400.      cout << "Please enter a complex number" << endl;
  11401.      cin >> input;
  11402.      //
  11403.      // prompt the user for the value of n
  11404.      //
  11405.      cout << "What root would you like of this number?" << endl;
  11406.      cin >> n;
  11407.      //
  11408.      // calculate the value of omega
  11409.      //
  11410.      omega = get_omega(n);
  11411.      cout << "Here is omega " << omega << endl;
  11412.      //
  11413.      // calculate the value of sigma
  11414.      //
  11415.      sigma = get_sigma(input,n);
  11416.      cout << "Here is sigma " << sigma << endl;
  11417.      cout << "Here are the " << n << " roots of " << input << endl;
  11418.      for (int i = 0; i < n ; i++)
  11419.      {
  11420.           cout << sigma*(pow(omega,i)) << endl;
  11421.      }
  11422. }
  11423.  
  11424. If you enter the complex value (-7, 24) for z and 2 for n, this program 
  11425. produces the following output: 
  11426.  
  11427. Here is omega ( -1, 1.22465e-16)
  11428. Here is sigma ( 3, 4)
  11429. Here are the 2 roots of ( -7, 24)
  11430. ( 3, 4)
  11431. ( -3, -4)
  11432.  
  11433.  
  11434. ΓòÉΓòÉΓòÉ 6.1.2. Defining Your Own complex_error Function ΓòÉΓòÉΓòÉ
  11435.  
  11436. You can either use the default version of complex_error() described in Errors 
  11437. Handled by the Complex Mathematics Library, or you can define your own version 
  11438. of the function.  In the following example, complex_error() is redefined: 
  11439.  
  11440. #include <iostream.h>
  11441. #include <complex.h>
  11442. #include <float.h>
  11443.  
  11444. //
  11445. // redefinition of the complex_error function
  11446. //
  11447. int complex_error(c_exception &c)
  11448. {
  11449.      cout << "================" << endl;
  11450.      cout << "   Exception    " << endl;
  11451.      cout << "type = " << c.type << endl;
  11452.      cout << "name = " << c.name << endl;
  11453.      cout << "arg1 = " << c.arg1 << endl;
  11454.      cout << "arg2 = " << c.arg2 << endl;
  11455.      cout << "retval = " << c.retval << endl;
  11456.      cout << "================" << endl;
  11457.      return 0;
  11458. }
  11459.  
  11460. void main()
  11461. {
  11462.      complex c1(DBL_MAX,0);
  11463.      complex result;
  11464.      result = exp(c1);
  11465.      cout << "exp(" << c1 << ")= " << result << endl;
  11466. }
  11467.  
  11468. This example produces the following output: 
  11469.  
  11470. ================
  11471.    Exception
  11472. type = 3
  11473. name = exp
  11474. arg1 = ( 1.79769e+308, 0)
  11475. arg2 = ( 0, 0)
  11476. retval = ( infinity, -infinity)
  11477. ================
  11478. exp(( 1.79769e+308, 0)) = ( infinity, -infinity)
  11479.  
  11480. If the redefinition of complex_error() in the above code is commented out, the 
  11481. default definition of complex_error() is used, and the program produces the 
  11482. following output 
  11483.  
  11484. exp(( 1.79769e+308, 0)) = ( infinity, -infinity)
  11485.  
  11486.  
  11487. ΓòÉΓòÉΓòÉ 6.2. Extended Task Library Examples ΓòÉΓòÉΓòÉ
  11488.  
  11489. This appendix contains two examples that show how you can use the Task Library. 
  11490.  
  11491. The two examples are: 
  11492.  
  11493. o An example of implementing parallel algorithms 
  11494. o An example of implementing event-driven simulations 
  11495.  
  11496.  
  11497. ΓòÉΓòÉΓòÉ 6.2.1. An Example of Implementing Parallel Algorithms ΓòÉΓòÉΓòÉ
  11498.  
  11499. Note:  In the following text, exponents are shown in italicized square 
  11500. brackets. 
  11501.  
  11502. The following example shows how you can use the Task Library to implement 
  11503. parallel algorithms. In this example, the "fan-in" parallel algorithm is used 
  11504. in the calculation of the inner product of two vectors. The program creates a 
  11505. series of objects of the class proc, each of which is identified by a node 
  11506. number. These proc objects are used to calculate the inner product. The program 
  11507. also calculates the inner product serially. 
  11508.  
  11509. The "fan-in" algorithm calculates the inner product of two vectors, A and B. If 
  11510. A and B are both of size n, their inner product is the sum of the products of 
  11511. the corresponding elements: A[0]*B[0] + A[1]*B[1] + . . . + A[n]*B[n]. The 
  11512. algorithm begins with the creation of n nodes numbered 0 to n-1. Each node i 
  11513. contains the product of the ith elements of the two vectors. Thus, node 0 
  11514. contains A[0]*B[0]. At the end of the first step, each even-numbered node i 
  11515. contains A[i]*B[i] + A[i+1]*B[i+1], and the odd-numbered nodes are no longer 
  11516. used. Thus, node 0 contains A[0]*B[0] + A[1]*B[1] at the end of the first step. 
  11517. At the end of the second step, each node i (where i == 0 modulo 4) contains the 
  11518. sum of its previous contents and the contents of node i + 2. Thus, each node i 
  11519. (where i == 0 modulo 4) contains A[i]*B[i] + A[i+1]*B[i+1] + A[i+2]*B[i+2] + 
  11520. A[i +3]*B[i+3], and the other nodes are no longer used. In general, at the end 
  11521. of step s, node i (where i = 0 modulo 2[S]) contains the sum A[i]*B[i] + 
  11522. A[i+1]*B[i+1] + . . . + A[2[S]-1]*B[2[S]-1]. At the end of the algorithm, node 
  11523. 0 contains the inner product of the two vectors. 
  11524.  
  11525. #include <task.h>
  11526. #include <iostream.h>
  11527. #include <math.h>
  11528.  
  11529. //
  11530. // The size of the two vectors a and b.
  11531. // This size also denotes the number
  11532. // of processors that will be used
  11533. // in the parallel execution.
  11534. //
  11535. #define VECTOR_SIZE 10000
  11536.  
  11537. int power2( int m)
  11538. {
  11539.      //
  11540.      // compute 2^m
  11541.      //
  11542.      int product = 1;
  11543.      for (register i=1; i<=m; ++i)
  11544.           product *= 2;
  11545.      return product;
  11546. }
  11547.  
  11548. inline double log2( double x)
  11549. {
  11550.      //
  11551.      // compute the logarithm of x base 2
  11552.      //
  11553.      return (log(x)/log(2));
  11554. }
  11555. //
  11556. // receiver() returns the role of the processor with id "node"
  11557. // at the step "step".
  11558. // It returns true if the particular processor is supposed
  11559. // to "receive" the result of another processor.
  11560. // It returns false if the particular processor is supposed to
  11561. // stop executing and make its result available to another
  11562. // processor.
  11563.  
  11564. int receiver (int node, int step) {
  11565.      int numerator, denominator;
  11566.      if (node == 0) return 1;
  11567.      else {
  11568.           numerator = node - power2(step-1);
  11569.           denominator = power2(step);
  11570.           if (((float(numerator)/float(denominator)) -
  11571.                 numerator/denominator) <= 1E-6)
  11572.                return 0;
  11573.           else
  11574.                return 1;
  11575.      }
  11576. }
  11577.  
  11578. //
  11579. // Input data.
  11580. //
  11581. struct vectors {
  11582.      int *a, *b;
  11583.      double ab;  // Inner product a*b
  11584.      vectors();
  11585. };
  11586.  
  11587. vectors::vectors() {
  11588.      //
  11589.      // Initialize vectors a and b with random numbers
  11590.      // in the interval [1,5]
  11591.      //
  11592.      urand rand(1,5);
  11593.      a = new int [VECTOR_SIZE];
  11594.      b = new int [VECTOR_SIZE];
  11595.      for (register i=0; i<VECTOR_SIZE; ++i) {
  11596.           a[i] = rand.draw();
  11597.           b[i] = rand.draw();
  11598.      }
  11599.      ab = 0;
  11600.      //
  11601.      // Calculate the inner product serially
  11602.      //
  11603.      for (i=0; i<VECTOR_SIZE; ++i)
  11604.           ab += a[i]*b[i] ;
  11605. }
  11606.  
  11607. //
  11608. // A processor task
  11609. //
  11610. class proc : public task {
  11611.      int      my_node ;
  11612. public:
  11613.      proc(int, vectors *, task **);
  11614. };
  11615.  
  11616. proc::proc(int n, vectors *vptr, task **t):my_node(n) {
  11617.      int steps = (int) log2 ((double) VECTOR_SIZE) ;
  11618.      if ((log2((double) VECTOR_SIZE) - steps) >= 1E-6) steps ++;
  11619.      int s=vptr->a[my_node]*vptr->b[my_node];
  11620.      //
  11621.      // multiply Ai*Bi
  11622.      //
  11623.      int wait_for;
  11624.      for( int step=1; step <= steps; ++step) {
  11625.           if (receiver(my_node, step)) {
  11626.                wait_for=my_node+power2(step-1);
  11627.                //
  11628.                // Special case when
  11629.                // VECTOR_SIZE is not an
  11630.                // integral power of 2.
  11631.                //
  11632.                if (wait_for >= VECTOR_SIZE)
  11633.                     continue;
  11634.                s += t[wait_for]->result();
  11635.           }
  11636.           else {
  11637.                break; // terminate
  11638.           }
  11639.      }
  11640.      resultis(s);
  11641. }
  11642.  
  11643. void main() {
  11644.      cout << "Initializing the vectors a and b of size: "
  11645.           << VECTOR_SIZE
  11646.           << " with random numbers in the interval [1,5]... "
  11647.           << endl;
  11648.      vectors v;
  11649.      cout << "Calculating inner product ..." << endl;
  11650.      vectors *vptr = &v;
  11651.      task **processor;
  11652.      processor = new task *[VECTOR_SIZE] ;
  11653.      //
  11654.      // Order of creation of tasks must be from last to first
  11655.      //
  11656.      for (register i=VECTOR_SIZE-1; i>=0; --i)
  11657.           processor[i] = new proc(i,vptr,processor);
  11658.      int sum;
  11659.      sum= processor[0]->result();
  11660.      //
  11661.      // Wait for result of processor 0.
  11662.      // display and validate result
  11663.      //
  11664.      cout << "This is the inner product of the given vectors\n"
  11665.           << "Calculated serially: " << v.ab << endl
  11666.           << "Calculated through the fan-in algorithm: "
  11667.           << sum << endl;
  11668.      thistask ->resultis(0);
  11669. }
  11670.  
  11671. The output created by this program looks like this: 
  11672.  
  11673. Initializing the vectors a and b of size: 10000 with random numbers
  11674. in the interval [1,5]...
  11675. Calculating inner product ...
  11676. This is the inner product of the given vectors
  11677. Calculated serially: 89735
  11678. Calculated through the fan-in algorithm: 89735
  11679.  
  11680.  
  11681. ΓòÉΓòÉΓòÉ 6.2.2. An Example of Implementing Event-Driven Simulations ΓòÉΓòÉΓòÉ
  11682.  
  11683. The following example shows how you can use the Task Library to implement 
  11684. event-driven simulations. This example simulates a system consisting of two 
  11685. M/M/1 queues that accept packets, process them, and release them. The two M/M/1 
  11686. queues have different service rates. In the simulation, the packets are 
  11687. produced by the producer object input. The M/M/1 queues are represented by 
  11688. exponential_service_center objects mm1_q1 and mm1_q2. The packets are accepted 
  11689. at the end of their processing by the consumer object output. output calculates 
  11690. the throughput of the system (the total number of packets processed divided by 
  11691. the total time taken to process them). The producer object input, the two M/M/1 
  11692. queues, and the consumer object output are connected by a series of Task 
  11693. Library queues. 
  11694.  
  11695. The following example could easily be modified to model systems that have 
  11696. exponential service centers in parallel. 
  11697.  
  11698. Note:  The word "queue" is used in two different senses in this section. The 
  11699. M/M/1 queues that are implemented by the exponential_service_center objects are 
  11700. meant to simulate the behavior of systems that perform some kind of operation 
  11701. on a series of customers. The amount of time that each operation takes is 
  11702. independent of the previous state of the system. The Task Library queues, on 
  11703. the other hand, are queue data structures. See Queue Management Classes for a 
  11704. description of the qhead and qtail classes. 
  11705.  
  11706. This simulation follows the steps illustrated in Structure of the Event-Driven 
  11707. Simulation Example: 
  11708.  
  11709.  1. The producer object input creates packet objects that it places on the 
  11710.     qtail object packet_q. 
  11711.  
  11712.  2. The exponential_service_center object mm1_q1 takes packet objects from the 
  11713.     qhead object that corresponds with the qtail object packet_q. mm1_q1 
  11714.     "processes" these packet objects and then places them on the qtail object 
  11715.     that corresponds with the qhead object intermediate_result_q. 
  11716.  
  11717.  3. The exponential_service_center object mm1_q2 takes packet objects from the 
  11718.     qhead object intermediate_result_q. mm1_q2 "processes" these packet objects 
  11719.     and then places them on the qtail object that corresponds to the qhead 
  11720.     object result_q. 
  11721.  
  11722.  4. The consumer object output takes packet objects from the qhead object 
  11723.     result_q and calculates throughput statistics. 
  11724.  
  11725.  
  11726.                                   ΓöîΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÉ
  11727.                                   Γöé  input  Γöé
  11728.                                   ΓööΓöÇΓöÇΓöÇΓöÇΓö¼ΓöÇΓöÇΓöÇΓöÇΓöÿ
  11729.                                        
  11730.                           ΓöîΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÉ
  11731.                           Γöé        packet_q        Γöé
  11732.                           ΓööΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓö¼ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÿ
  11733.                                        
  11734.                                  ΓöîΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÉ
  11735.                                  Γöé   mm1_q   Γöé
  11736.                                  ΓööΓöÇΓöÇΓöÇΓöÇΓöÇΓö¼ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÿ
  11737.                                        
  11738.                           ΓöîΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÉ
  11739.                           Γöé  intermediate_result_q Γöé
  11740.                           ΓööΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓö¼ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÿ
  11741.                                        
  11742.                                  ΓöîΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÉ
  11743.                                  Γöé   mm1_q2  Γöé
  11744.                                  ΓööΓöÇΓöÇΓöÇΓöÇΓöÇΓö¼ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÿ
  11745.                                        
  11746.                           ΓöîΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÉ
  11747.                           Γöé        result_q        Γöé
  11748.                           ΓööΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓö¼ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÿ
  11749.                                        
  11750.                                   ΓöîΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÉ
  11751.                                   Γöé output  Γöé
  11752.                                   ΓööΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÿ
  11753.  
  11754. Structure of the Event-Driven Simulation Example 
  11755.  
  11756. #include <task.h>
  11757. #include <iostream.h>
  11758. #include <time.h>
  11759. #include <limits.h>
  11760. #define abs(A) (((A)<0.0)? -(A):(A))
  11761. static myseed = abs(time(0)+0x74637602) ;
  11762. // Request to the system
  11763. class packet : public object {
  11764. public:
  11765.         int     interval;
  11766.         long    sys_arrival_time;
  11767.         long    sys_departure_time;
  11768. };
  11769. //
  11770. // Task that produces packet arrivals to the system.
  11771. //
  11772. class producer : public task {
  11773.         erand *rnd;
  11774. public:
  11775.         producer(int, float, qtail *, task*);
  11776.         ~producer() { delete rnd; }
  11777. };
  11778.  
  11779. producer::producer(int arrivals, float lambda, qtail *packet_q, task *end) {
  11780.         packet *pckt;
  11781.         int interarrival_time, prev_event_time = 0;
  11782.         float *f = new float(0.0) ;
  11783.         rnd = new erand(int(1/lambda));         // random number generator.
  11784.         rnd->seed(myseed++);
  11785.         for (register i=0; i<arrivals; ++i) {
  11786.                 //
  11787.                 // create packet arrivals
  11788.                 //
  11789.                 pckt = new packet;
  11790.                 interarrival_time=rnd->draw();
  11791.                 *f += interarrival_time;
  11792.                 pckt->interval = interarrival_time;
  11793.                 pckt->sys_arrival_time =
  11794.                       prev_event_time+interarrival_time;
  11795.                 prev_event_time += interarrival_time;
  11796.                 packet_q->put(pckt);
  11797.         }
  11798.         //
  11799.         // Wait for the result analyzer to terminate
  11800.         //
  11801.         (void) end->result();
  11802.         *f = *f/arrivals;
  11803.         //
  11804.         // Return achieved input rate
  11805.         //
  11806.         *f = 1.0/(*f) ;
  11807.         resultis((int) f);
  11808. }
  11809. //
  11810. // An M/M/1 queue simulator. This task receives packets
  11811. // from a queue and places them into another
  11812. // queue after "processing".
  11813. //
  11814. class exponential_service_center : public task {
  11815.         erand *rnd;
  11816.         qtail *local_q_t;
  11817.         qhead *local_q_h;
  11818. public:
  11819.         exponential_service_center(int, float, qhead*,
  11820.              qtail*, task *);
  11821.         ~exponential_service_center()
  11822.              { delete local_q_t; delete rnd; }
  11823. };
  11824.  
  11825. exponential_service_center::exponential_service_center (
  11826.      int arrivals, float mu,
  11827.      qhead *input, qtail *output, task *end)
  11828. {
  11829.         rnd = new erand(int (1/mu));
  11830.         rnd -> seed (myseed++);
  11831.         local_q_t = new qtail;
  11832.         local_q_h = local_q_t -> head();
  11833.  
  11834.         int cust=0, service_time, terminated = 0;
  11835.         register int cur_arrivals = 0;   // Arrivals so far
  11836.         long next_arrival, next_departure;
  11837.         float *f = new float (0.0);
  11838.         packet *pckt = (packet *) input->get(), *departing_pckt;
  11839.         next_arrival = sched::get_clock() + pckt -> interval;
  11840.         next_departure = LONG_MAX;        // "LONG_MAX" is used as a flag
  11841.  
  11842.         //
  11843.         // Event loop (Note that every packet generates two events --
  11844.         //             arrival/departure)
  11845.         //
  11846.         for (register i=0; i<2*arrivals; ++i) {
  11847.                 if ((next_arrival == LONG_MAX)
  11848.                    && (next_departure == LONG_MAX)) {
  11849.                    //
  11850.                    // Wait for an arrival of a packet
  11851.                    // at the input queue.
  11852.                    //
  11853.                         pckt = (packet *) input->get();
  11854.                         next_arrival = sched::get_clock()
  11855.                              + pckt -> interval;
  11856.                 }
  11857.                 //
  11858.                 //      Wait for event
  11859.                 //
  11860.                 delay((next_arrival<next_departure)?
  11861.                       (next_arrival - sched::get_clock()):
  11862.                       (next_departure - sched::get_clock()) ) ;
  11863.  
  11864.                 if ( next_arrival < next_departure ) {
  11865.                         //
  11866.                         // Packet arrival
  11867.                        //
  11868.                         cust += 1;
  11869.                         if (cust == 1) {
  11870.                                 //
  11871.                                 // If there's only one packet in the local
  11872.                                 // "departure" queue, schedule its departure
  11873.                                 //
  11874.                                 service_time = rnd->draw();
  11875.                                 *f += service_time;
  11876.                                 next_departure = sched::get_clock()
  11877.                                       + service_time;
  11878.                                 pckt -> interval = 0 ;  // No more inter-arrival time
  11879.                         }
  11880.                         //
  11881.                         // Put packet into a local "departure" queue
  11882.                         //
  11883.                         local_q_t -> put(pckt);
  11884.                         //
  11885.                         // Schedule new arrival if there are
  11886.                         // packets in the input queue
  11887.                         if (++cur_arrivals < arrivals) {
  11888.                                 if (input->rdcount() > 0) {
  11889.                                         pckt = (packet *) input->get();
  11890.                                         next_arrival = sched::get_clock() +
  11891.                                                        pckt -> interval;
  11892.                                 }
  11893.                                 else
  11894.                                         next_arrival = LONG_MAX;
  11895.                         }
  11896.                         else if (cur_arrivals == arrivals)
  11897.                                 next_arrival = LONG_MAX;
  11898.                 }
  11899.                 else {
  11900.                      //
  11901.                      // packet departure
  11902.                      //
  11903.                      cust -= 1;
  11904.                      //
  11905.                      // Remove packet from local "departure"
  11906.                      // queue and put it in the output queue
  11907.                      //
  11908.                      departing_pckt = (packet *) local_q_h->get();
  11909.                      departing_pckt -> sys_departure_time =
  11910.                                        sched::get_clock();
  11911.                      output -> put(departing_pckt);
  11912.                      if (cust >0) {
  11913.                              service_time = rnd->draw();
  11914.                              *f += service_time;
  11915.                              next_departure = sched::get_clock()
  11916.                                   + service_time;
  11917.                              departing_pckt = (packet *) local_q_h->get();
  11918.                              departing_pckt -> interval = 0 ;
  11919.                              local_q_h->putback(departing_pckt);
  11920.                      }
  11921.                      else
  11922.                              next_departure = LONG_MAX;
  11923.                 }
  11924.         }
  11925.         //
  11926.         // Wait for termination of result analyzer
  11927.         //
  11928.         (void) end->result();
  11929.         //
  11930.         // Return achieved output rate (1/service rate)
  11931.         //
  11932.         *f = *f/arrivals;
  11933.         *f = 1/(*f) ;
  11934.         resultis ((int) f);
  11935. }
  11936.  
  11937. //
  11938. // This task generates the average system throughput and delay
  11939. //
  11940. class consumer : public task {
  11941. public:
  11942.         float   throughput, delay;
  11943.                 consumer(int, qhead *);
  11944. };
  11945.  
  11946. consumer::consumer(int arrivals, qhead *result_q) {
  11947.         packet *pckt;
  11948.         int total_packets=0;
  11949.         long total_delay=0;
  11950.         for (register i=0; i<arrivals; ++i) {
  11951.                 pckt=(packet *) result_q->get();
  11952.                 total_packets += 1;
  11953.                 total_delay +=
  11954.                    pckt->sys_departure_time-pckt->sys_arrival_time;
  11955.         }
  11956.         throughput = float (total_packets) / sched::get_clock();
  11957.         delay = float (total_delay) / total_packets ;
  11958.         resultis (0);
  11959. }
  11960.  
  11961. void main()
  11962. {
  11963.         sched::setclock(0);
  11964.         float lambda=0.01;          // in packets per msec
  11965.         float mu1=0.05;             // in packets per msec
  11966.         float mu2=0.07;             // in packets per msec
  11967.         int arrivals=50000;
  11968.         qtail *packet_q = new qtail;
  11969.         qhead *intermediate_result_q = new qhead;
  11970.         qhead *result_q = new qhead;
  11971.         consumer *output = new consumer(arrivals, result_q);
  11972.         producer *input = new producer(arrivals, lambda, packet_q, output);
  11973.         exponential_service_center *mm1_q1 =
  11974.             new exponential_service_center
  11975.                            (arrivals, mu1, packet_q->qtail::head(),
  11976.                            intermediate_result_q ->qhead::tail(),
  11977.                            output);
  11978.         exponential_service_center *mm1_q2 =
  11979.             new exponential_service_center
  11980.                            (arrivals, mu2, intermediate_result_q,
  11981.                             result_q ->qhead::tail(), output);
  11982.         (void) output->result();
  11983.         float actual_lambda = *((float *) (input->result()));
  11984.         float actual_mu1 = *((float *) (mm1_q1->result()));
  11985.         float actual_mu2 = *((float *) (mm1_q2->result()));
  11986.         float analytic_throughput = output->throughput;
  11987.         cout << "Simulation Results :\n"
  11988.              << "Average Throughput: " << actual_lambda
  11989.              << " packets/msec\n"
  11990.              << "Average delay: " <<  output->delay
  11991.              << " msec\n" << endl ;
  11992.         float analytic_delay =
  11993.               (1/actual_mu1) / (1.0-actual_lambda/actual_mu1)+
  11994.               (1/actual_mu2) / (1.0-((actual_lambda<actual_mu1)
  11995.                  ? actual_lambda:actual_mu1)/actual_mu2);
  11996.         cout << "Analytic results : \n"
  11997.              << "Average Throughput: " << analytic_throuput
  11998.              << " packets/msec\n"
  11999.              << "Average delay: " <<  analytic_delay
  12000.              << " msec\n" << endl ;
  12001.  
  12002.         thistask->resultis(0);
  12003. }
  12004.  
  12005. The output created by this program looks like this: 
  12006.  
  12007. Simulation Results:
  12008. Average Throughput: 0.00999937 packets/msec
  12009. Average delay: 39.7195 msec
  12010.  
  12011. Analytic results:
  12012. Average Throughput: 0.0099992 packets/msec
  12013. Average delay: 39.7881 msec
  12014.  
  12015.  
  12016. ΓòÉΓòÉΓòÉ 6.3. Glossary ΓòÉΓòÉΓòÉ
  12017.  
  12018.  
  12019. ΓòÉΓòÉΓòÉ 6.3.1. abstract class ΓòÉΓòÉΓòÉ
  12020.  
  12021. abstract class 
  12022.  
  12023. A class with at least one pure virtual function. It is a C++ class used as a 
  12024. base class for other classes. The abstract class represents a concept; classes 
  12025. derived from it represent implementations of the concept. You cannot have a 
  12026. direct object of an abstract class. (See also base class.) 
  12027.  
  12028.  
  12029. ΓòÉΓòÉΓòÉ 6.3.2. access ΓòÉΓòÉΓòÉ
  12030.  
  12031. access 
  12032.  
  12033. Determines whether or not a class member is accessible in an expression or 
  12034. declaration. 
  12035.  
  12036.  
  12037. ΓòÉΓòÉΓòÉ 6.3.3. access declaration ΓòÉΓòÉΓòÉ
  12038.  
  12039. access declaration 
  12040.  
  12041. Used to restore access to members of a base class. 
  12042.  
  12043.  
  12044. ΓòÉΓòÉΓòÉ 6.3.4. access resolution ΓòÉΓòÉΓòÉ
  12045.  
  12046. access resolution 
  12047.  
  12048. The process by which the accessibility of a particular class member is 
  12049. determined. 
  12050.  
  12051.  
  12052. ΓòÉΓòÉΓòÉ 6.3.5. access specifiers ΓòÉΓòÉΓòÉ
  12053.  
  12054. access specifiers 
  12055.  
  12056. One of the C++ keywords: public, private, and protected. 
  12057.  
  12058.  
  12059. ΓòÉΓòÉΓòÉ 6.3.6. anonymous union ΓòÉΓòÉΓòÉ
  12060.  
  12061. anonymous union 
  12062.  
  12063. A union without a class name. It must not be followed by a declarator. 
  12064.  
  12065.  
  12066. ΓòÉΓòÉΓòÉ 6.3.7. argument declaration ΓòÉΓòÉΓòÉ
  12067.  
  12068. argument declaration 
  12069.  
  12070. See parameter declaration. 
  12071.  
  12072.  
  12073. ΓòÉΓòÉΓòÉ 6.3.8. array ΓòÉΓòÉΓòÉ
  12074.  
  12075. array 
  12076.  
  12077. A variable that contains an ordered group of data objects. All data items (or 
  12078. elements) in an array have the same data type. 
  12079.  
  12080.  
  12081. ΓòÉΓòÉΓòÉ 6.3.9. array element ΓòÉΓòÉΓòÉ
  12082.  
  12083. array element 
  12084.  
  12085. A single data item in an array. 
  12086.  
  12087.  
  12088. ΓòÉΓòÉΓòÉ 6.3.10. base class ΓòÉΓòÉΓòÉ
  12089.  
  12090. base class 
  12091.  
  12092. A class from which other classes are derived. May itself be derived from 
  12093. another base class. (See also abstract class.) 
  12094.  
  12095.  
  12096. ΓòÉΓòÉΓòÉ 6.3.11. buffer flush ΓòÉΓòÉΓòÉ
  12097.  
  12098. buffer flush 
  12099.  
  12100. A process that removes the contents of a buffer.  After a buffer flush, the 
  12101. buffer is empty. 
  12102.  
  12103.  
  12104. ΓòÉΓòÉΓòÉ 6.3.12. case clause ΓòÉΓòÉΓòÉ
  12105.  
  12106. case clause 
  12107.  
  12108. In a switch statement, a case label followed by any number of statements. 
  12109.  
  12110.  
  12111. ΓòÉΓòÉΓòÉ 6.3.13. case label ΓòÉΓòÉΓòÉ
  12112.  
  12113. case label 
  12114.  
  12115. The word case  followed by a constant expression and a colon. When the selector 
  12116. evaluates the value of the constant expression, the statements following the 
  12117. case label are processed. 
  12118.  
  12119.  
  12120. ΓòÉΓòÉΓòÉ 6.3.14. cast expression ΓòÉΓòÉΓòÉ
  12121.  
  12122. cast expression 
  12123.  
  12124. A cast expression explicitly converts its operand to a specified arithmetic, 
  12125. scalar, or class type. 
  12126.  
  12127.  
  12128. ΓòÉΓòÉΓòÉ 6.3.15. cast operator ΓòÉΓòÉΓòÉ
  12129.  
  12130. cast operator 
  12131.  
  12132. The cast operator is used for explicit type conversions. 
  12133.  
  12134.  
  12135. ΓòÉΓòÉΓòÉ 6.3.16. catch block ΓòÉΓòÉΓòÉ
  12136.  
  12137. catch block 
  12138.  
  12139. A block associated with a try block that receives control when an exception 
  12140. matching its argument is thrown. 
  12141.  
  12142.  
  12143. ΓòÉΓòÉΓòÉ 6.3.17. class ΓòÉΓòÉΓòÉ
  12144.  
  12145. class 
  12146.  
  12147. A user-defined data type that can contain both data representations (data 
  12148. members) and functions (member functions). 
  12149.  
  12150.  
  12151. ΓòÉΓòÉΓòÉ 6.3.18. class key ΓòÉΓòÉΓòÉ
  12152.  
  12153. class key 
  12154.  
  12155. One of the C++ keywords: class, struct and union. 
  12156.  
  12157.  
  12158. ΓòÉΓòÉΓòÉ 6.3.19. class library ΓòÉΓòÉΓòÉ
  12159.  
  12160. class library 
  12161.  
  12162. A collection of C++ classes. 
  12163.  
  12164.  
  12165. ΓòÉΓòÉΓòÉ 6.3.20. class member operators ΓòÉΓòÉΓòÉ
  12166.  
  12167. class member operators 
  12168.  
  12169. Used to access class members through class objects or pointers to class 
  12170. objects. They are ., ->, .*, and ->*. 
  12171.  
  12172.  
  12173. ΓòÉΓòÉΓòÉ 6.3.21. class template ΓòÉΓòÉΓòÉ
  12174.  
  12175. class template 
  12176.  
  12177. A blueprint describing how a set of related classes can be constructed. 
  12178.  
  12179.  
  12180. ΓòÉΓòÉΓòÉ 6.3.22. compilation unit ΓòÉΓòÉΓòÉ
  12181.  
  12182. compilation unit 
  12183.  
  12184. A single compiled file and all its associated include files. 
  12185.  
  12186.  
  12187. ΓòÉΓòÉΓòÉ 6.3.23. complete class name ΓòÉΓòÉΓòÉ
  12188.  
  12189. complete class name 
  12190.  
  12191. The complete qualification of a nested class name including all enclosing class 
  12192. names. 
  12193.  
  12194.  
  12195. ΓòÉΓòÉΓòÉ 6.3.24. Complex Mathematics Library ΓòÉΓòÉΓòÉ
  12196.  
  12197. Complex Mathematics Library 
  12198.  
  12199. A class library that provides the facilities to manipulate complex numbers and 
  12200. perform standard mathematical operations on them. 
  12201.  
  12202.  
  12203. ΓòÉΓòÉΓòÉ 6.3.25. complex number ΓòÉΓòÉΓòÉ
  12204.  
  12205. complex number 
  12206.  
  12207. A number made up of a real part and an imaginary part.  A complex number can be 
  12208. represented by an ordered pair (a, b), where a is the value of the real part 
  12209. and b is the value of the imaginary part.  The same complex number could also 
  12210. be represented as a + bi, where i is the square root of -1. 
  12211.  
  12212.  
  12213. ΓòÉΓòÉΓòÉ 6.3.26. constructor ΓòÉΓòÉΓòÉ
  12214.  
  12215. constructor 
  12216.  
  12217. A class member function with the same name as its class, used to construct 
  12218. class objects and sometimes to initialize them. 
  12219.  
  12220.  
  12221. ΓòÉΓòÉΓòÉ 6.3.27. conversion ΓòÉΓòÉΓòÉ
  12222.  
  12223. conversion 
  12224.  
  12225. A change in the type of a value. For example, when you add values having 
  12226. different data types, the compiler converts both values to the same type before 
  12227. adding them. 
  12228.  
  12229.  
  12230. ΓòÉΓòÉΓòÉ 6.3.28. conversion function ΓòÉΓòÉΓòÉ
  12231.  
  12232. conversion function 
  12233.  
  12234. A member function that specifies a conversion from its class type to another 
  12235. type. 
  12236.  
  12237.  
  12238. ΓòÉΓòÉΓòÉ 6.3.29. copy constructor ΓòÉΓòÉΓòÉ
  12239.  
  12240. copy constructor 
  12241.  
  12242. A constructor used to make a copy of a class object from another class object 
  12243. of the same class type. 
  12244.  
  12245.  
  12246. ΓòÉΓòÉΓòÉ 6.3.30. data abstraction ΓòÉΓòÉΓòÉ
  12247.  
  12248. data abstraction 
  12249.  
  12250. See abstraction (data) 
  12251.  
  12252.  
  12253. ΓòÉΓòÉΓòÉ 6.3.31. default clause ΓòÉΓòÉΓòÉ
  12254.  
  12255. default clause 
  12256.  
  12257. In a switch statement, the keyword default followed by a colon, and one or more 
  12258. statements. When the conditions of the specified case labels in the switch 
  12259. statement do not hold, the default clause is chosen. 
  12260.  
  12261.  
  12262. ΓòÉΓòÉΓòÉ 6.3.32. default constructor ΓòÉΓòÉΓòÉ
  12263.  
  12264. default constructor 
  12265.  
  12266. A constructor that takes no arguments, or if it takes any arguments, all its 
  12267. arguments have default values. 
  12268.  
  12269.  
  12270. ΓòÉΓòÉΓòÉ 6.3.33. default initialization ΓòÉΓòÉΓòÉ
  12271.  
  12272. default initialization 
  12273.  
  12274. The initial value assigned to a data object by the compiler if no initial value 
  12275. is specified by the programmer. 
  12276.  
  12277.  
  12278. ΓòÉΓòÉΓòÉ 6.3.34. define statement ΓòÉΓòÉΓòÉ
  12279.  
  12280. define statement 
  12281.  
  12282. A preprocessor statement that causes the preprocessor to replace an identifier 
  12283. or macro call with specified code. 
  12284.  
  12285.  
  12286. ΓòÉΓòÉΓòÉ 6.3.35. demangling ΓòÉΓòÉΓòÉ
  12287.  
  12288. demangling 
  12289.  
  12290. The conversion of mangled names back to their original source code names. 
  12291. During compilation, identifiers such as function and static class member names 
  12292. are mangled (encoded) with type and scoping information to ensure type-safe 
  12293. linkage.  These mangled names appear in the object file and the final 
  12294. executable file. Demangling converts these names back to their original names 
  12295. to make program debugging easier. 
  12296.  
  12297.  
  12298. ΓòÉΓòÉΓòÉ 6.3.36. delete ΓòÉΓòÉΓòÉ
  12299.  
  12300. delete 
  12301.  
  12302. 1) The keyword delete identifies a free store deallocation operator. 2) The 
  12303. delete operator is used to destroy objects created by new. 
  12304.  
  12305.  
  12306. ΓòÉΓòÉΓòÉ 6.3.37. derived class ΓòÉΓòÉΓòÉ
  12307.  
  12308. derived class 
  12309.  
  12310. A class that inherits the proper base class become members of a derived class 
  12311. object. You can add additional data members and member functions to the derived 
  12312. class. A derived class object can be manipulated as if it is a base class 
  12313. object. The derived class can override virtual functions of the base class. 
  12314.  
  12315.  
  12316. ΓòÉΓòÉΓòÉ 6.3.38. destructor ΓòÉΓòÉΓòÉ
  12317.  
  12318. destructor 
  12319.  
  12320. A special member function with the same name as its class preceded by a ~ 
  12321. (tilde).  A destructor, which has no arguments and a void return type, "cleans 
  12322. up" after an object by, for example, freeing any storage that was dynamically 
  12323. allocated when the object was created. 
  12324.  
  12325.  
  12326. ΓòÉΓòÉΓòÉ 6.3.39. dynamic binding ΓòÉΓòÉΓòÉ
  12327.  
  12328. dynamic binding 
  12329.  
  12330. Binding that occurs at run-time. 
  12331.  
  12332.  
  12333. ΓòÉΓòÉΓòÉ 6.3.40. element ΓòÉΓòÉΓòÉ
  12334.  
  12335. element 
  12336.  
  12337. The component of an array, subrange, enumeration, or set. 
  12338.  
  12339.  
  12340. ΓòÉΓòÉΓòÉ 6.3.41. enumeration constant ΓòÉΓòÉΓòÉ
  12341.  
  12342. enumeration constant 
  12343.  
  12344. An identifier (that has an associated integer value) defined by an enumeration 
  12345. type. You can use an enumeration constant anywhere an integer constant is 
  12346. allowed. 
  12347.  
  12348.  
  12349. ΓòÉΓòÉΓòÉ 6.3.42. enumeration tag ΓòÉΓòÉΓòÉ
  12350.  
  12351. enumeration tag 
  12352.  
  12353. The identifier that names an enumeration data type. 
  12354.  
  12355.  
  12356. ΓòÉΓòÉΓòÉ 6.3.43. enumeration type ΓòÉΓòÉΓòÉ
  12357.  
  12358. enumeration type 
  12359.  
  12360. In C++, an enumeration type is a distinct data type that is not an integral 
  12361. type. An enumeration type defines a set of enumeration constants. 
  12362.  
  12363.  
  12364. ΓòÉΓòÉΓòÉ 6.3.44. enumerator ΓòÉΓòÉΓòÉ
  12365.  
  12366. enumerator 
  12367.  
  12368. An enumeration constant and its associated value. 
  12369.  
  12370.  
  12371. ΓòÉΓòÉΓòÉ 6.3.45. EOF ΓòÉΓòÉΓòÉ
  12372.  
  12373. EOF 
  12374.  
  12375. End-of-file.  A character value used to represent the end of an ASCII file. 
  12376. Corresponds to the numeric value (-1). 
  12377.  
  12378.  
  12379. ΓòÉΓòÉΓòÉ 6.3.46. exception ΓòÉΓòÉΓòÉ
  12380.  
  12381. exception 
  12382.  
  12383. Any user, logic, or system error detected by a function that does not itself 
  12384. deal with the error but passes the error on to a handling routine. Passing this 
  12385. error is called throwing an exception. 
  12386.  
  12387.  
  12388. ΓòÉΓòÉΓòÉ 6.3.47. exception handler ΓòÉΓòÉΓòÉ
  12389.  
  12390. exception handler 
  12391.  
  12392. Exception handlers are catch blocks in C++. Catch blocks catch exceptions when 
  12393. they are thrown from a function enclosed in a try block. Try blocks, catch 
  12394. blocks and throw expressions are the constructs used to implement formal 
  12395. exception handling in C++. 
  12396.  
  12397.  
  12398. ΓòÉΓòÉΓòÉ 6.3.48. exception handling ΓòÉΓòÉΓòÉ
  12399.  
  12400. exception handling 
  12401.  
  12402. A type of error handling that allows control and information to be passed to an 
  12403. exception handler when an exception occurs. 
  12404.  
  12405.  
  12406. ΓòÉΓòÉΓòÉ 6.3.49. external data definition ΓòÉΓòÉΓòÉ
  12407.  
  12408. external data definition 
  12409.  
  12410. A definition appearing outside a function. The defined object is accessible to 
  12411. all functions that follow the definition and are located within the same source 
  12412. file as the definition. 
  12413.  
  12414.  
  12415. ΓòÉΓòÉΓòÉ 6.3.50. float constant ΓòÉΓòÉΓòÉ
  12416.  
  12417. float constant 
  12418.  
  12419. A number containing a decimal point, an exponent, or both a decimal point and 
  12420. an exponent. The exponent contains an e or E, an optional sign (+ or -), and 
  12421. one or more digits (0 through 9). 
  12422.  
  12423.  
  12424. ΓòÉΓòÉΓòÉ 6.3.51. free store ΓòÉΓòÉΓòÉ
  12425.  
  12426. free store 
  12427.  
  12428. Dynamically allocates memory. New and delete are used to allocate and 
  12429. deallocate free store. 
  12430.  
  12431.  
  12432. ΓòÉΓòÉΓòÉ 6.3.52. friend class ΓòÉΓòÉΓòÉ
  12433.  
  12434. friend class 
  12435.  
  12436. A class in which all the member functions are granted access to the private and 
  12437. protected members of another class. It is named in the declaration of another 
  12438. class and uses the keyword friend as a prefix to the class. For example: 
  12439.  
  12440. class me {
  12441.     friend class you;
  12442.      // ...
  12443. };
  12444.  
  12445. makes all the functions in class you friends of class me. 
  12446.  
  12447.  
  12448. ΓòÉΓòÉΓòÉ 6.3.53. friend function ΓòÉΓòÉΓòÉ
  12449.  
  12450. friend function 
  12451.  
  12452. A function that is granted access to the private and protected parts of a 
  12453. class. It is named in the declaration of the class and uses the keyword friend 
  12454. as a prefix. 
  12455.  
  12456.  
  12457. ΓòÉΓòÉΓòÉ 6.3.54. function definition ΓòÉΓòÉΓòÉ
  12458.  
  12459. function definition 
  12460.  
  12461. The complete description of a function. A function definition contains an 
  12462. optional storage class specifier, an optional type specifier, a function 
  12463. declarator, parameter declarations, and a block statement (the function body). 
  12464.  
  12465.  
  12466. ΓòÉΓòÉΓòÉ 6.3.55. function template ΓòÉΓòÉΓòÉ
  12467.  
  12468. function template 
  12469.  
  12470. Provides a blueprint describing how a set of related individual functions can 
  12471. be constructed. 
  12472.  
  12473.  
  12474. ΓòÉΓòÉΓòÉ 6.3.56. generic class ΓòÉΓòÉΓòÉ
  12475.  
  12476. generic class 
  12477.  
  12478. See class templates. 
  12479.  
  12480.  
  12481. ΓòÉΓòÉΓòÉ 6.3.57. header file ΓòÉΓòÉΓòÉ
  12482.  
  12483. header file 
  12484.  
  12485. A file that contains declarations used by a group of functions or users. 
  12486.  
  12487.  
  12488. ΓòÉΓòÉΓòÉ 6.3.58. I/O Stream Library ΓòÉΓòÉΓòÉ
  12489.  
  12490. I/O Stream Library 
  12491.  
  12492. A class library that provides the facilities to deal with many varieties of 
  12493. input and output. 
  12494.  
  12495.  
  12496. ΓòÉΓòÉΓòÉ 6.3.59. identifier ΓòÉΓòÉΓòÉ
  12497.  
  12498. identifier 
  12499.  
  12500. A name that refers to a data object. An identifier contains some combination of 
  12501. letters, digits, and underscores, but its first character cannot be a digit. 
  12502.  
  12503.  
  12504. ΓòÉΓòÉΓòÉ 6.3.60. include file ΓòÉΓòÉΓòÉ
  12505.  
  12506. include file 
  12507.  
  12508. A text file that contains declarations used by a group of functions, programs, 
  12509. or users. Also known as a header file. 
  12510.  
  12511.  
  12512. ΓòÉΓòÉΓòÉ 6.3.61. include statement ΓòÉΓòÉΓòÉ
  12513.  
  12514. include statement 
  12515.  
  12516. A preprocessor statement that causes the preprocessor to replace the statement 
  12517. with the contents of a specified file. 
  12518.  
  12519.  
  12520. ΓòÉΓòÉΓòÉ 6.3.62. inheritance ΓòÉΓòÉΓòÉ
  12521.  
  12522. inheritance 
  12523.  
  12524. An object-oriented programming technique that allows you to use existing 
  12525. classes as bases for creating other classes. 
  12526.  
  12527.  
  12528. ΓòÉΓòÉΓòÉ 6.3.63. initializer ΓòÉΓòÉΓòÉ
  12529.  
  12530. initializer 
  12531.  
  12532. An expression used to initialize data objects. In C++, there are three types of 
  12533. initializers: 1) You can use an expression followed by an assignment operator 
  12534. to initialize fundamental data type objects or class objects that have copy 
  12535. constructors. 2) You can use an expression enclosed in braces ( {} ) to 
  12536. initialize aggregates. 3) You can use a parenthesized expression list to 
  12537. initialize base classes and members using constructors. 
  12538.  
  12539.  
  12540. ΓòÉΓòÉΓòÉ 6.3.64. inline function ΓòÉΓòÉΓòÉ
  12541.  
  12542. inline function 
  12543.  
  12544. Inlining is a hint to the compiler to perform inline expansion of the body of a 
  12545. function member. Functions declared and defined simultaneously in a class 
  12546. definition are inline. You can also explicitly declare a function inline by 
  12547. using the keyword inline. Both member and nonmember functions can be inlined. 
  12548.  
  12549.  
  12550. ΓòÉΓòÉΓòÉ 6.3.65. instance ΓòÉΓòÉΓòÉ
  12551.  
  12552. instance 
  12553.  
  12554. An object-oriented programming term synonymous with 'object'. An instance is a 
  12555. particular instantiation of a data type. It is simply a region of storage that 
  12556. contains a value or group of values. For example, if a class box is previously 
  12557. defined, two instances of a class box could be instantiated with the 
  12558. declaration: 
  12559.  
  12560.    box box1, box2;
  12561.  
  12562.  
  12563. ΓòÉΓòÉΓòÉ 6.3.66. instantiate ΓòÉΓòÉΓòÉ
  12564.  
  12565. instantiate 
  12566.  
  12567. To create or generate a particular instance (or object) of a data type. For 
  12568. example, an instance box1 of class box could be instantiated with the 
  12569. declaration: 
  12570.  
  12571.  box box1;
  12572.  
  12573.  
  12574. ΓòÉΓòÉΓòÉ 6.3.67. integral object ΓòÉΓòÉΓòÉ
  12575.  
  12576. integral object 
  12577.  
  12578. A character object, an object having variations of the type int, or an object 
  12579. that is a bit field. 
  12580.  
  12581.  
  12582. ΓòÉΓòÉΓòÉ 6.3.68. internal data definition ΓòÉΓòÉΓòÉ
  12583.  
  12584. internal data definition 
  12585.  
  12586. A description of a variable appearing in a block that directs the system to 
  12587. allocate storage for that variable and makes that variable accessible to the 
  12588. current block after its point of declaration. 
  12589.  
  12590.  
  12591. ΓòÉΓòÉΓòÉ 6.3.69. manipulator ΓòÉΓòÉΓòÉ
  12592.  
  12593. manipulator 
  12594.  
  12595. A value tha can be inserted into streams or extracted from streams to affect or 
  12596. query the behaviour of the stream. 
  12597.  
  12598.  
  12599. ΓòÉΓòÉΓòÉ 6.3.70. member ΓòÉΓòÉΓòÉ
  12600.  
  12601. member 
  12602.  
  12603. A data object or function in a structure, union or class. Members can also be 
  12604. classes, enumerations, bit fields and type names. 
  12605.  
  12606.  
  12607. ΓòÉΓòÉΓòÉ 6.3.71. member function ΓòÉΓòÉΓòÉ
  12608.  
  12609. member function 
  12610.  
  12611. Operators and functions that are declared as members of a class. A member 
  12612. function has access to the private and protected data members and member 
  12613. functions of an object of its class. Member functions are also called methods. 
  12614.  
  12615.  
  12616. ΓòÉΓòÉΓòÉ 6.3.72. memory leak ΓòÉΓòÉΓòÉ
  12617.  
  12618. memory leak 
  12619.  
  12620. Occurs when dynamic memory that has been allocated to an application is not 
  12621. freed by the application when the memory is no longer needed. 
  12622.  
  12623.  
  12624. ΓòÉΓòÉΓòÉ 6.3.73. method ΓòÉΓòÉΓòÉ
  12625.  
  12626. method 
  12627.  
  12628. Method is an object-oriented programming term synonymous with member function. 
  12629.  
  12630.  
  12631. ΓòÉΓòÉΓòÉ 6.3.74. multiple inheritance ΓòÉΓòÉΓòÉ
  12632.  
  12633. multiple inheritance 
  12634.  
  12635. An object-oriented programming technique implemented in C++ through derivation, 
  12636. in which the derived class inherits members from more than one base class. See 
  12637. also inheritance. 
  12638.  
  12639.  
  12640. ΓòÉΓòÉΓòÉ 6.3.75. nested class ΓòÉΓòÉΓòÉ
  12641.  
  12642. nested class 
  12643.  
  12644. A class defined within the scope of another class. 
  12645.  
  12646.  
  12647. ΓòÉΓòÉΓòÉ 6.3.76. new ΓòÉΓòÉΓòÉ
  12648.  
  12649. new 
  12650.  
  12651. A keyword identifying a free store allocation operator. The new operator may be 
  12652. used to create class objects. 
  12653.  
  12654.  
  12655. ΓòÉΓòÉΓòÉ 6.3.77. object ΓòÉΓòÉΓòÉ
  12656.  
  12657. object 
  12658.  
  12659. A region of storage. An object is created when a variable is defined or new is 
  12660. invoked. An object is destroyed when it goes out of scope. See also instance. 
  12661.  
  12662.  
  12663. ΓòÉΓòÉΓòÉ 6.3.78. object-oriented programming ΓòÉΓòÉΓòÉ
  12664.  
  12665. object-oriented programming 
  12666.  
  12667. A programming approach based on the concepts of data abstraction and 
  12668. inheritance. Unlike procedural programming techniques, object-oriented 
  12669. programming concentrates not on how something is accomplished but instead on 
  12670. what data objects comprise the problem and how they are manipulated. 
  12671.  
  12672.  
  12673. ΓòÉΓòÉΓòÉ 6.3.79. operator function ΓòÉΓòÉΓòÉ
  12674.  
  12675. operator function 
  12676.  
  12677. An overloaded operator that is either a member of a class, or takes at least 
  12678. one argument that is a class type or a reference to a class type. 
  12679.  
  12680.  
  12681. ΓòÉΓòÉΓòÉ 6.3.80. overflow ΓòÉΓòÉΓòÉ
  12682.  
  12683. overflow 
  12684.  
  12685. That portion of an operation's result that exceeds the capacity of the intended 
  12686. unit of storage. 
  12687.  
  12688.  
  12689. ΓòÉΓòÉΓòÉ 6.3.81. overflow condition ΓòÉΓòÉΓòÉ
  12690.  
  12691. overflow condition 
  12692.  
  12693. A condition that occurs when a portion of the result of an operation exceeds 
  12694. the capacity of the intended unit of storage. 
  12695.  
  12696.  
  12697. ΓòÉΓòÉΓòÉ 6.3.82. overloading ΓòÉΓòÉΓòÉ
  12698.  
  12699. overloading 
  12700.  
  12701. Allows you to redefine functions and most standard C++ operators when the 
  12702. functions and operators are used with class types. 
  12703.  
  12704.  
  12705. ΓòÉΓòÉΓòÉ 6.3.83. pad ΓòÉΓòÉΓòÉ
  12706.  
  12707. pad 
  12708.  
  12709. To fill unused positions in a field with dummy data, usually zeros, ones, or 
  12710. blanks. 
  12711.  
  12712.  
  12713. ΓòÉΓòÉΓòÉ 6.3.84. parameter declaration ΓòÉΓòÉΓòÉ
  12714.  
  12715. parameter declaration 
  12716.  
  12717. A description of a value that a function receives. A parameter declaration 
  12718. determines the storage class and the data type of the value. 
  12719.  
  12720.  
  12721. ΓòÉΓòÉΓòÉ 6.3.85. pointer ΓòÉΓòÉΓòÉ
  12722.  
  12723. pointer 
  12724.  
  12725. A variable that holds the address of a data object or function. 
  12726.  
  12727.  
  12728. ΓòÉΓòÉΓòÉ 6.3.86. pointer to member ΓòÉΓòÉΓòÉ
  12729.  
  12730. pointer to member 
  12731.  
  12732. Used to access the address of non static members of a class. 
  12733.  
  12734.  
  12735. ΓòÉΓòÉΓòÉ 6.3.87. precedence ΓòÉΓòÉΓòÉ
  12736.  
  12737. precedence 
  12738.  
  12739. The priority system for grouping different types of operators with their 
  12740. operands. 
  12741.  
  12742.  
  12743. ΓòÉΓòÉΓòÉ 6.3.88. private ΓòÉΓòÉΓòÉ
  12744.  
  12745. private 
  12746.  
  12747. A private member of a class is only accessible to member functions and friends 
  12748. of that class. 
  12749.  
  12750.  
  12751. ΓòÉΓòÉΓòÉ 6.3.89. protected ΓòÉΓòÉΓòÉ
  12752.  
  12753. protected 
  12754.  
  12755. A protected member of a class is accessible to member functions and friends of 
  12756. that class, or member functions and friends of classes derived from that class. 
  12757.  
  12758.  
  12759. ΓòÉΓòÉΓòÉ 6.3.90. prototype ΓòÉΓòÉΓòÉ
  12760.  
  12761. prototype 
  12762.  
  12763. See function prototype. 
  12764.  
  12765.  
  12766. ΓòÉΓòÉΓòÉ 6.3.91. public ΓòÉΓòÉΓòÉ
  12767.  
  12768. public 
  12769.  
  12770. A public member of a class is accessible to all functions. 
  12771.  
  12772.  
  12773. ΓòÉΓòÉΓòÉ 6.3.92. pure virtual function ΓòÉΓòÉΓòÉ
  12774.  
  12775. pure virtual function 
  12776.  
  12777. A virtual function is declared pure by replacing the function definition with 
  12778. '=0;'. See also abstract classes. 
  12779.  
  12780.  
  12781. ΓòÉΓòÉΓòÉ 6.3.93. qualified class name ΓòÉΓòÉΓòÉ
  12782.  
  12783. qualified class name 
  12784.  
  12785. Any class name or class name qualified with one or more :: (scope) operators. 
  12786.  
  12787.  
  12788. ΓòÉΓòÉΓòÉ 6.3.94. qualified name ΓòÉΓòÉΓòÉ
  12789.  
  12790. qualified name 
  12791.  
  12792. Used to qualify a nonclass type name such as a member by its class name. 
  12793.  
  12794.  
  12795. ΓòÉΓòÉΓòÉ 6.3.95. qualified type name ΓòÉΓòÉΓòÉ
  12796.  
  12797. qualified type name 
  12798.  
  12799. Used to reduce complex class name syntax by using typedefs to represent 
  12800. qualified class names, 
  12801.  
  12802.  
  12803. ΓòÉΓòÉΓòÉ 6.3.96. static ΓòÉΓòÉΓòÉ
  12804.  
  12805. static 
  12806.  
  12807. A keyword used for defining the scope and linkage of variables and functions. 
  12808. For internal variables, the variable has block scope and retains its value 
  12809. between function calls. For external values, the variable has file scope and 
  12810. retains its value within the source file. For class variables, the variable is 
  12811. shared by all objects of the class and retains its value within the entire 
  12812. program. 
  12813.  
  12814.  
  12815. ΓòÉΓòÉΓòÉ 6.3.97. static binding ΓòÉΓòÉΓòÉ
  12816.  
  12817. static binding 
  12818.  
  12819. Binding that occurs at compilation time based on the resolution of overloaded 
  12820. functions. or more instructions enclosed in braces ({}). 
  12821.  
  12822.  
  12823. ΓòÉΓòÉΓòÉ 6.3.98. storage class specifier ΓòÉΓòÉΓòÉ
  12824.  
  12825. storage class specifier 
  12826.  
  12827. One of: auto, register, static, or extern. 
  12828.  
  12829.  
  12830. ΓòÉΓòÉΓòÉ 6.3.99. stream ΓòÉΓòÉΓòÉ
  12831.  
  12832. stream 
  12833.  
  12834. A continuous stream of data elements being transmitted, or intended for 
  12835. transmission, in character or binary-digit form, using a defined format. 
  12836.  
  12837.  
  12838. ΓòÉΓòÉΓòÉ 6.3.100. stream buffer ΓòÉΓòÉΓòÉ
  12839.  
  12840. stream buffer 
  12841.  
  12842. A stream buffer is a buffer between the ultimate consumer and the I/O Stream 
  12843. Library functions that format data.  It is implemented in the I/O Stream 
  12844. Library by the streambuf class and the classes derived from streambuf. 
  12845.  
  12846.  
  12847. ΓòÉΓòÉΓòÉ 6.3.101. structure ΓòÉΓòÉΓòÉ
  12848.  
  12849. structure 
  12850.  
  12851. A class data type that contains an ordered group of data objects and member 
  12852. functions. Unlike an array, the data objects within a structure can have varied 
  12853. data types. A structure can be used in all places a class is used. The initial 
  12854. projection is public. 
  12855.  
  12856.  
  12857. ΓòÉΓòÉΓòÉ 6.3.102. structure tag ΓòÉΓòÉΓòÉ
  12858.  
  12859. structure tag 
  12860.  
  12861. The identifier that names a structure data type. 
  12862.  
  12863.  
  12864. ΓòÉΓòÉΓòÉ 6.3.103. switch expression ΓòÉΓòÉΓòÉ
  12865.  
  12866. switch expression 
  12867.  
  12868. The controlling expression of a switch statement. 
  12869.  
  12870.  
  12871. ΓòÉΓòÉΓòÉ 6.3.104. switch statement ΓòÉΓòÉΓòÉ
  12872.  
  12873. switch statement 
  12874.  
  12875. A C++ language statement that causes control to be transferred to one of 
  12876. several statements depending on the value of an expression. 
  12877.  
  12878.  
  12879. ΓòÉΓòÉΓòÉ 6.3.105. task ΓòÉΓòÉΓòÉ
  12880.  
  12881. task 
  12882.  
  12883. A lightweight, nonpreemptive routine that you can use to simulate the operation 
  12884. of programs.  Only a single task executes at any one time. Less time and space 
  12885. are required to create a task than a true operating system process. 
  12886.  
  12887.  
  12888. ΓòÉΓòÉΓòÉ 6.3.106. Task Library ΓòÉΓòÉΓòÉ
  12889.  
  12890. Task Library 
  12891.  
  12892. A class library that provides the facilities to write programs that are made up 
  12893. of tasks. 
  12894.  
  12895.  
  12896. ΓòÉΓòÉΓòÉ 6.3.107. template ΓòÉΓòÉΓòÉ
  12897.  
  12898. template 
  12899.  
  12900. A family of classes or functions with variable types. 
  12901.  
  12902.  
  12903. ΓòÉΓòÉΓòÉ 6.3.108. template class ΓòÉΓòÉΓòÉ
  12904.  
  12905. template class 
  12906.  
  12907. A class instance generated by a class template. 
  12908.  
  12909.  
  12910. ΓòÉΓòÉΓòÉ 6.3.109. template function ΓòÉΓòÉΓòÉ
  12911.  
  12912. template function 
  12913.  
  12914. A function generated by a function template. 
  12915.  
  12916.  
  12917. ΓòÉΓòÉΓòÉ 6.3.110. this ΓòÉΓòÉΓòÉ
  12918.  
  12919. this 
  12920.  
  12921. A keyword that identifies a special type of pointer that references in a member 
  12922. function the class object with which the member function was invoked. 
  12923.  
  12924.  
  12925. ΓòÉΓòÉΓòÉ 6.3.111. type balancing ΓòÉΓòÉΓòÉ
  12926.  
  12927. type balancing 
  12928.  
  12929. A conversion that makes both operands have the same data type. If the operands 
  12930. do not have the same size data type, the compiler converts the value of the 
  12931. operand with the smaller type to a value having the larger type. 
  12932.  
  12933.  
  12934. ΓòÉΓòÉΓòÉ 6.3.112. type definition ΓòÉΓòÉΓòÉ
  12935.  
  12936. type definition 
  12937.  
  12938. A definition of a data type. 
  12939.  
  12940.  
  12941. ΓòÉΓòÉΓòÉ 6.3.113. type specifier ΓòÉΓòÉΓòÉ
  12942.  
  12943. type specifier 
  12944.  
  12945. Used to indicate the data type of an object or function being declared. 
  12946.  
  12947.  
  12948. ΓòÉΓòÉΓòÉ 6.3.114. ultimate consumer ΓòÉΓòÉΓòÉ
  12949.  
  12950. ultimate consumer 
  12951.  
  12952. The target of data in an input and output operation. Can be a file, a device, 
  12953. or an array of bytes in memory. 
  12954.  
  12955.  
  12956. ΓòÉΓòÉΓòÉ 6.3.115. ultimate producer ΓòÉΓòÉΓòÉ
  12957.  
  12958. ultimate producer 
  12959.  
  12960. The source of data in an input and output operation. Can be a file, a device, 
  12961. or an array of bytes in memory. 
  12962.  
  12963.  
  12964. ΓòÉΓòÉΓòÉ 6.3.116. union ΓòÉΓòÉΓòÉ
  12965.  
  12966. union 
  12967.  
  12968. A variable that can hold any one of several data types, but only one data type 
  12969. at a time. 
  12970.  
  12971.  
  12972. ΓòÉΓòÉΓòÉ 6.3.117. union tag ΓòÉΓòÉΓòÉ
  12973.  
  12974. union tag 
  12975.  
  12976. The identifier that names a union data type. 
  12977.  
  12978.  
  12979. ΓòÉΓòÉΓòÉ 6.3.118. virtual function ΓòÉΓòÉΓòÉ
  12980.  
  12981. virtual function 
  12982.  
  12983. A class function declared with the keyword virtual. The implementation that is 
  12984. executed when you call a virtual function is determined at run-time based on 
  12985. the type of the object for which it is called.