[openjk] 63/130: Split ISavedGame into two classes

Simon McVittie smcv at debian.org
Fri Oct 28 11:09:19 UTC 2016


This is an automated email from the git hooks/post-receive script.

smcv pushed a commit to branch debian/master
in repository openjk.

commit 149fcb96272fc9d4a17373dcca0761014688f7cf
Author: bibendovsky <bibendovsky at hotmail.com>
Date:   Fri Aug 5 20:07:35 2016 +0300

    Split ISavedGame into two classes
---
 code/CMakeLists.txt                                |   9 +-
 code/game/CMakeLists.txt                           |   7 +-
 code/rd-vanilla/CMakeLists.txt                     |   7 +-
 codeJK2/game/CMakeLists.txt                        |   7 +-
 shared/qcommon/ojk_i_saved_game_stream.h           | 103 ++++++++++++++
 ...{ojk_saved_game.cpp => ojk_saved_game_file.cpp} |  86 ++++++------
 .../{ojk_saved_game.h => ojk_saved_game_file.h}    |  61 ++++-----
 ...saved_game.h => ojk_saved_game_stream_helper.h} | 130 +++++++++---------
 ...me_fwd.h => ojk_saved_game_stream_helper_fwd.h} | 149 ++++-----------------
 9 files changed, 287 insertions(+), 272 deletions(-)

diff --git a/code/CMakeLists.txt b/code/CMakeLists.txt
index 571aba2..db03e34 100644
--- a/code/CMakeLists.txt
+++ b/code/CMakeLists.txt
@@ -176,12 +176,13 @@ if(BuildSPEngine OR BuildJK2SPEngine)
 
 		"${SharedDir}/qcommon/ojk_exception.h"
 		"${SharedDir}/qcommon/ojk_exception.cpp"
-		"${SharedDir}/qcommon/ojk_i_saved_game.h"
-		"${SharedDir}/qcommon/ojk_i_saved_game_fwd.h"
-		"${SharedDir}/qcommon/ojk_saved_game.h"
-		"${SharedDir}/qcommon/ojk_saved_game.cpp"
 		"${SharedDir}/qcommon/ojk_saved_game_exception.h"
 		"${SharedDir}/qcommon/ojk_saved_game_exception.cpp"
+		"${SharedDir}/qcommon/ojk_i_saved_game_stream.h"
+		"${SharedDir}/qcommon/ojk_saved_game_file.h"
+		"${SharedDir}/qcommon/ojk_saved_game_file.cpp"
+		"${SharedDir}/qcommon/ojk_saved_game_stream_helper.h"
+		"${SharedDir}/qcommon/ojk_saved_game_stream_helper_fwd.h"
 		"${SharedDir}/qcommon/ojk_scope_guard.h"
 
 		${SharedCommonFiles}
diff --git a/code/game/CMakeLists.txt b/code/game/CMakeLists.txt
index 9438679..df94c7e 100644
--- a/code/game/CMakeLists.txt
+++ b/code/game/CMakeLists.txt
@@ -261,8 +261,11 @@ set(SPGameCommonFiles
 	"${SPDir}/qcommon/q_shared.cpp"
 	"${SPDir}/qcommon/q_shared.h"
 	"${SPDir}/rd-common/mdx_format.h"
-	"${SharedDir}/qcommon/ojk_i_saved_game.h"
-	"${SharedDir}/qcommon/ojk_i_saved_game_fwd.h"
+	"${SharedDir}/qcommon/ojk_i_saved_game_stream.h"
+	"${SharedDir}/qcommon/ojk_saved_game_file.h"
+	"${SharedDir}/qcommon/ojk_saved_game_file.cpp"
+	"${SharedDir}/qcommon/ojk_saved_game_stream_helper.h"
+	"${SharedDir}/qcommon/ojk_saved_game_stream_helper_fwd.h"
 	"${SharedDir}/qcommon/ojk_scope_guard.h"
 	${SharedCommonFiles}
 	)
diff --git a/code/rd-vanilla/CMakeLists.txt b/code/rd-vanilla/CMakeLists.txt
index 5172eb9..dd4e50b 100644
--- a/code/rd-vanilla/CMakeLists.txt
+++ b/code/rd-vanilla/CMakeLists.txt
@@ -78,8 +78,11 @@ if(BuildSPRdVanilla OR BuildJK2SPRdVanilla)
 		"${SPDir}/qcommon/q_math.cpp"
 		"${SPDir}/qcommon/q_shared.cpp"
 		"${SPDir}/qcommon/q_shared.h"
-		"${SharedDir}/qcommon/ojk_i_saved_game.h"
-		"${SharedDir}/qcommon/ojk_i_saved_game_fwd.h"
+		"${SharedDir}/qcommon/ojk_i_saved_game_stream.h"
+		"${SharedDir}/qcommon/ojk_saved_game_file.h"
+		"${SharedDir}/qcommon/ojk_saved_game_file.cpp"
+		"${SharedDir}/qcommon/ojk_saved_game_stream_helper.h"
+		"${SharedDir}/qcommon/ojk_saved_game_stream_helper_fwd.h"
 		"${SharedDir}/qcommon/ojk_scope_guard.h"
 		${SharedCommonFiles}
 		)
