From bcc6eaff82162d21e6b7e3b3de11e44cd5bc7680 Mon Sep 17 00:00:00 2001 From: Emiel Bruijntjes Date: Mon, 14 Apr 2014 16:04:49 +0200 Subject: [PATCH] added const access for operator[] (issue #7) --- include/array.h | 12 +++++++++++- include/table.h | 10 ++++++++++ src/array.cpp | 2 +- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/include/array.h b/include/array.h index 4eb5c30..ddc07b7 100644 --- a/include/array.h +++ b/include/array.h @@ -110,7 +110,7 @@ public: * @param index field index * @return Field */ - const Field &get(uint8_t index); + const Field &get(uint8_t index) const; /** * Get number of elements on this array @@ -141,6 +141,16 @@ public: { return ArrayFieldProxy(this, index); } + + /** + * Get a const field + * @param index field index + * @return Field + */ + const Field &operator[](uint8_t index) const + { + return get(index); + } /** * Write encoded payload to the given buffer. diff --git a/include/table.h b/include/table.h index f7682e5..997c210 100644 --- a/include/table.h +++ b/include/table.h @@ -122,6 +122,16 @@ public: return AssociativeFieldProxy(this, name); } + /** + * Get a const field + * + * @param name field name + */ + const Field &operator[](const std::string& name) const + { + return get(name); + } + /** * Write encoded payload to the given buffer. * @param buffer diff --git a/src/array.cpp b/src/array.cpp index 175c168..d04c865 100644 --- a/src/array.cpp +++ b/src/array.cpp @@ -58,7 +58,7 @@ Array::Array(const Array &array) * @param index field index * @return Field */ -const Field &Array::get(uint8_t index) +const Field &Array::get(uint8_t index) const { // used if index does not exist static ShortString empty;