Browse Source

Remove ReferenceCountable from Event and EventInstancer

Michael Ragazzon 6 years ago
parent
commit
ebe3e3a14a

+ 4 - 3
Include/RmlUi/Core/Event.h

@@ -51,7 +51,7 @@ enum class DefaultActionPhase { None, Target = (int)EventPhase::Target, Bubble =
 	@author Lloyd Weehuizen
  */
 
-class RMLUICORE_API Event : public ReferenceCountable
+class RMLUICORE_API Event : public Releasable
 {
 public:
 	/// Constructor
@@ -113,10 +113,11 @@ public:
 	/// @return The dictionary of parameters
 	const Dictionary* GetParameters() const;
 
-	/// Release this event.
-	virtual void OnReferenceDeactivate() override;
 
 private:
+	/// Release this event.
+	void Release() override;
+
 	/// Project the mouse coordinates to the current element to enable
 	/// interacting with transformed elements.
 	void ProjectMouse(Element* element);

+ 3 - 5
Include/RmlUi/Core/EventInstancer.h

@@ -44,7 +44,7 @@ class Event;
 	@author Lloyd Weehuizen
  */
 
-class RMLUICORE_API EventInstancer : public ReferenceCountable
+class RMLUICORE_API EventInstancer : public Releasable
 {
 public:
 	virtual ~EventInstancer();
@@ -54,17 +54,15 @@ public:
 	/// @param[in] name Name of this event.
 	/// @param[in] parameters Additional parameters for this event.
 	/// @param[in] interruptible If the event propagation can be stopped.
-	virtual Event* InstanceEvent(Element* target, EventId id, const String& type, const Dictionary& parameters, bool interruptible) = 0;
+	virtual UniquePtr<Event> InstanceEvent(Element* target, EventId id, const String& type, const Dictionary& parameters, bool interruptible) = 0;
 
 	/// Releases an event instanced by this instancer.
 	/// @param[in] event The event to release.
 	virtual void ReleaseEvent(Event* event) = 0;
 
+protected:
 	/// Releases this event instancer.
 	virtual void Release() = 0;
-
-private:
-	virtual void OnReferenceDeactivate();
 };
 
 }

+ 2 - 4
Include/RmlUi/Core/EventListenerInstancer.h

@@ -46,7 +46,7 @@ class EventListener;
 	@author Lloyd Weehuizen
  */
 
-class RMLUICORE_API EventListenerInstancer : public ReferenceCountable
+class RMLUICORE_API EventListenerInstancer : public Releasable
 {
 public:
 	virtual ~EventListenerInstancer();
@@ -56,11 +56,9 @@ public:
 	/// @param element Element that triggers the events.
 	virtual EventListener* InstanceEventListener(const String& value, Element* element) = 0;
 
+protected:
 	/// Releases this event listener instancer.
 	virtual void Release() = 0;
-
-protected:
-	virtual void OnReferenceDeactivate();
 };
 
 }

+ 3 - 3
Include/RmlUi/Core/Factory.h

@@ -153,18 +153,18 @@ public:
 	/// Registers an instancer for all events.
 	/// @param[in] instancer The instancer to be called.
 	/// @return The registered instanced on success, NULL on failure.
-	static EventInstancer* RegisterEventInstancer(EventInstancer* instancer);
+	static EventInstancer* RegisterEventInstancer(UniquePtr<EventInstancer> instancer);
 	/// Instance and event object
 	/// @param[in] target Target element of this event.
 	/// @param[in] name Name of this event.
 	/// @param[in] parameters Additional parameters for this event.
 	/// @param[in] interruptible If the event propagation can be stopped.
 	/// @return The instanced event.
-	static Event* InstanceEvent(Element* target, EventId id, const String& type, const Dictionary& parameters, bool interruptible);
+	static UniquePtr<Event> InstanceEvent(Element* target, EventId id, const String& type, const Dictionary& parameters, bool interruptible);
 
 	/// Register the instancer to be used for all event listeners.
 	/// @return The registered instancer on success, NULL on failure.
-	static EventListenerInstancer* RegisterEventListenerInstancer(EventListenerInstancer* instancer);
+	static EventListenerInstancer* RegisterEventListenerInstancer(UniquePtr<EventListenerInstancer> instancer);
 	/// Instance an event listener with the given string. This is used for instancing listeners for the on* events from
 	/// RML.
 	/// @param[in] value The parameters to the event listener.

+ 1 - 3
Samples/basic/animation/src/main.cpp

@@ -360,9 +360,7 @@ int main(int RMLUI_UNUSED_PARAMETER(argc), char** RMLUI_UNUSED_PARAMETER(argv))
 	Input::SetContext(context);
 	shell_renderer->SetContext(context);
 
-	EventInstancer* event_instancer = new EventInstancer();
-	Rml::Core::Factory::RegisterEventListenerInstancer(event_instancer);
-	event_instancer->RemoveReference();
+	Rml::Core::Factory::RegisterEventListenerInstancer(Rml::Core::UniquePtr<Rml::Core::EventListenerInstancer>(new EventInstancer));
 
 	Shell::LoadFonts("assets/");
 

