Преглед изворни кода

Merge in ReaderWriter.h and simplify code

Daniele Bartolini пре 11 година
родитељ
комит
d78dd7cb76

+ 1 - 4
engine/CMakeLists.txt

@@ -98,8 +98,6 @@ set (FILESYSTEM_SRC
 )
 
 set (FILESYSTEM_HEADERS
-	core/filesystem/BinaryReader.h
-	core/filesystem/BinaryWriter.h
 	core/filesystem/DiskFile.h
 	core/filesystem/DiskFilesystem.h
 	core/filesystem/File.h
@@ -108,8 +106,7 @@ set (FILESYSTEM_HEADERS
 	core/filesystem/NetworkFile.h
 	core/filesystem/NetworkFilesystem.h
 	core/filesystem/NullFile.h
-	core/filesystem/TextReader.h
-	core/filesystem/TextWriter.h
+	core/filesystem/ReaderWriter.h
 )
 
 set (JSON_SRC

+ 3 - 6
engine/Crown.h

@@ -76,14 +76,11 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "PoolAllocator.h"
 
 // Core/Filesystem
-#include "File.h"
 #include "DiskFile.h"
-#include "NullFile.h"
-#include "BinaryReader.h"
-#include "BinaryWriter.h"
-#include "TextReader.h"
-#include "TextWriter.h"
+#include "File.h"
 #include "Filesystem.h"
+#include "NullFile.h"
+#include "ReaderWriter.h"
 
 // Core/Json
 #include "JSON.h"

+ 0 - 1
engine/Device.cpp

@@ -45,7 +45,6 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "ResourceManager.h"
 #include "StringSetting.h"
 #include "StringUtils.h"
-#include "TextReader.h"
 #include "Touch.h"
 #include "Types.h"
 #include "Bundle.h"

+ 0 - 114
engine/core/filesystem/BinaryReader.h

@@ -1,114 +0,0 @@
-/*
-Copyright (c) 2013 Daniele Bartolini, Michele Rossi
-Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
-
-Permission is hereby granted, free of charge, to any person
-obtaining a copy of this software and associated documentation
-files (the "Software"), to deal in the Software without
-restriction, including without limitation the rights to use,
-copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the
-Software is furnished to do so, subject to the following
-conditions:
-
-The above copyright notice and this permission notice shall be
-included in all copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
-OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
-HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
-WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
-FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
-OTHER DEALINGS IN THE SOFTWARE.
-*/
-
-#pragma once
-
-#include "Types.h"
-
-namespace crown
-{
-
-class File;
-
-/// A reader that offers a convenient way to read from a File
-class BinaryReader
-{
-public:
-
-	//-----------------------------------------------------------------------------
-	BinaryReader(File& file) : m_file(file) {}
-
-	//-----------------------------------------------------------------------------
-	int8_t read_byte()
-	{
-		int8_t buffer;
-		m_file.read(&buffer, sizeof(int8_t));
-		return buffer;
-	}
-
-	//-----------------------------------------------------------------------------
-	int16_t read_int16()
-	{
-		int16_t buffer;
-		m_file.read(&buffer, sizeof(int16_t));
-		return buffer;
-	}
-
-	//-----------------------------------------------------------------------------
-	uint16_t read_uint16()
-	{
-		uint16_t buffer;
-		m_file.read(&buffer, sizeof(uint16_t));
-		return buffer;
-	}
-
-	//-----------------------------------------------------------------------------
-	int32_t read_int32()
-	{
-		int32_t buffer;
-		m_file.read(&buffer, sizeof(int32_t));
-		return buffer;
-	}
-
-	//-----------------------------------------------------------------------------
-	uint32_t read_uint32()
-	{
-		uint32_t buffer;
-		m_file.read(&buffer, sizeof(uint32_t));
-		return buffer;
-	}
-
-	//-----------------------------------------------------------------------------
-	int64_t read_int64()
-	{
-		int64_t buffer;
-		m_file.read(&buffer, sizeof(int64_t));
-		return buffer;
-	}
-
-	//-----------------------------------------------------------------------------
-	double read_double()
-	{
-		double buffer;
-		m_file.read(&buffer, sizeof(double));
-		return buffer;
-	}
-
-	//-----------------------------------------------------------------------------
-	float read_float()
-	{
-		float buffer;
-		m_file.read(&buffer, sizeof(float));
-		return buffer;
-	}
-
-private:
-
-	File& m_file;
-};
-
-} // namespace crown
-

+ 0 - 100
engine/core/filesystem/BinaryWriter.h

@@ -1,100 +0,0 @@
-/*
-Copyright (c) 2013 Daniele Bartolini, Michele Rossi
-Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
-
-Permission is hereby granted, free of charge, to any person
-obtaining a copy of this software and associated documentation
-files (the "Software"), to deal in the Software without
-restriction, including without limitation the rights to use,
-copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the
-Software is furnished to do so, subject to the following
-conditions:
-
-The above copyright notice and this permission notice shall be
-included in all copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
-OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
-HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
-WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
-FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
-OTHER DEALINGS IN THE SOFTWARE.
-*/
-
-#pragma once
-
-#include "Types.h"
-
-namespace crown
-{
-
-class File;
-
-/// A writer that offers a convenient way to write to a File
-///
-/// @ingroup Filesystem
-class BinaryWriter
-{
-public:
-
-	//-----------------------------------------------------------------------------
-	BinaryWriter(File& file) : m_file(file) {}
-
-	//-----------------------------------------------------------------------------
-	void write_byte(int8_t buffer)
-	{
-		m_file.write(&buffer, sizeof(int8_t));
-	}
-
-	//-----------------------------------------------------------------------------
-	void write_int16(int16_t buffer)
-	{
-		m_file.write(&buffer, sizeof(int16_t));
-	}
-
-	//-----------------------------------------------------------------------------
-	void write_uint16(uint16_t buffer)
-	{
-		m_file.write(&buffer, sizeof(uint16_t));
-	}
-
-	//-----------------------------------------------------------------------------
-	void write_int32(int32_t buffer)
-	{
-		m_file.write(&buffer, sizeof(int32_t));
-	}
-
-	//-----------------------------------------------------------------------------
-	void write_uint32(uint32_t buffer)
-	{
-		m_file.write(&buffer, sizeof(uint32_t));
-	}
-
-	//-----------------------------------------------------------------------------
-	void write_int64(int64_t buffer)
-	{
-		m_file.write(&buffer, sizeof(int64_t));
-	}
-
-	//-----------------------------------------------------------------------------
-	void write_double(double buffer)
-	{
-		m_file.write(&buffer, sizeof(double));
-	}
-
-	//-----------------------------------------------------------------------------
-	void write_float(float buffer)
-	{
-		m_file.write(&buffer, sizeof(float));
-	}
-
-private:
-
-	File& m_file;
-};
-
-} // namespace crown
-

+ 19 - 20
engine/core/filesystem/DiskFile.h

@@ -41,63 +41,62 @@ class DiskFile: public File
 public:
 
 	/// Opens @a filename with specified @a mode
-					DiskFile(FileOpenMode mode, const char* filename);
-	virtual			~DiskFile();
+	DiskFile(FileOpenMode mode, const char* filename);
+	virtual ~DiskFile();
 
 	/// @copydoc File::seek() 
-	void			seek(size_t position);
+	void seek(size_t position);
 
 	/// @copydoc File::seek_to_end() 
-	void			seek_to_end();
+	void seek_to_end();
 
 	/// @copydoc File::skip() 
-	void			skip(size_t bytes);
+	void skip(size_t bytes);
 
 	/// @copydoc File::read() 
-	void			read(void* buffer, size_t size);
+	void read(void* buffer, size_t size);
 
 	/// @copydoc File::write() 
-	void			write(const void* buffer, size_t size);
+	void write(const void* buffer, size_t size);
 
 	/// @copydoc File::copy_to() 
-	bool			copy_to(File& file, size_t size = 0);
+	bool copy_to(File& file, size_t size = 0);
 
 	/// @copydoc File::flush() 
-	void			flush();
+	void flush();
 
 	/// @copydoc File::end_of_file() 
-	bool			end_of_file();
+	bool end_of_file();
 
 	/// @copydoc File::is_valid() 
-	bool			is_valid();
+	bool is_valid();
 
 	/// @copydoc File::size() 
-	size_t			size();
+	size_t size();
 
 	/// @copydoc File::position() 
-	size_t			position();
+	size_t position();
 
 	/// @copydoc File::can_read() 
-	bool			can_read() const;
+	bool can_read() const;
 
 	/// @copydoc File::can_write() 
-	bool			can_write() const;
+	bool can_write() const;
 
 	/// @copydoc File::can_seek() 
-	bool			can_seek() const;
+	bool can_seek() const;
 
 protected:
 
-	OsFile			m_file;
-	bool			m_last_was_read;
+	OsFile m_file;
+	bool m_last_was_read;
 
 protected:
 
-	inline void		check_valid() const
+	inline void check_valid() const
 	{
 		CE_ASSERT(m_file.is_open(), "File is not open");
 	}
 };
 
 } // namespace crown
