Panda3D
pandaFileStream.h
1 // Filename: pandaFileStream.h
2 // Created by: drose (08Sep08)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #ifndef PANDAFILESTREAM_H
16 #define PANDAFILESTREAM_H
17 
18 #include "dtoolbase.h"
19 
20 #ifdef USE_PANDAFILESTREAM
21 
22 #include "pandaFileStreamBuf.h"
23 
24 ////////////////////////////////////////////////////////////////////
25 // Class : IFileStream
26 // Description : Implements a C++ stream object suitable for reading
27 // from files on disk. This is similar to ifstream, but
28 // it provides low-level support for Panda's
29 // simple-threading implementation (using this interface
30 // will block only the current thread, rather than the
31 // entire process, on I/O waits).
32 ////////////////////////////////////////////////////////////////////
33 class EXPCL_DTOOL IFileStream : public istream {
34 PUBLISHED:
35  INLINE IFileStream();
36  INLINE IFileStream(const char *filename, ios::openmode mode = ios::in);
37  INLINE ~IFileStream();
38 
39  INLINE void open(const char *filename, ios::openmode mode = ios::in);
40 
41 public:
42 #ifdef _WIN32
43  INLINE void attach(const char *filename, HANDLE handle, ios::openmode mode = ios::in);
44 #else
45  INLINE void attach(const char *filename, int fd, ios::openmode mode = ios::in);
46 #endif
47 
48 PUBLISHED:
49  INLINE void close();
50 
51 private:
52  PandaFileStreamBuf _buf;
53 };
54 
55 ////////////////////////////////////////////////////////////////////
56 // Class : OFileStream
57 // Description : Implements a C++ stream object suitable for writing
58 // to files on disk. This is similar to ofstream, but
59 // it provides low-level support for Panda's
60 // simple-threading implementation (using this interface
61 // will block only the current thread, rather than the
62 // entire process, on I/O waits).
63 ////////////////////////////////////////////////////////////////////
64 class EXPCL_DTOOL OFileStream : public ostream {
65 PUBLISHED:
66  INLINE OFileStream();
67  INLINE OFileStream(const char *filename, ios::openmode mode = ios::out);
68  INLINE ~OFileStream();
69 
70  INLINE void open(const char *filename, ios::openmode mode = ios::out);
71 
72 public:
73 #ifdef _WIN32
74  INLINE void attach(const char *filename, HANDLE handle, ios::openmode mode = ios::out);
75 #else
76  INLINE void attach(const char *filename, int fd, ios::openmode mode = ios::out);
77 #endif
78 
79 PUBLISHED:
80  INLINE void close();
81 
82 private:
83  PandaFileStreamBuf _buf;
84 };
85 
86 ////////////////////////////////////////////////////////////////////
87 // Class : FileStream
88 // Description : Implements a C++ stream object suitable for reading
89 // from and/or writing to files on disk. This is
90 // similar to fstream, but it provides low-level support
91 // for Panda's simple-threading implementation (using
92 // this interface will block only the current thread,
93 // rather than the entire process, on I/O waits).
94 ////////////////////////////////////////////////////////////////////
95 class EXPCL_DTOOL FileStream : public iostream {
96 PUBLISHED:
97  INLINE FileStream();
98  INLINE FileStream(const char *filename, ios::openmode mode = ios::in);
99  INLINE ~FileStream();
100 
101  INLINE void open(const char *filename, ios::openmode mode = ios::in);
102 
103 public:
104 #ifdef _WIN32
105  INLINE void attach(const char *filename, HANDLE handle, ios::openmode mode);
106 #else
107  INLINE void attach(const char *filename, int fd, ios::openmode mode);
108 #endif
109 
110 PUBLISHED:
111  INLINE void close();
112 
113 private:
114  PandaFileStreamBuf _buf;
115 };
116 
117 #include "pandaFileStream.I"
118 
119 typedef IFileStream pifstream;
120 typedef OFileStream pofstream;
121 typedef FileStream pfstream;
122 
123 #else // USE_PANDAFILESTREAM
124 
125 typedef ifstream pifstream;
126 typedef ofstream pofstream;
127 typedef fstream pfstream;
128 
129 #endif // USE_PANDAFILESTREAM
130 
131 #endif