From ae2dc0d92ed34c0d1bb8fae93d5077c02db0da4a Mon Sep 17 00:00:00 2001
From: ilor <kailoran@gmail.com>
Date: Wed, 9 Mar 2011 10:09:44 +0100
Subject: [PATCH] MatchVector indexing operators and size() member

---
 libwccl/values/matchvector.cpp |  9 +++++++++
 libwccl/values/matchvector.h   | 18 ++++++++++++++++++
 2 files changed, 27 insertions(+)

diff --git a/libwccl/values/matchvector.cpp b/libwccl/values/matchvector.cpp
index 668267d..87a0f35 100644
--- a/libwccl/values/matchvector.cpp
+++ b/libwccl/values/matchvector.cpp
@@ -1,6 +1,7 @@
 #include <libwccl/values/matchvector.h>
 #include <libpwrutils/foreach.h>
 #include <sstream>
+#include <libwccl/exception.h>
 
 namespace Wccl {
 
@@ -74,5 +75,13 @@ void MatchVector::append(const boost::shared_ptr<Match> &m)
 	matches_.push_back(m);
 }
 
+const boost::shared_ptr<Match>& MatchVector::submatch(size_t idx)
+{
+	if (idx < matches_.size()) {
+		return matches_[size];
+	} else {
+		throw Wccl::WcclError("Match vector index out of range");
+	}
+}
 
 } /* end ns Wccl */
diff --git a/libwccl/values/matchvector.h b/libwccl/values/matchvector.h
index c4912de..bcf2650 100644
--- a/libwccl/values/matchvector.h
+++ b/libwccl/values/matchvector.h
@@ -32,6 +32,24 @@ public:
 	/// Append a sub-match
 	void append(const boost::shared_ptr<Match>& m);
 
+	/// Size (number of direct sub-matches)
+	size_t size() const {
+		return matches_.size();
+	}
+
+	/**
+	 * Submatch accesor with bounds checking, throws if out of bounds
+	 */
+	const boost::shared_ptr<Match>& submatch(size_t idx);
+
+	/**
+	 * Submatch indexing operator. Per C++ container tradition, no bounds
+	 * checking is done.
+	 */
+	const boost::shared_ptr<Match>& operator[](size_t idx) const {
+		return matches_[idx];
+	}
+
 private:
 	std::vector< boost::shared_ptr<Match> > matches_;
 };
-- 
GitLab