-

+ 19 - 20
engine/core/filesystem/File.h

@@ -53,32 +53,32 @@ class File
 public:
 
 	/// Opens the file with the given @a mode
-						File(FileOpenMode mode) : m_open_mode(mode) {}
-	virtual				~File() {};
+	File(FileOpenMode mode) : m_open_mode(mode) {}
+	virtual ~File() {};
 
 	/// Sets the position indicator of the file to position.
-	virtual void		seek(size_t position) = 0;
+	virtual void seek(size_t position) = 0;
 
 	/// Sets the position indicator to the end of the file
-	virtual void		seek_to_end() = 0;
+	virtual void seek_to_end() = 0;
 
 	/// Sets the position indicator to bytes after current position
-	virtual void		skip(size_t bytes) = 0;
+	virtual void skip(size_t bytes) = 0;
 
 	/// Reads a block of data from the file.
-	virtual void		read(void* buffer, size_t size) = 0;
+	virtual void read(void* buffer, size_t size) = 0;
 
 	/// Writes a block of data to the file.
-	virtual void		write(const void* buffer, size_t size) = 0;
+	virtual void write(const void* buffer, size_t size) = 0;
 
 	/// Copies a chunk of 'size' bytes of data from this to another file.
-	virtual bool		copy_to(File& file, size_t size = 0) = 0;
+	virtual bool copy_to(File& file, size_t size = 0) = 0;
 
 	/// Zips a chunk of 'size' bytes of data from this to another file using compressor.
