浏览代码

Remove ReferenceCountable from Stream

Michael Ragazzon 6 年之前
父节点
当前提交
3ee01de68c

+ 83 - 87
Include/RmlUi/Core/Stream.h

@@ -45,94 +45,90 @@ class StreamListener;
 	@author Lloyd Weehuizen
  */
 
-class RMLUICORE_API Stream : public ReferenceCountable
+class RMLUICORE_API Stream : public NonCopyMoveable
 {
-	public:
-		// Stream modes.
-		enum StreamMode
-		{
-			MODE_WRITE = 1 << 0,
-			MODE_APPEND = 1 << 1,
-			MODE_READ = 1 << 2,
-			MODE_ASYNC = 1 << 3,
-
-			MODE_MASK = MODE_WRITE | MODE_APPEND | MODE_READ
-		};
-
-		Stream();
-		virtual ~Stream();
-
-		/// Closes the stream.
-		virtual void Close();
-
-		/// Returns the mode the stream was opened in.
-		int GetStreamMode() const;
-
-		/// Obtain the source url of this stream (if available)
-		const URL& GetSourceURL() const;
-
-		/// Are we at the end of the stream
-		virtual bool IsEOS() const;
-
-		/// Returns the size of this stream (in bytes).
-		virtual size_t Length() const = 0;
-
-		/// Returns the position of the stream pointer (in bytes).
-		virtual size_t Tell() const = 0;
-		/// Sets the stream position (in bytes).
-		virtual bool Seek(long offset, int origin) const = 0;
-
-		/// Read from the stream.
-		virtual size_t Read(void* buffer, size_t bytes) const = 0;
-		/// Read from the stream into another stream.
-		virtual size_t Read(Stream* stream, size_t bytes) const;
-		/// Read from the stream and append to the string buffer
-		virtual size_t Read(String& buffer, size_t bytes) const;
-		/// Read from the stream, without increasing the stream offset.
-		virtual size_t Peek(void* buffer, size_t bytes) const;
-
-		/// Write to the stream at the current position.
-		virtual size_t Write(const void* buffer, size_t bytes) = 0;
-		/// Write to this stream from another stream.
-		virtual size_t Write(const Stream* stream, size_t bytes);
-		/// Write a character array to the stream.
-		virtual size_t Write(const char* string);
-		/// Write a string to the stream
-		virtual size_t Write(const String& string);
-
-		/// Truncate the stream to the specified length.
-		virtual size_t Truncate(size_t bytes) = 0;
-
-		/// Push onto the front of the stream.
-		virtual size_t PushFront(const void* buffer, size_t bytes);
-		/// Push onto the back of the stream.
-		virtual size_t PushBack(const void* buffer, size_t bytes);
-
-		/// Pop from the front of the stream.
-		virtual size_t PopFront(size_t bytes);
-		/// Pop from the back of the stream.
-		virtual size_t PopBack(size_t bytes);
-
-		/// Returns true if the stream is ready for reading, false otherwise.
-		/// This is usually only implemented on streams supporting asynchronous
-		/// operations.
-		virtual bool IsReadReady() = 0;
-		/// Returns true if the stream is ready for writing, false otherwise.
-		/// This is usually only implemented on streams supporting asynchronous
-		/// operations.
-		virtual bool IsWriteReady() = 0;
-
-	protected:
-
-		/// Sets the mode on the stream; should be called by a stream when it is opened.
-		void SetStreamDetails(const URL& url, int stream_mode);		
-
-		/// Deletes the stream.
-		virtual void OnReferenceDeactivate();
-
-	private:
-		URL url;
-		int stream_mode;
+public:
+	// Stream modes.
+	enum StreamMode
+	{
+		MODE_WRITE = 1 << 0,
+		MODE_APPEND = 1 << 1,
+		MODE_READ = 1 << 2,
+		MODE_ASYNC = 1 << 3,
+
+		MODE_MASK = MODE_WRITE | MODE_APPEND | MODE_READ
+	};
+
+	Stream();
+	virtual ~Stream();
+
+	/// Closes the stream.
+	virtual void Close();
+
+	/// Returns the mode the stream was opened in.
+	int GetStreamMode() const;
+
+	/// Obtain the source url of this stream (if available)
+	const URL& GetSourceURL() const;
+
+	/// Are we at the end of the stream
+	virtual bool IsEOS() const;
+
+	/// Returns the size of this stream (in bytes).
+	virtual size_t Length() const = 0;
+
+	/// Returns the position of the stream pointer (in bytes).
+	virtual size_t Tell() const = 0;
+	/// Sets the stream position (in bytes).
+	virtual bool Seek(long offset, int origin) const = 0;
+
+	/// Read from the stream.
+	virtual size_t Read(void* buffer, size_t bytes) const = 0;
+	/// Read from the stream into another stream.
+	virtual size_t Read(Stream* stream, size_t bytes) const;
+	/// Read from the stream and append to the string buffer
+	virtual size_t Read(String& buffer, size_t bytes) const;
+	/// Read from the stream, without increasing the stream offset.
+	virtual size_t Peek(void* buffer, size_t bytes) const;
+
+	/// Write to the stream at the current position.
+	virtual size_t Write(const void* buffer, size_t bytes) = 0;
+	/// Write to this stream from another stream.
+	virtual size_t Write(const Stream* stream, size_t bytes);
+	/// Write a character array to the stream.
+	virtual size_t Write(const char* string);
+	/// Write a string to the stream
+	virtual size_t Write(const String& string);
+
+	/// Truncate the stream to the specified length.
+	virtual size_t Truncate(size_t bytes) = 0;
+
+	/// Push onto the front of the stream.
+	virtual size_t PushFront(const void* buffer, size_t bytes);
+	/// Push onto the back of the stream.
+	virtual size_t PushBack(const void* buffer, size_t bytes);
+
+	/// Pop from the front of the stream.
+	virtual size_t PopFront(size_t bytes);
+	/// Pop from the back of the stream.
+	virtual size_t PopBack(size_t bytes);
+
+	/// Returns true if the stream is ready for reading, false otherwise.
+	/// This is usually only implemented on streams supporting asynchronous
+	/// operations.
+	virtual bool IsReadReady() = 0;
+	/// Returns true if the stream is ready for writing, false otherwise.
+	/// This is usually only implemented on streams supporting asynchronous
+	/// operations.
+	virtual bool IsWriteReady() = 0;
+
+protected:
+	/// Sets the mode on the stream; should be called by a stream when it is opened.
+	void SetStreamDetails(const URL& url, int stream_mode);		
+
+private:
+	URL url;
+	int stream_mode;
 };
 
 }

