a5460e9d |
1 | // Copyright (c) 2016 OPEN CASCADE SAS |
2 | // |
3 | // This file is part of Open CASCADE Technology software library. |
4 | // |
5 | // This library is free software; you can redistribute it and/or modify it under |
6 | // the terms of the GNU Lesser General Public License version 2.1 as published |
7 | // by the Free Software Foundation, with special exception defined in the file |
8 | // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT |
9 | // distribution for complete text of the license and disclaimer of any warranty. |
10 | // |
11 | // Alternatively, this file may be used under the terms of Open CASCADE |
12 | // commercial license or contractual agreement. |
13 | |
14 | #ifndef _Standard_ArrayStreamBuffer_HeaderFile |
15 | #define _Standard_ArrayStreamBuffer_HeaderFile |
16 | |
17 | #include <Standard_Type.hxx> |
18 | |
19 | #include <fstream> |
20 | |
71c810df |
21 | // Suppress VC9 warning on xsputn() function |
22 | #ifdef _MSC_VER |
23 | #pragma warning(push) |
24 | #pragma warning(disable: 4996) |
25 | #endif |
26 | |
a5460e9d |
27 | //! Custom buffer object implementing STL interface std::streambuf for streamed reading from allocated memory block. |
28 | //! Implements minimal sub-set of methods for passing buffer to std::istream, including seek support. |
29 | //! |
30 | //! This class can be used for creating a seekable input stream in cases, |
31 | //! when the source data does not satisfies Reader requirements (non-seekable stream, compressed data) |
32 | //! or represents an in-memory resource. |
33 | //! |
34 | //! The memory itself is NOT managed by this class - it is up to the caller to ensure that passed memory pointer |
35 | //! is not released during Standard_ArrayStreamBuffer lifetime. |
36 | //! |
37 | //! Usage example: |
38 | //! @code |
39 | //! const char* theBuffer; |
40 | //! const size_t theBufferLength; |
41 | //! Standard_ArrayStreamBuffer aStreamBuffer (theBuffer, theBufferLength); |
42 | //! std::istream aStream (&aStreamBuffer); |
43 | //! TopoDS_Shape aShape; |
44 | //! BRep_Builder aBuilder; |
45 | //! BRepTools::Read (aShape, aStream, aBuilder); |
46 | //! @endcode |
47 | class Standard_ArrayStreamBuffer : public std::streambuf |
48 | { |
49 | public: |
50 | |
51 | //! Main constructor. |
52 | //! Passed pointer is stored as is (memory is NOT copied nor released with destructor). |
53 | //! @param theBegin pointer to the beggining of pre-allocated buffer |
54 | //! @param theSize length of pre-allocated buffer |
55 | Standard_EXPORT Standard_ArrayStreamBuffer (const char* theBegin, |
56 | const size_t theSize); |
57 | |
58 | //! Destructor. |
59 | Standard_EXPORT virtual ~Standard_ArrayStreamBuffer(); |
60 | |
61 | //! (Re)-initialize the stream. |
62 | //! Passed pointer is stored as is (memory is NOT copied nor released with destructor). |
63 | //! @param theBegin pointer to the beggining of pre-allocated buffer |
64 | //! @param theSize length of pre-allocated buffer |
65 | Standard_EXPORT virtual void Init (const char* theBegin, |
66 | const size_t theSize); |
67 | |
68 | protected: |
69 | |
70 | //! Get character on underflow. |
71 | //! Virtual function called by other member functions to get the current character |
72 | //! in the controlled input sequence without changing the current position. |
73 | Standard_EXPORT virtual int_type underflow() Standard_OVERRIDE; |
74 | |
75 | //! Get character on underflow and advance position. |
76 | //! Virtual function called by other member functions to get the current character |
77 | //! in the controlled input sequence and then advance the position indicator to the next character. |
78 | Standard_EXPORT virtual int_type uflow() Standard_OVERRIDE; |
79 | |
80 | //! Put character back in the case of backup underflow. |
81 | //! Virtual function called by other member functions to put a character back |
82 | //! into the controlled input sequence and decrease the position indicator. |
83 | Standard_EXPORT virtual int_type pbackfail (int_type ch) Standard_OVERRIDE; |
84 | |
85 | //! Get number of characters available. |
86 | //! Virtual function (to be read s-how-many-c) called by other member functions |
87 | //! to get an estimate on the number of characters available in the associated input sequence. |
88 | Standard_EXPORT virtual std::streamsize showmanyc() Standard_OVERRIDE; |
89 | |
90 | //! Seek to specified position. |
91 | Standard_EXPORT virtual pos_type seekoff (off_type theOff, |
92 | std::ios_base::seekdir theWay, |
93 | std::ios_base::openmode theWhich) Standard_OVERRIDE; |
94 | |
95 | //! Change to specified position, according to mode. |
96 | Standard_EXPORT virtual pos_type seekpos (pos_type thePosition, |
97 | std::ios_base::openmode theWhich) Standard_OVERRIDE; |
98 | |
99 | public: |
100 | |
101 | //! Read a bunch of bytes at once. |
102 | Standard_EXPORT virtual std::streamsize xsgetn (char* thePtr, |
103 | std::streamsize theCount) Standard_OVERRIDE; |
104 | |
105 | private: |
106 | |
107 | // copying is not allowed |
108 | Standard_ArrayStreamBuffer (const Standard_ArrayStreamBuffer& ); |
109 | Standard_ArrayStreamBuffer& operator= (const Standard_ArrayStreamBuffer& ); |
110 | |
111 | protected: |
112 | |
113 | const char* myBegin; |
114 | const char* myEnd; |
115 | const char* myCurrent; |
116 | |
117 | }; |
118 | |
71c810df |
119 | #ifdef _MSC_VER |
120 | #pragma warning(pop) |
121 | #endif |
122 | |
a5460e9d |
123 | #endif // _Standard_ArrayStreamBuffer_HeaderFile |