-	virtual bool		compress_to(File& file, size_t size, size_t& compressed_size, Compressor& compressor);
+	virtual bool compress_to(File& file, size_t size, size_t& compressed_size, Compressor& compressor);
 
 	/// Unzip a zipped file of data from this to another file using compressor.
-	virtual bool		uncompress_to(File& file, size_t& uncompressed_size, Compressor& compressor);
+	virtual bool uncompress_to(File& file, size_t& uncompressed_size, Compressor& compressor);
 
 	/// Forces the previouses write operations to complete.
 	/// Generally, when a File is attached to a file,
@@ -86,38 +86,37 @@ public:
 	/// may be stored to a temporary buffer before making its way to
 	/// the file. This method forces all the pending output operations
 	/// to be written to the file.
-	virtual void		flush() = 0;
+	virtual void flush() = 0;
 
 	/// Returns whether the file is valid.
 	/// A file is valid when the buffer where it operates
 	/// exists. (i.e. a file descriptor is attached to the file, 
 	/// a memory area is attached to the file etc.)
-	virtual bool		is_valid() = 0;
+	virtual bool is_valid() = 0;
 
 	/// Returns whether the position is at end of file.
-	virtual bool		end_of_file() = 0;
+	virtual bool end_of_file() = 0;
 
 	/// Returns the size of file in bytes.
-	virtual size_t		size() = 0;
+	virtual size_t size() = 0;
 
 	/// Returns the current position in file.
 	/// Generally, for binary data, it means the number of bytes
 	/// from the beginning of the file.
-	virtual size_t		position() = 0;
+	virtual size_t position() = 0;
 
 	/// Returns whether the file can be read.
-	virtual bool		can_read() const = 0;
+	virtual bool can_read() const = 0;
 
 	/// Returns whether the file can be wrote.
-	virtual bool		can_write() const = 0;
+	virtual bool can_write() const = 0;
 
 	/// Returns whether the file can be sought.
-	virtual bool		can_seek() const = 0;
+	virtual bool can_seek() const = 0;
 
 protected:
 
-	FileOpenMode		m_open_mode;
+	FileOpenMode m_open_mode;
 };
 
 } // namespace crown
-

+ 21 - 22
engine/core/filesystem/NetworkFile.h

@@ -42,59 +42,58 @@ class NetworkFile: public File
 public:
 
 	/// Reads the file named @a filename from the given @a socket.
-					NetworkFile(const NetAddress& addr, uint16_t port, const char* filename);
-	virtual			~NetworkFile();
+	NetworkFile(const NetAddress& addr, uint16_t port, const char* filename);
+	virtual ~NetworkFile();
 
 	/// @copydoc File::seek() 
-	void			seek(size_t position);
+	void seek(size_t position);
 
 	/// @copydoc File::seek_to_end() 
-	void			seek_to_end();
+	void seek_to_end();
 
 	/// @copydoc File::skip() 
-	void			skip(size_t bytes);
+	void skip(size_t bytes);
 
 	/// @copydoc File::read() 
-	void			read(void* buffer, size_t size);
+	void read(void* buffer, size_t size);
 
 	/// @copydoc File::write() 
-	void			write(const void* buffer, size_t size);
+	void write(const void* buffer, size_t size);
 
 	/// @copydoc File::copy_to() 
-	bool			copy_to(File& file, size_t size = 0);
+	bool copy_to(File& file, size_t size = 0);
 
 	/// @copydoc File::flush() 
-	void			flush();
+	void flush();
 
 	/// @copydoc File::end_of_file() 
-	bool			end_of_file();
+	bool end_of_file();
 
 	/// @copydoc File::is_valid() 
-	bool			is_valid();
+	bool is_valid();
 
 	/// @copydoc File::size() 