+ 13 - 13
Include/RmlUi/Core/StreamMemory.h

@@ -52,39 +52,39 @@ public:
 	virtual ~StreamMemory();
 
 	/// Close the stream
-	virtual void Close();	
+	void Close() override;	
 
 	/// Are we at the end of the stream
-	virtual bool IsEOS() const;
+	bool IsEOS() const override;
 
 	/// Size of this stream ( in bytes )
-	virtual size_t Length() const;
+	size_t Length() const override;
 
 	/// Get Stream position ( in bytes )
-	size_t Tell() const;
+	size_t Tell() const override;
 
 	/// Set Stream position ( in bytes )
-	bool Seek(long offset, int origin) const;
+	bool Seek(long offset, int origin) const override;
 
 	/// Read from the stream
 	using Stream::Read;
-	virtual size_t Read(void* buffer, size_t bytes) const;
+	size_t Read(void* buffer, size_t bytes) const override;
 
 	/// Peek into the stream
-	virtual size_t Peek(void *buffer, size_t bytes) const;
+	size_t Peek(void *buffer, size_t bytes) const override;
 
 	/// Write to the stream
 	using Stream::Write;
-	virtual size_t Write(const void* buffer, size_t bytes);
+	size_t Write(const void* buffer, size_t bytes) override;
 
 	/// Truncate the stream to the specified length
-	virtual size_t Truncate(size_t bytes);
+	size_t Truncate(size_t bytes) override;
 
 	/// Push onto the front of the stream
-	virtual size_t PushFront(const void* buffer, size_t bytes);
+	size_t PushFront(const void* buffer, size_t bytes) override;
 
 	/// Pop from the front of the stream
-	virtual size_t PopFront(size_t bytes);
+	size_t PopFront(size_t bytes) override;
 
 	/// Raw access to the stream
 	const byte* RawStream() const;
@@ -93,10 +93,10 @@ public:
 	void Erase(size_t offset, size_t bytes);
 
 	/// Does the stream have data available for reading
