athena/include/athena/MemoryWriter.hpp

177 lines
5.6 KiB
C++
Raw Normal View History

2015-03-01 12:42:39 -08:00
#ifndef MEMORYWRITER_HPP
#define MEMORYWRITER_HPP
2014-04-20 02:14:15 -07:00
#include <string>
#include <memory>
2014-04-20 02:14:15 -07:00
#include <functional>
2016-03-04 15:00:12 -08:00
#include "athena/IStreamWriter.hpp"
2014-04-20 02:14:15 -07:00
2016-03-04 15:00:12 -08:00
namespace athena
2014-04-20 02:14:15 -07:00
{
namespace io
{
/*! @class MemoryWriter
* @brief A Stream class for writing data to a memory position
2014-04-20 02:14:15 -07:00
*
* A Class for writing binary data to a file or memory stream,
* all work is done using a memory buffer, and not written directly to the disk
* this allows for fast, flexible code as well as the ability to quickly modify data
* @sa Stream
2014-04-20 02:14:15 -07:00
*/
class MemoryWriter : public IStreamWriter
2014-04-20 02:14:15 -07:00
{
public:
2016-03-19 21:08:23 -07:00
virtual ~MemoryWriter();
/*! @brief This constructor references an existing buffer to write to in-place.
2014-04-20 02:14:15 -07:00
*
* @param data The existing buffer
* @param length The length of the existing buffer
2014-04-20 02:14:15 -07:00
*/
2016-03-19 21:08:23 -07:00
explicit MemoryWriter(atUint8* data, atUint64 length, bool takeOwnership = false);
2014-04-20 02:14:15 -07:00
/*! @brief Sets the buffers position relative to the specified position.<br />
2014-04-20 02:14:15 -07:00
* It seeks relative to the current position by default.
* @param position where in the buffer to seek
* @param origin The Origin to seek @sa SeekOrigin
2014-04-20 02:14:15 -07:00
*/
void seek(atInt64 pos, SeekOrigin origin = SeekOrigin::Current);
2015-05-18 20:24:56 -07:00
2014-04-20 02:14:15 -07:00
/*! @brief Returns the current position in the stream.
2014-04-20 02:14:15 -07:00
*
* @return Int64 The current position in the stream.
2014-04-20 02:14:15 -07:00
*/
2015-07-01 02:28:40 -07:00
inline atUint64 position() const
{return m_position;}
2014-04-20 02:14:15 -07:00
/*! @brief Returns whether or not the stream is at the end.
2014-04-20 02:14:15 -07:00
*
* @return bool True if at end; False otherwise.
2014-04-20 02:14:15 -07:00
*/
2015-07-01 02:28:40 -07:00
inline atUint64 length() const
{return m_length;}
2014-04-20 02:14:15 -07:00
inline bool isOpen() const {return true;}
/** @brief Sets the buffer to the given one, deleting the current one if it owns it.<br />
* @param data The new buffer.
* @param length The length of the new buffer.
* @param takeOwnership Whether the Stream now owns the buffer.
2014-04-20 02:14:15 -07:00
*/
2016-03-19 21:08:23 -07:00
void setData(atUint8* data, atUint64 length, bool takeOwnership = false);
2014-04-20 02:14:15 -07:00
/*! @brief Returns a copy of the current buffer.<br />
2014-04-20 02:14:15 -07:00
* Changes to the copy do not affect the buffer so it's perfectly safe to
* directly edit the buffer and use setData to set the new information.<br />
* @return Uint8* The copy of the buffer.
2014-04-20 02:14:15 -07:00
*/
atUint8* data() const;
2014-04-20 02:14:15 -07:00
/*! @brief Sets the target file
2014-04-20 02:14:15 -07:00
*
* @param filepath The path to write to.
2014-04-20 02:14:15 -07:00
*/
2015-07-01 02:28:40 -07:00
inline void setFilepath(const std::string& filepath)
{m_filepath = filepath;}
2014-04-20 02:14:15 -07:00
/*! @brief
2014-04-20 02:14:15 -07:00
* Returns the target file
*/
2015-07-01 02:28:40 -07:00
inline std::string filepath() const
{return m_filepath;}
2014-04-20 02:14:15 -07:00
/*! @brief Saves the file to the specified file.
2014-04-20 02:14:15 -07:00
*
* @param filename If not empty, the filename to save to
2014-04-20 02:14:15 -07:00
*/
2015-05-18 20:24:56 -07:00
void save(const std::string& filename = "");
2014-04-20 02:14:15 -07:00
/*! @brief Writes the given buffer with the specified length, buffers can be bigger than the length
2014-04-20 02:14:15 -07:00
* however it's undefined behavior to try and write a buffer which is smaller than the given length.
* If you are needing to fill in an area please use IStreamWriter::fill(atUint64) instead.
2014-04-20 02:14:15 -07:00
*
* @param data The buffer to write
* @param length The amount to write
2014-04-20 02:14:15 -07:00
*/
2015-06-30 20:01:04 -07:00
void writeUBytes(const atUint8* data, atUint64 len);
2014-04-20 02:14:15 -07:00
protected:
2015-07-07 23:34:59 -07:00
MemoryWriter() {}
atUint8* m_data;
atUint64 m_length;
atUint64 m_position;
2016-03-19 21:08:23 -07:00
bool m_bufferOwned;
std::string m_filepath; //!< Path to the target file
2014-04-20 02:14:15 -07:00
};
class MemoryCopyWriter : public MemoryWriter
{
public:
/*! @brief This constructor copies an existing buffer to write to.
*
* @param data The existing buffer
* @param length The length of the existing buffer
*/
2015-07-07 23:34:59 -07:00
explicit MemoryCopyWriter(atUint8* data=nullptr, atUint64 length=0x10);
/*! @brief This constructor creates an instance from a file on disk.
*
* @param filename The file to create the stream from
*/
MemoryCopyWriter(const std::string& filename);
/*! @brief Sets the buffers position relative to the specified position.<br />
2015-07-07 23:34:59 -07:00
* It seeks relative to the current position by default.
* @param position where in the buffer to seek
* @param origin The Origin to seek @sa SeekOrigin
2015-07-07 23:34:59 -07:00
*/
void seek(atInt64 pos, SeekOrigin origin = SeekOrigin::Current);
/*! @brief Sets the buffer to the given one, deleting the current one.<br />
2015-07-07 23:34:59 -07:00
* <b>BEWARE:</b> As this deletes the current buffer it WILL cause a loss of data
* if that was not the intent.<br />
* Once you pass the data to setData <b>DO NOT</b> delete the buffer
* as Stream now owns the address, this is done to keep memory usage down.
* @param data The new buffer.
* @param length The length of the new buffer.
* @throw IOException
2015-07-07 23:34:59 -07:00
*/
void setData(const atUint8* data, atUint64 length);
/*! @brief Writes the given buffer with the specified length, buffers can be bigger than the length
2015-07-07 23:34:59 -07:00
* however it's undefined behavior to try and write a buffer which is smaller than the given length.
*
* @param data The buffer to write
* @param length The amount to write
2015-07-07 23:34:59 -07:00
*/
void writeUBytes(const atUint8* data, atUint64 len);
protected:
std::unique_ptr<atUint8[]> m_dataCopy;
2015-07-07 23:34:59 -07:00
private:
void resize(atUint64 newSize);
};
2014-04-20 02:14:15 -07:00
}
}
2015-03-01 12:42:39 -08:00
#ifndef MEMORYWRITER_BASE
#define MEMORYWRITER_BASE() \
2014-04-20 02:14:15 -07:00
private: \
2016-03-04 15:00:12 -08:00
typedef athena::io::MemoryWriter base
2014-04-20 02:14:15 -07:00
#endif // BINARYWRITER_BASE
#ifndef MEMORYCOPYWRITER_BASE
#define MEMORYCOPYWRITER_BASE() \
private: \
2016-03-04 15:00:12 -08:00
typedef athena::io::MemoryCopyWriter base
#endif // BINARYWRITER_BASE
2015-03-01 12:42:39 -08:00
#endif // MEMORYWRITER_HPP