Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
fstream.h
Go to the documentation of this file.
1
6#pragma once
7
8#include <fstream>
9
10#include <ext/ostream>
11#include <ext/istream>
12
13namespace ext {
14
15class ofstream : public ostream {
16public:
17 ofstream ( const char * filename ) : ostream ( new std::filebuf ( ) ) {
18 open ( filename );
19 }
20
21 ofstream ( const std::string & filename ) : ofstream ( filename.c_str ( ) ) {
22 }
23
24 ofstream ( ofstream && rhs ) noexcept : ostream ( std::move ( rhs ) ) {
25 rhs = ofstream ( "" );
26 }
27
28 ofstream & operator = ( ofstream && ) noexcept = default;
29
30 void swap ( ofstream & other ) {
31 ofstream tmp ( std::move ( * this ) );
32 * this = std::move ( other );
33 other = std::move ( tmp );
34 }
35
36 void open ( const char * filename, std::ios_base::openmode mode = std::ios_base::out) {
37 std::ostream & os = static_cast < std::ostream & > ( * this );
38 if ( ! rdbuf ( )->open ( filename, mode ) ) {
39 os.setstate ( std::ios_base::failbit );
40 } else {
41 os.clear ( );
42 }
43 }
44
45 void close ( ) {
46 std::ostream & os = static_cast < std::ostream & > ( * this );
47 if ( rdbuf ( )->close ( ) )
48 os.setstate ( std::ios_base::failbit );
49 }
50
51 bool is_open ( ) {
52 return rdbuf ( )->is_open ( );
53 }
54
55 bool is_open ( ) const {
56 return rdbuf ( )->is_open ( );
57 }
58
59 std::filebuf * rdbuf ( ) {
60 return static_cast < std::filebuf * > ( ostream::rdbuf ( ) );
61 }
62
63 const std::filebuf * rdbuf ( ) const {
64 return static_cast < const std::filebuf * > ( ostream::rdbuf ( ) );
65 }
66};
67
68class ifstream : public istream {
69public:
70 ifstream ( const char * filename ) : istream ( new std::filebuf ( ) ) {
71 open ( filename );
72 }
73
74 ifstream ( const std::string & filename ) : ifstream ( filename.c_str ( ) ) {
75 }
76
77 ifstream ( ifstream && rhs ) noexcept : istream ( std::move ( rhs ) ) {
78 rhs = ifstream ( "" );
79 }
80
81 ifstream & operator = ( ifstream && ) noexcept = default;
82
83 void swap ( ifstream & other ) {
84 ifstream tmp ( std::move ( * this ) );
85 * this = std::move ( other );
86 other = std::move ( tmp );
87 }
88
89 void open ( const char * filename, std::ios_base::openmode mode = std::ios_base::in ) {
90 std::istream & os = static_cast < std::istream & > ( * this );
91 if ( ! rdbuf ( )->open ( filename, mode ) ) {
92 os.setstate ( std::ios_base::failbit );
93 } else {
94 os.clear ( );
95 }
96 }
97
98 void close ( ) {
99 std::istream & os = static_cast < std::istream & > ( * this );
100 if ( rdbuf ( )->close ( ) )
101 os.setstate ( std::ios_base::failbit );
102 }
103
104 bool is_open ( ) {
105 return rdbuf ( )->is_open ( );
106 }
107
108 bool is_open ( ) const {
109 return rdbuf ( )->is_open ( );
110 }
111
112 std::filebuf * rdbuf ( ) {
113 return static_cast < std::filebuf * > ( istream::rdbuf ( ) );
114 }
115
116 const std::filebuf * rdbuf ( ) const {
117 return static_cast < const std::filebuf * > ( istream::rdbuf ( ) );
118 }
119};
120
121} /* namespace ext */
Definition: fstream.h:68
ifstream(const std::string &filename)
Definition: fstream.h:74
bool is_open()
Definition: fstream.h:104
void swap(ifstream &other)
Definition: fstream.h:83
bool is_open() const
Definition: fstream.h:108
ifstream & operator=(ifstream &&) noexcept=default
std::filebuf * rdbuf()
Definition: fstream.h:112
const std::filebuf * rdbuf() const
Definition: fstream.h:116
void close()
Definition: fstream.h:98
ifstream(const char *filename)
Definition: fstream.h:70
ifstream(ifstream &&rhs) noexcept
Definition: fstream.h:77
void open(const char *filename, std::ios_base::openmode mode=std::ios_base::in)
Definition: fstream.h:89
Definition: istream.h:32
istream(std::basic_streambuf< char > *sb)
Definition: istream.cpp:10
std::streambuf * rdbuf() const
Definition: istream.cpp:88
Definition: fstream.h:15
ofstream & operator=(ofstream &&) noexcept=default
void open(const char *filename, std::ios_base::openmode mode=std::ios_base::out)
Definition: fstream.h:36
void swap(ofstream &other)
Definition: fstream.h:30
ofstream(const std::string &filename)
Definition: fstream.h:21
const std::filebuf * rdbuf() const
Definition: fstream.h:63
bool is_open() const
Definition: fstream.h:55
std::filebuf * rdbuf()
Definition: fstream.h:59
ofstream(const char *filename)
Definition: fstream.h:17
ofstream(ofstream &&rhs) noexcept
Definition: fstream.h:24
bool is_open()
Definition: fstream.h:51
void close()
Definition: fstream.h:45
Definition: ostream.h:14
ostream(std::basic_streambuf< char > *sb)
Definition: ostream.cpp:10
std::streambuf * rdbuf() const
Definition: ostream.cpp:65
Definition: sigHandler.cpp:20
Definition: FordFulkerson.hpp:16