Jpp 19.3.0-rc.3
the software that should make you happy
Loading...
Searching...
No Matches
JAccessibleStream.hh
Go to the documentation of this file.
1#ifndef __JLANG__JACCESSIBLESTREAM__
2#define __JLANG__JACCESSIBLESTREAM__
3
4#include <fstream>
5
7#include "JLang/JException.hh"
8
9
10/**
11 * \author mdejong
12 */
13
14namespace JLANG {}
15namespace JPP { using namespace JLANG; }
16
17namespace JLANG {
18
19 /**
20 * Accessible input stream.
21 *
22 * This class is a simple wrapper around the std::ifstream class.
23 * It implements the JAccessible interface.
24 */
26 protected std::ifstream,
27 public virtual JAccessible
28 {
29 public:
30 /**
31 * Default constructor.
32 */
35
36
37 /**
38 * Constructor.
39 *
40 * \param file_name file name
41 */
42 JAccessibleInputStream(const char* file_name)
43 {
44 open(file_name);
45 }
46
47
48 /**
49 * Check is file is open.
50 *
51 * \return true if open; else false
52 */
53 virtual bool is_open() const
54 {
55 return std::ifstream::is_open();
56 }
57
58
59 /**
60 * Open file.
61 *
62 * \param file_name file name
63 */
64 virtual void open(const char* file_name)
65 {
66 std::ifstream::open(file_name);
67
68 if (!this->is_open()) {
69 Throw(JFileOpenException(std::string("Error opening file ") + file_name));
70 }
71 }
72
73
74 /**
75 * Close file.
76 */
77 virtual void close()
78 {
79 std::ifstream::close();
80 }
81 };
82
83
84 /**
85 * Accessible output stream.
86 *
87 * This class is a simple wrapper around the std::ofstream class.
88 * It class implements the JAccessible interface.
89 */
91 protected std::ofstream,
92 public virtual JAccessible
93 {
94 public:
95 /**
96 * Default constructor.
97 */
100
101
102 /**
103 * Constructor.
104 *
105 * \param file_name file name
106 */
107 JAccessibleOutputStream(const char* file_name)
108 {
109 open(file_name);
110 }
111
112
113 /**
114 * Check is file is open.
115 *
116 * \return true if open; else false
117 */
118 virtual bool is_open() const
119 {
120 return std::ofstream::is_open();
121 }
122
123
124 /**
125 * Open file.
126 *
127 * \param file_name file name
128 */
129 virtual void open(const char* file_name)
130 {
131 std::ofstream::open(file_name);
132
133 if (!this->is_open()) {
134 Throw(JFileOpenException(std::string("Error opening file ") + file_name));
135 }
136 }
137
138
139 /**
140 * Close file.
141 */
142 virtual void close()
143 {
144 std::ofstream::close();
145 }
146 };
147}
148
149#endif
Exceptions.
Accessible input stream.
virtual void open(const char *file_name)
Open file.
virtual void close()
Close file.
JAccessibleInputStream()
Default constructor.
JAccessibleInputStream(const char *file_name)
Constructor.
virtual bool is_open() const
Check is file is open.
Accessible output stream.
virtual void close()
Close file.
JAccessibleOutputStream(const char *file_name)
Constructor.
virtual bool is_open() const
Check is file is open.
JAccessibleOutputStream()
Default constructor.
virtual void open(const char *file_name)
Open file.
Interface for named access of a device.
Exception for opening of file.
static void Throw(const bool option)
Definition JThrow.hh:37
Auxiliary classes and methods for language specific functionality.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).