]> git.cworth.org Git - vogl/blob - src/voglcore/vogl_dynamic_stream.h
Initial vogl checkin
[vogl] / src / voglcore / vogl_dynamic_stream.h
1 /**************************************************************************
2  *
3  * Copyright 2013-2014 RAD Game Tools and Valve Software
4  * Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC
5  * All Rights Reserved.
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy
8  * of this software and associated documentation files (the "Software"), to deal
9  * in the Software without restriction, including without limitation the rights
10  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies of the Software, and to permit persons to whom the Software is
12  * furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23  * THE SOFTWARE.
24  *
25  **************************************************************************/
26
27 // File: vogl_dynamic_stream.h
28 #pragma once
29
30 #include "vogl_core.h"
31 #include "vogl_data_stream.h"
32
33 namespace vogl
34 {
35     class dynamic_stream : public data_stream
36     {
37     public:
38         dynamic_stream(uint initial_size, const char *pName = "dynamic_stream", uint attribs = cDataStreamSeekable | cDataStreamWritable | cDataStreamReadable)
39             : data_stream(pName, attribs),
40               m_ofs(0)
41         {
42             open(initial_size, pName, attribs);
43         }
44
45         dynamic_stream(const void *pBuf, uint size, const char *pName = "dynamic_stream", uint attribs = cDataStreamSeekable | cDataStreamWritable | cDataStreamReadable)
46             : data_stream(pName, attribs),
47               m_ofs(0)
48         {
49             open(pBuf, size, pName, attribs);
50         }
51
52         dynamic_stream()
53             : data_stream(),
54               m_ofs(0)
55         {
56             open();
57         }
58
59         virtual ~dynamic_stream()
60         {
61         }
62
63         bool open(uint initial_size = 0, const char *pName = "dynamic_stream", uint attribs = cDataStreamSeekable | cDataStreamWritable | cDataStreamReadable)
64         {
65             close();
66
67             m_opened = true;
68             //m_buf.clear();
69             m_buf.resize(initial_size);
70             m_ofs = 0;
71             m_name.set(pName ? pName : "dynamic_stream");
72             m_attribs = static_cast<attribs_t>(attribs);
73             return true;
74         }
75
76         bool reopen(const char *pName, uint attribs)
77         {
78             if (!m_opened)
79             {
80                 return open(0, pName, attribs);
81             }
82
83             m_name.set(pName ? pName : "dynamic_stream");
84             m_attribs = static_cast<attribs_t>(attribs);
85             return true;
86         }
87
88         bool open(const void *pBuf, uint size, const char *pName = "dynamic_stream", uint attribs = cDataStreamSeekable | cDataStreamWritable | cDataStreamReadable)
89         {
90             if (!m_opened)
91             {
92                 m_opened = true;
93                 m_buf.resize(size);
94                 if (size)
95                 {
96                     VOGL_ASSERT(pBuf);
97                     memcpy(&m_buf[0], pBuf, size);
98                 }
99                 m_ofs = 0;
100                 m_name.set(pName ? pName : "dynamic_stream");
101                 m_attribs = static_cast<attribs_t>(attribs);
102                 return true;
103             }
104
105             return false;
106         }
107
108         virtual bool close()
109         {
110             if (m_opened)
111             {
112                 m_opened = false;
113                 m_buf.clear();
114                 m_ofs = 0;
115                 return true;
116             }
117
118             return false;
119         }
120
121         bool reset()
122         {
123             if (m_opened)
124             {
125                 m_opened = false;
126                 m_buf.resize(0);
127                 m_ofs = 0;
128                 return true;
129             }
130
131             return false;
132         }
133
134         const vogl::vector<uint8> &get_buf() const
135         {
136             return m_buf;
137         }
138         vogl::vector<uint8> &get_buf()
139         {
140             return m_buf;
141         }
142
143         void reserve(uint size)
144         {
145             if (m_opened)
146             {
147                 m_buf.reserve(size);
148             }
149         }
150
151         virtual const void *get_ptr() const
152         {
153             return m_buf.is_empty() ? NULL : &m_buf[0];
154         }
155
156         virtual uint read(void *pBuf, uint len)
157         {
158             VOGL_ASSERT(len <= 0x7FFFFFFF);
159
160             if ((!m_opened) || (!is_readable()) || (!len))
161                 return 0;
162
163             VOGL_ASSERT(m_ofs <= m_buf.size());
164
165             uint bytes_left = m_buf.size() - m_ofs;
166
167             len = math::minimum<uint>(len, bytes_left);
168
169             if (len)
170                 memcpy(pBuf, &m_buf[m_ofs], len);
171
172             m_ofs += len;
173
174             return len;
175         }
176
177         virtual uint write(const void *pBuf, uint len)
178         {
179             VOGL_ASSERT(len <= 0x7FFFFFFF);
180
181             if ((!m_opened) || (!is_writable()) || (!len))
182                 return 0;
183
184             VOGL_ASSERT(m_ofs <= m_buf.size());
185
186             uint new_ofs = m_ofs + len;
187             if (new_ofs > m_buf.size())
188                 m_buf.resize(new_ofs);
189
190             memcpy(&m_buf[m_ofs], pBuf, len);
191             m_ofs = new_ofs;
192
193             return len;
194         }
195
196         virtual bool flush()
197         {
198             if (!m_opened)
199                 return false;
200
201             return true;
202         }
203
204         virtual uint64_t get_size() const
205         {
206             if (!m_opened)
207                 return 0;
208
209             return m_buf.size();
210         }
211
212         virtual uint64_t get_remaining() const
213         {
214             if (!m_opened)
215                 return 0;
216
217             VOGL_ASSERT(m_ofs <= m_buf.size());
218
219             return m_buf.size() - m_ofs;
220         }
221
222         virtual uint64_t get_ofs() const
223         {
224             if (!m_opened)
225                 return 0;
226
227             return m_ofs;
228         }
229
230         virtual bool seek(int64_t ofs, bool relative)
231         {
232             if ((!m_opened) || (!is_seekable()))
233                 return false;
234
235             int64_t new_ofs = relative ? (m_ofs + ofs) : ofs;
236
237             if (new_ofs < 0)
238                 return false;
239             else if (new_ofs > m_buf.size())
240                 return false;
241
242             m_ofs = static_cast<uint>(new_ofs);
243
244             post_seek();
245
246             return true;
247         }
248
249     private:
250         vogl::vector<uint8> m_buf;
251         uint m_ofs;
252     };
253
254 } // namespace vogl