-	virtual bool IsReadReady();
+	bool IsReadReady() override;
 
 	/// Is the stream able to accept data now
-	virtual bool IsWriteReady();
+	bool IsWriteReady() override;
 
 	/// Sets this streams source URL, useful data that is stored
 	/// in memory streams that originated from files

+ 5 - 11
Source/Core/Context.cpp

@@ -241,16 +241,12 @@ ElementDocument* Context::CreateDocument(const String& tag)
 // Load a document into the context.
 ElementDocument* Context::LoadDocument(const String& document_path)
 {	
-	StreamFile* stream = new StreamFile();
+	auto stream = std::make_unique<StreamFile>();
+
 	if (!stream->Open(document_path))
-	{
-		stream->RemoveReference();
 		return nullptr;
-	}
 
-	ElementDocument* document = LoadDocument(stream);
-
-	stream->RemoveReference();
+	ElementDocument* document = LoadDocument(stream.get());
 
 	return document;
 }
@@ -286,13 +282,11 @@ ElementDocument* Context::LoadDocument(Stream* stream)
 ElementDocument* Context::LoadDocumentFromMemory(const String& string)
 {
 	// Open the stream based on the string contents.
-	StreamMemory* stream = new StreamMemory((byte*)string.c_str(), string.size());
+	auto stream = std::make_unique<StreamMemory>((byte*)string.c_str(), string.size());
 	stream->SetSourceURL("[document from memory]");
 
 	// Load the document from the stream.
-	ElementDocument* document = LoadDocument(stream);
-
-	stream->RemoveReference();
+	ElementDocument* document = LoadDocument(stream.get());
 
 	return document;
 }

+ 7 - 10
Source/Core/ElementDocument.cpp

@@ -101,10 +101,10 @@ void ElementDocument::ProcessHeader(const DocumentHeader* document_header)
 		for (size_t i = 0;i < header.rcss_inline.size(); i++)
 		{			
 			std::unique_ptr<StyleSheet> inline_sheet = std::make_unique<StyleSheet>();
-			StreamMemory* stream = new StreamMemory((const byte*) header.rcss_inline[i].c_str(), header.rcss_inline[i].size());
+			auto stream = std::make_unique<StreamMemory>((const byte*) header.rcss_inline[i].c_str(), header.rcss_inline[i].size());
 			stream->SetSourceURL(document_header->source);
 
-			if (inline_sheet->LoadStyleSheet(stream))
+			if (inline_sheet->LoadStyleSheet(stream.get()))
 			{
 				if (new_style_sheet)
 				{
@@ -115,7 +115,7 @@ void ElementDocument::ProcessHeader(const DocumentHeader* document_header)
 					new_style_sheet = std::move(inline_sheet);
 			}
 
-			stream->RemoveReference();
+			stream.reset();
 		}		
 	}
 
@@ -128,19 +128,16 @@ void ElementDocument::ProcessHeader(const DocumentHeader* document_header)
 	// Load external scripts.
 	for (size_t i = 0; i < header.scripts_external.size(); i++)
 	{
-		StreamFile* stream = new StreamFile();
+		auto stream = std::make_unique<StreamFile>();
 		if (stream->Open(header.scripts_external[i]))
-			LoadScript(stream, header.scripts_external[i]);
-
-		stream->RemoveReference();
+			LoadScript(stream.get(), header.scripts_external[i]);
 	}
 
 	// Load internal scripts.
 	for (size_t i = 0; i < header.scripts_inline.size(); i++)
 	{
-		StreamMemory* stream = new StreamMemory((const byte*) header.scripts_inline[i].c_str(), header.scripts_inline[i].size());
-		LoadScript(stream, "");
-		stream->RemoveReference();
+		auto stream = std::make_unique<StreamMemory>((const byte*) header.scripts_inline[i].c_str(), header.scripts_inline[i].size());
+		LoadScript(stream.get(), "");
 	}
 
 	// Hide this document.

+ 6 - 11
Source/Core/Factory.cpp