diff --git a/codeJK2/game/CMakeLists.txt b/codeJK2/game/CMakeLists.txt
index 06faddc..4ddfdaf 100644
--- a/codeJK2/game/CMakeLists.txt
+++ b/codeJK2/game/CMakeLists.txt
@@ -233,8 +233,11 @@ set(JK2SPGameCommonFiles
 	"${SPDir}/qcommon/q_shared.h"
 	"${SPDir}/qcommon/strippublic.h"
 	"${SPDir}/rd-common/mdx_format.h"
-	"${SharedDir}/qcommon/ojk_i_saved_game.h"
-	"${SharedDir}/qcommon/ojk_i_saved_game_fwd.h"
+	"${SharedDir}/qcommon/ojk_i_saved_game_stream.h"
+	"${SharedDir}/qcommon/ojk_saved_game_file.h"
+	"${SharedDir}/qcommon/ojk_saved_game_file.cpp"
+	"${SharedDir}/qcommon/ojk_saved_game_stream_helper.h"
+	"${SharedDir}/qcommon/ojk_saved_game_stream_helper_fwd.h"
 	"${SharedDir}/qcommon/ojk_scope_guard.h"
 	${SharedCommonFiles}
 	)
diff --git a/shared/qcommon/ojk_i_saved_game_stream.h b/shared/qcommon/ojk_i_saved_game_stream.h
new file mode 100644
index 0000000..4c6a6bf
--- /dev/null
+++ b/shared/qcommon/ojk_i_saved_game_stream.h
@@ -0,0 +1,103 @@
+//
+// Saved game stream interface.
+//
+
+
+#ifndef OJK_I_SAVED_GAME_STREAM_INCLUDED
+#define OJK_I_SAVED_GAME_STREAM_INCLUDED
+
+
+#include <cstdint>
+
+
+namespace ojk
+{
+
+
+class ISavedGameStream
+{
+public:
+	ISavedGameStream()
+	{
+	}
+
+	ISavedGameStream(
+		const ISavedGameStream& that) = delete;
+
+	ISavedGameStream& operator=(
+		const ISavedGameStream& that) = delete;
+
+	virtual ~ISavedGameStream()
+	{
+	}
+
+
+	// Returns true if the saved game opened for reading.
+	virtual bool is_readable() const = 0;
+
+	// Returns true if the saved game opened for writing.
+	virtual bool is_writable() const = 0;
+
+
+	// Reads a chunk from the file into the internal buffer.
+	virtual void read_chunk(
+		const uint32_t chunk_id) = 0;
+
+	// Returns true if all data read from the internal buffer.
+	virtual bool is_all_data_read() const = 0;
+
+	// Throws an exception if all data not read.
+	virtual void ensure_all_data_read() const = 0;
+
+
+	// Writes a chunk into the file from the internal buffer.
+	virtual void write_chunk(
+		const uint32_t chunk_id) = 0;
+
+
+	// Reads a raw data from the internal buffer.
+	virtual void read(
+		void* dst_data,
+		int dst_size) = 0;
+
+	// Writes a raw data into the internal buffer.
+	virtual void write(
+		const void* src_data,
+		int src_size) = 0;
+
+	// Increments buffer's offset by the specified non-negative count.
+	virtual void skip(
+		int count) = 0;
+
+
+	// Stores current I/O buffer and it's position.
+	virtual void save_buffer() = 0;
+
+	// Restores saved I/O buffer and it's position.
+	virtual void load_buffer() = 0;
+
+	// Returns a pointer to data in the I/O buffer.
+	virtual const void* get_buffer_data() const = 0;
+
+	// Returns a current size of the I/O buffer.
+	virtual int get_buffer_size() const = 0;
+
+
+	// Clears buffer and resets it's offset to the beginning.
+	virtual void reset_buffer() = 0;
+
+	// Resets buffer offset to the beginning.
+	virtual void reset_buffer_offset() = 0;
+
+
+	// If true won't throw an exception when buffer offset is beyond it's size.
+	// Although, no data will be read beyond the buffer.
+	virtual void allow_read_overflow(
+		bool value) = 0;
+}; // ISavedGameStream
+
+
+} // ojk
+
+
+#endif // OJK_I_SAVED_GAME_STREAM_INCLUDED
diff --git a/shared/qcommon/ojk_saved_game.cpp b/shared/qcommon/ojk_saved_game_file.cpp
similarity index 89%
rename from shared/qcommon/ojk_saved_game.cpp
rename to shared/qcommon/ojk_saved_game_file.cpp
index a5a9748..bcee5f7 100644
--- a/shared/qcommon/ojk_saved_game.cpp
+++ b/shared/qcommon/ojk_saved_game_file.cpp
@@ -1,4 +1,10 @@
-#include "ojk_saved_game.h"
+//
+// Saved game file.
+//
+
+
+#include "ojk_saved_game_file.h"
+#include "ojk_saved_game_stream_helper.h"
 #include <algorithm>
 #include "ojk_saved_game_exception.h"
 #include "qcommon/qcommon.h"
