aboutsummaryrefslogtreecommitdiffstats
path: root/host/lib
diff options
context:
space:
mode:
authorLane Kolbly <lane.kolbly@ni.com>2020-06-22 18:06:38 -0500
committerAaron Rossetto <aaron.rossetto@ni.com>2020-06-25 13:36:08 -0500
commitd6cb55ef08f51c9ebef57e5bbcb95b03a65972a9 (patch)
tree8b8f1350d9d45d513f00e2b8e836a52600d27e66 /host/lib
parente9ae5fb949903cce1720b75b894401930cce1ebe (diff)
downloaduhd-d6cb55ef08f51c9ebef57e5bbcb95b03a65972a9.tar.gz
uhd-d6cb55ef08f51c9ebef57e5bbcb95b03a65972a9.tar.bz2
uhd-d6cb55ef08f51c9ebef57e5bbcb95b03a65972a9.zip
uhd: Create discoverable feature registry implementation
Classes which want to implement discoverable_feature can simply inherit from this registry and get access to an ergonomic map-backed registry of features.
Diffstat (limited to 'host/lib')
-rw-r--r--host/lib/CMakeLists.txt1
-rw-r--r--host/lib/features/CMakeLists.txt12
-rw-r--r--host/lib/features/discoverable_feature_registry.cpp34
-rw-r--r--host/lib/include/uhdlib/features/discoverable_feature_registry.hpp42
4 files changed, 89 insertions, 0 deletions
diff --git a/host/lib/CMakeLists.txt b/host/lib/CMakeLists.txt
index c5046420a..c0b39faea 100644
--- a/host/lib/CMakeLists.txt
+++ b/host/lib/CMakeLists.txt
@@ -83,6 +83,7 @@ LIBUHD_REGISTER_COMPONENT("DPDK" ENABLE_DPDK ON "ENABLE_MPMD;DPDK_FOUND" OFF OFF
########################################################################
INCLUDE_SUBDIRECTORY(include)
INCLUDE_SUBDIRECTORY(cal)
+INCLUDE_SUBDIRECTORY(features)
INCLUDE_SUBDIRECTORY(ic_reg_maps)
INCLUDE_SUBDIRECTORY(types)
INCLUDE_SUBDIRECTORY(convert)
diff --git a/host/lib/features/CMakeLists.txt b/host/lib/features/CMakeLists.txt
new file mode 100644
index 000000000..2cdd1dd56
--- /dev/null
+++ b/host/lib/features/CMakeLists.txt
@@ -0,0 +1,12 @@
+#
+# Copyright 2020 Ettus Research, a National Instruments Brand
+#
+# SPDX-License-Identifier: GPL-3.0-or-later
+#
+
+########################################################################
+# This file included, use CMake directory variables
+########################################################################
+LIBUHD_APPEND_SOURCES(
+ ${CMAKE_CURRENT_SOURCE_DIR}/discoverable_feature_registry.cpp
+)
diff --git a/host/lib/features/discoverable_feature_registry.cpp b/host/lib/features/discoverable_feature_registry.cpp
new file mode 100644
index 000000000..5bed6a06e
--- /dev/null
+++ b/host/lib/features/discoverable_feature_registry.cpp
@@ -0,0 +1,34 @@
+//
+// Copyright 2020 Ettus Research, a National Instruments Brand
+//
+// SPDX-License-Identifier: GPL-3.0-or-later
+//
+
+#include <uhd/exception.hpp>
+#include <uhd/features/discoverable_feature.hpp>
+#include <uhdlib/features/discoverable_feature_registry.hpp>
+#include <vector>
+
+namespace uhd { namespace features {
+
+std::vector<std::string>
+discoverable_feature_registry::enumerate_features()
+{
+ std::vector<std::string> features;
+ for (auto& entry : _features) {
+ features.push_back(entry.second->get_feature_name());
+ }
+ return features;
+}
+
+discoverable_feature::sptr discoverable_feature_registry::get_feature_ptr(
+ discoverable_feature::feature_id_t feature_id)
+{
+ auto it = _features.find(feature_id);
+ if (it == _features.end()) {
+ return discoverable_feature::sptr();
+ }
+ return it->second;
+}
+
+}} // namespace uhd::features
diff --git a/host/lib/include/uhdlib/features/discoverable_feature_registry.hpp b/host/lib/include/uhdlib/features/discoverable_feature_registry.hpp
new file mode 100644
index 000000000..f27f1a4f1
--- /dev/null
+++ b/host/lib/include/uhdlib/features/discoverable_feature_registry.hpp
@@ -0,0 +1,42 @@
+//
+// Copyright 2020 Ettus Research, a National Instruments Brand
+//
+// SPDX-License-Identifier: GPL-3.0-or-later
+//
+
+#pragma once
+
+#include <uhd/exception.hpp>
+#include <uhd/features/discoverable_feature.hpp>
+#include <uhd/features/discoverable_feature_getter_iface.hpp>
+#include <map>
+#include <memory>
+#include <vector>
+
+namespace uhd { namespace features {
+
+/*! Map-based registry to implement discoverable_feature_getter_iface
+ */
+class discoverable_feature_registry : public virtual discoverable_feature_getter_iface
+{
+public:
+ virtual ~discoverable_feature_registry() = default;
+
+ std::vector<std::string> enumerate_features() override;
+
+ template <typename T>
+ void register_feature(std::shared_ptr<T> feature)
+ {
+ if (!_features.emplace(T::get_feature_id(), feature).second) {
+ UHD_ASSERT_THROW(false);
+ }
+ }
+
+private:
+ discoverable_feature::sptr get_feature_ptr(
+ discoverable_feature::feature_id_t feature_id) override;
+
+ std::map<discoverable_feature::feature_id_t, discoverable_feature::sptr> _features;
+};
+
+}} // namespace uhd::features