+ 1 - 3
Samples/basic/benchmark/src/main.cpp

@@ -321,9 +321,7 @@ int main(int RMLUI_UNUSED_PARAMETER(argc), char** RMLUI_UNUSED_PARAMETER(argv))
 	Input::SetContext(context);
 	shell_renderer->SetContext(context);
 
-	EventInstancer* event_instancer = new EventInstancer();
-	Rml::Core::Factory::RegisterEventListenerInstancer(event_instancer);
-	event_instancer->RemoveReference();
+	Rml::Core::Factory::RegisterEventListenerInstancer(Rml::Core::UniquePtr<Rml::Core::EventListenerInstancer>(new EventInstancer));
 
 	Shell::LoadFonts("assets/");
 

+ 2 - 2
Samples/invaders/src/Event.h

@@ -42,10 +42,10 @@ public:
 	virtual ~Event();
 
 	/// Sends the event value through to Invader's event processing system.
-	virtual void ProcessEvent(Rml::Core::Event& event) override;
+	void ProcessEvent(Rml::Core::Event& event) override;
 
 	/// Destroys the event.
-	virtual void OnDetach(Rml::Core::Element* element) override;
+	void OnDetach(Rml::Core::Element* element) override;
 
 private:
 	Rml::Core::String value;

+ 3 - 2
Samples/invaders/src/EventInstancer.h

@@ -42,10 +42,11 @@ public:
 	virtual ~EventInstancer();
 
 	/// Instances a new event handle for Invaders.
-	virtual Rml::Core::EventListener* InstanceEventListener(const Rml::Core::String& value, Rml::Core::Element* element);
+	Rml::Core::EventListener* InstanceEventListener(const Rml::Core::String& value, Rml::Core::Element* element) override;
 
+protected:
 	/// Destroys the instancer.
-	virtual void Release();
+	void Release() override;
 };
 
 #endif

+ 1 - 3
Samples/invaders/src/main.cpp

@@ -134,9 +134,7 @@ int main(int RMLUI_UNUSED_PARAMETER(argc), char** RMLUI_UNUSED_PARAMETER(argv))
 	HighScores::Initialise();
 
 	// Initialise the event instancer and handlers.
-	EventInstancer* event_instancer = new EventInstancer();
-	Rml::Core::Factory::RegisterEventListenerInstancer(event_instancer);
-	event_instancer->RemoveReference();
+	Rml::Core::Factory::RegisterEventListenerInstancer(Rml::Core::UniquePtr<Rml::Core::EventListenerInstancer>(new EventInstancer));
 
 	EventManager::RegisterEventHandler("start_game", new EventHandlerStartGame());
 	EventManager::RegisterEventHandler("high_score", new EventHandlerHighScore());

+ 1 - 1
Source/Core/Event.cpp

@@ -132,7 +132,7 @@ const Dictionary* Event::GetParameters() const
 	return &parameters;
 }
 
-void Event::OnReferenceDeactivate()
+void Event::Release()
 {
 	instancer->ReleaseEvent(this);
 }

+ 2 - 2
Source/Core/EventDispatcher.cpp

@@ -110,7 +110,7 @@ void EventDispatcher::DetachAllEvents()
 
 bool EventDispatcher::DispatchEvent(Element* target_element, EventId id, const String& type, const Dictionary& parameters, bool interruptible, bool bubbles, DefaultActionPhase default_action_phase)
 {
-	Event* event = Factory::InstanceEvent(target_element, id, type, parameters, interruptible);
+	UniquePtr<Event> event = Factory::InstanceEvent(target_element, id, type, parameters, interruptible);
 	if (!event)
 		return false;
 
@@ -156,7 +156,7 @@ bool EventDispatcher::DispatchEvent(Element* target_element, EventId id, const S
 	}
 
 	bool propagating = event->IsPropagating();
-	event->RemoveReference();
+
 	return propagating;
 }
 

+ 0 - 5
Source/Core/EventInstancer.cpp

@@ -36,10 +36,5 @@ EventInstancer::~EventInstancer()
 {
 }
 
-void EventInstancer::OnReferenceDeactivate()
-{
-	Release();
-}
-
 }
 }

+ 2 - 2
Source/Core/EventInstancerDefault.cpp

@@ -41,9 +41,9 @@ EventInstancerDefault::~EventInstancerDefault()
 {
 }
 
-Event* EventInstancerDefault::InstanceEvent(Element* target, EventId id, const String& type, const Dictionary& parameters, bool interruptible)
+UniquePtr<Event> EventInstancerDefault::InstanceEvent(Element* target, EventId id, const String& type, const Dictionary& parameters, bool interruptible)
 {
-	return new Event(target, id, type, parameters, interruptible);
+	return UniquePtr<Event>(new Event(target, id, type, parameters, interruptible));
 }
 
 // Releases an event instanced by this instancer.

+ 3 - 3
Source/Core/EventInstancerDefault.h

