source: libcfa/src/iostream.hfa@ b544afa

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since b544afa was 65240bb, checked in by Peter A. Buhr <pabuhr@…>, 6 years ago

first attempt to add streams exit and abort, rename private iostream nl function to ends

  • Property mode set to 100644
File size: 18.6 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// iostream.hfa --
8//
9// Author : Peter A. Buhr
10// Created On : Wed May 27 17:56:53 2015
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Fri Jul 12 12:08:38 2019
13// Update Count : 334
14//
15
16#pragma once
17
18#include "iterator.hfa"
19
20
21//*********************************** ostream ***********************************
22
23
24trait ostream( dtype ostype ) {
25 // private
26 bool sepPrt( ostype & ); // get separator state (on/off)
27 void sepReset( ostype & ); // set separator state to default state
28 void sepReset( ostype &, bool ); // set separator and default state
29 const char * sepGetCur( ostype & ); // get current separator string
30 void sepSetCur( ostype &, const char * ); // set current separator string
31 bool getNL( ostype & ); // check newline
32 void setNL( ostype &, bool ); // saw newline
33 bool getANL( ostype & ); // get auto newline (on/off)
34 bool getPrt( ostype & ); // get fmt called in output cascade
35 void setPrt( ostype &, bool ); // set fmt called in output cascade
36 // public
37 void sepOn( ostype & ); // turn separator state on
38 void sepOff( ostype & ); // turn separator state off
39 bool sepDisable( ostype & ); // set default state to off, and return previous state
40 bool sepEnable( ostype & ); // set default state to on, and return previous state
41 void nlOn( ostype & ); // turn auto-newline state on
42 void nlOff( ostype & ); // turn auto-newline state off
43
44 const char * sepGet( ostype & ); // get separator string
45 void sepSet( ostype &, const char * ); // set separator to string (15 character maximum)
46 const char * sepGetTuple( ostype & ); // get tuple separator string
47 void sepSetTuple( ostype &, const char * ); // set tuple separator to string (15 character maximum)
48
49 void ends( ostype & os ); // end of output statement
50 int fail( ostype & );
51 int flush( ostype & );
52 void open( ostype & os, const char * name, const char * mode );
53 void close( ostype & os );
54 ostype & write( ostype &, const char *, size_t );
55 int fmt( ostype &, const char format[], ... ) __attribute__(( format(printf, 2, 3) ));
56}; // ostream
57
58// trait writeable( otype T ) {
59// forall( dtype ostype | ostream( ostype ) ) ostype & ?|?( ostype &, T );
60// }; // writeable
61
62trait writeable( otype T, dtype ostype | ostream( ostype ) ) {
63 ostype & ?|?( ostype &, T );
64}; // writeable
65
66// implement writable for intrinsic types
67
68forall( dtype ostype | ostream( ostype ) ) {
69 ostype & ?|?( ostype &, zero_t );
70 void ?|?( ostype &, zero_t );
71 ostype & ?|?( ostype &, one_t );
72 void ?|?( ostype &, one_t );
73
74 ostype & ?|?( ostype &, bool );
75 void ?|?( ostype &, bool );
76
77 ostype & ?|?( ostype &, char );
78 void ?|?( ostype &, char );
79 ostype & ?|?( ostype &, signed char );
80 void ?|?( ostype &, signed char );
81 ostype & ?|?( ostype &, unsigned char );
82 void ?|?( ostype &, unsigned char );
83
84 ostype & ?|?( ostype &, short int );
85 void ?|?( ostype &, short int );
86 ostype & ?|?( ostype &, unsigned short int );
87 void ?|?( ostype &, unsigned short int );
88 ostype & ?|?( ostype &, int );
89 void ?|?( ostype &, int );
90 ostype & ?|?( ostype &, unsigned int );
91 void ?|?( ostype &, unsigned int );
92 ostype & ?|?( ostype &, long int );
93 void ?|?( ostype &, long int );
94 ostype & ?|?( ostype &, long long int );
95 void ?|?( ostype &, long long int );
96 ostype & ?|?( ostype &, unsigned long int );
97 void ?|?( ostype &, unsigned long int );
98 ostype & ?|?( ostype &, unsigned long long int );
99 void ?|?( ostype &, unsigned long long int );
100
101 ostype & ?|?( ostype &, float );
102 void ?|?( ostype &, float );
103 ostype & ?|?( ostype &, double );
104 void ?|?( ostype &, double );
105 ostype & ?|?( ostype &, long double );
106 void ?|?( ostype &, long double );
107
108 ostype & ?|?( ostype &, float _Complex );
109 void ?|?( ostype &, float _Complex );
110 ostype & ?|?( ostype &, double _Complex );
111 void ?|?( ostype &, double _Complex );
112 ostype & ?|?( ostype &, long double _Complex );
113 void ?|?( ostype &, long double _Complex );
114
115 ostype & ?|?( ostype &, const char * );
116 void ?|?( ostype &, const char * );
117 // ostype & ?|?( ostype &, const char16_t * );
118#if ! ( __ARM_ARCH_ISA_ARM == 1 && __ARM_32BIT_STATE == 1 ) // char32_t == wchar_t => ambiguous
119 // ostype & ?|?( ostype &, const char32_t * );
120#endif // ! ( __ARM_ARCH_ISA_ARM == 1 && __ARM_32BIT_STATE == 1 )
121 // ostype & ?|?( ostype &, const wchar_t * );
122 ostype & ?|?( ostype &, const void * );
123 void ?|?( ostype &, const void * );
124
125 // manipulators
126 ostype & ?|?( ostype &, ostype & (*)( ostype & ) );
127 void ?|?( ostype &, ostype & (*)( ostype & ) );
128 ostype & nl( ostype & );
129 ostype & nonl( ostype & );
130 ostype & sep( ostype & );
131 ostype & sepTuple( ostype & );
132 ostype & sepOn( ostype & );
133 ostype & sepOff( ostype & );
134 ostype & sepDisable( ostype & );
135 ostype & sepEnable( ostype & );
136 ostype & nlOn( ostype & );
137 ostype & nlOff( ostype & );
138} // distribution
139
140// tuples
141forall( dtype ostype, otype T, ttype Params | writeable( T, ostype ) | { ostype & ?|?( ostype &, Params ); } ) {
142 ostype & ?|?( ostype & os, T arg, Params rest );
143 void ?|?( ostype & os, T arg, Params rest );
144} // distribution
145
146// writes the range [begin, end) to the given stream
147forall( dtype ostype, otype elt_type | writeable( elt_type, ostype ), otype iterator_type | iterator( iterator_type, elt_type ) ) {
148 void write( iterator_type begin, iterator_type end, ostype & os );
149 void write_reverse( iterator_type begin, iterator_type end, ostype & os );
150} // distribution
151
152//*********************************** manipulators ***********************************
153
154forall( otype T )
155struct _Ostream_Manip {
156 T val; // polymorphic base-type
157 unsigned int wd, pc; // width, precision
158 char base; // numeric base / floating-point style
159 union {
160 unsigned char all;
161 struct {
162 unsigned char pc:1; // precision specified
163 unsigned char left:1; // left justify
164 unsigned char nobsdp:1; // base prefix / decimal point
165 unsigned char sign:1; // plus / minus sign
166 unsigned char pad0:1; // zero pad
167 } flags;
168 };
169}; // _Ostream_Manip
170
171//*********************************** integral ***********************************
172
173// See 6.7.9. 19) The initialization shall occur in initializer list order, each initializer provided for a particular
174// subobject overriding any previously listed initializer for the same subobject; ***all subobjects that are not
175// initialized explicitly shall be initialized implicitly the same as objects that have static storage duration.***
176
177#define IntegralFMTDecl( T, CODE ) \
178static inline { \
179 _Ostream_Manip(T) bin( T val ) { return (_Ostream_Manip(T))@{ val, 1, 0, 'b', { .all : 0 } }; } \
180 _Ostream_Manip(T) oct( T val ) { return (_Ostream_Manip(T))@{ val, 1, 0, 'o', { .all : 0 } }; } \
181 _Ostream_Manip(T) hex( T val ) { return (_Ostream_Manip(T))@{ val, 1, 0, 'x', { .all : 0 } }; } \
182 _Ostream_Manip(T) wd( unsigned int w, T val ) { return (_Ostream_Manip(T))@{ val, w, 0, CODE, { .all : 0 } }; } \
183 _Ostream_Manip(T) wd( unsigned int w, unsigned char pc, T val ) { return (_Ostream_Manip(T))@{ val, w, pc, CODE, { .flags.pc : true } }; } \
184 _Ostream_Manip(T) & wd( unsigned int w, _Ostream_Manip(T) & fmt ) { fmt.wd = w; return fmt; } \
185 _Ostream_Manip(T) & wd( unsigned int w, unsigned char pc, _Ostream_Manip(T) & fmt ) { fmt.wd = w; fmt.pc = pc; fmt.flags.pc = true; return fmt; } \
186 _Ostream_Manip(T) & left( _Ostream_Manip(T) & fmt ) { fmt.flags.left = true; return fmt; } \
187 _Ostream_Manip(T) & upcase( _Ostream_Manip(T) & fmt ) { if ( fmt.base == 'x' || fmt.base == 'b' ) fmt.base -= 32; /* upper case */ return fmt; } \
188 _Ostream_Manip(T) & nobase( _Ostream_Manip(T) & fmt ) { fmt.flags.nobsdp = true; return fmt; } \
189 _Ostream_Manip(T) & pad0( _Ostream_Manip(T) & fmt ) { fmt.flags.pad0 = true; return fmt; } \
190 _Ostream_Manip(T) sign( T val ) { return (_Ostream_Manip(T))@{ val, 1, 0, CODE, { .flags.sign : true } }; } \
191 _Ostream_Manip(T) & sign( _Ostream_Manip(T) & fmt ) { fmt.flags.sign = true; return fmt; } \
192} /* distribution */ \
193forall( dtype ostype | ostream( ostype ) ) { \
194 ostype & ?|?( ostype & os, _Ostream_Manip(T) f ); \
195 void ?|?( ostype & os, _Ostream_Manip(T) f ); \
196} // ?|?
197
198IntegralFMTDecl( signed char, 'd' )
199IntegralFMTDecl( unsigned char, 'u' )
200IntegralFMTDecl( signed short int, 'd' )
201IntegralFMTDecl( unsigned short int, 'u' )
202IntegralFMTDecl( signed int, 'd' )
203IntegralFMTDecl( unsigned int, 'u' )
204IntegralFMTDecl( signed long int, 'd' )
205IntegralFMTDecl( unsigned long int, 'u' )
206IntegralFMTDecl( signed long long int, 'd' )
207IntegralFMTDecl( unsigned long long int, 'u' )
208
209//*********************************** floating point ***********************************
210
211// Default suffix for values with no fraction is "."
212#define FloatingPointFMTDecl( T ) \
213static inline { \
214 _Ostream_Manip(T) hex( T val ) { return (_Ostream_Manip(T))@{ val, 1, 0, 'a', { .all : 0 } }; } \
215 _Ostream_Manip(T) sci( T val ) { return (_Ostream_Manip(T))@{ val, 1, 0, 'e', { .all : 0 } }; } \
216 _Ostream_Manip(T) wd( unsigned int w, T val ) { return (_Ostream_Manip(T))@{ val, w, 0, 'f', { .all : 0 } }; } \
217 _Ostream_Manip(T) wd( unsigned int w, unsigned char pc, T val ) { return (_Ostream_Manip(T))@{ val, w, pc, 'f', { .flags.pc : true } }; } \
218 _Ostream_Manip(T) ws( unsigned int w, unsigned char pc, T val ) { return (_Ostream_Manip(T))@{ val, w, pc, 'g', { .flags.pc : true } }; } \
219 _Ostream_Manip(T) & wd( unsigned int w, _Ostream_Manip(T) & fmt ) { fmt.wd = w; return fmt; } \
220 _Ostream_Manip(T) & wd( unsigned int w, unsigned char pc, _Ostream_Manip(T) & fmt ) { fmt.wd = w; fmt.pc = pc; fmt.flags.pc = true; return fmt; } \
221 _Ostream_Manip(T) & left( _Ostream_Manip(T) & fmt ) { fmt.flags.left = true; return fmt; } \
222 _Ostream_Manip(T) upcase( T val ) { return (_Ostream_Manip(T))@{ val, 1, 0, 'G', { .all : 0 } }; } \
223 _Ostream_Manip(T) & upcase( _Ostream_Manip(T) & fmt ) { fmt.base -= 32; /* upper case */ return fmt; } \
224 _Ostream_Manip(T) & pad0( _Ostream_Manip(T) & fmt ) { fmt.flags.pad0 = true; return fmt; } \
225 _Ostream_Manip(T) sign( T val ) { return (_Ostream_Manip(T))@{ val, 1, 0, 'g', { .flags.sign : true } }; } \
226 _Ostream_Manip(T) & sign( _Ostream_Manip(T) & fmt ) { fmt.flags.sign = true; return fmt; } \
227 _Ostream_Manip(T) nodp( T val ) { return (_Ostream_Manip(T))@{ val, 1, 0, 'g', { .flags.nobsdp : true } }; } \
228 _Ostream_Manip(T) & nodp( _Ostream_Manip(T) & fmt ) { fmt.flags.nobsdp = true; return fmt; } \
229} /* distribution */ \
230forall( dtype ostype | ostream( ostype ) ) { \
231 ostype & ?|?( ostype & os, _Ostream_Manip(T) f ); \
232 void ?|?( ostype & os, _Ostream_Manip(T) f ); \
233} // ?|?
234
235FloatingPointFMTDecl( double )
236FloatingPointFMTDecl( long double )
237
238//*********************************** character ***********************************
239
240static inline {
241 _Ostream_Manip(char) bin( char c ) { return (_Ostream_Manip(char))@{ c, 1, 0, 'b', { .all : 0 } }; }
242 _Ostream_Manip(char) oct( char c ) { return (_Ostream_Manip(char))@{ c, 1, 0, 'o', { .all : 0 } }; }
243 _Ostream_Manip(char) hex( char c ) { return (_Ostream_Manip(char))@{ c, 1, 0, 'x', { .all : 0 } }; }
244 _Ostream_Manip(char) wd( unsigned int w, char c ) { return (_Ostream_Manip(char))@{ c, w, 0, 'c', { .all : 0 } }; }
245 _Ostream_Manip(char) & wd( unsigned int w, _Ostream_Manip(char) & fmt ) { fmt.wd = w; return fmt; }
246 _Ostream_Manip(char) & left( _Ostream_Manip(char) & fmt ) { fmt.flags.left = true; return fmt; }
247 _Ostream_Manip(char) & upcase( _Ostream_Manip(char) & fmt ) { if ( fmt.base == 'x' || fmt.base == 'b' ) fmt.base -= 32; /* upper case */ return fmt; }
248 _Ostream_Manip(char) & nobase( _Ostream_Manip(char) & fmt ) { fmt.flags.nobsdp = true; return fmt; }
249} // distribution
250forall( dtype ostype | ostream( ostype ) ) {
251 ostype & ?|?( ostype & os, _Ostream_Manip(char) f );
252 void ?|?( ostype & os, _Ostream_Manip(char) f );
253} // ?|?
254
255//*********************************** C string ***********************************
256
257static inline {
258 _Ostream_Manip(const char *) bin( const char * s ) { return (_Ostream_Manip(const char *))@{ s, 1, 0, 'b', { .all : 0 } }; }
259 _Ostream_Manip(const char *) oct( const char * s ) { return (_Ostream_Manip(const char *))@{ s, 1, 0, 'o', { .all : 0 } }; }
260 _Ostream_Manip(const char *) hex( const char * s ) { return (_Ostream_Manip(const char *))@{ s, 1, 0, 'x', { .all : 0 } }; }
261 _Ostream_Manip(const char *) wd( unsigned int w, const char * s ) { return (_Ostream_Manip(const char *))@{ s, w, 0, 's', { .all : 0 } }; }
262 _Ostream_Manip(const char *) wd( unsigned int w, unsigned char pc, const char * s ) { return (_Ostream_Manip(const char *))@{ s, w, pc, 's', { .flags.pc : true } }; }
263 _Ostream_Manip(const char *) & wd( unsigned int w, _Ostream_Manip(const char *) & fmt ) { fmt.wd = w; return fmt; }
264 _Ostream_Manip(const char *) & wd( unsigned int w, unsigned char pc, _Ostream_Manip(const char *) & fmt ) { fmt.wd = w; fmt.pc = pc; fmt.flags.pc = true; return fmt; }
265 _Ostream_Manip(const char *) & left( _Ostream_Manip(const char *) & fmt ) { fmt.flags.left = true; return fmt; }
266 _Ostream_Manip(const char *) & nobase( _Ostream_Manip(const char *) & fmt ) { fmt.flags.nobsdp = true; return fmt; }
267} // distribution
268forall( dtype ostype | ostream( ostype ) ) {
269 ostype & ?|?( ostype & os, _Ostream_Manip(const char *) f );
270 void ?|?( ostype & os, _Ostream_Manip(const char *) f );
271} // ?|?
272
273
274//*********************************** istream ***********************************
275
276
277trait istream( dtype istype ) {
278 void nlOn( istype & ); // read newline
279 void nlOff( istype & ); // scan newline
280 bool getANL( istype & ); // get scan newline (on/off)
281 int fail( istype & );
282 int eof( istype & );
283 void open( istype & is, const char * name );
284 void close( istype & is );
285 istype & read( istype &, char *, size_t );
286 istype & ungetc( istype &, char );
287 int fmt( istype &, const char format[], ... ) __attribute__(( format(scanf, 2, 3) ));
288}; // istream
289
290trait readable( otype T ) {
291 forall( dtype istype | istream( istype ) ) istype & ?|?( istype &, T );
292}; // readable
293
294forall( dtype istype | istream( istype ) ) {
295 istype & ?|?( istype &, bool & );
296
297 istype & ?|?( istype &, char & );
298 istype & ?|?( istype &, signed char & );
299 istype & ?|?( istype &, unsigned char & );
300
301 istype & ?|?( istype &, short int & );
302 istype & ?|?( istype &, unsigned short int & );
303 istype & ?|?( istype &, int & );
304 istype & ?|?( istype &, unsigned int & );
305 istype & ?|?( istype &, long int & );
306 istype & ?|?( istype &, long long int & );
307 istype & ?|?( istype &, unsigned long int & );
308 istype & ?|?( istype &, unsigned long long int & );
309
310 istype & ?|?( istype &, float & );
311 istype & ?|?( istype &, double & );
312 istype & ?|?( istype &, long double & );
313
314 istype & ?|?( istype &, float _Complex & );
315 istype & ?|?( istype &, double _Complex & );
316 istype & ?|?( istype &, long double _Complex & );
317
318// istype & ?|?( istype &, const char * );
319 istype & ?|?( istype &, char * );
320
321 // manipulators
322 istype & ?|?( istype &, istype & (*)( istype & ) );
323 istype & nl( istype & is );
324 istype & nlOn( istype & );
325 istype & nlOff( istype & );
326} // distribution
327
328//*********************************** manipulators ***********************************
329
330struct _Istream_Cstr {
331 char * s;
332 const char * scanset;
333 int wd; // width
334 union {
335 unsigned char all;
336 struct {
337 unsigned char ignore:1; // do not change input argument
338 unsigned char inex:1; // include/exclude characters in scanset
339 } flags;
340 };
341}; // _Istream_Cstr
342
343static inline {
344 _Istream_Cstr skip( unsigned int n ) { return (_Istream_Cstr){ 0p, 0p, n, { .all : 0 } }; }
345 _Istream_Cstr skip( const char * scanset ) { return (_Istream_Cstr){ 0p, scanset, -1, { .all : 0 } }; }
346 _Istream_Cstr incl( const char * scanset, char * s ) { return (_Istream_Cstr){ s, scanset, -1, { .flags.inex : false } }; }
347 _Istream_Cstr & incl( const char * scanset, _Istream_Cstr & fmt ) { fmt.scanset = scanset; fmt.flags.inex = false; return fmt; }
348 _Istream_Cstr excl( const char * scanset, char * s ) { return (_Istream_Cstr){ s, scanset, -1, { .flags.inex : true } }; }
349 _Istream_Cstr & excl( const char * scanset, _Istream_Cstr & fmt ) { fmt.scanset = scanset; fmt.flags.inex = true; return fmt; }
350 _Istream_Cstr ignore( const char * s ) { return (_Istream_Cstr)@{ s, 0p, -1, { .flags.ignore : true } }; }
351 _Istream_Cstr & ignore( _Istream_Cstr & fmt ) { fmt.flags.ignore = true; return fmt; }
352 _Istream_Cstr wdi( unsigned int w, char * s ) { return (_Istream_Cstr)@{ s, 0p, w, { .all : 0 } }; }
353 _Istream_Cstr & wdi( unsigned int w, _Istream_Cstr & fmt ) { fmt.wd = w; return fmt; }
354} // distribution
355forall( dtype istype | istream( istype ) ) istype & ?|?( istype & is, _Istream_Cstr f );
356
357struct _Istream_Char {
358 bool ignore; // do not change input argument
359}; // _Istream_Char
360
361static inline {
362 _Istream_Char ignore( const char c ) { return (_Istream_Char)@{ true }; }
363 _Istream_Char & ignore( _Istream_Char & fmt ) { fmt.ignore = true; return fmt; }
364} // distribution
365forall( dtype istype | istream( istype ) ) istype & ?|?( istype & is, _Istream_Char f );
366
367forall( otype T )
368struct _Istream_Manip {
369 T & val; // polymorphic base-type
370 int wd; // width
371 bool ignore; // do not change input argument
372}; // _Istream_Manip
373
374#define InputFMTDecl( T ) \
375static inline { \
376 _Istream_Manip(T) ignore( const T & val ) { return (_Istream_Manip(T))@{ (T &)val, -1, true }; } \
377 _Istream_Manip(T) & ignore( _Istream_Manip(T) & fmt ) { fmt.ignore = true; return fmt; } \
378 _Istream_Manip(T) wdi( unsigned int w, T & val ) { return (_Istream_Manip(T))@{ val, w, false }; } \
379 _Istream_Manip(T) & wdi( unsigned int w, _Istream_Manip(T) & fmt ) { fmt.wd = w; return fmt; } \
380} /* distribution */ \
381forall( dtype istype | istream( istype ) ) { \
382 istype & ?|?( istype & is, _Istream_Manip(T) f ); \
383} // ?|?
384
385InputFMTDecl( signed char )
386InputFMTDecl( unsigned char )
387InputFMTDecl( signed short int )
388InputFMTDecl( unsigned short int )
389InputFMTDecl( signed int )
390InputFMTDecl( unsigned int )
391InputFMTDecl( signed long int )
392InputFMTDecl( unsigned long int )
393InputFMTDecl( signed long long int )
394InputFMTDecl( unsigned long long int )
395
396InputFMTDecl( float )
397InputFMTDecl( double )
398InputFMTDecl( long double )
399
400InputFMTDecl( float _Complex )
401InputFMTDecl( double _Complex )
402InputFMTDecl( long double _Complex )
403
404
405//*********************************** time ***********************************
406
407
408#include <time_t.hfa> // Duration (constructors) / Time (constructors)
409
410forall( dtype ostype | ostream( ostype ) ) {
411 ostype & ?|?( ostype & os, Duration dur );
412 void ?|?( ostype & os, Duration dur );
413 ostype & ?|?( ostype & os, Time time );
414 void ?|?( ostype & os, Time time );
415} // distribution
416
417// Local Variables: //
418// tab-width: 4 //
419// End: //
Note: See TracBrowser for help on using the repository browser.