//
// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
//
// The contents of this file are covered under the licence agreement in the
// file "LICENCE" distributed with Cforall.
//
// iostream --
//
// Author           : Peter A. Buhr
// Created On       : Wed May 27 17:56:53 2015
// Last Modified By : Peter A. Buhr
// Last Modified On : Mon Sep 11 09:17:07 2017
// Update Count     : 137
//

#pragma once

#include <uchar.h>
#include <wchar.h>
#include "iterator"

trait ostream( dtype ostype ) {
	// private
	_Bool sepPrt( ostype * );							// return separator state (on/off)
	void sepReset( ostype * );							// set separator state to default state
	void sepReset( ostype *, _Bool );					// set separator and default state
	const char * sepGetCur( ostype * );					// get current separator string
	void sepSetCur( ostype *, const char * );			// set current separator string
	_Bool getNL( ostype * );							// check newline
	void setNL( ostype *, _Bool );						// saw newline
	// public
	void sepOn( ostype * );								// turn separator state on
	void sepOff( ostype * );							// turn separator state off
	_Bool sepDisable( ostype * );						// set default state to off, and return previous state
	_Bool sepEnable( ostype * );						// set default state to on, and return previous state

	const char * sepGet( ostype * );					// get separator string
	void sepSet( ostype *, const char * );				// set separator to string (15 character maximum)
	const char * sepGetTuple( ostype * );				// get tuple separator string
	void sepSetTuple( ostype *, const char * );			// set tuple separator to string (15 character maximum)

	int fail( ostype * );
	int flush( ostype * );
	void open( ostype * os, const char * name, const char * mode );
	void close( ostype * os );
	ostype * write( ostype *, const char *, unsigned long int );
	int fmt( ostype *, const char fmt[], ... );
}; // ostream

// trait writeable( otype T ) {
// 	forall( dtype ostype | ostream( ostype ) ) ostype * ?|?( ostype *, T );
// }; // writeable

trait writeable( otype T, dtype ostype | ostream( ostype ) ) {
	ostype * ?|?( ostype *, T );
}; // writeable

// implement writable for intrinsic types

forall( dtype ostype | ostream( ostype ) ) ostype * ?|?( ostype *, char );
forall( dtype ostype | ostream( ostype ) ) ostype * ?|?( ostype *, signed char );
forall( dtype ostype | ostream( ostype ) ) ostype * ?|?( ostype *, unsigned char );

forall( dtype ostype | ostream( ostype ) ) ostype * ?|?( ostype *, short int );
forall( dtype ostype | ostream( ostype ) ) ostype * ?|?( ostype *, unsigned short int );
forall( dtype ostype | ostream( ostype ) ) ostype * ?|?( ostype *, int );
forall( dtype ostype | ostream( ostype ) ) ostype * ?|?( ostype *, unsigned int );
forall( dtype ostype | ostream( ostype ) ) ostype * ?|?( ostype *, long int );
forall( dtype ostype | ostream( ostype ) ) ostype * ?|?( ostype *, long long int );
forall( dtype ostype | ostream( ostype ) ) ostype * ?|?( ostype *, unsigned long int );
forall( dtype ostype | ostream( ostype ) ) ostype * ?|?( ostype *, unsigned long long int );

forall( dtype ostype | ostream( ostype ) ) ostype * ?|?( ostype *, float ); // FIX ME: should not be required
forall( dtype ostype | ostream( ostype ) ) ostype * ?|?( ostype *, double );
forall( dtype ostype | ostream( ostype ) ) ostype * ?|?( ostype *, long double );

forall( dtype ostype | ostream( ostype ) ) ostype * ?|?( ostype *, float _Complex );
forall( dtype ostype | ostream( ostype ) ) ostype * ?|?( ostype *, double _Complex );
forall( dtype ostype | ostream( ostype ) ) ostype * ?|?( ostype *, long double _Complex );

forall( dtype ostype | ostream( ostype ) ) ostype * ?|?( ostype *, const char * );
forall( dtype ostype | ostream( ostype ) ) ostype * ?|?( ostype *, const char16_t * );
forall( dtype ostype | ostream( ostype ) ) ostype * ?|?( ostype *, const char32_t * );
forall( dtype ostype | ostream( ostype ) ) ostype * ?|?( ostype *, const wchar_t * );
forall( dtype ostype | ostream( ostype ) ) ostype * ?|?( ostype *, const void * );