@@ -51,14 +51,14 @@ public:
 	/// @param[in] name Name of this event.
 	/// @param[in] parameters Additional parameters for this event.
 	/// @param[in] interruptible If the event propagation can be stopped.
-	virtual Event* InstanceEvent(Element* target, EventId id, const String& type, const Dictionary& parameters, bool interruptible) override;
+	UniquePtr<Event> InstanceEvent(Element* target, EventId id, const String& type, const Dictionary& parameters, bool interruptible) override;
 
 	/// Releases an event instanced by this instancer.
 	/// @param[in] event The event to release.
-	virtual void ReleaseEvent(Event* event) override;
+	void ReleaseEvent(Event* event) override;
 
 	/// Releases this event instancer.
-	virtual void Release() override;
+	void Release() override;
 };
 
 }

+ 0 - 5
Source/Core/EventListenerInstancer.cpp

@@ -36,10 +36,5 @@ EventListenerInstancer::~EventListenerInstancer()
 {
 }
 
-void EventListenerInstancer::OnReferenceDeactivate()
-{
-	Release();
-}
-
 }
 }

+ 19 - 33
Source/Core/Factory.cpp

@@ -70,10 +70,10 @@ static FontEffectInstancerMap font_effect_instancers;
 static SharedPtr<ContextInstancer> context_instancer;
 
 // The event instancer
-static EventInstancer* event_instancer = NULL;
+static UniquePtr<EventInstancer> event_instancer;
 
 // Event listener instancer.
-static EventListenerInstancer* event_listener_instancer = NULL;
+static UniquePtr<EventListenerInstancer> event_listener_instancer;
 
 Factory::Factory()
 {
@@ -90,12 +90,12 @@ bool Factory::Initialise()
 		context_instancer = std::make_shared<ContextInstancerDefault>();
 
 	// Bind default event instancer
-	if (event_instancer == NULL)
-		event_instancer = new EventInstancerDefault();
+	if (!event_instancer)
+		event_instancer = UniquePtr<EventInstancer>(new EventInstancerDefault);
 
 	// No default event listener instancer
-	if (event_listener_instancer == NULL)
-		event_listener_instancer = NULL;
+	if (!event_listener_instancer)
+		event_listener_instancer = nullptr;
 
 	// Bind the default element instancers
 	RegisterElementInstancer("*", ElementInstancerPtr(new ElementInstancerGeneric< Element >));
@@ -132,13 +132,9 @@ void Factory::Shutdown()
 
 	context_instancer.reset();
 
-	if (event_listener_instancer)
-		event_listener_instancer->RemoveReference();
-	event_listener_instancer = NULL;
+	event_listener_instancer.reset();
 
-	if (event_instancer)
-		event_instancer->RemoveReference();
-	event_instancer = NULL;
+	event_instancer.reset();
 
 	XMLParser::ReleaseHandlers();
 }
@@ -389,37 +385,27 @@ void Factory::ClearTemplateCache()
 }
 
 // Registers an instancer for all RmlEvents
-EventInstancer* Factory::RegisterEventInstancer(EventInstancer* instancer)
+EventInstancer* Factory::RegisterEventInstancer(UniquePtr<EventInstancer> instancer)
 {
-	instancer->AddReference();
-
-	if (event_instancer)
-		event_instancer->RemoveReference();
-
-	event_instancer = instancer;
-	return instancer;
+	event_instancer = std::move(instancer);
+	return event_instancer.get();
 }
 
 // Instance an event object.
-Event* Factory::InstanceEvent(Element* target, EventId id, const String& type, const Dictionary& parameters, bool interruptible)
+UniquePtr<Event> Factory::InstanceEvent(Element* target, EventId id, const String& type, const Dictionary& parameters, bool interruptible)
 {
-	Event* event = event_instancer->InstanceEvent(target, id, type, parameters, interruptible);
-	if (event != NULL)
-		event->instancer = event_instancer;
+	UniquePtr<Event> event = event_instancer->InstanceEvent(target, id, type, parameters, interruptible);
+	if (event)
+		event->instancer = event_instancer.get();
 
 	return event;
 }
 
 // Register an instancer for all event listeners
-EventListenerInstancer* Factory::RegisterEventListenerInstancer(EventListenerInstancer* instancer)
+EventListenerInstancer* Factory::RegisterEventListenerInstancer(UniquePtr<EventListenerInstancer> instancer)
 {
-	instancer->AddReference();
-
-	if (event_listener_instancer)
-		event_listener_instancer->RemoveReference();
-
-	event_listener_instancer = instancer;
-	return instancer;
+	event_listener_instancer = std::move(instancer);
+	return event_listener_instancer.get();
 }
 
 // Instance an event listener with the given string
@@ -429,7 +415,7 @@ EventListener* Factory::InstanceEventListener(const String& value, Element* elem
 	if (event_listener_instancer)
 		return event_listener_instancer->InstanceEventListener(value, element);
 
-	return NULL;
+	return nullptr;
 }
 
 }