aboutsummaryrefslogtreecommitdiffstats
path: root/host/lib/rfnoc/nocscript/function_table.cpp
diff options
context:
space:
mode:
authorMartin Braun <martin.braun@ettus.com>2016-08-01 18:17:41 -0700
committerMartin Braun <martin.braun@ettus.com>2016-08-09 12:42:52 -0700
commit3bf4b000f7d9a7f4af82c21753556ede7e8df6e3 (patch)
tree2228d7eb58c4d83d91192cb9b6a908e4e49f6317 /host/lib/rfnoc/nocscript/function_table.cpp
parentc5b076173e2d866f3ee99c113a37183c5ec20f0b (diff)
downloaduhd-3bf4b000f7d9a7f4af82c21753556ede7e8df6e3.tar.gz
uhd-3bf4b000f7d9a7f4af82c21753556ede7e8df6e3.tar.bz2
uhd-3bf4b000f7d9a7f4af82c21753556ede7e8df6e3.zip
Merging RFNoC support for X310
Diffstat (limited to 'host/lib/rfnoc/nocscript/function_table.cpp')
-rw-r--r--host/lib/rfnoc/nocscript/function_table.cpp113
1 files changed, 113 insertions, 0 deletions
diff --git a/host/lib/rfnoc/nocscript/function_table.cpp b/host/lib/rfnoc/nocscript/function_table.cpp
new file mode 100644
index 000000000..bebceb8dc
--- /dev/null
+++ b/host/lib/rfnoc/nocscript/function_table.cpp
@@ -0,0 +1,113 @@
+//
+// Copyright 2015 Ettus Research LLC
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program. If not, see <http://www.gnu.org/licenses/>.
+//
+
+#include "function_table.hpp"
+#include "basic_functions.hpp"
+#include <boost/bind.hpp>
+#include <boost/foreach.hpp>
+#include <boost/format.hpp>
+#include <map>
+
+using namespace uhd::rfnoc::nocscript;
+
+class function_table_impl : public function_table
+{
+ public:
+ struct function_info {
+ expression::type_t return_type;
+ function_ptr function;
+
+ function_info() {};
+ function_info(const expression::type_t return_type_, const function_ptr &function_)
+ : return_type(return_type_), function(function_)
+ {};
+ };
+ // Should be an unordered_map... sigh, we'll get to C++11 someday.
+ typedef std::map<std::string, std::map<expression_function::argtype_list_type, function_info> > table_type;
+
+ /************************************************************************
+ * Structors
+ ***********************************************************************/
+ function_table_impl()
+ {
+ _REGISTER_ALL_FUNCS();
+ }
+
+ ~function_table_impl() {};
+
+
+ /************************************************************************
+ * Interface implementation
+ ***********************************************************************/
+ bool function_exists(const std::string &name) const {
+ return bool(_table.count(name));
+ }
+
+ bool function_exists(
+ const std::string &name,
+ const expression_function::argtype_list_type &arg_types
+ ) const {
+ table_type::const_iterator it = _table.find(name);
+ return (it != _table.end()) and bool(it->second.count(arg_types));
+ }
+
+ expression::type_t get_type(
+ const std::string &name,
+ const expression_function::argtype_list_type &arg_types
+ ) const {
+ table_type::const_iterator it = _table.find(name);
+ if (it == _table.end() or (it->second.find(arg_types) == it->second.end())) {
+ throw uhd::syntax_error(str(
+ boost::format("Unable to retrieve return value for function %s")
+ % expression_function::to_string(name, arg_types)
+ ));
+ }
+ return it->second.find(arg_types)->second.return_type;
+ }
+
+ expression_literal eval(
+ const std::string &name,
+ const expression_function::argtype_list_type &arg_types,
+ expression_container::expr_list_type &arguments
+ ) {
+ if (not function_exists(name, arg_types)) {
+ throw uhd::syntax_error(str(
+ boost::format("Cannot eval() function %s, not a known signature")
+ % expression_function::to_string(name, arg_types)
+ ));
+ }
+
+ return _table[name][arg_types].function(arguments);
+ }
+
+ void register_function(
+ const std::string &name,
+ const function_table::function_ptr &ptr,
+ const expression::type_t return_type,
+ const expression_function::argtype_list_type &sig
+ ) {
+ _table[name][sig] = function_info(return_type, ptr);
+ }
+
+ private:
+ table_type _table;
+};
+
+function_table::sptr function_table::make()
+{
+ return sptr(new function_table_impl());
+}