Mitsuba Renderer  0.5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
mstream.h
Go to the documentation of this file.
1 /*
2  This file is part of Mitsuba, a physically based rendering system.
3 
4  Copyright (c) 2007-2014 by Wenzel Jakob and others.
5 
6  Mitsuba is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License Version 3
8  as published by the Free Software Foundation.
9 
10  Mitsuba is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #pragma once
20 #if !defined(__MITSUBA_CORE_MSTREAM_H_)
21 #define __MITSUBA_CORE_MSTREAM_H_
22 
23 #include <mitsuba/mitsuba.h>
24 
26 
27 /** \brief Simple memory buffer-based stream with automatic memory management
28  *
29  * The underlying memory storage of this implementation dynamically expands
30  * as data is written to the stream.
31  *
32  * \ingroup libcore
33  * \ingroup libpython
34  */
36 public:
37  // =============================================================
38  //! @{ \name Constructors
39  // =============================================================
40 
41  /// Create a new memory stream
42  MemoryStream(size_t initialSize = 512);
43 
44  /**
45  * \brief Create a memory stream, which operates on a
46  * pre-allocated buffer.
47  *
48  * A memory stream created in this way will never resize the
49  * underlying buffer. An exception is thrown e.g. when attempting
50  * to extend its size
51  *
52  * \remark This constructor is not available in the python bindings
53  */
54  MemoryStream(void *ptr, size_t size);
55 
56  //! @}
57  // =============================================================
58 
59  // =============================================================
60  //! @{ \name Memory stream-specific features
61  // =============================================================
62 
63  /// Return the underlying data
64  inline uint8_t *getData() { return m_data; }
65 
66  /// Return the underlying data (const version)
67  inline const uint8_t *getData() const { return m_data; }
68 
69  /// Return the underlying data at the current position
70  inline uint8_t *getCurrentData() { return m_data + m_pos; }
71 
72  /// Return the underlying data at the current position (const version)
73  inline const uint8_t *getCurrentData() const { return m_data + m_pos; }
74 
75  /// Set size and position to zero without changing the underlying buffer
76  void reset();
77 
78  //! @}
79  // =============================================================
80 
81  // =============================================================
82  //! @{ \name Implementation of the Stream interface
83  // =============================================================
84 
85  void read(void *ptr, size_t size);
86  void write(const void *ptr, size_t size);
87  void seek(size_t pos);
88  size_t getPos() const;
89  size_t getSize() const;
90  void truncate(size_t size);
91  void flush();
92  bool canWrite() const;
93  bool canRead() const;
94 
95  //! @}
96  // =============================================================
97 
98  /// Return a string representation
99  std::string toString() const;
100 
102 protected:
103  void resize(size_t newSize);
104 
105  // \brief Virtual destructor
106  virtual ~MemoryStream();
107 protected:
108  size_t m_capacity;
109  size_t m_size;
110  size_t m_pos;
111  bool m_ownsBuffer;
112  uint8_t *m_data;
113 };
114 
116 
117 #endif /* __MITSUBA_CORE_MSTREAM_H_ */
const uint8_t * getCurrentData() const
Return the underlying data at the current position (const version)
Definition: mstream.h:73
const uint8_t * getData() const
Return the underlying data (const version)
Definition: mstream.h:67
virtual void flush()=0
Flush the stream&#39;s buffers.
uint8_t * getData()
Return the underlying data.
Definition: mstream.h:64
virtual bool canRead() const =0
Can we read from the stream?
#define MTS_EXPORT_CORE
Definition: getopt.h:29
virtual bool canWrite() const =0
Can we write to the stream?
#define MTS_NAMESPACE_BEGIN
Definition: platform.h:137
virtual void read(void *ptr, size_t size)=0
Read a specified amount of data from the stream.
uint8_t * getCurrentData()
Return the underlying data at the current position.
Definition: mstream.h:70
Abstract seekable stream class.
Definition: stream.h:58
#define MTS_DECLARE_CLASS()
This macro must be used in the initial definition in classes that derive from Object.
Definition: class.h:158
Simple memory buffer-based stream with automatic memory management.
Definition: mstream.h:35
virtual void write(const void *ptr, size_t size)=0
Write a specified amount of data into the stream.
virtual size_t getPos() const =0
Get the current position inside the stream.
#define MTS_NAMESPACE_END
Definition: platform.h:138
virtual std::string toString() const
Return a string representation.
virtual void seek(size_t pos)=0
Seek to a position inside the stream.
virtual size_t getSize() const =0
Return the size of the stream.
virtual void truncate(size_t size)=0
Truncate the stream to a given size.