@@ -215,14 +215,13 @@ bool Factory::InstanceElementText(Element* parent, const String& text)
 		(system_interface->TranslateString(translated_data, text) > 0 ||
 		 translated_data.find("<") != String::npos))
 	{
-		StreamMemory* stream = new StreamMemory(translated_data.size() + 32);
+		auto stream = std::make_unique<StreamMemory>(translated_data.size() + 32);
 		stream->Write("<body>", 6);
 		stream->Write(translated_data);
 		stream->Write("</body>", 7);
 		stream->Seek(0, SEEK_SET);
 
-		InstanceElementStream(parent, stream);
-		stream->RemoveReference();
+		InstanceElementStream(parent, stream.get());
 	}
 	else
 	{
@@ -343,20 +342,16 @@ FontEffectInstancer* Factory::GetFontEffectInstancer(const String& name)
 // Creates a style sheet containing the passed in styles.
 SharedPtr<StyleSheet> Factory::InstanceStyleSheetString(const String& string)
 {
-	StreamMemory* memory_stream = new StreamMemory((const byte*) string.c_str(), string.size());
-	SharedPtr<StyleSheet> style_sheet = InstanceStyleSheetStream(memory_stream);
-	memory_stream->RemoveReference();
-	return style_sheet;
+	auto memory_stream = std::make_unique<StreamMemory>((const byte*) string.c_str(), string.size());
+	return InstanceStyleSheetStream(memory_stream.get());
 }
 
 // Creates a style sheet from a file.
 SharedPtr<StyleSheet> Factory::InstanceStyleSheetFile(const String& file_name)
 {
-	StreamFile* file_stream = new StreamFile();
+	auto file_stream = std::make_unique<StreamFile>();
 	file_stream->Open(file_name);
-	SharedPtr<StyleSheet> style_sheet = InstanceStyleSheetStream(file_stream);
-	file_stream->RemoveReference();
-	return style_sheet;
+	return InstanceStyleSheetStream(file_stream.get());
 }
 
 // Creates a style sheet from an Stream.

+ 2 - 8
Source/Core/Stream.cpp

@@ -36,13 +36,14 @@ namespace Core {
 
 const size_t READ_BLOCK_SIZE = 1024;
 
-Stream::Stream() : ReferenceCountable(1)
+Stream::Stream()
 {
 	stream_mode = 0;
 }
 
 Stream::~Stream()
 {
+	Close();
 }
 
 void Stream::Close()
@@ -160,12 +161,5 @@ void Stream::SetStreamDetails(const URL& _url, int _stream_mode)
 	stream_mode = _stream_mode;
 }
 
-// Deletes the stream.
-void Stream::OnReferenceDeactivate()
-{
-	Close();
-	delete this;
-}
-
 }
 }

+ 9 - 9
Source/Core/StreamFile.h

@@ -48,31 +48,31 @@ public:
 	/// Attempts to open the stream pointing at a given location.
 	bool Open(const String& path);
 	/// Closes the stream.
-	virtual void Close();
+	void Close() override;
 
 	/// Returns the size of this stream (in bytes).
-	virtual size_t Length() const;
+	size_t Length() const override;
 
 	/// Returns the position of the stream pointer (in bytes).
-	virtual size_t Tell() const;
+	size_t Tell() const override;
 	/// Sets the stream position (in bytes).
-	virtual bool Seek(long offset, int origin) const;
+	bool Seek(long offset, int origin) const override;
 
 	/// Read from the stream.
-	virtual size_t Read(void* buffer, size_t bytes) const;
+	size_t Read(void* buffer, size_t bytes) const override;
 	using Stream::Read;
 
 	/// Write to the stream at the current position.
-	virtual size_t Write(const void* buffer, size_t bytes);
+	size_t Write(const void* buffer, size_t bytes) override;
 	using Stream::Write;
 
 	/// Truncate the stream to the specified length.
-	virtual size_t Truncate(size_t bytes);
+	size_t Truncate(size_t bytes) override;
 
 	/// Returns true if the stream is ready for reading, false otherwise.
-	virtual bool IsReadReady();
+	bool IsReadReady() override;
 	/// Returns false.
-	virtual bool IsWriteReady();
+	bool IsWriteReady() override;
 
 private:
 	// Determines the length of the stream.

+ 2 - 4
Source/Core/StyleSheetFactory.cpp

@@ -178,17 +178,15 @@ SharedPtr<StyleSheet> StyleSheetFactory::LoadStyleSheet(const String& sheet)
 
 	// Open stream, construct new sheet and pass the stream into the sheet
 	// TODO: Make this support ASYNC
