From a40105e1b9c57f83080ba255b0ec0b61243e9357 Mon Sep 17 00:00:00 2001 From: Guido Kanschat Date: Fri, 16 May 2014 07:26:04 +0000 Subject: [PATCH] add trial access git-svn-id: https://svn.dealii.org/trunk@32918 0785d39b-7218-0410-832d-ea1e28bc413d --- deal.II/include/deal.II/algorithms/any_data.h | 65 +++++++++++++++++-- 1 file changed, 61 insertions(+), 4 deletions(-) diff --git a/deal.II/include/deal.II/algorithms/any_data.h b/deal.II/include/deal.II/algorithms/any_data.h index d5a92111d9..8eab3cb3f1 100644 --- a/deal.II/include/deal.II/algorithms/any_data.h +++ b/deal.II/include/deal.II/algorithms/any_data.h @@ -52,20 +52,53 @@ class AnyData : /** * @brief Access to stored data object by name. + * + * Find the object with given name, try to convert it to + * type and return it. This function throws an exception + * if either the name does not exist or if the conversion + * fails. If such an exception is not desired, use try_read() + * instead. */ template type entry (const std::string& name); - /// Read-only access to stored data object by name. + /** + * @brief Read-only access to stored data object by name. + * + * Find the object with given name, try to convert it to + * type and return it. This function throws an exception + * if either the name does not exist or if the conversion + * fails. If such an exception is not desired, use try_read() + * instead. + */ template const type entry (const std::string& name) const; - /// Dedicated read only access by name. + /** + * @brief Dedicated read only access by name. + * + * For a constant object, this function equals entry(). For a + * non-const object, it forces read only access to the data. In + * particular, it throws an exception if the object is not found + * or cannot be converted to type. If such an exception is not + * desired, use try_read() instead. + */ template const type read (const std::string& name) const; + /** + * @brief Dedicated read only access by name without exceptions. + * + * This function tries to find the name in the list and return a + * pointer to the associated object. If either the name is not + * found or the object cannot be converted to the return type, a + * null pointer is returned. + */ + template + const type * try_read (const std::string& name) const; + /** - * @brief Access to stored data object by index. + * Access to stored data object by index. */ template type entry (const unsigned int i); @@ -81,7 +114,12 @@ class AnyData : /// Name of object at index. const std::string &name(const unsigned int i) const; - /// Find index of a named object + /** + * @brief Find index of a named object + * + * Try to find the objecty and return its index in the list. Throw + * an exception if the object has not been found. + */ unsigned int find(const std::string &name) const; /// Find out if object is of a certain type @@ -221,6 +259,25 @@ AnyData::read(const std::string& n) const } +template +inline +const type* +AnyData::try_read(const std::string& n) const +{ + // Try to find name + std::vector::const_iterator it = + std::find(names.begin(), names.end(), n); + // Return null pointer if not found + if (it == names.end()) + return 0; + + // Compute index and return casted pointer + unsigned int i=it-names.begin(); + const type* p = boost::any_cast(&data[i]); + return *p; +} + + template inline void -- 2.39.5