aboutsummaryrefslogtreecommitdiffstats
path: root/host/lib/convert/convert_impl.cpp
blob: 6a5a1465d97c7d133d344f1958f6aba371eb8e3e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
//
// Copyright 2011 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 <uhd/convert.hpp>
#include <uhd/utils/static.hpp>
#include <uhd/utils/exception.hpp>
#include <iostream>

using namespace uhd;

#include "convert_pred.hpp"

static const bool debug = false;

/***********************************************************************
 * Define types for the function tables
 **********************************************************************/
struct fcn_table_entry_type{
    convert::priority_type prio;
    convert::function_type fcn;
    fcn_table_entry_type(void)
    : prio(convert::PRIORITY_EMPTY), fcn(NULL){
        /* NOP */
    }
};
typedef std::vector<fcn_table_entry_type> fcn_table_type;

/***********************************************************************
 * Setup the table registry
 **********************************************************************/
UHD_SINGLETON_FCN(fcn_table_type, get_cpu_to_otw_table);
UHD_SINGLETON_FCN(fcn_table_type, get_otw_to_cpu_table);

fcn_table_type &get_table(dir_type dir){
    switch(dir){
    case DIR_OTW_TO_CPU: return get_otw_to_cpu_table();
    case DIR_CPU_TO_OTW: return get_cpu_to_otw_table();
    }
    UHD_THROW_INVALID_CODE_PATH();
}

/***********************************************************************
 * The registry functions
 **********************************************************************/
void uhd::convert::register_converter(
    const std::string &markup,
    function_type fcn,
    priority_type prio
){
    //extract the predicate and direction from the markup
    dir_type dir;
    pred_type pred = make_pred(markup, dir);

    //get a reference to the function table
    fcn_table_type &table = get_table(dir);

    //resize the table so that its at least pred+1
    if (table.size() <= pred) table.resize(pred+1);

    //register the function if higher priority
    if (table[pred].prio < prio){
        table[pred].fcn = fcn;
        table[pred].prio = prio;
    }

    //----------------------------------------------------------------//
    if (debug) std::cout << "register_converter: " << markup << std::endl
        << "    prio: " << prio << std::endl
        << "    pred: " << pred << std::endl
        << "    dir: " << dir << std::endl
        << std::endl
    ;
    //----------------------------------------------------------------//
}

/***********************************************************************
 * The converter functions
 **********************************************************************/
const convert::function_type &convert::get_converter_cpu_to_otw(
    const io_type_t &io_type,
    const otw_type_t &otw_type,
    size_t num_input_buffs,
    size_t num_output_buffs
){
    pred_type pred = make_pred(io_type, otw_type, num_input_buffs, num_output_buffs);
    return get_cpu_to_otw_table().at(pred).fcn;
}

const convert::function_type &convert::get_converter_otw_to_cpu(
    const io_type_t &io_type,
    const otw_type_t &otw_type,
    size_t num_input_buffs,
    size_t num_output_buffs
){
    pred_type pred = make_pred(io_type, otw_type, num_input_buffs, num_output_buffs);
    return get_otw_to_cpu_table().at(pred).fcn;
}