source: libcfa/src/iostream.hfa@ c5cbc099

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

remove special print for zero_t and one_t

  • Property mode set to 100644
File size: 19.0 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 : Tue Aug 11 22:16:14 2020
13// Update Count : 350
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 &, bool );
70 void ?|?( ostype &, bool );
71
72 ostype & ?|?( ostype &, char );
73 void ?|?( ostype &, char );
74 ostype & ?|?( ostype &, signed char );
75 void ?|?( ostype &, signed char );
76 ostype & ?|?( ostype &, unsigned char );
77 void ?|?( ostype &, unsigned char );
78
79 ostype & ?|?( ostype &, short int );
80 void ?|?( ostype &, short int );
81 ostype & ?|?( ostype &, unsigned short int );
82 void ?|?( ostype &, unsigned short int );
83 ostype & ?|?( ostype &, int );
84 void ?|?( ostype &, int );
85 ostype & ?|?( ostype &, unsigned int );
86 void ?|?( ostype &, unsigned int );
87 ostype & ?|?( ostype &, long int );
88 void ?|?( ostype &, long int );
89 ostype & ?|?( ostype &, long long int );
90 void ?|?( ostype &, long long int );
91 ostype & ?|?( ostype &, unsigned long int );
92 void ?|?( ostype &, unsigned long int );
93 ostype & ?|?( ostype &, unsigned long long int );
94 void ?|?( ostype &, unsigned long long int );
95#if defined( __SIZEOF_INT128__ )
96 ostype & ?|?( ostype &, int128 );
97 void ?|?( ostype &, int128 );
98 ostype & ?|?( ostype &, unsigned int128 );
99 void ?|?( ostype &, unsigned int128 );
100#endif // __SIZEOF_INT128__
101
102 ostype & ?|?( ostype &, float );
103 void ?|?( ostype &, float );
104 ostype & ?|?( ostype &, double );
105 void ?|?( ostype &, double );
106 ostype & ?|?( ostype &, long double );
107 void ?|?( ostype &, long double );
108
109 ostype & ?|?( ostype &, float _Complex );
110 void ?|?( ostype &, float _Complex );
111 ostype & ?|?( ostype &, double _Complex );
112 void ?|?( ostype &, double _Complex );
113 ostype & ?|?( ostype &, long double _Complex );
114 void ?|?( ostype &, long double _Complex );
115
116 ostype & ?|?( ostype &, const char [] );
117 void ?|?( ostype &, const char [] );
118 // ostype & ?|?( ostype &, const char16_t * );
119#if ! ( __ARM_ARCH_ISA_ARM == 1 && __ARM_32BIT_STATE == 1 ) // char32_t == wchar_t => ambiguous
120 // ostype & ?|?( ostype &, const char32_t * );
121#endif // ! ( __ARM_ARCH_ISA_ARM == 1 && __ARM_32BIT_STATE == 1 )
122 // ostype & ?|?( ostype &, const wchar_t * );
123 ostype & ?|?( ostype &, const void * );
124 void ?|?( ostype &, const void * );
125
126 // manipulators
127 ostype & ?|?( ostype &, ostype & (*)( ostype & ) );
128 void ?|?( ostype &, ostype & (*)( ostype & ) );
129 ostype & nl( ostype & );
130 ostype & nonl( ostype & );
131 ostype & sep( ostype & );
132 ostype & sepTuple( ostype & );
133 ostype & sepOn( ostype & );
134 ostype & sepOff( ostype & );
135 ostype & sepDisable( ostype & );
136 ostype & sepEnable( ostype & );
137 ostype & nlOn( ostype & );
138 ostype & nlOff( ostype & );
139} // distribution
140
141// tuples
142forall( dtype ostype, otype T, ttype Params | writeable( T, ostype ) | { ostype & ?|?( ostype &, Params ); } ) {
143 ostype & ?|?( ostype & os, T arg, Params rest );
144 void ?|?( ostype & os, T arg, Params rest );
145} // distribution
146
147// writes the range [begin, end) to the given stream
148forall( dtype ostype, otype elt_type | writeable( elt_type, ostype ), otype iterator_type | iterator( iterator_type, elt_type ) ) {
149 void write( iterator_type begin, iterator_type end, ostype & os );
150 void write_reverse( iterator_type begin, iterator_type end, ostype & os );
151} // distribution
152
153// *********************************** manipulators ***********************************
154
155forall( otype T )
156struct _Ostream_Manip {
157 T val; // polymorphic base-type
158 unsigned int wd, pc; // width, precision
159 char base; // numeric base / floating-point style
160 union {
161 unsigned char all;
162 struct {
163 unsigned char neg:1; // val is negative
164 unsigned char pc:1; // precision specified
165 unsigned char left:1; // left justify
166 unsigned char nobsdp:1; // base prefix / decimal point
167 unsigned char sign:1; // plus / minus sign
168 unsigned char pad0:1; // zero pad
169 } flags;
170 };
171}; // _Ostream_Manip
172
173// *********************************** integral ***********************************
174
175// See 6.7.9. 19) The initialization shall occur in initializer list order, each initializer provided for a particular
176// subobject overriding any previously listed initializer for the same subobject; ***all subobjects that are not
177// initialized explicitly shall be initialized implicitly the same as objects that have static storage duration.***
178
179#define IntegralFMTDecl( T, CODE ) \
180static inline { \
181 _Ostream_Manip(T) bin( T val ) { return (_Ostream_Manip(T))@{ val, 1, 0, 'b', { .all : 0 } }; } \
182 _Ostream_Manip(T) oct( T val ) { return (_Ostream_Manip(T))@{ val, 1, 0, 'o', { .all : 0 } }; } \
183 _Ostream_Manip(T) hex( T val ) { return (_Ostream_Manip(T))@{ val, 1, 0, 'x', { .all : 0 } }; } \
184 _Ostream_Manip(T) wd( unsigned int w, T val ) { return (_Ostream_Manip(T))@{ val, w, 0, CODE, { .all : 0 } }; } \
185 _Ostream_Manip(T) wd( unsigned int w, unsigned char pc, T val ) { return (_Ostream_Manip(T))@{ val, w, pc, CODE, { .flags.pc : true } }; } \
186 _Ostream_Manip(T) & wd( unsigned int w, _Ostream_Manip(T) & fmt ) { fmt.wd = w; return fmt; } \
187 _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; } \
188 _Ostream_Manip(T) & left( _Ostream_Manip(T) & fmt ) { fmt.flags.left = true; return fmt; } \
189 _Ostream_Manip(T) & upcase( _Ostream_Manip(T) & fmt ) { if ( fmt.base == 'x' || fmt.base == 'b' ) fmt.base -= 32; /* upper case */ return fmt; } \
190 _Ostream_Manip(T) & nobase( _Ostream_Manip(T) & fmt ) { fmt.flags.nobsdp = true; return fmt; } \
191 _Ostream_Manip(T) & pad0( _Ostream_Manip(T) & fmt ) { fmt.flags.pad0 = true; return fmt; } \
192 _Ostream_Manip(T) sign( T val ) { return (_Ostream_Manip(T))@{ val, 1, 0, CODE, { .flags.sign : true } }; } \
193 _Ostream_Manip(T) & sign( _Ostream_Manip(T) & fmt ) { fmt.flags.sign = true; return fmt; } \
194} /* distribution */ \
195forall( dtype ostype | ostream( ostype ) ) { \
196 ostype & ?|?( ostype & os, _Ostream_Manip(T) f ); \
197 void ?|?( ostype & os, _Ostream_Manip(T) f ); \
198} // ?|?
199
200IntegralFMTDecl( signed char, 'd' )
201IntegralFMTDecl( unsigned char, 'u' )
202IntegralFMTDecl( signed short int, 'd' )
203IntegralFMTDecl( unsigned short int, 'u' )
204IntegralFMTDecl( signed int, 'd' )
205IntegralFMTDecl( unsigned int, 'u' )
206IntegralFMTDecl( signed long int, 'd' )
207IntegralFMTDecl( unsigned long int, 'u' )
208IntegralFMTDecl( signed long long int, 'd' )
209IntegralFMTDecl( unsigned long long int, 'u' )
210#if defined( __SIZEOF_INT128__ )
211IntegralFMTDecl( int128, 'd' )
212IntegralFMTDecl( unsigned int128, 'u' )
213#endif // __SIZEOF_INT128__
214
215// *********************************** floating point ***********************************
216
217// Default suffix for values with no fraction is "."
218#define FloatingPointFMTDecl( T ) \
219static inline { \
220 _Ostream_Manip(T) hex( T val ) { return (_Ostream_Manip(T))@{ val, 1, 0, 'a', { .all : 0 } }; } \
221 _Ostream_Manip(T) sci( T val ) { return (_Ostream_Manip(T))@{ val, 1, 0, 'e', { .all : 0 } }; } \
222 _Ostream_Manip(T) wd( unsigned int w, T val ) { return (_Ostream_Manip(T))@{ val, w, 0, 'f', { .all : 0 } }; } \
223 _Ostream_Manip(T) wd( unsigned int w, unsigned char pc, T val ) { return (_Ostream_Manip(T))@{ val, w, pc, 'f', { .flags.pc : true } }; } \
224 _Ostream_Manip(T) ws( unsigned int w, unsigned char pc, T val ) { return (_Ostream_Manip(T))@{ val, w, pc, 'g', { .flags.pc : true } }; } \
225 _Ostream_Manip(T) & wd( unsigned int w, _Ostream_Manip(T) & fmt ) { fmt.wd = w; return fmt; } \
226 _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; } \
227 _Ostream_Manip(T) & left( _Ostream_Manip(T) & fmt ) { fmt.flags.left = true; return fmt; } \
228 _Ostream_Manip(T) upcase( T val ) { return (_Ostream_Manip(T))@{ val, 1, 0, 'G', { .all : 0 } }; } \
229 _Ostream_Manip(T) & upcase( _Ostream_Manip(T) & fmt ) { fmt.base -= 32; /* upper case */ return fmt; } \
230 _Ostream_Manip(T) & pad0( _Ostream_Manip(T) & fmt ) { fmt.flags.pad0 = true; return fmt; } \
231 _Ostream_Manip(T) sign( T val ) { return (_Ostream_Manip(T))@{ val, 1, 0, 'g', { .flags.sign : true } }; } \
232 _Ostream_Manip(T) & sign( _Ostream_Manip(T) & fmt ) { fmt.flags.sign = true; return fmt; } \
233 _Ostream_Manip(T) nodp( T val ) { return (_Ostream_Manip(T))@{ val, 1, 0, 'g', { .flags.nobsdp : true } }; } \
234 _Ostream_Manip(T) & nodp( _Ostream_Manip(T) & fmt ) { fmt.flags.nobsdp = true; return fmt; } \
235} /* distribution */ \
236forall( dtype ostype | ostream( ostype ) ) { \
237 ostype & ?|?( ostype & os, _Ostream_Manip(T) f ); \
238 void ?|?( ostype & os, _Ostream_Manip(T) f ); \
239} // ?|?
240
241FloatingPointFMTDecl( double )
242FloatingPointFMTDecl( long double )
243
244// *********************************** character ***********************************
245
246static inline {
247 _Ostream_Manip(char) bin( char c ) { return (_Ostream_Manip(char))@{ c, 1, 0, 'b', { .all : 0 } }; }
248 _Ostream_Manip(char) oct( char c ) { return (_Ostream_Manip(char))@{ c, 1, 0, 'o', { .all : 0 } }; }
249 _Ostream_Manip(char) hex( char c ) { return (_Ostream_Manip(char))@{ c, 1, 0, 'x', { .all : 0 } }; }
250 _Ostream_Manip(char) wd( unsigned int w, char c ) { return (_Ostream_Manip(char))@{ c, w, 0, 'c', { .all : 0 } }; }
251 _Ostream_Manip(char) & wd( unsigned int w, _Ostream_Manip(char) & fmt ) { fmt.wd = w; return fmt; }
252 _Ostream_Manip(char) & left( _Ostream_Manip(char) & fmt ) { fmt.flags.left = true; return fmt; }
253 _Ostream_Manip(char) & upcase( _Ostream_Manip(char) & fmt ) { if ( fmt.base == 'x' || fmt.base == 'b' ) fmt.base -= 32; /* upper case */ return fmt; }
254 _Ostream_Manip(char) & nobase( _Ostream_Manip(char) & fmt ) { fmt.flags.nobsdp = true; return fmt; }
255} // distribution
256forall( dtype ostype | ostream( ostype ) ) {
257 ostype & ?|?( ostype & os, _Ostream_Manip(char) f );
258 void ?|?( ostype & os, _Ostream_Manip(char) f );
259} // ?|?
260
261// *********************************** C string ***********************************
262
263static inline {
264 _Ostream_Manip(const char *) bin( const char s[] ) { return (_Ostream_Manip(const char *))@{ s, 1, 0, 'b', { .all : 0 } }; }
265 _Ostream_Manip(const char *) oct( const char s[] ) { return (_Ostream_Manip(const char *))@{ s, 1, 0, 'o', { .all : 0 } }; }
266 _Ostream_Manip(const char *) hex( const char s[] ) { return (_Ostream_Manip(const char *))@{ s, 1, 0, 'x', { .all : 0 } }; }
267 _Ostream_Manip(const char *) wd( unsigned int w, const char s[] ) { return (_Ostream_Manip(const char *))@{ s, w, 0, 's', { .all : 0 } }; }
268 _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 } }; }
269 _Ostream_Manip(const char *) & wd( unsigned int w, _Ostream_Manip(const char *) & fmt ) { fmt.wd = w; return fmt; }
270 _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; }
271 _Ostream_Manip(const char *) & left( _Ostream_Manip(const char *) & fmt ) { fmt.flags.left = true; return fmt; }
272 _Ostream_Manip(const char *) & nobase( _Ostream_Manip(const char *) & fmt ) { fmt.flags.nobsdp = true; return fmt; }
273} // distribution
274forall( dtype ostype | ostream( ostype ) ) {
275 ostype & ?|?( ostype & os, _Ostream_Manip(const char *) f );
276 void ?|?( ostype & os, _Ostream_Manip(const char *) f );
277} // ?|?
278
279
280// *********************************** istream ***********************************
281
282
283trait istream( dtype istype ) {
284 void nlOn( istype & ); // read newline
285 void nlOff( istype & ); // scan newline
286 bool getANL( istype & ); // get scan newline (on/off)
287 int fail( istype & );
288 int eof( istype & );
289 void open( istype & is, const char name[] );
290 void close( istype & is );
291 istype & read( istype &, char *, size_t );
292 istype & ungetc( istype &, char );
293 int fmt( istype &, const char format[], ... ) __attribute__(( format(scanf, 2, 3) ));
294}; // istream
295
296trait readable( otype T ) {
297 forall( dtype istype | istream( istype ) ) istype & ?|?( istype &, T );
298}; // readable
299
300forall( dtype istype | istream( istype ) ) {
301 istype & ?|?( istype &, bool & );
302
303 istype & ?|?( istype &, char & );
304 istype & ?|?( istype &, signed char & );
305 istype & ?|?( istype &, unsigned char & );
306
307 istype & ?|?( istype &, short int & );
308 istype & ?|?( istype &, unsigned short int & );
309 istype & ?|?( istype &, int & );
310 istype & ?|?( istype &, unsigned int & );
311 istype & ?|?( istype &, long int & );
312 istype & ?|?( istype &, unsigned long int & );
313 istype & ?|?( istype &, long long int & );
314 istype & ?|?( istype &, unsigned long long int & );
315#if defined( __SIZEOF_INT128__ )
316 istype & ?|?( istype &, int128 & );
317 istype & ?|?( istype &, unsigned int128 & );
318#endif // __SIZEOF_INT128__
319
320 istype & ?|?( istype &, float & );
321 istype & ?|?( istype &, double & );
322 istype & ?|?( istype &, long double & );
323
324 istype & ?|?( istype &, float _Complex & );
325 istype & ?|?( istype &, double _Complex & );
326 istype & ?|?( istype &, long double _Complex & );
327
328// istype & ?|?( istype &, const char [] );
329 istype & ?|?( istype &, char * );
330
331 // manipulators
332 istype & ?|?( istype &, istype & (*)( istype & ) );
333 istype & nl( istype & is );
334 istype & nlOn( istype & );
335 istype & nlOff( istype & );
336} // distribution
337
338// *********************************** manipulators ***********************************
339
340struct _Istream_Cstr {
341 char * s;
342 const char * scanset;
343 int wd; // width
344 union {
345 unsigned char all;
346 struct {
347 unsigned char ignore:1; // do not change input argument
348 unsigned char inex:1; // include/exclude characters in scanset
349 } flags;
350 };
351}; // _Istream_Cstr
352
353static inline {
354 _Istream_Cstr skip( unsigned int n ) { return (_Istream_Cstr){ 0p, 0p, n, { .all : 0 } }; }
355 _Istream_Cstr skip( const char scanset[] ) { return (_Istream_Cstr){ 0p, scanset, -1, { .all : 0 } }; }
356 _Istream_Cstr incl( const char scanset[], char * s ) { return (_Istream_Cstr){ s, scanset, -1, { .flags.inex : false } }; }
357 _Istream_Cstr & incl( const char scanset[], _Istream_Cstr & fmt ) { fmt.scanset = scanset; fmt.flags.inex = false; return fmt; }
358 _Istream_Cstr excl( const char scanset[], char * s ) { return (_Istream_Cstr){ s, scanset, -1, { .flags.inex : true } }; }
359 _Istream_Cstr & excl( const char scanset[], _Istream_Cstr & fmt ) { fmt.scanset = scanset; fmt.flags.inex = true; return fmt; }
360 _Istream_Cstr ignore( char s[] ) { return (_Istream_Cstr)@{ s, 0p, -1, { .flags.ignore : true } }; }
361 _Istream_Cstr & ignore( _Istream_Cstr & fmt ) { fmt.flags.ignore = true; return fmt; }
362 _Istream_Cstr wdi( unsigned int w, char s[] ) { return (_Istream_Cstr)@{ s, 0p, w, { .all : 0 } }; }
363 _Istream_Cstr & wdi( unsigned int w, _Istream_Cstr & fmt ) { fmt.wd = w; return fmt; }
364} // distribution
365forall( dtype istype | istream( istype ) ) istype & ?|?( istype & is, _Istream_Cstr f );
366
367struct _Istream_Char {
368 bool ignore; // do not change input argument
369}; // _Istream_Char
370
371static inline {
372 _Istream_Char ignore( const char ) { return (_Istream_Char)@{ true }; }
373 _Istream_Char & ignore( _Istream_Char & fmt ) { fmt.ignore = true; return fmt; }
374} // distribution
375forall( dtype istype | istream( istype ) ) istype & ?|?( istype & is, _Istream_Char f );
376
377forall( dtype T | sized( T ) )
378struct _Istream_Manip {
379 T & val; // polymorphic base-type
380 int wd; // width
381 bool ignore; // do not change input argument
382}; // _Istream_Manip
383
384#define InputFMTDecl( T ) \
385static inline { \
386 _Istream_Manip(T) ignore( const T & val ) { return (_Istream_Manip(T))@{ (T &)val, -1, true }; } \
387 _Istream_Manip(T) & ignore( _Istream_Manip(T) & fmt ) { fmt.ignore = true; return fmt; } \
388 _Istream_Manip(T) wdi( unsigned int w, T & val ) { return (_Istream_Manip(T))@{ val, w, false }; } \
389 _Istream_Manip(T) & wdi( unsigned int w, _Istream_Manip(T) & fmt ) { fmt.wd = w; return fmt; } \
390} /* distribution */ \
391forall( dtype istype | istream( istype ) ) { \
392 istype & ?|?( istype & is, _Istream_Manip(T) f ); \
393} // ?|?
394
395InputFMTDecl( signed char )
396InputFMTDecl( unsigned char )
397InputFMTDecl( signed short int )
398InputFMTDecl( unsigned short int )
399InputFMTDecl( signed int )
400InputFMTDecl( unsigned int )
401InputFMTDecl( signed long int )
402InputFMTDecl( unsigned long int )
403InputFMTDecl( signed long long int )
404InputFMTDecl( unsigned long long int )
405
406InputFMTDecl( float )
407InputFMTDecl( double )
408InputFMTDecl( long double )
409
410InputFMTDecl( float _Complex )
411InputFMTDecl( double _Complex )
412InputFMTDecl( long double _Complex )
413
414
415// *********************************** time ***********************************
416
417
418#include <time_t.hfa> // Duration (constructors) / Time (constructors)
419
420forall( dtype ostype | ostream( ostype ) ) {
421 ostype & ?|?( ostype & os, Duration dur );
422 void ?|?( ostype & os, Duration dur );
423 ostype & ?|?( ostype & os, Time time );
424 void ?|?( ostype & os, Time time );
425} // distribution
426
427// Local Variables: //
428// tab-width: 4 //
429// End: //
Note: See TracBrowser for help on using the repository browser.