-	size_t			size();
+	size_t size();
 
 	/// @copydoc File::position() 
-	size_t			position();
+	size_t position();
 
 	/// @copydoc File::can_read() 
-	bool			can_read() const;
+	bool can_read() const;
 
 	/// @copydoc File::can_write() 
-	bool			can_write() const;
+	bool can_write() const;
 
 	/// @copydoc File::can_seek() 
-	bool			can_seek() const;
+	bool can_seek() const;
 
 private:
 
-	char			m_filename[MAX_PATH_LENGTH];
-	NetAddress		m_address;
-	uint16_t		m_port;
-	TCPSocket		m_socket;
-	size_t			m_position;
+	char m_filename[MAX_PATH_LENGTH];
+	NetAddress m_address;
+	uint16_t m_port;
+	TCPSocket m_socket;
+	size_t m_position;
 };
 
 } // namespace crown
-

+ 78 - 8
engine/core/filesystem/TextReader.h → engine/core/filesystem/ReaderWriter.h

@@ -24,15 +24,12 @@ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 OTHER DEALINGS IN THE SOFTWARE.
 */
 
-#pragma once
-
 #include "Types.h"
+#include "File.h"
 
 namespace crown
 {
 
-class File;
-
 /// A reader that offers a convenient way to read text from a File
 ///
 /// @ingroup Filesystem
@@ -41,9 +38,7 @@ class TextReader
 public:
 
 	//-----------------------------------------------------------------------------
-	TextReader(File& file) : m_file(file)
-	{
-	}
+	TextReader(File& file) : m_file(file) {}
 
 	/// Reads characters from file and stores them as a C string
 	/// into string until (size-1) characters have been read or
@@ -81,5 +76,80 @@ private:
 	File& m_file;
 };
 
-} // namespace crown
+/// A reader that offers a convenient way to write text to a File
+///
+/// @ingroup Filesystem
+class TextWriter
+{
+public:
+
+	//-----------------------------------------------------------------------------
+	TextWriter(File& file) : m_file(file) {}
+
+	/// Writes the string pointed by string to the file.
+	/// The function begins copying from the address specified (string)
+	/// until it reaches the terminating null character ('\0').
+	/// The final null character is not copied to the file.
+	void write_string(const char* string)
+	{
+		m_file.write(string, string::strlen(string));
+	}
+
+private:
+
+	File& m_file;
+};
+
+/// A writer that offers a convenient way to write to a File
+///
+/// @ingroup Filesystem
+class BinaryWriter
+{
+public:
+
+	//-----------------------------------------------------------------------------
+	BinaryWriter(File& file) : m_file(file) {}
+
+	template <typename T>
+	void write(const T& data)
+	{
+		m_file.write(&data, sizeof(T));
+	}
+
+	void skip(size_t bytes)
+	{
+		m_file.skip(bytes);
+	}
+
+private:
+
+	File& m_file;
+};
+
+/// A reader that offers a convenient way to read from a File
+///
+/// @ingroup Filesystem
+class BinaryReader
+{
+public:
 
+	//-----------------------------------------------------------------------------
+	BinaryReader(File& file) : m_file(file) {}
+
+	template <typename T>
+	void read(T& data)
+	{
+		m_file.read(&data, sizeof(T));
+	}
+
+	void skip(size_t bytes)
+	{
+		m_file.skip(bytes);
+	}
+
+private:
+
+	File& m_file;
+};
+
+} // namespace crown

+ 0 - 61
engine/core/filesystem/TextWriter.h

@@ -1,61 +0,0 @@
-/*
-Copyright (c) 2013 Daniele Bartolini, Michele Rossi
-Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
-
-Permission is hereby granted, free of charge, to any person
-obtaining a copy of this software and associated documentation
-files (the "Software"), to deal in the Software without
-restriction, including without limitation the rights to use,
-copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the
-Software is furnished to do so, subject to the following
-conditions:
-
-The above copyright notice and this permission notice shall be
-included in all copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
-OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
-HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
-WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
-FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
-OTHER DEALINGS IN THE SOFTWARE.
-*/
-
-#pragma once
-
-namespace crown
-{
-
-class File;
-
-/// A reader that offers a convenient way to write text to a File
-///
-/// @ingroup Filesystem
-class TextWriter
-{
-public:
-
-	//-----------------------------------------------------------------------------
-	TextWriter(File& file) : m_file(file)
-	{
-	}
-
-	/// Writes the string pointed by string to the file.
-	/// The function begins copying from the address specified (string)
-	/// until it reaches the terminating null character ('\0').
-	/// The final null character is not copied to the file.
-	void write_string(const char* string)
-	{
-		m_file.write(string, string::strlen(string));
-	}
-
-private:
-
-	File& m_file;
-};
-
-} // namespace crown
-