From ed18ed376bcc71938ad73872148a3146814d831f Mon Sep 17 00:00:00 2001 From: Lane Kolbly Date: Thu, 23 Dec 2021 09:39:39 -0600 Subject: host: Throw exception when accessing properties with incorrect type --- host/include/uhd/property_tree.hpp | 17 +++++++++++++---- host/include/uhd/property_tree.ipp | 19 +++++++++++++------ 2 files changed, 26 insertions(+), 10 deletions(-) (limited to 'host/include') diff --git a/host/include/uhd/property_tree.hpp b/host/include/uhd/property_tree.hpp index 9fc9b3b97..a1d292858 100644 --- a/host/include/uhd/property_tree.hpp +++ b/host/include/uhd/property_tree.hpp @@ -17,6 +17,15 @@ namespace uhd { +/*! + * A non-templated class which exists solely so we can + * dynamic_cast between properties. + */ +class UHD_API property_iface { +public: + virtual ~property_iface() = default; +}; + /*! * A templated property interface for holding the state * associated with a property in a uhd::property_tree @@ -65,7 +74,7 @@ namespace uhd { * - T must have an assignment operator */ template -class UHD_API_HEADER property : uhd::noncopyable +class UHD_API_HEADER property : uhd::noncopyable, public property_iface { public: typedef std::function subscriber_type; @@ -245,14 +254,14 @@ public: private: //! Internal pop function - virtual std::shared_ptr _pop(const fs_path& path) = 0; + virtual std::shared_ptr _pop(const fs_path& path) = 0; //! Internal create property with wild-card type virtual void _create(const fs_path& path, - const std::shared_ptr& prop) = 0; + const std::shared_ptr& prop) = 0; //! Internal access property with wild-card type - virtual std::shared_ptr& _access(const fs_path& path) const = 0; + virtual std::shared_ptr& _access(const fs_path& path) const = 0; }; } // namespace uhd diff --git a/host/include/uhd/property_tree.ipp b/host/include/uhd/property_tree.ipp index 273227174..e9dd3072e 100644 --- a/host/include/uhd/property_tree.ipp +++ b/host/include/uhd/property_tree.ipp @@ -9,8 +9,8 @@ #pragma once #include -#include #include +#include /*********************************************************************** * Implement templated property impl @@ -179,21 +179,28 @@ namespace uhd { template property& property_tree::create(const fs_path& path, coerce_mode_t coerce_mode) { - this->_create(path, std::make_shared >(coerce_mode)); + this->_create(path, std::make_shared>(coerce_mode)); return this->access(path); } template property& property_tree::access(const fs_path& path) { - return *std::static_pointer_cast >( - this->_access(path)); + auto ptr = std::dynamic_pointer_cast>(this->_access(path)); + if (!ptr) { + throw uhd::type_error("Property " + path + " exists, but was accessed with wrong type"); + } + return *ptr; } template -typename std::shared_ptr > property_tree::pop(const fs_path& path) +typename std::shared_ptr> property_tree::pop(const fs_path& path) { - return std::static_pointer_cast >(this->_pop(path)); + auto ptr = std::dynamic_pointer_cast>(this->_pop(path)); + if (!ptr) { + throw uhd::type_error("Property " + path + " exists, but was accessed with wrong type"); + } + return ptr; } } // namespace uhd -- cgit v1.2.3