source: libcfa/src/iostream.hfa @ 3c573e9

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 3c573e9 was 3c573e9, checked in by Peter A. Buhr <pabuhr@…>, 5 years ago

first draft of output manipulators and start input manipulators

  • Property mode set to 100644
File size: 17.4 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 Jun  4 17:33:43 2019
13// Update Count     : 286
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        int fail( ostype & );
50        int flush( ostype & );
51        void open( ostype & os, const char * name, const char * mode );
52        void close( ostype & os );
53        ostype & write( ostype &, const char *, size_t );
54        int fmt( ostype &, const char format[], ... ) __attribute__(( format(printf, 2, 3) ));
55}; // ostream
56
57// trait writeable( otype T ) {
58//      forall( dtype ostype | ostream( ostype ) ) ostype & ?|?( ostype &, T );
59// }; // writeable
60
61trait writeable( otype T, dtype ostype | ostream( ostype ) ) {
62        ostype & ?|?( ostype &, T );
63}; // writeable
64
65// implement writable for intrinsic types
66
67forall( dtype ostype | ostream( ostype ) ) {
68        ostype & ?|?( ostype &, zero_t );
69        void ?|?( ostype &, zero_t );
70        ostype & ?|?( ostype &, one_t );
71        void ?|?( ostype &, one_t );
72
73        ostype & ?|?( ostype &, bool );
74        void ?|?( ostype &, bool );
75
76        ostype & ?|?( ostype &, char );
77        void ?|?( ostype &, char );
78        ostype & ?|?( ostype &, signed char );
79        void ?|?( ostype &, signed char );
80        ostype & ?|?( ostype &, unsigned char );
81        void ?|?( ostype &, unsigned char );
82
83        ostype & ?|?( ostype &, short int );
84        void ?|?( ostype &, short int );
85        ostype & ?|?( ostype &, unsigned short int );
86        void ?|?( ostype &, unsigned short int );
87        ostype & ?|?( ostype &, int );
88        void ?|?( ostype &, int );
89        ostype & ?|?( ostype &, unsigned int );
90        void ?|?( ostype &, unsigned int );
91        ostype & ?|?( ostype &, long int );
92        void ?|?( ostype &, long int );
93        ostype & ?|?( ostype &, long long int );
94        void ?|?( ostype &, long long int );
95        ostype & ?|?( ostype &, unsigned long int );
96        void ?|?( ostype &, unsigned long int );
97        ostype & ?|?( ostype &, unsigned long long int );
98        void ?|?( ostype &, unsigned long long int );
99
100        ostype & ?|?( ostype &, float ); // FIX ME: should not be required
101        void ?|?( ostype &, float ); // FIX ME: should not be required
102        ostype & ?|?( ostype &, double );
103        void ?|?( ostype &, double );
104        ostype & ?|?( ostype &, long double );
105        void ?|?( ostype &, long double );
106
107        ostype & ?|?( ostype &, float _Complex );
108        void ?|?( ostype &, float _Complex );
109        ostype & ?|?( ostype &, double _Complex );
110        void ?|?( ostype &, double _Complex );
111        ostype & ?|?( ostype &, long double _Complex );
112        void ?|?( ostype &, long double _Complex );
113
114        ostype & ?|?( ostype &, const char * );
115        void ?|?( ostype &, const char * );
116        // ostype & ?|?( ostype &, const char16_t * );
117#if ! ( __ARM_ARCH_ISA_ARM == 1 && __ARM_32BIT_STATE == 1 ) // char32_t == wchar_t => ambiguous
118        // ostype & ?|?( ostype &, const char32_t * );
119#endif // ! ( __ARM_ARCH_ISA_ARM == 1 && __ARM_32BIT_STATE == 1 )
120        // ostype & ?|?( ostype &, const wchar_t * );
121        ostype & ?|?( ostype &, const void * );
122        void ?|?( ostype &, const void * );
123
124        // manipulators
125        ostype & ?|?( ostype &, ostype & (*)( ostype & ) );
126        void ?|?( ostype &, ostype & (*)( ostype & ) );
127        ostype & nl( ostype & );
128        void 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 char 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#define IntegralFMTDecl( T, CODE ) \
174static inline { \
175        _Ostream_Manip(T) bin( T v ) { return (_Ostream_Manip(T))@{ v, 1, 0, 'b', { .all : 0 } }; } \
176        _Ostream_Manip(T) oct( T v ) { return (_Ostream_Manip(T))@{ v, 1, 0, 'o', { .all : 0 } }; } \
177        _Ostream_Manip(T) hex( T v ) { return (_Ostream_Manip(T))@{ v, 1, 0, 'x', { .all : 0 } }; } \
178        _Ostream_Manip(T) wd( unsigned char w, T v ) { return (_Ostream_Manip(T))@{ v, w, 0, CODE, { .all : 0 } }; } \
179        _Ostream_Manip(T) wd( unsigned char w, unsigned char p, T v ) { return (_Ostream_Manip(T))@{ v, w, p, CODE, { .flags.pc : true } }; } \
180        _Ostream_Manip(T) & wd( unsigned char w, _Ostream_Manip(T) & fmt ) with(fmt) { wd = w; return fmt; } \
181        _Ostream_Manip(T) & wd( unsigned char w, unsigned char p, _Ostream_Manip(T) & fmt ) with(fmt) { wd = w; pc = p; flags.pc = true; return fmt; } \
182        _Ostream_Manip(T) & left( _Ostream_Manip(T) & fmt ) { fmt.flags.left = true; return fmt; } \
183        _Ostream_Manip(T) & upcase( _Ostream_Manip(T) & fmt ) { if ( fmt.base == 'x' || fmt.base == 'b' ) fmt.base -= 32; /* upper case */ return fmt; } \
184        _Ostream_Manip(T) & nobase( _Ostream_Manip(T) & fmt ) { fmt.flags.nobsdp = true; return fmt; } \
185        _Ostream_Manip(T) & pad0( _Ostream_Manip(T) & fmt ) { fmt.flags.pad0 = true; return fmt; } \
186        _Ostream_Manip(T) sign( T v ) { return (_Ostream_Manip(T))@{ v, 1, 0, CODE, { .flags.sign : true } }; } \
187        _Ostream_Manip(T) & sign( _Ostream_Manip(T) & fmt ) { fmt.flags.sign = true; return fmt; } \
188} \
189forall( dtype ostype | ostream( ostype ) ) { \
190        ostype & ?|?( ostype & os, _Ostream_Manip(T) f ); \
191        void ?|?( ostype & os, _Ostream_Manip(T) f ); \
192}
193
194IntegralFMTDecl( signed char, 'd' )
195IntegralFMTDecl( unsigned char, 'u' )
196IntegralFMTDecl( signed short int, 'd' )
197IntegralFMTDecl( unsigned short int, 'u' )
198IntegralFMTDecl( signed int, 'd' )
199IntegralFMTDecl( unsigned int, 'u' )
200IntegralFMTDecl( signed long int, 'd' )
201IntegralFMTDecl( unsigned long int, 'u' )
202IntegralFMTDecl( signed long long int, 'd' )
203IntegralFMTDecl( unsigned long long int, 'u' )
204
205//*********************************** Floating Point ***********************************
206
207// Default suffix for values with no fraction is "."
208#define FloatingPointFMTDecl( T ) \
209static inline { \
210        _Ostream_Manip(T) sci( T v ) { return (_Ostream_Manip(T))@{ v, 1, 0, 'e', { .all : 0 } }; } \
211        _Ostream_Manip(T) hex( T v ) { return (_Ostream_Manip(T))@{ v, 1, 0, 'a', { .all : 0 } }; } \
212        _Ostream_Manip(T) wd( unsigned char w, T v ) { return (_Ostream_Manip(T))@{ v, w, 0, 'f', { .all : 0 } }; } \
213        _Ostream_Manip(T) wd( unsigned char w, unsigned char p, T v ) { return (_Ostream_Manip(T))@{ v, w, p, 'f', { .flags.pc : true } }; } \
214        _Ostream_Manip(T) ws( unsigned char w, unsigned char p, T v ) { return (_Ostream_Manip(T))@{ v, w, p, 'g', { .flags.pc : true } }; } \
215        _Ostream_Manip(T) & wd( unsigned char w, _Ostream_Manip(T) & fmt ) with(fmt) { wd = w; return fmt; } \
216        _Ostream_Manip(T) & wd( unsigned char w, unsigned char p, _Ostream_Manip(T) & fmt ) with(fmt) { wd = w; pc = p; flags.pc = true; return fmt; } \
217        _Ostream_Manip(T) & left( _Ostream_Manip(T) & fmt ) { fmt.flags.left = true; return fmt; } \
218        _Ostream_Manip(T) upcase( T v ) { return (_Ostream_Manip(T))@{ v, 1, 0, 'G', { .all : 0 } }; } \
219        _Ostream_Manip(T) & upcase( _Ostream_Manip(T) & fmt ) { fmt.base -= 32; /* upper case */ return fmt; } \
220        _Ostream_Manip(T) & pad0( _Ostream_Manip(T) & fmt ) { fmt.flags.pad0 = true; return fmt; } \
221        _Ostream_Manip(T) sign( T v ) { return (_Ostream_Manip(T))@{ v, 1, 0, 'g', { .flags.sign : true } }; } \
222        _Ostream_Manip(T) & sign( _Ostream_Manip(T) & fmt ) { fmt.flags.sign = true; return fmt; } \
223        _Ostream_Manip(T) nodp( T v ) { return (_Ostream_Manip(T))@{ v, 1, 0, 'g', { .flags.nobsdp : true } }; } \
224        _Ostream_Manip(T) & nodp( _Ostream_Manip(T) & fmt ) { fmt.flags.nobsdp = true; return fmt; } \
225} \
226forall( dtype ostype | ostream( ostype ) ) { \
227        ostype & ?|?( ostype & os, _Ostream_Manip(T) f ); \
228        void ?|?( ostype & os, _Ostream_Manip(T) f ); \
229}
230
231FloatingPointFMTDecl( double )
232FloatingPointFMTDecl( long double )
233
234//*********************************** Character ***********************************
235
236static inline {
237        _Ostream_Manip(char) bin( char v ) { return (_Ostream_Manip(char))@{ v, 1, 0, 'b', { .all : 0 } }; }
238        _Ostream_Manip(char) oct( char v ) { return (_Ostream_Manip(char))@{ v, 1, 0, 'o', { .all : 0 } }; }
239        _Ostream_Manip(char) hex( char v ) { return (_Ostream_Manip(char))@{ v, 1, 0, 'x', { .all : 0 } }; }
240        _Ostream_Manip(char) wd( unsigned char w, char v ) { return (_Ostream_Manip(char))@{ v, w, 0, 'c', { .all : 0 } }; }
241        _Ostream_Manip(char) & wd( unsigned char w, _Ostream_Manip(char) & fmt ) with(fmt) { wd = w; return fmt; }
242        _Ostream_Manip(char) & left( _Ostream_Manip(char) & fmt ) { fmt.flags.left = true; return fmt; }
243        _Ostream_Manip(char) & upcase( _Ostream_Manip(char) & fmt ) { if ( fmt.base == 'x' || fmt.base == 'b' ) fmt.base -= 32; /* upper case */ return fmt; }
244        _Ostream_Manip(char) & nobase( _Ostream_Manip(char) & fmt ) { fmt.flags.nobsdp = true; return fmt; }
245} // distribution
246forall( dtype ostype | ostream( ostype ) ) {
247        ostype & ?|?( ostype & os, _Ostream_Manip(char) f );
248        void ?|?( ostype & os, _Ostream_Manip(char) f );
249}
250
251//*********************************** C String ***********************************
252
253static inline {
254        _Ostream_Manip(const char *) bin( const char * v ) { return (_Ostream_Manip(const char *))@{ v, 1, 0, 'b', { .all : 0 } }; }
255        _Ostream_Manip(const char *) oct( const char * v ) { return (_Ostream_Manip(const char *))@{ v, 1, 0, 'o', { .all : 0 } }; }
256        _Ostream_Manip(const char *) hex( const char * v ) { return (_Ostream_Manip(const char *))@{ v, 1, 0, 'x', { .all : 0 } }; }
257        _Ostream_Manip(const char *) wd( unsigned char w, const char * v ) { return (_Ostream_Manip(const char *))@{ v, w, 0, 's', { .all : 0 } }; }
258        _Ostream_Manip(const char *) wd( unsigned char w, unsigned char p, const char * v ) { return (_Ostream_Manip(const char *))@{ v, w, p, 's', { .flags.pc : true } }; }
259        _Ostream_Manip(const char *) & wd( unsigned char w, _Ostream_Manip(const char *) & fmt ) with(fmt) { wd = w; return fmt; }
260        _Ostream_Manip(const char *) & wd( unsigned char w, unsigned char p, _Ostream_Manip(const char *) & fmt ) with(fmt) { wd = w; pc = p; flags.pc = true; return fmt; }
261        _Ostream_Manip(const char *) & left( _Ostream_Manip(const char *) & fmt ) { fmt.flags.left = true; return fmt; }
262        _Ostream_Manip(const char *) & nobase( _Ostream_Manip(const char *) & fmt ) { fmt.flags.nobsdp = true; return fmt; }
263} // distribution
264forall( dtype ostype | ostream( ostype ) ) {
265        ostype & ?|?( ostype & os, _Ostream_Manip(const char *) f );
266        void ?|?( ostype & os, _Ostream_Manip(const char *) f );
267}
268
269
270//*********************************** Istream ***********************************
271
272
273trait istream( dtype istype ) {
274        void nlOn( istype & );                                                          // read newline
275        void nlOff( istype & );                                                         // scan newline
276        bool getANL( istype & );                                                        // get scan newline (on/off)
277        int fail( istype & );
278        int eof( istype & );
279        void open( istype & is, const char * name );
280        void close( istype & is );
281        istype & read( istype &, char *, size_t );
282        istype & ungetc( istype &, char );
283        int fmt( istype &, const char format[], ... ) __attribute__(( format(scanf, 2, 3) ));
284}; // istream
285
286trait readable( otype T ) {
287        forall( dtype istype | istream( istype ) ) istype & ?|?( istype &, T );
288}; // readable
289
290forall( dtype istype | istream( istype ) ) {
291        istype & ?|?( istype &, bool & );
292
293        istype & ?|?( istype &, char & );
294        istype & ?|?( istype &, signed char & );
295        istype & ?|?( istype &, unsigned char & );
296
297        istype & ?|?( istype &, short int & );
298        istype & ?|?( istype &, unsigned short int & );
299        istype & ?|?( istype &, int & );
300        istype & ?|?( istype &, unsigned int & );
301        istype & ?|?( istype &, long int & );
302        istype & ?|?( istype &, long long int & );
303        istype & ?|?( istype &, unsigned long int & );
304        istype & ?|?( istype &, unsigned long long int & );
305
306        istype & ?|?( istype &, float & );
307        istype & ?|?( istype &, double & );
308        istype & ?|?( istype &, long double & );
309
310        istype & ?|?( istype &, float _Complex & );
311        istype & ?|?( istype &, double _Complex & );
312        istype & ?|?( istype &, long double _Complex & );
313
314        // manipulators
315        istype & ?|?( istype &, istype & (*)( istype & ) );
316        istype & nl( istype & is );
317        istype & nlOn( istype & );
318        istype & nlOff( istype & );
319} // distribution
320
321struct _Istream_cstrUC { char * s; };
322_Istream_cstrUC cstr( char * );
323forall( dtype istype | istream( istype ) ) istype & ?|?( istype &, _Istream_cstrUC );
324
325struct _Istream_cstrC { char * s; int size; };
326_Istream_cstrC cstr( char *, int size );
327forall( dtype istype | istream( istype ) ) istype & ?|?( istype &, _Istream_cstrC );
328
329#if 0
330struct _Istream_Cstr {
331        char * s, * scanset;
332        int wd;                                                                                         // width
333        bool ignore;                                                                            // no input argument
334};
335static inline _Istream_Cstr skip( char * s ) { return (_Istream_Cstr){ 0p, s, -1, false }; }
336static inline _Istream_Cstr incl( const char * scanset, char * s ) { return (_Istream_Cstr){ s, scanset, -1, false }; }
337static inline _Istream_Cstr excl( const char * scanset, char * s ) { return (_Istream_Cstr){ s, scanset, -1, false }; }
338static inline _Istream_Cstr cstr( char * s ) { return (_Istream_Cstr){ s, 0p, -1, false }; }
339static inline _Istream_Cstr ignore( char * s ) { return (_Istream_Cstr)@{ s, 0p, -1, true }; }
340static inline _Istream_Cstr ignore( _Istream_Cstr & fmt ) { fmt.ignore = true; return fmt; }
341static inline _Istream_Cstr wd( unsigned int w, char * s ) { return (_Istream_Cstr)@{ s, 0p, w, false }; }
342forall( dtype istype | istream( istype ) ) istype & ?|?( istype &, _Istream_Cstr );
343
344//*********************************** Manipulators ***********************************
345
346forall( otype T )
347struct _Istream_Manip {
348        T & val;                                                                                        // polymorphic base-type
349        int wd;                                                                                         // width
350        bool ignore;                                                                            // no input argument
351}; // _Istream_Manip
352
353#define InputFMTDecl( T ) \
354static inline _Istream_Manip(T) ignore( T & v ) { return (_Istream_Manip(T))@{ v, -1, true }; } \
355static inline _Istream_Manip(T) ignore( _Istream_Manip(T) & fmt ) { fmt.ignore = true; return fmt; } \
356static inline _Istream_Manip(T) wd( unsigned int w, T & v ) { return (_Istream_Manip(T))@{ v, w, false }; } \
357forall( dtype istype | istream( istype ) ) { \
358        istype & ?|?( istype & is, _Istream_Manip(T) f ); \
359}
360
361InputFMTDecl( char )
362InputFMTDecl( signed char )
363InputFMTDecl( unsigned char )
364InputFMTDecl( signed short int )
365InputFMTDecl( unsigned short int )
366InputFMTDecl( signed int )
367InputFMTDecl( unsigned int )
368InputFMTDecl( signed long int )
369InputFMTDecl( unsigned long int )
370InputFMTDecl( signed long long int )
371InputFMTDecl( unsigned long long int )
372
373InputFMTDecl( float )
374InputFMTDecl( double )
375InputFMTDecl( long double )
376
377InputFMTDecl( float _Complex )
378InputFMTDecl( double _Complex )
379InputFMTDecl( long double _Complex )
380#endif // 0
381
382
383//*********************************** Time ***********************************
384
385
386#include <time_t.hfa>                                                                   // Duration (constructors) / Time (constructors)
387
388forall( dtype ostype | ostream( ostype ) ) {
389        ostype & ?|?( ostype & os, Duration dur );
390        void ?|?( ostype & os, Duration dur );
391        ostype & ?|?( ostype & os, Time time );
392        void ?|?( ostype & os, Time time );
393} // distribution
394
395// Local Variables: //
396// tab-width: 4 //
397// End: //
Note: See TracBrowser for help on using the repository browser.