@@ -9,7 +15,7 @@ namespace ojk
 {
 
 
-SavedGame::SavedGame() :
+SavedGameFile::SavedGameFile() :
 		file_handle_(),
 		io_buffer_(),
 		saved_io_buffer_(),
@@ -23,12 +29,12 @@ SavedGame::SavedGame() :
 {
 }
 
-SavedGame::~SavedGame()
+SavedGameFile::~SavedGameFile()
 {
 	close();
 }
 
-bool SavedGame::open(
+bool SavedGameFile::open(
 	const std::string& base_file_name)
 {
 	close();
@@ -66,7 +72,9 @@ bool SavedGame::open(
 
 	if (is_succeed)
 	{
-		static_cast<void>(read_chunk<int32_t>(
+		SavedGameStreamHelper sgsh(this);
+
+		static_cast<void>(sgsh.read_chunk<int32_t>(
 			INT_ID('_', 'V', 'E', 'R'),
 			sg_version));
 
@@ -90,7 +98,7 @@ bool SavedGame::open(
 	return is_succeed;
 }
 
-bool SavedGame::create(
+bool SavedGameFile::create(
 	const std::string& base_file_name)
 {
 	close();
@@ -123,14 +131,16 @@ bool SavedGame::create(
 
 	const auto sg_version = iSAVEGAME_VERSION;
 
-	write_chunk<int32_t>(
+	SavedGameStreamHelper sgsh(this);
+
+	sgsh.write_chunk<int32_t>(
 		INT_ID('_', 'V', 'E', 'R'),
 		sg_version);
 
 	return true;
 }
 
-void SavedGame::close()
+void SavedGameFile::close()
 {
 	if (file_handle_ != 0)
 	{
@@ -146,18 +156,18 @@ void SavedGame::close()
 	is_write_failed_ = false;
 }
 
-bool SavedGame::is_readable() const
+bool SavedGameFile::is_readable() const
 {
 	return is_readable_;
 }
 
-bool SavedGame::is_writable() const
+bool SavedGameFile::is_writable() const
 {
 	return is_writable_;
 }
 
-void SavedGame::read_chunk(
-	const SavedGame::ChunkId chunk_id)
+void SavedGameFile::read_chunk(
+	const uint32_t chunk_id)
 {
 	io_buffer_offset_ = 0;
 
@@ -316,12 +326,12 @@ void SavedGame::read_chunk(
 	}
 }
 
-bool SavedGame::is_all_data_read() const
+bool SavedGameFile::is_all_data_read() const
 {
 	return io_buffer_.size() == io_buffer_offset_;
 }
 
-void SavedGame::ensure_all_data_read() const
+void SavedGameFile::ensure_all_data_read() const
 {
 	if (!is_all_data_read())
 	{
@@ -330,8 +340,8 @@ void SavedGame::ensure_all_data_read() const
 	}
 }
 
-void SavedGame::write_chunk(
-	const SavedGame::ChunkId chunk_id)
+void SavedGameFile::write_chunk(
+	const uint32_t chunk_id)
 {
 	const auto&& chunk_id_string = get_chunk_id_string(
 		chunk_id);
@@ -490,7 +500,7 @@ void SavedGame::write_chunk(
 	}
 }
 
-void SavedGame::raw_read(
+void SavedGameFile::read(
 	void* dst_data,
 	int dst_size)
 {
@@ -539,7 +549,7 @@ void SavedGame::raw_read(
 	io_buffer_offset_ += dst_size;
 }
 
-void SavedGame::raw_write(
+void SavedGameFile::write(
 	const void* src_data,
 	int src_size)
 {
@@ -579,12 +589,12 @@ void SavedGame::raw_write(
 	io_buffer_offset_ = new_buffer_size;
 }
 
-bool SavedGame::is_write_failed() const
+bool SavedGameFile::is_write_failed() const
 {
 	return is_write_failed_;
 }
 
-void SavedGame::skip(
+void SavedGameFile::skip(
 	int count)
 {
 	if (!is_readable_ && !is_writable_)
@@ -627,29 +637,29 @@ void SavedGame::skip(
 	io_buffer_offset_ = new_offset;
 }
 
-void SavedGame::save_buffer()
+void SavedGameFile::save_buffer()
 {
 	saved_io_buffer_ = io_buffer_;
 	saved_io_buffer_offset_ = io_buffer_offset_;
 }
 
-void SavedGame::load_buffer()
+void SavedGameFile::load_buffer()
 {
 	io_buffer_ = saved_io_buffer_;
 	io_buffer_offset_ = saved_io_buffer_offset_;
 }
 
-const void* SavedGame::get_buffer_data() const
+const void* SavedGameFile::get_buffer_data() const
 {
 	return io_buffer_.data();
 }
 
-int SavedGame::get_buffer_size() const
+int SavedGameFile::get_buffer_size() const
 {
 	return static_cast<int>(io_buffer_.size());
 }
 
-void SavedGame::rename(
+void SavedGameFile::rename(
 	const std::string& old_base_file_name,
 	const std::string& new_base_file_name)
 {
@@ -672,7 +682,7 @@ void SavedGame::rename(
 	}
 }
 
-void SavedGame::remove(
+void SavedGameFile::remove(
 	const std::string& base_file_name)
 {
 	const auto&& path = generate_path(
@@ -682,33 +692,33 @@ void SavedGame::remove(
 		path.c_str());
 }
 
-SavedGame& SavedGame::get_instance()
+SavedGameFile& SavedGameFile::get_instance()
 {
-	static SavedGame result;
+	static SavedGameFile result;
 	return result;
 }
 
-void SavedGame::allow_read_overflow(
+void SavedGameFile::allow_read_overflow(
 	bool value)
 {
 	is_read_overflow_allowed_ = value;
 }
 
-void SavedGame::throw_error(
+void SavedGameFile::throw_error(
 	const char* message)
 {
 	throw SavedGameException(
 		message);
 }
 
-void SavedGame::throw_error(
+void SavedGameFile::throw_error(
 	const std::string& message)
 {
 	throw SavedGameException(
 		message);
 }
 
-void SavedGame::compress(
+void SavedGameFile::compress(
 	const Buffer& src_buffer,
 	Buffer& dst_buffer)
 {
@@ -771,7 +781,7 @@ void SavedGame::compress(
 		dst_index);
 }
 
-void SavedGame::decompress(
+void SavedGameFile::decompress(
 	const Buffer& src_buffer,
 	Buffer& dst_buffer)
 {
@@ -811,7 +821,7 @@ void SavedGame::decompress(
 	}
 }
 
-std::string SavedGame::generate_path(
+std::string SavedGameFile::generate_path(
 	const std::string& base_file_name)
 {
 	auto normalized_file_name = base_file_name;
@@ -827,7 +837,7 @@ std::string SavedGame::generate_path(
 	return path;
 }
 
-std::string SavedGame::get_chunk_id_string(
+std::string SavedGameFile::get_chunk_id_string(
 	uint32_t chunk_id)
 {
 	std::string result(4, '\0');
@@ -840,18 +850,18 @@ std::string SavedGame::get_chunk_id_string(
 	return result;
 }
 
-void SavedGame::reset_buffer()
+void SavedGameFile::reset_buffer()
 {
 	io_buffer_.clear();
 	reset_buffer_offset();
 }
 
-void SavedGame::reset_buffer_offset()
+void SavedGameFile::reset_buffer_offset()
 {
 	io_buffer_offset_ = 0;
 }
 
-constexpr uint32_t SavedGame::get_jo_magic_value()
+constexpr uint32_t SavedGameFile::get_jo_magic_value()
 {
 	return 0x1234ABCD;
 }
diff --git a/shared/qcommon/ojk_saved_game.h b/shared/qcommon/ojk_saved_game_file.h
similarity index 84%
rename from shared/qcommon/ojk_saved_game.h
rename to shared/qcommon/ojk_saved_game_file.h
index 6e874a4..1ffa06c 100644
--- a/shared/qcommon/ojk_saved_game.h
+++ b/shared/qcommon/ojk_saved_game_file.h
@@ -1,39 +1,36 @@
 //
-// Saved game.
+// Saved game file.
 // (forward declaration)
 //
 
 
-#ifndef OJK_SAVED_GAME_FWD_INCLUDED
-#define OJK_SAVED_GAME_FWD_INCLUDED
+#ifndef OJK_SAVED_GAME_FILE_INCLUDED
+#define OJK_SAVED_GAME_FILE_INCLUDED
 
 
 #include <cstdint>
 #include <string>
 #include <vector>
-#include "ojk_i_saved_game.h"
+#include "ojk_i_saved_game_stream.h"
 
 
 namespace ojk
 {
 
 
-class SavedGame :
-	public ISavedGame
+class SavedGameFile :
+	public ISavedGameStream
 {
 public:
-	using ChunkId = uint32_t;
+	SavedGameFile();
 
+	SavedGameFile(
+		const SavedGameFile& that) = delete;
 
-	SavedGame();
+	SavedGameFile& operator=(
+		const SavedGameFile& that) = delete;
 
-	SavedGame(
-		const SavedGame& that) = delete;
-
-	SavedGame& operator=(
-		const SavedGame& that) = delete;
-
-	virtual ~SavedGame();
+	virtual ~SavedGameFile();
 
 
 	// Creates a new saved game file for writing.
@@ -57,9 +54,9 @@ public:
 
 	// Reads a chunk from the file into the internal buffer.
 	void read_chunk(
-		const ChunkId chunk_id) override;
+		const uint32_t chunk_id) override;
 
-	using ISavedGame::read_chunk;
+	using ISavedGameStream::read_chunk;
 
 
 	// Returns true if all data read from the internal buffer.
@@ -71,26 +68,20 @@ public:
 
 	// Writes a chunk into the file from the internal buffer.
 	void write_chunk(
-		const ChunkId chunk_id) override;
-
-	using ISavedGame::write_chunk;
+		const uint32_t chunk_id) override;
 
 
 	// Reads a raw data from the internal buffer.
-	void raw_read(
+	void read(
 		void* dst_data,
 		int dst_size) override;
 
-	using ISavedGame::read;
-
 
 	// Writes a raw data into the internal buffer.
-	void raw_write(
+	void write(
 		const void* src_data,
 		int src_size) override;
 
-	using ISavedGame::write;
-
 
 	bool is_write_failed() const;
 
@@ -119,6 +110,11 @@ public:
 	// Resets buffer offset to the beginning.
 	void reset_buffer_offset() override;
 
+	// If true won't throw an exception when buffer offset is beyond it's size.
+	// Although, no data will be read beyond the buffer.
+	void allow_read_overflow(
+		bool value) override;
+
 
 	// Renames a saved game file.
 	static void rename(
@@ -130,14 +126,7 @@ public:
 		const std::string& base_file_name);
 
 	// Returns a default instance of the class.
-	static SavedGame& get_instance();
-
-
-protected:
-	// If true won't throw an exception when buffer offset is beyond it's size.
-	// Although, no data will be read beyond the buffer.
-	void allow_read_overflow(
-		bool value) override;
+	static SavedGameFile& get_instance();
 
 
 private:
@@ -206,10 +195,10 @@ private:
 		uint32_t chunk_id);
 
 	static constexpr uint32_t get_jo_magic_value();
-}; // SavedGame
+}; // SavedGameFile
 
 
 } // ojk
 
 
-#endif // OJK_SAVED_GAME_FWD_INCLUDED
+#endif // OJK_SAVED_GAME_FILE_INCLUDED
diff --git a/shared/qcommon/ojk_i_saved_game.h b/shared/qcommon/ojk_saved_game_stream_helper.h
similarity index 81%
rename from shared/qcommon/ojk_i_saved_game.h
rename to shared/qcommon/ojk_saved_game_stream_helper.h
index ed8eaed..6af5dd0 100644
--- a/shared/qcommon/ojk_i_saved_game.h
+++ b/shared/qcommon/ojk_saved_game_stream_helper.h
@@ -1,15 +1,15 @@
 //
-// Saved game interface.
+// Saved game stream helper.
 //
 
 
-#ifndef OJK_I_SAVED_GAME_INCLUDED
-#define OJK_I_SAVED_GAME_INCLUDED
+#ifndef OJK_SAVED_GAME_STREAM_HELPER_INCLUDED
+#define OJK_SAVED_GAME_STREAM_HELPER_INCLUDED
 
 
 #include <cstdint>
 #include <type_traits>
-#include "ojk_i_saved_game_fwd.h"
+#include "ojk_saved_game_stream_helper_fwd.h"
 #include "ojk_scope_guard.h"
 
 
@@ -20,11 +20,9 @@ namespace ojk
 // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
 // Class stuff
 
-inline ISavedGame::ISavedGame()
-{
-}
-
-inline ISavedGame::~ISavedGame()
+inline SavedGameStreamHelper::SavedGameStreamHelper(
+	ISavedGameStream* saved_game_stream) :
+		saved_game_stream_(saved_game_stream)
 {
 }
 
@@ -36,33 +34,33 @@ inline ISavedGame::~ISavedGame()
 // read_chunk
 
 template<typename TSrc, typename TDst>
-void ISavedGame::read_chunk(
-	const ChunkId chunk_id,
+void SavedGameStreamHelper::read_chunk(
+	const uint32_t chunk_id,
 	TDst& dst_value)
 {
-	read_chunk(
+	saved_game_stream_->read_chunk(
 		chunk_id);
 
 	read<TSrc>(
 		dst_value);
 
-	ensure_all_data_read();
+	saved_game_stream_->ensure_all_data_read();
 }
 
 template<typename TSrc, typename TDst>
-void ISavedGame::read_chunk(
-	const ChunkId chunk_id,
+void SavedGameStreamHelper::read_chunk(
+	const uint32_t chunk_id,
 	TDst* dst_values,
 	int dst_count)
 {
-	read_chunk(
+	saved_game_stream_->read_chunk(
 		chunk_id);
 
 	read<TSrc>(
 		dst_values,
 		dst_count);
 
-	ensure_all_data_read();
+	saved_game_stream_->ensure_all_data_read();
 }
 
 // read_chunk
@@ -73,53 +71,53 @@ void ISavedGame::read_chunk(
 // write_chunk
 
 template<typename TSize>
-void ISavedGame::write_chunk_and_size(
-	const ChunkId size_chunk_id,
-	const ChunkId data_chunk_id)
+void SavedGameStreamHelper::write_chunk_and_size(
+	const uint32_t size_chunk_id,
+	const uint32_t data_chunk_id)
 {
-	save_buffer();
+	saved_game_stream_->save_buffer();
 
-	auto data_size = get_buffer_size();
+	auto data_size = saved_game_stream_->get_buffer_size();
 
-	reset_buffer();
+	saved_game_stream_->reset_buffer();
 
 	write_chunk<TSize>(
 		size_chunk_id,
 		data_size);
 
-	load_buffer();
+	saved_game_stream_->load_buffer();
 
-	write_chunk(
+	saved_game_stream_->write_chunk(
 		data_chunk_id);
 }
 
 template<typename TDst, typename TSrc>
-void ISavedGame::write_chunk(
-	const ChunkId chunk_id,
+void SavedGameStreamHelper::write_chunk(
+	const uint32_t chunk_id,
 	const TSrc& src_value)
 {
-	reset_buffer();
+	saved_game_stream_->reset_buffer();
 
 	write<TDst>(
 		src_value);
 
-	write_chunk(
+	saved_game_stream_->write_chunk(
 		chunk_id);
 }
 
 template<typename TDst, typename TSrc>
-void ISavedGame::write_chunk(
-	const ChunkId chunk_id,
+void SavedGameStreamHelper::write_chunk(
+	const uint32_t chunk_id,
 	const TSrc* src_values,
 	int src_count)
 {
-	reset_buffer();
+	saved_game_stream_->reset_buffer();
 
 	write<TDst>(
 		src_values,
 		src_count);
 
-	write_chunk(
+	saved_game_stream_->write_chunk(
 		chunk_id);
 }
 
@@ -131,7 +129,7 @@ void ISavedGame::write_chunk(
 // read
 
 template<typename TSrc, typename TDst>
-void ISavedGame::read(
+void SavedGameStreamHelper::read(
 	TDst& dst_value)
 {
 	using Tag = typename std::conditional <
@@ -170,7 +168,7 @@ void ISavedGame::read(
 }
 
 template<typename TSrc, typename TDst>
-void ISavedGame::read(
+void SavedGameStreamHelper::read(
 	TDst& dst_value,
 	BooleanTag)
 {
@@ -178,7 +176,7 @@ void ISavedGame::read(
 
 	TSrc src_value;
 
-	raw_read(
+	saved_game_stream_->read(
 		&src_value,
 		static_cast<int>(sizeof(TSrc)));
 
@@ -189,7 +187,7 @@ void ISavedGame::read(
 }
 
 template<typename TSrc, typename TDst>
-void ISavedGame::read(
+void SavedGameStreamHelper::read(
 	TDst& dst_value,
 	NumericTag)
 {
@@ -197,7 +195,7 @@ void ISavedGame::read(
 
 	TSrc src_value;
 
-	raw_read(
+	saved_game_stream_->read(
 		&src_value,
 		src_size);
 
@@ -208,7 +206,7 @@ void ISavedGame::read(
 }
 
 template<typename TSrc, typename TDst>
-void ISavedGame::read(
+void SavedGameStreamHelper::read(
 	TDst*& dst_value,
 	PointerTag)
 {
@@ -233,7 +231,7 @@ void ISavedGame::read(
 }
 
 template<typename TSrc, typename TDst>
-void ISavedGame::read(
+void SavedGameStreamHelper::read(
 	TDst& dst_value,
 	ClassTag)
 {
@@ -242,11 +240,11 @@ void ISavedGame::read(
 		"Unsupported types.");
 
 	dst_value.sg_import(
-		this);
+		saved_game_stream_);
 }
 
 template<typename TSrc, typename TDst, int TCount>
-void ISavedGame::read(
+void SavedGameStreamHelper::read(
 	TDst(&dst_values)[TCount],
 	Array1dTag)
 {
@@ -256,7 +254,7 @@ void ISavedGame::read(
 }
 
 template<typename TSrc, typename TDst, int TCount1, int TCount2>
-void ISavedGame::read(
+void SavedGameStreamHelper::read(
 	TDst(&dst_values)[TCount1][TCount2],
 	Array2dTag)
 {
@@ -273,19 +271,19 @@ void ISavedGame::read(
 // try_read
 
 template<typename TSrc, typename TDst>
-bool ISavedGame::try_read(
+bool SavedGameStreamHelper::try_read(
 	TDst& dst_value)
 {
 	ScopeGuard scope_guard(
-		[this]()
+		[saved_game_stream_]()
 		{
-			this->allow_read_overflow(
+			saved_game_stream_->allow_read_overflow(
 				true);
 		},
 
-		[this]()
+		[saved_game_stream_]()
 		{
-			this->allow_read_overflow(
+			saved_game_stream_->allow_read_overflow(
 				false);
 		}
 	);
@@ -294,7 +292,7 @@ bool ISavedGame::try_read(
 	read<TSrc>(
 		dst_value);
 
-	return is_all_data_read();
+	return saved_game_stream_->is_all_data_read();
 }
 
 // try_read
@@ -305,7 +303,7 @@ bool ISavedGame::try_read(
 // read (C-array)
 
 template<typename TSrc, typename TDst>
-void ISavedGame::read(
+void SavedGameStreamHelper::read(
 	TDst* dst_values,
 	int dst_count)
 {
@@ -362,14 +360,14 @@ void ISavedGame::read(
 }
 
 template<typename TSrc, typename TDst>
-void ISavedGame::read(
+void SavedGameStreamHelper::read(
 	TDst* dst_values,
 	int dst_count,
 	InplaceTag)
 {
 	const auto dst_size = dst_count * static_cast<int>(sizeof(TDst));
 
-	raw_read(
+	saved_game_stream_->read(
 		dst_values,
 		dst_size);
 
@@ -378,7 +376,7 @@ void ISavedGame::read(
 }
 
 template<typename TSrc, typename TDst>
-void ISavedGame::read(
+void SavedGameStreamHelper::read(
 	TDst* dst_values,
 	int dst_count,
 	CastTag)
@@ -413,7 +411,7 @@ void ISavedGame::read(
 // write
 
 template<typename TDst, typename TSrc>
-void ISavedGame::write(
+void SavedGameStreamHelper::write(
 	const TSrc& src_value)
 {
 	using Tag = typename std::conditional<
@@ -448,7 +446,7 @@ void ISavedGame::write(
 }
 
 template<typename TDst, typename TSrc>
-void ISavedGame::write(
+void SavedGameStreamHelper::write(
 	const TSrc& src_value,
 	NumericTag)
 {
@@ -459,13 +457,13 @@ void ISavedGame::write(
 	// FIXME Byte order
 	//
 
-	raw_write(
+	saved_game_stream_->write(
 		&dst_value,
 		dst_size);
 }
 
 template<typename TDst, typename TSrc>
-void ISavedGame::write(
+void SavedGameStreamHelper::write(
 	const TSrc* src_value,
 	PointerTag)
 {
@@ -483,7 +481,7 @@ void ISavedGame::write(
 }
 
 template<typename TDst, typename TSrc>
-void ISavedGame::write(
+void SavedGameStreamHelper::write(
 	const TSrc& src_value,
 	ClassTag)
 {
@@ -492,11 +490,11 @@ void ISavedGame::write(
 		"Unsupported types.");
 
 	src_value.sg_export(
-		this);
+		saved_game_stream_);
 }
 
 template<typename TDst, typename TSrc, int TCount>
-void ISavedGame::write(
+void SavedGameStreamHelper::write(
 	const TSrc(&src_values)[TCount],
 	Array1dTag)
 {
@@ -506,7 +504,7 @@ void ISavedGame::write(
 }
 
 template<typename TDst, typename TSrc, int TCount1, int TCount2>
-void ISavedGame::write(
+void SavedGameStreamHelper::write(
 	const TSrc(&src_values)[TCount1][TCount2],
 	Array2dTag)
 {
@@ -523,7 +521,7 @@ void ISavedGame::write(
 // write (C-array)
 
 template<typename TDst, typename TSrc>
-void ISavedGame::write(
+void SavedGameStreamHelper::write(
 	const TSrc* src_values,
 	int src_count)
 {
@@ -579,14 +577,14 @@ void ISavedGame::write(
 }
 
 template<typename TDst, typename TSrc>
-void ISavedGame::write(
+void SavedGameStreamHelper::write(
 	const TSrc* src_values,
 	int src_count,
 	InplaceTag)
 {
 	const auto src_size = src_count * static_cast<int>(sizeof(TSrc));
 
-	raw_write(
+	saved_game_stream_->write(
 		src_values,
 		src_size);
 
@@ -595,7 +593,7 @@ void ISavedGame::write(
 }
 
 template<typename TDst, typename TSrc>
-void ISavedGame::write(
+void SavedGameStreamHelper::write(
 	const TSrc* src_values,
 	int src_count,
 	CastTag)
@@ -629,4 +627,4 @@ void ISavedGame::write(
 } // ojk
 
 
-#endif // OJK_I_SAVED_GAME_INCLUDED
+#endif // OJK_SAVED_GAME_STREAM_HELPER_INCLUDED
diff --git a/shared/qcommon/ojk_i_saved_game_fwd.h b/shared/qcommon/ojk_saved_game_stream_helper_fwd.h
similarity index 58%
rename from shared/qcommon/ojk_i_saved_game_fwd.h
rename to shared/qcommon/ojk_saved_game_stream_helper_fwd.h
index 7a3bb59..63d2fe6 100644
--- a/shared/qcommon/ojk_i_saved_game_fwd.h
+++ b/shared/qcommon/ojk_saved_game_stream_helper_fwd.h
@@ -1,103 +1,64 @@
 //
-// Saved game interface.
+// Saved game stream helper.
 // (forward declaration)
 //
 
 
-#ifndef OJK_I_SAVED_GAME_FWD_INCLUDED
-#define OJK_I_SAVED_GAME_FWD_INCLUDED
+#ifndef OJK_SAVED_GAME_STREAM_HELPER_FWD_INCLUDED
+#define OJK_SAVED_GAME_STREAM_HELPER_FWD_INCLUDED
 
 
-#include <cstdint>
-#include <string>
+#include "ojk_i_saved_game_stream.h"
 
 
 namespace ojk
 {
 
 
-class ISavedGame
+class SavedGameStreamHelper
 {
 public:
-	using ChunkId = uint32_t;
+	SavedGameStreamHelper(
+		ISavedGameStream* saved_game_stream);
 
 
-	ISavedGame();
-
-	ISavedGame(
-		const ISavedGame& that) = delete;
-
-	ISavedGame& operator=(
-		const ISavedGame& that) = delete;
-
-	virtual ~ISavedGame();
-
-
-	// Returns true if the saved game opened for reading.
-	virtual bool is_readable() const = 0;
-
-	// Returns true if the saved game opened for writing.
-	virtual bool is_writable() const = 0;
-
-
-	// Reads a chunk from the file into the internal buffer.
-	virtual void read_chunk(
-		const ChunkId chunk_id) = 0;
-
 	// Reads a value or an array of values from the file via
 	// the internal buffer.
 	template<typename TSrc = void, typename TDst = void>
 	void read_chunk(
-		const ChunkId chunk_id,
+		const uint32_t chunk_id,
 		TDst& dst_value);
 
 	// Reads an array of values with specified count from
 	// the file via the internal buffer.
 	template<typename TSrc = void, typename TDst = void>
 	void read_chunk(
-		const ChunkId chunk_id,
+		const uint32_t chunk_id,
 		TDst* dst_values,
 		int dst_count);
 
-	// Returns true if all data read from the internal buffer.
-	virtual bool is_all_data_read() const = 0;
-
-	// Throws an exception if all data not read.
-	virtual void ensure_all_data_read() const = 0;
-
-
-	// Writes a chunk into the file from the internal buffer.
-	virtual void write_chunk(
-		const ChunkId chunk_id) = 0;
-
 	// Writes a data-chunk into the file from the internal buffer
 	// prepended with a size-chunk that holds a size of the data-chunk.
 	template<typename TSize>
 	void write_chunk_and_size(
-		const ChunkId size_chunk_id,
-		const ChunkId data_chunk_id);
+		const uint32_t size_chunk_id,
+		const uint32_t data_chunk_id);
 
 	// Writes a value or an array of values into the file via
 	// the internal buffer.
 	template<typename TDst = void, typename TSrc = void>
 	void write_chunk(
-		const ChunkId chunk_id,
+		const uint32_t chunk_id,
 		const TSrc& src_value);
 
 	// Writes an array of values with specified count into
 	// the file via the internal buffer.
 	template<typename TDst = void, typename TSrc = void>
 	void write_chunk(
-		const ChunkId chunk_id,
+		const uint32_t chunk_id,
 		const TSrc* src_values,
 		int src_count);
 
-
-	// Reads a raw data from the internal buffer.
-	virtual void raw_read(
-		void* dst_data,
-		int dst_size) = 0;
-
 	// Reads a value or array of values from the internal buffer.
 	template<typename TSrc = void, typename TDst = void>
 	void read(
@@ -117,11 +78,6 @@ public:
 		TDst& dst_value);
 
 
-	// Writes a raw data into the internal buffer.
-	virtual void raw_write(
-		const void* src_data,
-		int src_size) = 0;
-
 	// Writes a value or array of values into the internal buffer.
 	template<typename TDst = void, typename TSrc = void>
 	void write(
@@ -134,71 +90,19 @@ public:
 		int src_count);
 
 
-	// Increments buffer's offset by the specified non-negative count.
-	virtual void skip(
-		int count) = 0;
-
-
-	// Stores current I/O buffer and it's position.
-	virtual void save_buffer() = 0;
-
-	// Restores saved I/O buffer and it's position.
-	virtual void load_buffer() = 0;
+private:
+	ISavedGameStream* saved_game_stream_;
 
-	// Returns a pointer to data in the I/O buffer.
-	virtual const void* get_buffer_data() const = 0;
 
-	// Returns a current size of the I/O buffer.
-	virtual int get_buffer_size() const = 0;
-
-
-	// Clears buffer and resets it's offset to the beginning.
-	virtual void reset_buffer() = 0;
-
-	// Resets buffer offset to the beginning.
-	virtual void reset_buffer_offset() = 0;
-
-
-protected:
 	// Tags for dispatching.
-	class BooleanTag
-	{
-	public:
-	};
-	class NumericTag
-	{
-	public:
-	};
-	class PointerTag
-	{
-	public:
-	};
-	class ClassTag
-	{
-	public:
-	};
-	class Array1dTag
-	{
-	public:
-	};
-	class Array2dTag
-	{
-	public:
-	};
-	class InplaceTag
-	{
-	public:
-	};
-	class CastTag
-	{
-	public:
-	};
-
-
-	// If true won't throw an exception when buffer offset is beyond it's size.
-	// Although, no data will be read beyond the buffer.
-	virtual void allow_read_overflow(
-		bool value) = 0;
+	class BooleanTag { public: };
+	class NumericTag { public: };
+	class PointerTag { public: };
+	class ClassTag { public: };
+	class Array1dTag { public: };
+	class Array2dTag { public: };
+	class InplaceTag { public: };
+	class CastTag { public: };
 
 
 	template<typename TSrc, typename TDst>
@@ -282,10 +186,11 @@ protected:
 		const TSrc* src_values,
 		int src_count,
 		CastTag);
-}; // ISavedGame
+}; // SavedGameStreamHelper
+
 
+}
 
-} // ojk
 
+#endif // OJK_SAVED_GAME_STREAM_HELPER_FWD_INCLUDED
 
-#endif // OJK_I_SAVED_GAME_FWD_INCLUDED

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-games/openjk.git



More information about the Pkg-games-commits mailing list