openspades/Sources/Core/IStream.h

92 lines
2.2 KiB
C
Raw Normal View History

2013-08-29 11:45:22 +09:00
/*
Copyright (c) 2013 yvt
2013-08-29 11:45:22 +09:00
This file is part of OpenSpades.
2013-08-29 11:45:22 +09:00
OpenSpades is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
2013-08-29 11:45:22 +09:00
OpenSpades is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
2013-08-29 11:45:22 +09:00
You should have received a copy of the GNU General Public License
along with OpenSpades. If not, see <http://www.gnu.org/licenses/>.
2013-08-29 11:45:22 +09:00
*/
2013-08-18 16:18:06 +09:00
#pragma once
#include <cstdint>
#include <cstdio>
2013-08-18 16:18:06 +09:00
#include <string>
namespace spades {
/** operation mode for data-compression streams. */
enum CompressMode {
/** Compresses and writes to the base stream. */
CompressModeCompress = 0,
2013-08-18 16:18:06 +09:00
/** Decompresses the base stream. */
CompressModeDecompress
};
2013-08-18 16:18:06 +09:00
class IStream {
protected:
IStream() {}
2013-08-18 16:18:06 +09:00
public:
virtual ~IStream();
/** reads one byte and return in range [0, 255].
2013-08-18 16:18:06 +09:00
* -1 if EOF reached. */
virtual int ReadByte();
virtual size_t Read(void *, size_t bytes);
virtual std::string Read(size_t maxBytes);
2013-08-18 16:18:06 +09:00
virtual void WriteByte(int);
virtual void Write(const void *, size_t bytes);
virtual void Write(const std::string &);
2013-08-18 16:18:06 +09:00
virtual uint64_t GetPosition();
virtual void SetPosition(uint64_t);
2013-08-18 16:18:06 +09:00
virtual uint64_t GetLength();
virtual void SetLength(uint64_t);
2013-08-26 01:27:44 +09:00
virtual void Flush() {}
2013-08-18 16:18:06 +09:00
uint16_t ReadLittleShort();
uint32_t ReadLittleInt();
2013-08-18 16:18:06 +09:00
// utilities
virtual std::string ReadAllBytes();
};
/** makes management of stream lifetime easier.
2013-08-18 16:18:06 +09:00
* don't create multiple StreamHandles with the same IStream. */
class StreamHandle {
struct SharedStream {
IStream *stream;
int refCount;
SharedStream(IStream *);
~SharedStream();
void Retain();
void Release();
};
SharedStream *o;
2013-08-18 16:18:06 +09:00
public:
StreamHandle();
StreamHandle(IStream *);
StreamHandle(const StreamHandle &);
2013-08-18 16:18:06 +09:00
~StreamHandle();
spades::StreamHandle &operator=(const StreamHandle &);
2013-08-18 16:18:06 +09:00
void Reset();
IStream *operator->() const;
2013-08-18 16:18:06 +09:00
operator IStream *() const;
};
}