From 802dfeba295781ee17632ec14f1a1f235b6b53d1 Mon Sep 17 00:00:00 2001 From: Joshua Scott Date: Fri, 20 Apr 2018 01:41:33 +0100 Subject: [PATCH] dml: Fix some const-correctness problems --- include/ki/dml/Record.h | 20 ++++++++++++++++++-- src/dml/Record.cpp | 9 ++++++++- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/include/ki/dml/Record.h b/include/ki/dml/Record.h index f1e1079..c0b52b2 100644 --- a/include/ki/dml/Record.h +++ b/include/ki/dml/Record.h @@ -34,7 +34,23 @@ namespace dml return m_field_map.at(name)->is_type(); } - FieldBase *get_field(std::string name) const; + FieldBase *get_field(std::string name); + const FieldBase *get_field(std::string name) const; + + /** + * Returns a previously added field with the specified name + * and type. + * + * If the field was not previously added, then a nullptr is + * returned. + */ + template + Field *get_field(std::string name) + { + if (has_field(name)) + return dynamic_cast *>(m_field_map.at(name)); + return nullptr; + } /** * Returns a previously added field with the specified name @@ -44,7 +60,7 @@ namespace dml * returned. */ template - Field *get_field(std::string name) const + const Field *get_field(std::string name) const { if (has_field(name)) return dynamic_cast *>(m_field_map.at(name)); diff --git a/src/dml/Record.cpp b/src/dml/Record.cpp index 6cb7c0e..7fa7d5d 100644 --- a/src/dml/Record.cpp +++ b/src/dml/Record.cpp @@ -30,7 +30,14 @@ namespace dml return m_field_map.count(name); } - FieldBase *Record::get_field(std::string name) const + FieldBase *Record::get_field(std::string name) + { + if (has_field(name)) + return m_field_map.at(name); + return nullptr; + } + + const FieldBase *Record::get_field(std::string name) const { if (has_field(name)) return m_field_map.at(name);