From 7623de94e9566392ad69b2ca31dcf6453f816571 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Andr=C3=A9=20Nusser?= <andre.nusser@googlemail.com>
Date: Tue, 18 Feb 2020 11:53:40 +0100
Subject: Add two get(event_id) function to EventsDS.

Still needs testing and review.
---
 src/events_ds.cc | 14 ++++++++++++++
 src/events_ds.h  | 30 ++++++++++++++++++++++++++++++
 2 files changed, 44 insertions(+)

diff --git a/src/events_ds.cc b/src/events_ds.cc
index 097ddc1..3e1573f 100644
--- a/src/events_ds.cc
+++ b/src/events_ds.cc
@@ -56,6 +56,20 @@ void EventsDS::remove(EventID event_id)
 	id_to_info.remove(event_id);
 }
 
+Event* EventsDS::get(EventID event_id)
+{
+	auto const& info = id_to_info.get(event_id);
+
+	// add new event types here
+	switch (info.type)
+	{
+	case Event::Type::SampleEvent:
+		return &getSample<SampleEvent>(info);
+	default:
+		assert(false);
+	}
+}
+
 std::size_t EventsDS::numberOfEvents(channel_t ch) const
 {
 	auto& channel_data = channel_data_array[ch];
diff --git a/src/events_ds.h b/src/events_ds.h
index dd13ec0..2e8fa43 100644
--- a/src/events_ds.h
+++ b/src/events_ds.h
@@ -67,6 +67,15 @@ public:
 	//! Removes the event with id being event_id.
 	void remove(EventID event_id);
 
+	//! Returns a reference to the element with id begin event_id. Note that to
+	//! get an event with this function, one has to know its type!
+	template <typename T>
+	T& get(EventID event_id);
+
+	//! Returns a pointer to the event with id begin event_id. As Event has a
+	//! member variable that carries the type, it can be then casted appropriately.
+	Event* get(EventID event_id);
+
 	//! Returns the number of all events of a certain channel.
 	std::size_t numberOfEvents(channel_t ch) const;
 
@@ -129,6 +138,9 @@ private:
 	InstrumentID current_groups_instrument_id;
 
 	void removeGroup(EventGroupID group_id, InstrumentID instrument_id = InstrumentID());
+
+	template <typename T>
+	T& getSample(EventInfo const& info);
 };
 
 template <typename T, typename... Args>
@@ -156,6 +168,12 @@ T& EventsDS::emplace(channel_t ch, Args&&... args)
 	}
 }
 
+template <typename T>
+T& EventsDS::get(EventID event_id)
+{
+	return getSample<T>(id_to_info.get(event_id));
+}
+
 template <typename T>
 ContainerRange<std::vector<T>> EventsDS::iterateOver(channel_t ch)
 {
@@ -168,3 +186,15 @@ ContainerRange<std::vector<T>> EventsDS::iterateOver(channel_t ch)
 		return ContainerRange<std::vector<T>>(sample_events.begin(), sample_events.end());
 	}
 }
+
+template <typename T>
+T& EventsDS::getSample(EventInfo const& info)
+{
+	// add new event types here
+	static_assert(std::is_same<T, SampleEvent>::value);
+
+	if (std::is_same<T, SampleEvent>::value)
+	{
+		return channel_data_array[info.ch].sample_events[info.channel_event_index];
+	}
+}
-- 
cgit v1.2.3