diff --git a/libwccl/values/matchvector.cpp b/libwccl/values/matchvector.cpp index 668267d9c2b19106f1cc3bcb29e305167925607d..87a0f352aa537fbbac14a53488158c884423e4a0 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 c4912ded60042f7a4e4f9abb09497b9ebdd7cdb8..bcf26505db95f6cc343701d6c0256d02c49b660f 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_; };