name: Make 'at' method to check if requested components is in bounds
Change-Id: I28744a694e1ead48892bb3cfe918b7132b769a5a
diff --git a/src/name.hpp b/src/name.hpp
index 96d43c0..6d7582f 100644
--- a/src/name.hpp
+++ b/src/name.hpp
@@ -346,10 +346,21 @@
{
return get(i);
}
-
+
+ /**
+ * @brief Get component at the specified index
+ *
+ * Unlike get() and operator[] methods, at() checks for out of bounds
+ * and will throw Name::Error when it happens
+ *
+ * @throws Name::Error if index out of bounds
+ */
const Component&
at(ssize_t i) const
{
+ if ((i >= 0 && i >= size()) || (i < 0 && i < -size()))
+ throw Error("Requested component does not exist (out of bounds)");
+
return get(i);
}