// tuples
forall( dtype ostype, otype T, ttype Params | writeable( T, ostype ) | { ostype * ?|?( ostype *, Params ); } )
ostype * ?|?( ostype * os, T arg, Params rest );

// manipulators
forall( dtype ostype | ostream( ostype ) ) ostype * ?|?( ostype *, ostype * (*)( ostype * ) );
forall( dtype ostype | ostream( ostype ) ) ostype * endl( ostype * );
forall( dtype ostype | ostream( ostype ) ) ostype * sep( ostype * );
forall( dtype ostype | ostream( ostype ) ) ostype * sepTuple( ostype * );
forall( dtype ostype | ostream( ostype ) ) ostype * sepOn( ostype * );
forall( dtype ostype | ostream( ostype ) ) ostype * sepOff( ostype * );
forall( dtype ostype | ostream( ostype ) ) ostype * sepDisable( ostype * );
forall( dtype ostype | ostream( ostype ) ) ostype * sepEnable( ostype * );

// writes the range [begin, end) to the given stream
forall( dtype ostype, otype elt_type | writeable( elt_type, ostype ), otype iterator_type | iterator( iterator_type, elt_type ) )
void write( iterator_type begin, iterator_type end, ostype * os );

forall( dtype ostype, otype elt_type | writeable( elt_type, ostype ), otype iterator_type | iterator( iterator_type, elt_type ) )
void write_reverse( iterator_type begin, iterator_type end, ostype * os );

//---------------------------------------

trait istream( dtype istype ) {
	int fail( istype * );
	int eof( istype * );
	void open( istype * is, const char * name, const char * mode );
	void close( istype * is );
	istype * read( istype *, char *, unsigned long int );
	istype * ungetc( istype *, char );
	int fmt( istype *, const char fmt[], ... );
}; // istream

trait readable( otype T ) {
	forall( dtype istype | istream( istype ) ) istype * ?|?( istype *, T );
}; // readable

forall( dtype istype | istream( istype ) ) istype * ?|?( istype *, char & );
forall( dtype istype | istream( istype ) ) istype * ?|?( istype *, signed char & );
forall( dtype istype | istream( istype ) ) istype * ?|?( istype *, unsigned char & );

forall( dtype istype | istream( istype ) ) istype * ?|?( istype *, short int & );
forall( dtype istype | istream( istype ) ) istype * ?|?( istype *, unsigned short int & );
forall( dtype istype | istream( istype ) ) istype * ?|?( istype *, int & );
forall( dtype istype | istream( istype ) ) istype * ?|?( istype *, unsigned int & );
forall( dtype istype | istream( istype ) ) istype * ?|?( istype *, long int & );
forall( dtype istype | istream( istype ) ) istype * ?|?( istype *, long long int & );
forall( dtype istype | istream( istype ) ) istype * ?|?( istype *, unsigned long int & );
forall( dtype istype | istream( istype ) ) istype * ?|?( istype *, unsigned long long int & );

forall( dtype istype | istream( istype ) ) istype * ?|?( istype *, float & );
forall( dtype istype | istream( istype ) ) istype * ?|?( istype *, double & );
forall( dtype istype | istream( istype ) ) istype * ?|?( istype *, long double & );

forall( dtype istype | istream( istype ) ) istype * ?|?( istype *, float _Complex & );
forall( dtype istype | istream( istype ) ) istype * ?|?( istype *, double _Complex & );
forall( dtype istype | istream( istype ) ) istype * ?|?( istype *, long double _Complex & );

struct _Istream_cstrUC { char * s; };
_Istream_cstrUC cstr( char * );
forall( dtype istype | istream( istype ) ) istype * ?|?( istype *, _Istream_cstrUC );

struct _Istream_cstrC { char * s; int size; };
_Istream_cstrC cstr( char *, int size );
forall( dtype istype | istream( istype ) ) istype * ?|?( istype *, _Istream_cstrC );

// Local Variables: //
// mode: c //
// tab-width: 4 //
// End: //