-	StreamFile* stream = new StreamFile();
+	auto stream = std::make_unique<StreamFile>();
 	if (stream->Open(sheet))
 	{
 		new_style_sheet = std::make_shared<StyleSheet>();
-		if (!new_style_sheet->LoadStyleSheet(stream))
+		if (!new_style_sheet->LoadStyleSheet(stream.get()))
 		{
 			new_style_sheet = nullptr;
 		}
 	}
-
-	stream->RemoveReference();
 	return new_style_sheet;
 }
 

+ 4 - 3
Source/Core/StyleSheetParser.cpp

@@ -482,11 +482,12 @@ int StyleSheetParser::Parse(StyleSheetNode* node, Stream* _stream, const StyleSh
 
 bool StyleSheetParser::ParseProperties(PropertyDictionary& parsed_properties, const String& properties)
 {
-	stream = new StreamMemory((const byte*)properties.c_str(), properties.size());
+	RMLUI_ASSERT(!stream);
+	auto stream_owner = std::make_unique<StreamMemory>((const byte*)properties.c_str(), properties.size());
+	stream = stream_owner.get();
 	PropertySpecificationParser parser(parsed_properties, StyleSheetSpecification::GetPropertySpecification());
 	bool success = ReadProperties(parser);
-	stream->RemoveReference();
-	stream = NULL;
+	stream = nullptr;
 	return success;
 }
 

+ 6 - 9
Source/Core/Template.cpp

@@ -37,13 +37,10 @@ namespace Core {
 
 Template::Template()
 {
-	body = NULL;
 }
 
 Template::~Template()
 {
-	if (body)
-		body->RemoveReference();
 }
 
 const String& Template::GetName() const
@@ -97,18 +94,18 @@ bool Template::Load(Stream* stream)
 	}
 
 	// Create a stream around the header, parse it and store it
-	StreamMemory* header_stream = new StreamMemory((const byte*) head_start,head_end - head_start);
+	auto header_stream = std::make_unique<StreamMemory>((const byte*) head_start,head_end - head_start);
 	header_stream->SetSourceURL(stream->GetSourceURL());
 
-	XMLParser parser(NULL);
-	parser.Parse(header_stream);
+	XMLParser parser(nullptr);
+	parser.Parse(header_stream.get());
 
-	header_stream->RemoveReference();
+	header_stream.reset();
 
 	header = *parser.GetDocumentHeader();
 
 	// Store the body in stream form
-	body = new StreamMemory(body_end - body_start);	
+	body = std::make_unique<StreamMemory>(body_end - body_start);	
 	body->SetSourceURL(stream->GetSourceURL());
 	body->PushBack(body_start, body_end - body_start);
 
@@ -120,7 +117,7 @@ Element* Template::ParseTemplate(Element* element)
 	body->Seek(0, SEEK_SET);
 
 	XMLParser parser(element);
-	parser.Parse(body);
+	parser.Parse(body.get());
 
 	// If theres an inject attribute on the template, 
 	// attempt to find the required element

+ 1 - 4
Source/Core/Template.h

@@ -67,10 +67,7 @@ private:
 	String name;
 	String content;
 	DocumentHeader header;
-	StreamMemory* body;
-
-	String ReadAttribute(const String& string);
-	const String& FindTag(const String& tag, const String& string, bool closing_tag = false);
+	std::unique_ptr<StreamMemory> body;
 };
 
 }

+ 2 - 3
Source/Core/TemplateCache.cpp

@@ -74,11 +74,11 @@ Template* TemplateCache::LoadTemplate(const String& name)
 
 	// Nope, we better load it
 	Template* new_template = NULL;
-	StreamFile* stream = new StreamFile();
+	auto stream = std::make_unique<StreamFile>();
 	if (stream->Open(name))
 	{
 		new_template = new Template();
-		if (!new_template->Load(stream))
+		if (!new_template->Load(stream.get()))
 		{
 			Log::Message(Log::LT_ERROR, "Failed to load template %s.", name.c_str());
 			delete new_template;
@@ -100,7 +100,6 @@ Template* TemplateCache::LoadTemplate(const String& name)
 	{
 		Log::Message(Log::LT_ERROR, "Failed to open template file %s.", name.c_str());		
 	}
-	stream->RemoveReference();
 
 	return new_template;
 }