summaryrefslogtreecommitdiffstats
path: root/src/include/usr/util/utilstream.H
blob: 6390631112b7a2bbc884e6c648c60355649a1c60 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/include/usr/util/utilstream.H $                           */
/*                                                                        */
/* OpenPOWER HostBoot Project                                             */
/*                                                                        */
/* COPYRIGHT International Business Machines Corp. 2003,2014              */
/*                                                                        */
/* Licensed under the Apache License, Version 2.0 (the "License");        */
/* you may not use this file except in compliance with the License.       */
/* You may obtain a copy of the License at                                */
/*                                                                        */
/*     http://www.apache.org/licenses/LICENSE-2.0                         */
/*                                                                        */
/* Unless required by applicable law or agreed to in writing, software    */
/* distributed under the License is distributed on an "AS IS" BASIS,      */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or        */
/* implied. See the License for the specific language governing           */
/* permissions and limitations under the License.                         */
/*                                                                        */
/* IBM_PROLOG_END_TAG                                                     */

#ifndef UTILSTREAM_H
#define UTILSTREAM_H

/**
 * @file utilstream.H
 *
 * @brief       Stream manipulation
 *
 * Used for creating and manipulating streams
 *
 */

/*****************************************************************************/
// I n c l u d e s
/*****************************************************************************/
#include <stdint.h>
#include <errl/errlentry.H>

/*****************************************************************************/
// User Types
/*****************************************************************************/

//*****************************************************************************/
// C o n s t a n t s
/*****************************************************************************/

/*****************************************************************************/
// Stream class
/*****************************************************************************/
/**
 *  @brief  Stream Base class
 *
 *  Stream interface specification
 *
 */
class UtilStream
{
  public:

    enum whence
    {
        CURRENT = 0,
        START = 1,
        END = 2
    };


    /**
     *  @brief Default constructor
     *
     *  Initializes internals of the stream object.
     *
     */
    UtilStream();

    /**
     *  @brief Destructor
     *
     */
    virtual ~UtilStream();

    /**
     *  @brief Reads data from current position of the stream
     *
     *  Reads data from the current postion of the stream for the
     *  specified number of bytes.
     *
     *  @return # of bytes read or zero if none.
     *
     *  @note Errors are not reported by this interface, instead
     *        the user must call getLastError.
     *
     */
    virtual uint32_t read(
        void *      o_buffer,   ///< Buffer data is read into
        uint32_t    i_size      ///< Size in bytes of data to be read
        ) = 0;

    /**
     *  @brief Writes data to current position of the stream
     *
     *  Writes data to the current postion of the stream for the
     *  specified number of bytes.
     *
     *  @return # of bytes written or zero if none.
     *
     *  @note Errors are not reported by this interface, instead
     *        the user must call getLastError.
     *
     */
    virtual uint32_t write(
        const void *i_buffer,   ///< Source buffer data is written from
        uint32_t    i_size      ///< Size in bytes of data to be written
        ) = 0;

    /**
     *  @brief Seek to the specified position in the stream
     *
     *  Performs a seek based on the specified position and offset.
     *
     *  @return Current file position after the interface completes.
     *
     *  @note Errors are not reported by this interface, instead
     *        the user must call getLastError.
     *
     *  @example
     *  // To Get the current file position
     *
     *  UtilFile    l_file;
     *  ...
     *  uint32_t l_curpos = l_file.seek( 0, l_file.CURRENT );
     *
     *  @endexample
     *
     */
    virtual uint32_t seek(
        int32_t     i_pos,      ///< Seek offset in bytes
        whence      i_whence    ///< Position to seek from
        ) = 0;

    /**
     *  @brief Return the current size of the stream
     *
     *  Returns the current size of the stream.
     *
     */
    virtual uint32_t size() const = 0;

    /**
     *  @brief Return EOF status
     *
     *  Returns true if EOF has been reached, and false if it has not been
     *  reached.
     *
     */
    bool eof();

    /**
     *  @brief Get the last error received.
     *
     *  Returns the last error received and clears error status.
     *
     *  @note clearing the error status involves returning the last
     *        error and setting eof to false.  In other words, it wipes
     *        the slate clean.
     *
     */
    errlHndl_t getLastError();

    /**
     *  @brief Set the last error received.
     *
     *  Replaces the last error with the user supplied parameter.
     *
     *  @note If an error exists, it will be deleted and replaced by this
     *        call.  For this reason, the user should normally do one
     *        of the following:
     *        1. getLastError & append then setLastError, or
     *        2. peekLastError & append, or if it's null, just setLastError
     *
     */
    void setLastError( errlHndl_t i_error );

    /**
     *  @brief Peek last error
     *
     *  Returns the last error received but does NOT clear the error status.
     *
     *  @note
     *      The returned handle should not be:
     *      1. committed
     *      2. deleted
     *
     *      under any circumstances since it belongs to the UtilStream
     *      object.
     *
     */
    errlHndl_t peekLastError() const;

    /**
     *  @brief General Insertion to cover all outstanding cases
     *
     *  Writes the data value to the stream
     *
     *  @return A reference to the stream
     *
     *  @note Errors are not reported by this interface, instead
     *        the user must call getLastError.
     *
     */
    template< class D >
    UtilStream & operator<< ( const D & i_right )
    {
        write( &i_right, sizeof(D) );
        return *this;
    }

    /**
     *  @brief General Extraction to cover all outstanding cases
     *
     *  Reads the data value from the stream
     *
     *  @return A reference to the stream
     *
     *  @note Errors are not reported by this interface, instead
     *        the user must call getLastError.
     *
     */
    template< class D >
    UtilStream & operator>> ( D & i_right )
    {
        read( &i_right, sizeof(D) );
        return *this;
    }

protected:

    /**
     * @brief   Assignment operator
     *
     * Deletes any pending errors, and copies the eof file
     * from the right hand operand.
     *
     * @return A stream reference
     *
    */
    UtilStream & operator = ( const UtilStream & i_right );


    bool                iv_eof;                 ///<Stream EOF
    mutable errlHndl_t  iv_lastError;           ///<Last Error


private:

    // Disable assignment & copy-constructor until the meaning is defined
    UtilStream( const UtilStream & i_right );


};


/*****************************************************************************/
// I n l i n e s
/*****************************************************************************/

/*****************************************************************************/
// Query End of File
/*****************************************************************************/
inline bool UtilStream::eof()
{
    return iv_eof;
}


/*****************************************************************************/
// Get the last Error Log
/*****************************************************************************/
inline errlHndl_t UtilStream::getLastError()
{
    errlHndl_t l_lastError;

    l_lastError = iv_lastError;

    // Clear the error condition
    iv_lastError = 0;
    iv_eof = false;

    return l_lastError;
}


/*****************************************************************************/
// Peek last Error Log
/*****************************************************************************/
inline errlHndl_t UtilStream::peekLastError() const
{
    return iv_lastError;
}

#endif //UTILSTREAM_H

OpenPOWER on IntegriCloud