source: nrEngine/include/IStream.h @ 30

Revision 30, 5.6 KB checked in by art, 12 years ago (diff)

::std wurde durch std ueberall ersetzt!

Line 
1/***************************************************************************
2 *                                                                         *
3 *   (c) Art Tevs, MPI Informatik Saarbruecken                             *
4 *       mailto: <tevs@mpi-sb.mpg.de>                                      *
5 *                                                                         *
6 *   This program is free software; you can redistribute it and/or modify  *
7 *   it under the terms of the GNU General Public License as published by  *
8 *   the Free Software Foundation; either version 2 of the License, or     *
9 *   (at your option) any later version.                                   *
10 *                                                                         *
11 ***************************************************************************/
12
13
14#ifndef _NR_I_STREAM_H_
15#define _NR_I_STREAM_H_
16
17
18//----------------------------------------------------------------------------------
19// Includes
20//----------------------------------------------------------------------------------
21#include "Prerequisities.h"
22
23namespace nrEngine{
24
25        //! Stream is an interface for stream objects (files, urls, ...)
26        /**
27        * IStream represents an interface for every kind of stream objects.
28        * Stream objects should be able to pass the data from somewhere else
29        * to the requester (e.g. engine). The interface is similar to the
30        * std::istream object so you can think it is like a wrapper.
31        *
32        * Good example for such stream objects could be a file stream. In the
33        * standard c++ library you already have got the ifstream which can be
34        * reimplemented for this interface.
35        *
36        *
37        * \ingroup gp
38        **/
39        class _NRExport IStream {
40        public:
41
42                //! Start position for the seeking
43                enum {
44
45                        //! Compute new cursor position from the beginning
46                        START,
47
48                        //! Seek from the current position
49                        CURRENT,
50
51                        //! NEw position is computed from the end of file
52                        END
53                };
54
55                /**
56                * Empty default constructor.
57                * @param streamBufSize Size of the buffer for this stream. Only buffered streams.
58                **/
59                IStream(uint32 streamBufSize = STREAM_BUFFER_SIZE);
60
61                /**
62                * Constructor allows specify a name for the stream
63                * @param name Name of the stream
64                * @param streamBufSize Size of the buffer for this stream. Only buffered streams.
65                **/
66                IStream(const std::string& name, uint32 streamBufSize = STREAM_BUFFER_SIZE);
67
68                /**
69                * Virtual destructor allows to derive new classes from this interface
70                **/
71                virtual ~IStream();
72
73                /**
74                * Return the name of the stream
75                **/
76                virtual const std::string& getName()    { return mName; }
77
78                /**
79                * Streaming operator to stream the data to the variable
80                **/
81                template<typename T> IStream& operator>>(T& val);
82
83                /**
84                * Get the size of the buffer (only for buffered streams)
85                **/
86                NR_FORCEINLINE int32 getBufferSize() const { return mStreamBufSize; }
87
88                /**
89                * Read data from the stream (like read from stdio.h).
90                *
91                * @param buf Here the readed data will be stored
92                * @param count Number of bytes to be readed
93                *
94                * @return Count fo readed bytes.
95                * @note <i>buf</i> should have at least size <i>count</i>
96                **/
97                size_t read(void *buf, size_t count);
98
99                /**
100                * Same as read but allows reading of specified count of
101                * elements instead of bytes.
102                *
103                * @param buf Buffer where to store the readed data
104                * @param size Size of each element to be readed
105                * @param nmemb Count of elements
106                *
107                * @return Number of successfully readed elements (not bytes)
108                **/
109                virtual size_t read(void *buf, size_t size, size_t nmemb) = 0;
110
111                /**
112                * Same as read, but reads the line until it found
113                * the delimiter string in the data
114                **/
115                virtual size_t readDelim(void* buf, size_t count, const std::string& delim = std::string("\n")) = 0;
116
117                /**
118                * Returns the current byte offset from the beginning
119                * in other words position of the stream cursor
120                **/
121                virtual size_t tell() const = 0;
122
123                /**
124                * Returns true if end of the stream is reached
125                **/
126                virtual bool eof() const = 0;
127
128                /**
129                * Return the full data containing in the stream only if we were
130                * able to retrieve the data. If the stream is buffered, so it
131                * should return the content of the whole buffer and retrieve new data.
132                * @param count Return count of bytes returned by the function
133                **/
134                virtual byte* getData(size_t& count) const = 0;
135
136                /**
137                * Only for text-only streams. You have the abbility to return the whole
138                * stream content as a string
139                **/
140                virtual std::string getAsString();
141
142                /**
143                * Read a line from the stream. Only usefull for text-only-streams
144                **/
145                virtual std::string getLine();
146
147                /**
148                * Seek the read pointer to specified position.
149                *
150                * @param count Number of bytes to jump
151                * @param whence From where we should compute the new position
152                *                               CURRENT, START, END
153                * @return false if error occurs (e.g. eof or wrong whence position)
154                **/
155                virtual bool seek(int32 offset, int32 whence = CURRENT) = 0;
156
157                /**
158                * Returns the size of the stream (ength of the data in the stream)
159                * If size could not be determined, so return 0
160                **/
161                size_t size() const { return mSize; }
162
163                /**
164                * Close the stream. After you close it no operations like seek, or read
165                * are valid anymore. The stream is also automaticly closed if
166                * you call the destructor.
167                **/
168                virtual void close () = 0;
169
170                //! Default size of the buffer
171                static const uint32 STREAM_BUFFER_SIZE = 256;
172
173        protected:
174
175                //! Name of the stream
176                std::string     mName;
177
178                //! Size of the stream in bytes. If size could not be determined so 0
179                size_t  mSize;
180
181                //! Our stream buffer should be of this size
182                uint32 mStreamBufSize;
183
184                //! Buffer to hold the data.
185                byte*   mBuffer;
186
187        };
188
189}; // end namespace
190
191#endif
Note: See TracBrowser for help on using the repository browser.